Interact with smart contracts

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 worrying about the gas.

Create an identity

An identity is an account that you can use to send transaction without having to manage the gas fees. To known more about Identities, check out our documentation at Rockside identities

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 this contract address representing your Identity onchain. You will use it to interact with your contract logic.

Smart contract

Considering a very simple smart contract that saves 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 from your nodejs application, you typically need its ABI.

Install solcjs:

npm install -g solc

Then generate the ABI with:

solcjs rockanchor.sol --abi

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

NodeJS application

Create a new project with npm:

npm init

Install web3:

npm install web3 -save

Then using Rockside as a Web3 provider is really simple, just create an HttpProvider with your API_KEY in the correct 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 using the Identity created beforehand. Rockside will manage the gas fees 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 (i.e. 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)
});

Then get the hash of your transaction with:

ROCKSIDE_APIKEY=YOUR_APIKEY IDENTITY_ADDRESS=YOUR_IDENTITY_ADDRESS node index.js

.On Etherscan, on the Internal Transactions section of the contract, you will be able to see your transaction: Rockanchor smartcontract on etherscan