Interact with Smart contract

On this getting started guide you will see how simple it is to use Rockside as a web3 provider to interact with a contract without having to take care about gas.

Create an identity

An identity is an account that you can use to send transaction without having to take care about gas fees. To known more on identity, check our documentation: Rockside identities

To create an identity:

curl -X POST \
https://api.rockside.io/ethereum/ropsten/identities \
-H 'Content-Length: 0' \
-H 'apikey: YOUR_API_KEY'
`

You will get:

{
"address": "0x297634F1433Ef10162E2986760b7D4880c3F0247"
"transaction_hash":"0xa51fdb4b79c45fbe9bf6df5a822814f6ad47bf9dfad4bb2b59745b2968c1d38e"
}

Keep the address, you will use it to interact with your contract.

Smart Contract

Considering a very simple smart contract that save key / value data on the blockchain:

pragma solidity ^0.6.0;
contract Rockanchor {
mapping(bytes32 => bytes32) public keyvalues;
event Anchored(address from, bytes32 key, bytes32 value);
function anchor(bytes32 key, bytes32 value) public {
keyvalues[key] = value;
emit Anchored(msg.sender, key, value);
}
function lookup(bytes32 key) public view returns (bytes32) {
return keyvalues[key];
}
}

An instance of this smart contract is deployed on Ropsten at : 0xd612f0cBD1fc51382d8F7D8b8a97681bca73fa6A

To interact with this smart contract on your nodejs app, you need its ABI.

Generate the ABI with solcjs.

npm install -g solc

Then run:

solcjs rockanchor.sol --abi

You will get a file "rockanchor_sol_Rockanchor.abi" contraining the ABI

NodeJS application

Create a new project with npm:

npm init

You need web3:

npm install web3 -save

Using Rockside as a Web3 provider is really simple, just create an HttpProvider with your APIKEY on the header:

var options = {headers: [{name: 'apikey', value: apiKey}]}
var web3Provider = new Web3.providers.HttpProvider("https://api.rockside.io/ethereum/ropsten/jsonrpc", options)
var web3js = new Web3(web3Provider);

You can directly interact with the smart contract by using the identity you created before. Rockside will manage gas fee for you.

var RockanchorContract = new web3js.eth.Contract(contractABI, contractAddress);
RockanchorContract.methods.anchor(web3.utils.fromAscii("hello"), web3.utils.fromAscii("world !")).send({from: identityAddress}, function(error, result){
console.log(result)
});

Here is a full example of index.js:

const Web3 = require('web3')
var apiKey = process.env.ROCKSIDE_APIKEY
var identityAddress = process.env.IDENTITY_ADDRESS
var contractAddress = "0xd612f0cBD1fc51382d8F7D8b8a97681bca73fa6A";
var options = {headers: [{name: 'apikey', value: apiKey}]}
var web3Provider = new Web3.providers.HttpProvider("https://api.rockside.io/ethereum/ropsten/jsonrpc", options)
var web3js = new Web3(web3Provider);
var RockanchorContract = new web3js.eth.Contract([{"anonymous":false,"inputs":[{"indexed":false,"internalType":"address","name":"from","type":"address"},{"indexed":false,"internalType":"bytes32","name":"key","type":"bytes32"},{"indexed":false,"internalType":"bytes32","name":"value","type":"bytes32"}],"name":"Anchored","type":"event"},{"inputs":[{"internalType":"bytes32","name":"key","type":"bytes32"},{"internalType":"bytes32","name":"value","type":"bytes32"}],"name":"anchor","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"bytes32","name":"","type":"bytes32"}],"name":"keyvalues","outputs":[{"internalType":"bytes32","name":"","type":"bytes32"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"bytes32","name":"key","type":"bytes32"}],"name":"lookup","outputs":[{"internalType":"bytes32","name":"","type":"bytes32"}],"stateMutability":"view","type":"function"}], contractAddress);
RockanchorContract.methods.anchor(Web3.utils.fromAscii("hello"), Web3.utils.fromAscii("world !")).send({from: identityAddress}, function(error, result){
console.log(result)
});

By executing:

ROCKSIDE_APIKEY=YOUR_APIKEY IDENTITY_ADDRESS=YOUR_IDENTITY_ADDRESS node index.js

You will get the hash of your transaction.

On etherscan, on inner transactions of the contract you can see your transaction: Rockanchor smartcontract on etherscan