Writing scripts
Sample script walkthrough
Wasmkit boilerplate code has sample script scripts/sample-script.ts
with following content:
_39import { getAccountByName } from "@kubiklabs/wasmkit";_39_39import { CounterContract } from "../artifacts/typescript_schema/CounterContract";_39_39export default async function run () {_39 const runTs = String(new Date());_39 const contract_owner = await getAccountByName("account_0");_39 const counter_contract = new CounterContract();_39 await counter_contract.setupClient();_39_39 const deploy_response = await counter_contract.deploy(_39 contract_owner,_39 );_39 console.log(deploy_response);_39_39 const contract_info = await counter_contract.instantiate(_39 {_39 "count": 102,_39 },_39 `deploy test ${runTs}`,_39 contract_owner,_39 undefined, // transferAmount_39 // customFees, // custom fees here_39 );_39 console.log(contract_info);_39_39 const inc_response = await counter_contract.increment({account: contract_owner});_39 console.log(inc_response);_39_39 const response = await counter_contract.getCount();_39 console.log(response);_39_39 const ex_response = await counter_contract.increment(_39 {_39 account: contract_owner,_39 }_39 );_39 console.log(ex_response);_39}
A line-by-line breakdown of the above script:
Importing wasmkit, contract clients
- Import
getAccountByName
method from@kubiklabs/wasmkit
library. - Import
CounterContract
client class.
import { getAccountByName } from "@kubiklabs/wasmkit";import { CounterContract } from "../artifacts/typescript_schema/CounterContract";export default async function run () { const runTs = String(new Date()); const contract_owner = await getAccountByName("account_0"); const counter_contract = new CounterContract(); await counter_contract.setupClient(); const deploy_response = await counter_contract.deploy( contract_owner, ); console.log(deploy_response); const contract_info = await counter_contract.instantiate( { "count": 102, }, `deploy test ${runTs}`, contract_owner, undefined, // transferAmount // customFees, // custom fees here ); console.log(contract_info); const inc_response = await counter_contract.increment({account: contract_owner}); console.log(inc_response); const response = await counter_contract.getCount(); console.log(response); const ex_response = await counter_contract.increment( { account: contract_owner, } ); console.log(ex_response);}
Load accounts, contracts
- Fetch details of account
account_0
intocontract_owner
object. - Create contract client instance of
CounterContract
. - Connect contract client with the network.
import { getAccountByName } from "@kubiklabs/wasmkit";import { CounterContract } from "../artifacts/typescript_schema/CounterContract";export default async function run () { const runTs = String(new Date()); const contract_owner = await getAccountByName("account_0"); const counter_contract = new CounterContract(); await counter_contract.setupClient(); const deploy_response = await counter_contract.deploy( contract_owner, ); console.log(deploy_response); const contract_info = await counter_contract.instantiate( { "count": 102, }, `deploy test ${runTs}`, contract_owner, undefined, // transferAmount // customFees, // custom fees here ); console.log(contract_info); const inc_response = await counter_contract.increment({account: contract_owner}); console.log(inc_response); const response = await counter_contract.getCount(); console.log(response); const ex_response = await counter_contract.increment( { account: contract_owner, } ); console.log(ex_response);}
Deploy, init the contract
- Deploy the contract. Network is specified in the
wasmkit run scripts/<script-name> --network <network-name>
command. - Instantiate contract instance with initMsg, label and signing account. Any funds to send with initMsg and custom gas fees is optional.
import { getAccountByName } from "@kubiklabs/wasmkit";import { CounterContract } from "../artifacts/typescript_schema/CounterContract";export default async function run () { const runTs = String(new Date()); const contract_owner = await getAccountByName("account_0"); const counter_contract = new CounterContract(); await counter_contract.setupClient(); const deploy_response = await counter_contract.deploy( contract_owner, ); console.log(deploy_response); const contract_info = await counter_contract.instantiate( { "count": 102, }, `deploy test ${runTs}`, contract_owner, undefined, // transferAmount // customFees, // custom fees here ); console.log(contract_info); const inc_response = await counter_contract.increment({account: contract_owner}); console.log(inc_response); const response = await counter_contract.getCount(); console.log(response); const ex_response = await counter_contract.increment( { account: contract_owner, } ); console.log(ex_response);}
Execute a contract transaction
- Execute
increment()
transaction using accountcontract_owner
. Any funds to send with execMsg and custom gas fees is optional. Refer to WasmKit library API for detailed execute call.
import { getAccountByName } from "@kubiklabs/wasmkit";import { CounterContract } from "../artifacts/typescript_schema/CounterContract";export default async function run () { const runTs = String(new Date()); const contract_owner = await getAccountByName("account_0"); const counter_contract = new CounterContract(); await counter_contract.setupClient(); const deploy_response = await counter_contract.deploy( contract_owner, ); console.log(deploy_response); const contract_info = await counter_contract.instantiate( { "count": 102, }, `deploy test ${runTs}`, contract_owner, undefined, // transferAmount // customFees, // custom fees here ); console.log(contract_info); const inc_response = await counter_contract.increment({account: contract_owner}); console.log(inc_response); const response = await counter_contract.getCount(); console.log(response); const ex_response = await counter_contract.increment( { account: contract_owner, } ); console.log(ex_response);}
Query the contract
- Fetch count value using query
getCount()
. Refer to WasmKit library API for detailed query call.
import { getAccountByName } from "@kubiklabs/wasmkit";import { CounterContract } from "../artifacts/typescript_schema/CounterContract";export default async function run () { const runTs = String(new Date()); const contract_owner = await getAccountByName("account_0"); const counter_contract = new CounterContract(); await counter_contract.setupClient(); const deploy_response = await counter_contract.deploy( contract_owner, ); console.log(deploy_response); const contract_info = await counter_contract.instantiate( { "count": 102, }, `deploy test ${runTs}`, contract_owner, undefined, // transferAmount // customFees, // custom fees here ); console.log(contract_info); const inc_response = await counter_contract.increment({account: contract_owner}); console.log(inc_response); const response = await counter_contract.getCount(); console.log(response); const ex_response = await counter_contract.increment( { account: contract_owner, } ); console.log(ex_response);}
Importing wasmkit, contract clients
- Import
getAccountByName
method from@kubiklabs/wasmkit
library. - Import
CounterContract
client class.
Load accounts, contracts
- Fetch details of account
account_0
intocontract_owner
object. - Create contract client instance of
CounterContract
. - Connect contract client with the network.
Deploy, init the contract
- Deploy the contract. Network is specified in the
wasmkit run scripts/<script-name> --network <network-name>
command. - Instantiate contract instance with initMsg, label and signing account. Any funds to send with initMsg and custom gas fees is optional.
Execute a contract transaction
- Execute
increment()
transaction using accountcontract_owner
. Any funds to send with execMsg and custom gas fees is optional. Refer to WasmKit library API for detailed execute call.
Query the contract
- Fetch count value using query
getCount()
. Refer to WasmKit library API for detailed query call.
import { getAccountByName } from "@kubiklabs/wasmkit";import { CounterContract } from "../artifacts/typescript_schema/CounterContract";export default async function run () { const runTs = String(new Date()); const contract_owner = await getAccountByName("account_0"); const counter_contract = new CounterContract(); await counter_contract.setupClient(); const deploy_response = await counter_contract.deploy( contract_owner, ); console.log(deploy_response); const contract_info = await counter_contract.instantiate( { "count": 102, }, `deploy test ${runTs}`, contract_owner, undefined, // transferAmount // customFees, // custom fees here ); console.log(contract_info); const inc_response = await counter_contract.increment({account: contract_owner}); console.log(inc_response); const response = await counter_contract.getCount(); console.log(response); const ex_response = await counter_contract.increment( { account: contract_owner, } ); console.log(ex_response);}
Wasmkit Runtime Environment
Wasmkit runtime environment is used internally by Wasmkit. It is created when a wasmkit task is executed using bash command wasmkit ...
which can be accessed in REPL using env
variable and has following parameters:
-
config: Has paths of config file, contract sources, artifacts, project root and test path. Other config values such as networks config and mocha timeout.
-
runtimeArgs: Runtime metadata such as network to use etc. Network can be specified in a wasmkit command like
wasmkit ... --network <network-name>
. -
tasks: List of available tasks with details.
-
network: Details of the network currently being used.
Checkpoints
Checkpoints store the metadata of contract instance on the network. It stores the deploy metadata (codeId
, contractCodeHash
, deployTimestamp
) and instantiate metadata (contractAddress
, instantiateTimestamp
). This comes handy when a script is run which deploys, inits and does some interactions with the contracts.
Suppose the script fails after init step and now script is to be rerun after some fixes in the contract, here one does not want for the contract to be deployed and instantiated again, so wasmkit picks up the saved metadata from checkpoints file and directly skips to part after init and uses the previously deployed instance and user does not have to pay the extra gas and wait extra time to deploy, init the contract again. Same happens when there is error before init and rerun skips deploy and directly executes init step.
To skip using checkpoints when running script, use:
wasmkit run <script-path> --skip-checkpoints