Polar: Milestone 2 overview

Arufa Research
4 min readSep 19, 2021

Polar is a development framework for secret contracts development. For introduction and milestone 1 overview, checkout this link.

Following features have been added to the project and can be seen on the GitHub repository https://github.com/arufa-research/polar.

Schema parsing

Before, when compiling the secret contracts using command polar compile, only .wasm files used to be generated. But now it also generates .json files which represent the schema of each of these contracts. These schema files are used to interact with the network to instantiate/execute/query a secret contract. These also act a blueprint for DApp's front-end to interchange data with the contract.

Polar makes use of these schema files to provide user with a set of methods which can be executed as a .js script. This means a user can write simple .js scripts to play around with their secret contracts on a network. Want to deploy using a script? Query values from contract using a script? Execute contract transactions using a script? These all can be done with just few lines of code using polar run <script-path>.

Polar run scripts

Suppose you’ve created a secret contracts project using polar init <name> and updated rust files in contracts/ dir using custom contracts logic. Rust code is compiling fine using polar compile and you've generated compiled contracts .wasm files and schema .json files. Now's the time to deploy on testnet and do some experimentation. Write a simple script (like one shown below) and save it in scripts/ dir. Now run polar run scripts/<script-name> --network testnet, the script will execute using the config values from polar.config.js such as accounts, network configs.

// import Contract class and getAccountByName method for secret-polar module
const { Contract, getAccountByName } = require("secret-polar");
// run is default function of this script.
// default function of the script with runtimeEnv as argument
// will be called when 'polar run <script-path>' is executed.
async function run (runtimeEnv) {
// account_ values are read from polar.config.js
const contract_owner = getAccountByName("account_0", runtimeEnv);
// contract object created for contract named sample-project
const contract = new Contract('sample-project', runtimeEnv);
// schema of this contract is parsed and contract's query and execute
// methods are populated in the contract object.
await contract.parseSchema();
// deploys the contract using contract_owner account
const deploy_response = await contract.deploy(contract_owner);
// logs the deploy response
console.log(deploy_response);
// contract is instantiated with count value 102 and label "deploy test"
const contract_info = await contract.instantiate({"count": 102}, "deploy test", contract_owner);
// logs contract init response
console.log(contract_info);
// executes contract's increment() method using contract_owner account
const ex_response = await contract.tx.increment(contract_owner);
// logs response
console.log(ex_response);
// queries the contract to get count value
const response = await contract.query.get_count();
// logs response
console.log(response);
}
// sets run function as default for the script
module.exports = { default: run };

Like the above script, one can write a script to deploy one contract or ten, or to just execute a series of transactions and query them. Maybe write a script to test a use case of written contracts. Quick test contract’s response and fix/improve things accordingly with just a few lines of a script.

Checkpoint support

Suppose you’ve written a script to deploy, initialize a contract and ran 2 transactions afterwards. What if it fails after deployment? What if it fails after the first transaction? Running the script again will do all the steps again which is not desirable. Why should it be deployed again or why should a transaction be run again (now there are 3 transactions instead of 2)? This leads to more gas consumption and sometimes incorrect results too.

Hence comes checkpoints!

When scripts are executed, checkpoints are created after steps like, deploy, instantiate and any other point where user specifies in the script. Now when a contract is deployed, deploying again will tell the user that the contract is already deployed and same goes for contract init. This is done by storing .yaml files in artifacts/checkpoints directory. Checkpoints also keep track of contract metadata like contract address, code id, etc., so the user don't have to.

Polar repl (Read Eval Print Loop)

Polar has a REPL integrated for interactive script execution. Write polar repl and repl loads with polar.config.js data already loaded. Whatever can be done using a script can be done using REPL but in an interactive way. Polar config can be accessed using config object and Polar runtime environment using env object, rest all is same.

uditgulati@pop-os:~/blue$ polar repl --network testnet
★★★ Welcome to polar REPL ★★★
Try typing: config
polar> config
{
name: 'testnet',
config: {
accounts: [ [Object], [Object] ],
endpoint: 'http://bootstrap.secrettestnet.io',
chainId: 'holodeck-2',
trustNode: true,
keyringBackend: 'test',
types: {}
}
}
polar> const { Contract, getAccountByName } = require("secret-polar");
undefined
polar> const owner = getAccountByName("account_0", env);
Creating client for network: testnet
undefined

Polar node-info

Node info gives metadata related to a network. Simply use command polar node-info --network <network-name>. For example, polar default config has testnet config specified, so doing a polar node-info --network testnet will give information similar to one given below.

uditgulati@pop-os:~/blue$ polar node-info --network testnet
Creating client for network: testnet
Network: testnet
ChainId: holodeck-2
Block height: 4675143
Node Info: {
node_info: {
protocol_version: { p2p: '7', block: '10', app: '0' },
id: '64b03220d97e5dc21ec65bf7ee1d839afb6f7193',
listen_addr: 'tcp://0.0.0.0:26656',
network: 'holodeck-2',
version: '0.33.8',
channels: '4020212223303800',
moniker: 'ChainofSecretsBootstrap',
other: { tx_index: 'on', rpc_address: 'tcp://0.0.0.0:26657' }
},
application_version: {
name: 'SecretNetwork',
server_name: 'secretd',
client_name: 'secretcli',
version: '1.0.4-2-ge24cdfde',
commit: 'e24cdfde5cd3b4bdd9b6ca429aafaa552b95e2bf',
build_tags: 'netgo ledger hw develop',
go: 'go version go1.13.4 linux/amd64'
}
}

Polar clean

Polar clean cleans up data created in artifacts/ dir. Doing a polar clean will completely remove the artifacts/ dir. Doing polar clean <contract-name> will only remove artifacts files related to the given contract. Use this command with caution as it removes compiled .wasm files, schema files and checkpoints files too.

PS: If you like the project or want to follow any updates, checkout https://twitter.com/ArufaResearch and https://github.com/arufa-research.

--

--