literate-nil/contracts.org

1.9 KiB

Playing with smart contracts in org-mode

Simple counter contract in Solidity

Nothing fancy really. A function to increment a value, and a function to read a value.

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

contract SimpleStorage {
    int32 private value;

    function increment() public {
        value += 1;
    }

    function get() public view returns(int32) {
        return value;
    }
}

Compile the contract

Using off-the-shelf solc, the default Solidity compiler. We feed in the code from the previous slide.

mkdir -p contract
echo "$contract" | solc -o contract/ --bin --abi --overwrite -

Initialize the config and create a wallet

Because without a wallet we are powerless.

mkdir -p ~/.config/nil/
rm -rf ~/.config/nil/*
touch ~/.config/nil/config.yaml

nil_cli config set rpc_endpoint "http://127.0.0.1:8529" 2> /dev/null
nil_cli keygen new 2> /dev/null
nil_cli wallet new 2> /dev/null

wallet=$(nil_cli wallet info 2>&1 | grep -oP 'Address: \K\w+')
echo $wallet

Top-up the wallet

Gimme some tokens. Display new balance.

nil_cli wallet top-up 100000000 2> /dev/null
nil_cli wallet balance 2>&1 | grep -oP 'balance: \K\w+'

Deploy the contract

Save the address and reuse it later.

nil_cli wallet deploy ./contract/SimpleStorage.bin 2>&1 | grep -oP 'address: \K\w+'

Call the contract

Notice that we use $address from the previous execution of nil_cli.

nil_cli wallet send-message "$address" increment --abi ./contract/SimpleStorage.abi 2>&1 | grep -oP 'Transaction hash: \K\w+'
nil_cli contract call-readonly "$address" get --abi ./contract/SimpleStorage.abi 2>&1 | grep -oP 'Call result: \K\w+'