#smart-contracts #companion #concordium #testing #concordium-std

concordium-smart-contract-testing

concordium-std 的配套库,支持链外端到端测试智能合约

7个稳定版本 (3个主要版本)

4.2.0 2024年3月26日
4.1.0 2024年1月26日
3.2.0 2023年11月23日
3.1.0 2023年10月18日
1.0.0 2023年5月8日

#193 in 魔法豆

Download history 149/week @ 2024-04-08 109/week @ 2024-04-15 159/week @ 2024-04-22 33/week @ 2024-04-29 2/week @ 2024-05-13 18/week @ 2024-05-20 4/week @ 2024-05-27 34/week @ 2024-06-03 23/week @ 2024-06-10 7/week @ 2024-06-17 11/week @ 2024-06-24 5/week @ 2024-07-08 13/week @ 2024-07-15 97/week @ 2024-07-22

每月116次下载

MPL-2.0 许可证

250KB
4K SLoC

康德瑞姆智能合约测试

这是一个库,支持在Rust中编写康德瑞姆智能合约的集成测试。

这是 concordium-std 的配套,用于在Rust中编写智能合约。

文档

MSRV

最低支持的Rust版本可以在 Cargo.toml 清单文件中找到。MSRV的变化将伴随着至少一个次要版本的升级。


lib.rs:

康德瑞姆智能合约测试

这个库支持在Rust中为康德瑞姆智能合约编写集成测试。

要使用此库,您必须将其添加到您的 Cargo.toml 文件中的 [dev-dependencies] 部分。该库需要 rust 版本 2021 或更高。

[package]
# ...
edition = "2021"

[dev-dependencies]
concordium-smart-contract-testing = "4.0"

基本用法

use concordium_smart_contract_testing::*;

// Create a "chain" with default parameters.
let mut chain = Chain::new();

// Define an account address to be used.
const ACC: AccountAddress = AccountAddress([0;32]);

// Create an account with 10000 CCD in balance.
chain.create_account(Account::new(ACC, Amount::from_ccd(1000)));

// Deploy a smart contract module (built with [Cargo Concordium](https://developer.concordium.software/en/mainnet/smart-contracts/guides/setup-tools.html#cargo-concordium)).
let deployment = chain
    .module_deploy_v1(
        Signer::with_one_key(),
        ACC,
        module_load_v1("path/to/contract.wasm.v1").unwrap())
    .unwrap();

// Initialize a smart contract from the deployed module.
let initialization = chain
    .contract_init(
        Signer::with_one_key(), // Used for specifying the number of signatures.
        ACC, // Invoker account.
        Energy::from(10000), // Maximum energy allowed for initializing.
        InitContractPayload {
            mod_ref: deployment.module_reference, // Module to initialize from.
            init_name: OwnedContractName::new_unchecked("init_my_contract".into()), // Contract to init.
            param: OwnedParameter::from_serial(&"my_param").unwrap(), // Any type implementing [`Serial`] can be used.
            amount: Amount::zero(), // CCD to send the contract.
        }
    )
    .unwrap();

// Update the initialized contract.
let update = chain
    .contract_update(
        Signer::with_one_key(), // Used for specifying the number of signatures.
        ACC, // Invoker account.
        Address::Account(ACC), // Sender (can also be a contract).
        Energy::from(10000),  // Maximum energy allowed for the update.
        UpdateContractPayload {
            address: initialization.contract_address, // The contract to update.
            receive_name: OwnedReceiveName::new_unchecked("my_contract.my_entrypoint".into()), // The receive function to call.
            message: OwnedParameter::from_serial(&42u8).unwrap(), // The parameter sent to the contract.
            amount: Amount::from_ccd(100), // Sending the contract 100 CCD.
        }
    )
    .unwrap();

// Check the trace elements produced (updates, interrupts, resumes, transfers, etc.).
assert!(matches!(update.effective_trace_elements().collect::<Vec<_>>()[..], [ContractTraceElement::Updated{..}]));

// Check the return value.
assert_eq!(update.return_value, to_bytes(&84u8));

// Check the balances of both contracts and accounts.
assert_eq!(chain.contract_balance(initialization.contract_address), Some(Amount::from_ccd(100)));
assert_eq!(chain.account_balance_available(ACC), Some(
    Amount::from_ccd(1000)
    - Amount::from_ccd(100) // Amount sent to contract.
    - deployment.transaction_fee
    - initialization.transaction_fee
    - update.transaction_fee));
    

依赖项

~26–37MB
~523K SLoC