Skip to main content

Writing scripts

Sample script walkthrough

Wasmkit boilerplate code has sample script scripts/sample-script.ts with following content:


_39
import { getAccountByName } from "@kubiklabs/wasmkit";
_39
_39
import { CounterContract } from "../artifacts/typescript_schema/CounterContract";
_39
_39
export 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

  1. Import getAccountByName method from @kubiklabs/wasmkit library.
  2. 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

  1. Fetch details of account account_0 into contract_owner object.
  2. Create contract client instance of CounterContract.
  3. 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

  1. Deploy the contract. Network is specified in the wasmkit run scripts/<script-name> --network <network-name> command.
  2. 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

  1. Execute increment() transaction using account contract_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

  1. 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

  1. Import getAccountByName method from @kubiklabs/wasmkit library.
  2. Import CounterContract client class.

Load accounts, contracts

  1. Fetch details of account account_0 into contract_owner object.
  2. Create contract client instance of CounterContract.
  3. Connect contract client with the network.

Deploy, init the contract

  1. Deploy the contract. Network is specified in the wasmkit run scripts/<script-name> --network <network-name> command.
  2. 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

  1. Execute increment() transaction using account contract_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

  1. 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:

  1. config: Has paths of config file, contract sources, artifacts, project root and test path. Other config values such as networks config and mocha timeout.

  2. runtimeArgs: Runtime metadata such as network to use etc. Network can be specified in a wasmkit command like wasmkit ... --network <network-name>.

  3. tasks: List of available tasks with details.

  4. 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