【问题标题】:How to verify that proper promises were created in Rust smart contract tests?如何验证在 Rust 智能合约测试中创建了正确的 Promise?
【发布时间】:2020-09-17 13:08:07
【问题描述】:

在这里使用 linkdrop 合约:https://github.com/nearprotocol/near-linkdrop/blob/master/src/lib.rs

我正在编写一种新方法来部署具有有限访问密钥的合约。我想检查我的承诺是否已成功创建并在其他测试中找到此评论:

// TODO: verify that promise was created with funds for given username.

我将如何验证我的承诺是否被正确创建。测试通过,没有错误/恐慌,但是在使用near-shell 进行测试之前,所有这些都可以完成吗?

在我自己的 fork 上处理这个分支:https://github.com/mattlockyer/near-linkdrop/tree/deploy-contract

这是我的新方法:

/// Create and fund new account with limited access key to contract methods.
pub fn create_limited_contract_account(
    &mut self,
    new_account_id: AccountId,
    new_public_key: Base58PublicKey,
    allowance: u128,
    contract_bytes: Vec<u8>,
    method_names: Vec<u8>,
) -> Promise {
    assert_eq!(
        env::predecessor_account_id(),
        env::current_account_id(),
        "Create account and claim only can come from this account"
    );
    assert!(
        env::is_valid_account_id(new_account_id.as_bytes()),
        "Invalid account id"
    );
    let amount = self
        .accounts
        .remove(&env::signer_account_pk())
        .expect("Unexpected public key");

    Promise::new(new_account_id.clone())
        .create_account()
        .transfer(amount)
        .deploy_contract(contract_bytes)
        .add_access_key(
            new_public_key.into(),
            allowance,
            new_account_id,
            method_names
        ).then(
            ext_self::on_account_created_and_claimed(
            amount.into(),
            &env::current_account_id(),
            NO_DEPOSIT,
            ON_CREATE_ACCOUNT_CALLBACK_GAS,
        ))
}

这是我的测试:

#[test]
fn test_create_limited_contract_account() {
    let mut contract = LinkDrop::default();
    let pk: Base58PublicKey = "qSq3LoufLvTCTNGC3LJePMDGrok8dHMQ5A1YD9psbiz"
        .try_into()
        .unwrap();
    // Deposit money to linkdrop contract.
    let deposit = ACCESS_KEY_ALLOWANCE * 100;
    testing_env!(VMContextBuilder::new()
        .current_account_id(linkdrop())
        .attached_deposit(deposit)
        .finish());
    contract.send(pk.clone());
    // Now, send new transaction to link drop contract.
    let context = VMContextBuilder::new()
        .current_account_id(linkdrop())
        .predecessor_account_id(linkdrop())
        .signer_account_pk(pk.into())
        .account_balance(deposit)
        .finish();
    testing_env!(context);
    let pk2 = "2S87aQ1PM9o6eBcEXnTR5yBAVRTiNmvj8J8ngZ6FzSca"
        .try_into()
        .unwrap();
    let contract_bytes = include_bytes!("../res/multisig.wasm").to_vec();
    let method_names = "multisig_method_a,multisig_method_b".as_bytes().to_vec();

    contract.create_limited_contract_account(
        bob(),
        pk2,
        LIMITED_ACCESS_KEY_ALLOWANCE,
        contract_bytes,
        method_names
    );
}

【问题讨论】:

    标签: nearprotocol


    【解决方案1】:

    目前这是一个难题。然而,目前测试这个的最好方法(并且它正在进行中)是the standalone runtime,它允许您创建一个可以处理事务的模拟运行时。例如,您可以创建一个事务,该事务调用一个函数,该函数发出一个 Promise 调用,然后可以只处理该事务或它生成的所有后续收据。

    下面是它的用法示例:Here,也可以看看utils for how to set it up.

    【讨论】:

    • 谢谢!我发现使用近壳也有利于基本的合约交易测试,因为它比基本的单元测试更“真实”。不过,没有什么比设置一个基本的 JS 环境和与合约交互更重要的了:)
    猜你喜欢
    • 2021-03-09
    • 1970-01-01
    • 2020-09-17
    • 2021-08-11
    • 2021-12-28
    • 1970-01-01
    • 2022-06-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多