This package combines cosmwasm-vm-js with additional abstractions and state management to
more accurately simulate the effects of CosmWasm contracts on the blockchain environments on which
they are hosted.
- configure multiple host chain environments with chain-specific settings / state
- multiple simultaneous contract instances can exist per chain
- chain modules can be simulated through custom user code
- extensible for further instrumentation via custom middlewares
Import the cw-simulate library from NPM in your package.json.
$ npm install -S @terran-one/cw-simulateIf you're using Yarn:
$ yarn add @terran-one/cw-simulate- Create a
CWSimulateAppobject - this is a simulation environment describing a single chain. - As needed, per chain:
- Upload the WASM bytecode using
App.wasm.create. This will register a newcodeIdto reference the uploaded contract code. - Create a new contract instance using
App.wasm.instantiateContract, passing in thecodeIdgenerated in the previous step. - From the response, retrieve the
contractAddressto refer to the contract instance.
- Upload the WASM bytecode using
- You can now run
executeandquerymessages against the instance, and they should work as expected.
The following example creates a chain, instantiates a contract on it, and performs an execute and query.
import{CWSimulateApp}from'@terran-one/cw-simulate';import{readFileSync}from'fs';constsender='terra1hgm0p7khfk85zpz5v0j8wnej3a90w709vhkdfu';constfunds=[];constwasmBytecode=readFileSync('cw-template.wasm');constapp=newCWSimulateApp({chainId: 'phoenix-1',bech32Prefix: 'terra'});// import the wasm bytecodeconstcodeId=app.wasm.create(sender,wasmBytecode);// instantiate the contractletresult=awaitapp.wasm.instantiateContract(sender,funds,codeId,{count: 0});console.log('instantiateContract:',result.constructor.name,JSON.stringify(result,null,2));// pull out the contract addressconstcontractAddress=result.val.events[0].attributes[0].value;// execute the contractresult=awaitapp.wasm.executeContract(sender,funds,contractAddress,{increment: {}});console.log('executeContract:',result.constructor.name,JSON.stringify(result,null,2));// query the contractresult=awaitapp.wasm.query(contractAddress,{get_count: {}});console.log('query:',result.constructor.name,JSON.stringify(result,null,2));Vite doesn't include shims for Node variables like Webpack 4 does, and cw-simulate currently relies on these. The following workaround exists:
- Add the
bufferpackage (npm add buffer) - Add the following to your
index.html(inside thebodytag, before your other js imports):
<script>window.global=window;</script><scripttype="module">import{Buffer}from"buffer";window.Buffer=Buffer;</script>See this github issue for more details.