Skip to content

Commit

Permalink
interact with module
Browse files Browse the repository at this point in the history
  • Loading branch information
ngkuru committed Jul 14, 2023
1 parent 305198c commit 914999a
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 12 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions crates/aptos-api-tester/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,5 @@ rand = { workspace = true }
anyhow = { workspace = true }
aptos-cached-packages = { workspace = true }
aptos-framework = { workspace = true }
move-core-types = { workspace = true }

80 changes: 68 additions & 12 deletions crates/aptos-api-tester/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use std::future::Future;
use std::path::PathBuf;

use anyhow::{anyhow, Result};
use aptos_api_types::U64;
use aptos_api_types::{HexEncodedBytes, U64};
use aptos_cached_packages::aptos_stdlib::EntryFunctionCall;
use aptos_framework::{BuildOptions, BuiltPackage};
use aptos_rest_client::error::RestError;
Expand All @@ -21,6 +21,9 @@ use aptos_sdk::token_client::{
};
use aptos_sdk::types::LocalAccount;
use aptos_types::account_address::AccountAddress;
use aptos_types::transaction::{EntryFunction, TransactionPayload};
use move_core_types::ident_str;
use move_core_types::language_storage::ModuleId;
use once_cell::sync::Lazy;
use url::Url;

Expand Down Expand Up @@ -353,12 +356,11 @@ async fn test_mintnft(
Ok(TestResult::Success)
}

async fn test_module(
client: &Client,
account: &mut LocalAccount,
) -> Result<TestResult, TestFailure> {
/// Helper function that publishes module and returns the bytecode.
async fn publish_module(client: &Client, account: &mut LocalAccount) -> Result<HexEncodedBytes> {
// get file to compile
let move_dir = PathBuf::from("/Users/ngk/Documents/aptos-core/aptos-move/move-examples/hello_blockchain");
let move_dir =
PathBuf::from("/Users/ngk/Documents/aptos-core/aptos-move/move-examples/hello_blockchain");

// insert address
let mut named_addresses: BTreeMap<String, AccountAddress> = BTreeMap::new();
Expand All @@ -374,18 +376,72 @@ async fn test_module(
let metadata = package.extract_metadata()?;

// create payload
let payload = EntryFunctionCall::CodePublishPackageTxn {
metadata_serialized: bcs::to_bytes(&metadata).expect("PackageMetadata has BCS"),
code: blobs,
}
.encode();
let payload: aptos_types::transaction::TransactionPayload =
EntryFunctionCall::CodePublishPackageTxn {
metadata_serialized: bcs::to_bytes(&metadata).expect("PackageMetadata has BCS"),
code: blobs.clone(),
}
.encode();

// create and submit transaction
let pending_txn =
build_and_submit_transaction(client, account, payload, TransactionOptions::default())
.await?;
client.wait_for_transaction(&pending_txn).await?;

let blob = match blobs.get(0) {
Some(bytecode) => bytecode.clone(),
None => return Err(anyhow!("error while getting bytecode from blobs")),
};

Ok(HexEncodedBytes::from(blob))
}

/// Helper function that interacts with the message module.
async fn set_message(client: &Client, account: &mut LocalAccount) -> Result<()> {
// create payload
let payload = TransactionPayload::EntryFunction(EntryFunction::new(
ModuleId::new(account.address(), ident_str!("message").to_owned()),
ident_str!("set_message").to_owned(),
vec![],
vec![bcs::to_bytes("test message")?],
));

// create and submit transaction
let pending_txn =
build_and_submit_transaction(&client, account, payload, TransactionOptions::default())
build_and_submit_transaction(client, account, payload, TransactionOptions::default())
.await?;
client.wait_for_transaction(&pending_txn).await?;

Ok(())
}

/// Tests module publishing and interaction. Checks that:
/// - module data exists
async fn test_module(
client: &Client,
account: &mut LocalAccount,
) -> Result<TestResult, TestFailure> {
// publish module
let blob = publish_module(client, account).await?;

// check module data
let response = client
.get_account_module(account.address(), "message")
.await?;

let expected_bytecode = &blob;
let actual_bytecode = &response.inner().bytecode;

if expected_bytecode != actual_bytecode {
return Err(TestFailure::Fail("wrong bytecode"));
}

// interact with module
set_message(client, account).await?;

// todo: get account resource to see that interaction worked

Ok(TestResult::Success)
}

Expand Down

0 comments on commit 914999a

Please sign in to comment.