The Counter Smart Contract
Write, build and deploy a simple Smart Contract written in C
Prerequisites
You need to have erdpy installed.
Create the contract
In a folder of your choice, run the following command:
erdpy contract new --template="simple-counter" mycounter
This creates a new folder named mycounter
which contains the C source code for a simple Smart Contract - based on the template simple-counter. The file counter.c
is the implementation of the Smart Contract, which defines the following functions:
init()
: this function is executed when the contract is deployed on the Blockchainincrement()
anddecrement()
: these functions modify the internal state of the Smart Contractget()
: this is a pure function (does not modify the state) which we'll use to query the value of the counter
Build the contract
In order to build the contract to WASM, run the following command:
erdpy --verbose contract build mycounter
Above, mycounter
refers to the previously created folder, the one that holds the source code. After executing the command, you can inspect the generated files in mycounter/output
.
Deploy the contract on the Testnet
In order to deploy the contract on the Testnet you need to have an account with sufficient balance (required for the deployment fee) and the associated private key in PEM format.
The deployment command is as follows:
erdpy --verbose contract deploy --project=mycounter --pem="alice.pem" --gas-limit=5000000 --proxy="https://testnet-gateway.elrond.com" --outfile="counter.json" --recall-nonce --send
Above, mycounter
refers to the same folder that contains the source code and the build artifacts. The deploy
command knows to search for the WASM bytecode within this folder.
Note the last parameter of the command - this instructs erdpy to dump the output of the operation in the specified file. The output contains the address of the newly deployed contract and the hash of the deployment transaction.
counter.json
{
"emitted_tx": {
"tx": {
"nonce": 0,
"value": "0",
"receiver": "erd1qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq6gq4hu",
"sender": "erd1...",
"gasPrice": 1000000000,
"gasLimit": 5000000,
"data": "MDA2MTczNmQwMTAwMDA...MDA=",
"chainID": "T",
"version": 1,
"signature": "a0ba..."
},
"hash": "...",
"data": "...",
"address": "erd1qqqqqqqqqqqqqpgqp93y6..."
}
}
Feel free to inspect these values in the Explorer.
Interact with the deployed contract
Let's extract the contract address from counter.json
before proceeding to an actual contract execution.
export CONTRACT_ADDRESS=$(python3 -c "import json; data = json.load(open('counter.json')); print(data['emitted_tx']['address'])")
Now that we have the contract address saved in a shell variable, we can call the increment
function of the contract as follows:
erdpy --verbose contract call $CONTRACT_ADDRESS --pem="alice.pem" --gas-limit=2000000 --function="increment" --proxy="https://testnet-gateway.elrond.com" --recall-nonce --send
Execute the command above a few times, with some pause in between. Then feel free to experiment with calling the decrement
function.
Then, in order to query the value of the counter - that is, to execute the get
pure function of the contract - run the following:
erdpy contract query $CONTRACT_ADDRESS --function="get" --proxy="https://testnet-gateway.elrond.com"
The output should look like this:
[{'base64': 'AQ==', 'hex': '01', 'number': 1}]
Interaction script
The previous steps can be summed up in a simple script as follows:
#!/bin/bash
# Deployment
erdpy --verbose contract deploy --project=mycounter --pem="alice.pem" --gas-limit=5000000 --proxy="https://testnet-gateway.elrond.com" --outfile="counter.json" --recall-nonce --send
export CONTRACT_ADDRESS=$(python3 -c "import json; data = json.load(open('address.json')); print(data['emitted_tx']['contract'])")
# Interaction
erdpy --verbose contract call $CONTRACT_ADDRESS --pem="alice.pem" --gas-limit=2000000 --function="increment" --proxy="https://testnet-gateway.elrond.com" --recall-nonce --send
sleep 10
erdpy --verbose contract call $CONTRACT_ADDRESS --pem="alice.pem" --gas-limit=2000000 --function="increment" --proxy="https://testnet-gateway.elrond.com" --recall-nonce --send
sleep 10
erdpy --verbose contract call $CONTRACT_ADDRESS --pem="alice.pem" --gas-limit=2000000 --function="decrement" --proxy="https://testnet-gateway.elrond.com" --recall-nonce --send
sleep 10
# Querying
erdpy contract query $CONTRACT_ADDRESS --function="get" --proxy="https://testnet-gateway.elrond.com"