Search code examples
nearprotocol

How to verify that proper promises were created in Rust smart contract tests?


Working with the linkdrop contract here: https://github.com/nearprotocol/near-linkdrop/blob/master/src/lib.rs

I'm writing a new method to deploy a contract with a limited access key. I would like to check my promise was created successfully and found this comment in other tests:

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

How would I verify that my promise is being created correctly. The test passes with no errors/panic, but is that all that can be done prior to testing with near-shell?

Working out of this branch on my own fork here: https://github.com/mattlockyer/near-linkdrop/tree/deploy-contract

Here is my new method:

/// 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,
        ))
}

And here is my test:

#[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
    );
}

Solution

  • This is a hard problem currently. However, the best way to test this currently (and it's a work in progress) is the standalone runtime, which allows you to create a mock runtime that can process transactions. For example you can create a transaction that calls a function that makes a promise call and then can process just that transaction or all of the subsequent receipts that it generates.

    Here is an example of its usage: Here, but also look at the utils for how to set it up.