Skip to content

Commit

Permalink
[AptosFramework] add mint/transfer smoke test
Browse files Browse the repository at this point in the history
  • Loading branch information
zekun000 committed Mar 4, 2022
1 parent cc6a6ba commit 1e5463e
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 7 deletions.
26 changes: 21 additions & 5 deletions testsuite/forge/src/interface/aptos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,15 @@
use super::Test;
use crate::{CoreContext, Result, TestReport};
use diem_rest_client::Client as RestClient;
use diem_sdk::crypto::ed25519::Ed25519PublicKey;
use diem_sdk::types::transaction::authenticator::AuthenticationKeyPreimage;
use diem_sdk::{
crypto::ed25519::Ed25519PublicKey,
transaction_builder::TransactionFactory,
types::{chain_id::ChainId, transaction::authenticator::AuthenticationKey, LocalAccount},
types::{
account_address::AccountAddress,
chain_id::ChainId,
transaction::authenticator::{AuthenticationKey, AuthenticationKeyPreimage},
LocalAccount,
},
};
use diem_transaction_builder::aptos_stdlib;
use reqwest::Url;
Expand Down Expand Up @@ -65,8 +69,8 @@ impl<'t> AptosContext<'t> {
TransactionFactory::new(self.chain_id())
}

pub async fn create_user_account(&mut self, auth_key: &Ed25519PublicKey) -> Result<()> {
let preimage = AuthenticationKeyPreimage::ed25519(auth_key);
pub async fn create_user_account(&mut self, pubkey: &Ed25519PublicKey) -> Result<()> {
let preimage = AuthenticationKeyPreimage::ed25519(pubkey);
let auth_key = AuthenticationKey::from_preimage(&preimage);
let create_account_txn = self.public_info.root_account.sign_with_transaction_builder(
self.transaction_factory().payload(
Expand All @@ -82,6 +86,18 @@ impl<'t> AptosContext<'t> {
.await?;
Ok(())
}

pub async fn mint(&mut self, addr: AccountAddress, amount: u64) -> Result<()> {
let mint_txn = self.public_info.root_account.sign_with_transaction_builder(
self.transaction_factory()
.payload(aptos_stdlib::encode_mint_script_function(addr, amount)),
);
self.public_info
.rest_client
.submit_and_wait(&mint_txn)
.await?;
Ok(())
}
}

pub struct AptosPublicInfo<'t> {
Expand Down
17 changes: 17 additions & 0 deletions testsuite/smoke-test/src/aptos/account_creation.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// SPDX-License-Identifier: Apache-2.0

use diem_transaction_builder::aptos_stdlib;
use diem_types::transaction::authenticator::AuthenticationKeyPreimage;
use forge::{AptosContext, AptosTest, Result, Test};

pub struct AccountCreation;
Expand All @@ -13,9 +15,24 @@ impl Test for AccountCreation {
#[async_trait::async_trait]
impl AptosTest for AccountCreation {
async fn run<'t>(&self, ctx: &mut AptosContext<'t>) -> Result<()> {
// created by root account
let mut accounts = vec![];
for _ in 0..10 {
let local_account = ctx.random_account();
ctx.create_user_account(local_account.public_key()).await?;
accounts.push(local_account);
}
// created by user account
for mut account in accounts {
let new_account = ctx.random_account();
let preimage = AuthenticationKeyPreimage::ed25519(new_account.public_key());
let txn = account.sign_with_transaction_builder(ctx.transaction_factory().payload(
aptos_stdlib::encode_create_account_script_function(
new_account.address(),
preimage.into_vec(),
),
));
ctx.client().submit_and_wait(&txn).await?;
}
Ok(())
}
Expand Down
32 changes: 32 additions & 0 deletions testsuite/smoke-test/src/aptos/mint_transfer.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// SPDX-License-Identifier: Apache-2.0

use diem_transaction_builder::aptos_stdlib;
use forge::{AptosContext, AptosTest, Result, Test};

pub struct MintTransfer;

impl Test for MintTransfer {
fn name(&self) -> &'static str {
"smoke-test::aptos::mint-transfer"
}
}

#[async_trait::async_trait]
impl AptosTest for MintTransfer {
async fn run<'t>(&self, ctx: &mut AptosContext<'t>) -> Result<()> {
let mut account1 = ctx.random_account();
ctx.create_user_account(account1.public_key()).await?;
let account2 = ctx.random_account();
ctx.create_user_account(account2.public_key()).await?;

ctx.mint(account1.address(), 1000).await?;

let transfer_txn =
account1.sign_with_transaction_builder(ctx.transaction_factory().payload(
aptos_stdlib::encode_transfer_script_function(account2.address(), 400),
));
ctx.client().submit_and_wait(&transfer_txn).await?;

Ok(())
}
}
2 changes: 2 additions & 0 deletions testsuite/smoke-test/src/aptos/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@

mod account_creation;
pub use account_creation::*;
mod mint_transfer;
pub use mint_transfer::*;
4 changes: 2 additions & 2 deletions testsuite/smoke-test/tests/forge-aptos.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
// SPDX-License-Identifier: Apache-2.0

use forge::{forge_main, ForgeConfig, LocalFactory, Options, Result};
use smoke_test::aptos::AccountCreation;
use smoke_test::aptos::{AccountCreation, MintTransfer};

fn main() -> Result<()> {
let tests = ForgeConfig::default()
.with_aptos_tests(&[&AccountCreation])
.with_aptos_tests(&[&AccountCreation, &MintTransfer])
.with_genesis_modules_bytes(aptos_framework_releases::current_module_blobs().to_vec());

let options = Options::from_args();
Expand Down

0 comments on commit 1e5463e

Please sign in to comment.