1
votes

For a recent test it's been requested me to generate a Smart Contract with Ethernum and use some functions provided through ABI json to extract some info. I'm using https://ropsten.etherscan.io as suggested.

I studied Ethernum for the last 2 days and I tried with Solidity Remix to extract these info but I don't understand how to use the ABI functions with Solidity Remix.

All I have is an Address contract and an ABI contract. Is there someone available to provide me some info? Thanks

1

1 Answers

0
votes

I would recommend you to programmatically do it using a library like web3js, web3js allows you to interact with the Ethereum network (accounts, smart contracts) through the RPC webservice.

In the following example, I deployed a contract called SimpleStorage on a local blockchain with Truffle and Ganache (tools and framework for ethereum).

pragma solidity ^0.4.2;

contract SimpleStorage {
    uint public value;

    function SimpleStorage() {
        value = 1;
    }

    function setValue(uint val) {
        value = val;
    }

    function getValue() returns(uint) {
        return value;
    }
}

Each contract deployed on the Ethereum Blockchain has a ABI (Application Binary Interface) sort of Swagger for your Smart Contract. Programs use the ABI to interact with the Smart Contract via RPC.

Each contract is deployed at a unique address like 0x3450226a2fccb0d3668e7c3a730c43ef50ec8a06

1. Initiate a nodeJS project and add web3js library

$ npm init
$ npm install [email protected] -s

2. Create a JavaScrit file index.js

Inject the dependency

const Web3 = require('web3');

Declare the rpc endpoint of your node. I'm using a local blockchain, but you could easily connect to a Ropsten public node with Infura for example (depends on which network you contract is deployed)

const RPC_ENDPOINT = "http://localhost:8545" //https://ropsten.infura.io

Connect to a Ethereum node

var web3 = new Web3(new Web3.providers.HttpProvider(RPC_ENDPOINT));

Set default account

web3.eth.defaultAccount = web3.eth.accounts[0]

Put your ABI here and the address where the smart contract is deployed

var abi = [...];
var address = "0x3450226a2fccb0d3668e7c3a730c43ef50ec8a06";

Load the contract schema from the abi

var SimpleStorageContract = web3.eth.contract(abi);

Instantiate the contract by address

var simpleStorageContractInstance = SimpleStorageContract.at(address);

Call one of the ABI function

var value = simpleStorageContractInstance.getValue.call();
console.log("value="+value);

Result:

When I invoke the function getValue of my SimpleStorage contract instance, the function returns 1.

value=1

Full code:

const Web3 = require('web3');
const RPC_ENDPOINT = "http://localhost:8545"

// Connection to a Ethereum node
var web3 = new Web3(new Web3.providers.HttpProvider(RPC_ENDPOINT));

// Set default account
web3.eth.defaultAccount = web3.eth.accounts[0]

// ABI describes a smart contract interface developped in Solidity
var abi = [
    {
      "constant": true,
      "inputs": [],
      "name": "value",
      "outputs": [
        {
          "name": "",
          "type": "uint256"
        }
      ],
      "payable": false,
      "stateMutability": "view",
      "type": "function"
    },
    {
      "inputs": [],
      "payable": false,
      "stateMutability": "nonpayable",
      "type": "constructor"
    },
    {
      "constant": false,
      "inputs": [
        {
          "name": "val",
          "type": "uint256"
        }
      ],
      "name": "setValue",
      "outputs": [],
      "payable": false,
      "stateMutability": "nonpayable",
      "type": "function"
    },
    {
      "constant": false,
      "inputs": [],
      "name": "getValue",
      "outputs": [
        {
          "name": "",
          "type": "uint256"
        }
      ],
      "payable": false,
      "stateMutability": "nonpayable",
      "type": "function"
    }
  ];

// Address where the smart contract is deployed
var address = "0x3450226a2fccb0d3668e7c3a730c43ef50ec8a06";

// Load the contract schema from the abi
var SimpleStorageContract = web3.eth.contract(abi);

// Instanciate by address
var simpleStorageContractInstance = SimpleStorageContract.at(address);

// Call one of the ABI function
var value = simpleStorageContractInstance.getValue.call();
console.log("value="+value);

GitHub of the project:

https://github.com/gjeanmart/stackexchange/tree/master/51809356-create-smart-contract-and-use-abi-functions

Ethereum StackExchange

There is a dedicated StackExchange community for Ethereum questions here