CosmWasm x Osmosis integration testing library that, unlike cw-multi-test
, it allows you to test your cosmwasm contract against real chain's logic instead of mocks.
To demonstrate how osmosis-testing
works, let use simple example contract: cw-whitelist from cw-plus
.
Here is how to setup the test:
```rust use cosmwasmstd::Coin; use osmosistesting::OsmosisTestApp;
// create new osmosis appchain instance. let app = OsmosisTestApp::new();
// create new account with initial funds let accs = app .initaccounts( &[ Coin::new(1000000000000, "uatom"), Coin::new(1000000000_000, "uosmo"), ], 2, ) .unwrap();
let admin = &accs[0]; let new_admin = &accs[1]; ```
Now we have the appchain instance and accounts that have some initial balances and can interact with the appchain. This does not run Docker instance or spawning external process, it just load the appchain's code as a library create an in memory instance.
Note that init_accounts
is a convenience function that creates multiple accounts with the same initial balance.
If you want to create just one account, you can use init_account
instead.
```rust use cosmwasmstd::Coin; use osmosistesting::OsmosisTestApp;
let app = OsmosisTestApp::new();
let account = app.initaccount(&[ Coin::new(1000000000000, "uatom"), Coin::new(1000000000_000, "uosmo"), ]); ```
Now if we want to test a cosmwasm contract, we need to
Then we can start interacting with our contract. Let's do just that.
```rust use cosmwasmstd::Coin; use cw1whitelist::msg::{InstantiateMsg}; // for instantiating cw1whitelist contract use osmosistesting::{Account, Module, OsmosisTestApp, Wasm};
let app = OsmosisTestApp::new(); let accs = app .initaccounts( &[ Coin::new(1000000000000, "uatom"), Coin::new(1000000000000, "uosmo"), ], 2, ) .unwrap(); let admin = &accs[0]; let newadmin = &accs[1];
// ============= NEW CODE ================
// Wasm
is the module we use to interact with cosmwasm releated logic on the appchain
// it implements Module
trait which you will see more later.
let wasm = Wasm::new(&app);
// Load compiled wasm bytecode let wasmbytecode = std::fs::read("./testartifacts/cw1whitelist.wasm").unwrap(); let codeid = wasm .storecode(&wasmbytecode, None, admin) .unwrap() .data .code_id; ```
Not that in this example, it loads wasm bytecode from cw-plus release for simple demonstration purposes.
You might want to run cargo wasm
and find your wasm file in target/wasm32-unknown-unknown/release/<contract_name>.wasm
.
```rust use cosmwasmstd::Coin; use cw1whitelist::msg::{InstantiateMsg, QueryMsg, AdminListResponse}; use osmosis_testing::{Account, Module, OsmosisTestApp, Wasm};
let app = OsmosisTestApp::new(); let accs = app .initaccounts( &[ Coin::new(1000000000000, "uatom"), Coin::new(1000000000000, "uosmo"), ], 2, ) .unwrap(); let admin = &accs[0]; let newadmin = &accs[1];
let wasm = Wasm::new(&app);
let wasmbytecode = std::fs::read("./testartifacts/cw1whitelist.wasm").unwrap(); let codeid = wasm .storecode(&wasmbytecode, None, admin) .unwrap() .data .code_id;
// ============= NEW CODE ================
// instantiate contract with initial admin and make admin list mutable let initadmins = vec![admin.address()]; let contractaddr = wasm .instantiate( codeid, &InstantiateMsg { admins: initadmins.clone(), mutable: true, }, None, // contract admin used for migration, not the same as cw1_whitelist admin None, // contract label &[], // funds admin, // signer ) .unwrap() .data .address;
// query contract state to check if contract instantiation works properly
let adminlist = wasm
.query::
asserteq!(adminlist.admins, initadmins); assert!(adminlist.mutable); ```
Now let's execute the contract and verify that the contract's state is updated properly.
```rust use cosmwasmstd::Coin; use cw1whitelist::msg::{InstantiateMsg, QueryMsg, ExecuteMsg, AdminListResponse}; use osmosis_testing::{Account, Module, OsmosisTestApp, Wasm};
let app = OsmosisTestApp::new(); let accs = app .initaccounts( &[ Coin::new(1000000000000, "uatom"), Coin::new(1000000000000, "uosmo"), ], 2, ) .unwrap(); let admin = &accs[0]; let newadmin = &accs[1];
let wasm = Wasm::new(&app);
let wasmbytecode = std::fs::read("./testartifacts/cw1whitelist.wasm").unwrap(); let codeid = wasm .storecode(&wasmbytecode, None, admin) .unwrap() .data .code_id;
// instantiate contract with initial admin and make admin list mutable let initadmins = vec![admin.address()]; let contractaddr = wasm .instantiate( codeid, &InstantiateMsg { admins: initadmins.clone(), mutable: true, }, None, // contract admin used for migration, not the same as cw1_whitelist admin None, // contract label &[], // funds admin, // signer ) .unwrap() .data .address;
let adminlist = wasm
.query::
asserteq!(adminlist.admins, initadmins); assert!(adminlist.mutable);
// ============= NEW CODE ================
// update admin list and rechec the state
let newadmins = vec![newadmin.address()];
wasm.execute::
let adminlist = wasm
.query::
asserteq!(adminlist.admins, newadmins); assert!(adminlist.mutable); ```
In your contract code, if you want to debug, you can use deps.api.debug(..)
which will prints the debug message to stdout. wasmd
disabled this by default but OsmosisTestApp
allows stdout emission so that you can debug your smart contract while running tests.
In some cases, you might want interact directly with appchain logic to setup the environment or query appchain's state. Module wrappers provides convenient functions to interact with the appchain's module.
Let's try interact with Gamm
module:
```rust use cosmwasmstd::Coin; use osmosistesting::{Account, Module, OsmosisTestApp, Gamm};
let app = OsmosisTestApp::default(); let alice = app .initaccount(&[ Coin::new(1000000000000, "uatom"), Coin::new(1000000000_000, "uosmo"), ]) .unwrap();
// create Gamm Module Wrapper let gamm = Gamm::new(&app);
// create balancer pool with basic configuration let poolliquidity = vec![Coin::new(1000, "uatom"), Coin::new(1000, "uosmo")]; let poolid = gamm .createbasicpool(&poolliquidity, &alice) .unwrap() .data .poolid;
// query pool and assert if the pool is created successfully
let pool = gamm.querypool(poolid).unwrap();
asserteq!(
poolliquidity
.intoiter()
.map(|c| c.into())
.collect::
You might not find wrapper you want to use or the provided wrapper is too verbose. Good new is, it's trivial to create your own wrapper easily.
Here is how you can redefine Gamm
module wrapper as a library user:
```rust use osmosis_std::types::osmosis::gamm::{ poolmodels::balancer::v1beta1::{MsgCreateBalancerPool, MsgCreateBalancerPoolResponse}, v1beta1::{QueryPoolRequest, QueryPoolResponse}, };
use osmosistesting::{fnexecute, fnquery}; use osmosistesting::{Module, Runner};
// Boilerplate code, copy and rename should just do the trick pub struct Gamm<'a, R: Runner<'a>> { runner: &'a R, }
impl<'a, R: Runner<'a>> Module<'a, R> for Gamm<'a, R> { fn new(runner: &'a R) -> Self { Self { runner } } } // End Boilerplate code
impl<'a, R> Gamm<'a, R>
where
R: Runner<'a>,
{
// macro for creating execute function
fnexecute! {
// (pub)?
// macro for creating query function
fn_query! {
// (pub)? <fn_name> [<method_path>]: <request_type> => <response_type>
pub query_pool ["/osmosis.gamm.v1beta1.Query/Pool"]: QueryPoolRequest => QueryPoolResponse
}
} ```
If the macro generated function is not good enough for you, you write your own function manually. See module directory for more inspiration.