Call a Smart Contract

This guide is out of date, you can connect to's provided RPC ports


Working with Ethereum smart contracts in Elastos is quite similar as doing it on the main Ethereum blockchain. You can use usual tools such as solc, remix, truffle, RPC API calls, web3 library…

There are some specificities though, which are mostly:

  • A different RPC address for the Elastos Ethereum sidechain.
  • A different way to get ELA or transfer ELA (from the Elastos mainchain) to the ETH sidechain.

Here are a few easy steps to help you run your smart contract, or execute someone else’s smart contract on the Elastos ETH sidechain.

Ethereum library setup

# Install the standard web3js library provided by Ethereum. This is all we need.
npm install --save web3

Connecting to the ETH node

Your application must first connect to your Ethereum node (ex: on your local develap instance),

import Web3 from 'web3';

// Open a connection to the node that provides RPC APIs access to the ETH sidechain
// Assume that the Ethereum node is on your local computer, running on port 21636.
this.provider = new Web3.providers.HttpProvider("http://YOUR_LOCAL_COMPUTER_IP:21636")
this.web3 = new Web3(this.provider)

Loading contract information

After your contract was generated by truffle during the contract deployment process, a JSON file was created.

You first have to copy this JSON file to your client application project in order to call its methods.

let myContractJson: any = null;
let myContract: Web3.eth.Contract = null;
let contractAddress = "0xAfbE12D1405E624B181AED0195F5d95F99E1b0DF"

// Load JSON payload of a previously deployed contract.
let myContractJson = require("PATH_TO_YOUR_CONTRACT.json")
let myContract = new this.web3.eth.Contract(myContractJson.abi, contractAddress);

Calling a read-only method

Contracts contain methods that can alter their state (ex: store a value), but also read-only ones (ex: just read an information).

To call a read-only method, not much has to be provided:

myContract.methods.MY_READ_ONLY_METHOD().call({}).then((res) => {
}).catch((e, r) => {
    console.log(JSON.stringify(e, Object.getOwnPropertyNames(e)))

Calling a state changing method

If a method alters contract state, this has to be requested to the blockchain through a transaction, and requests some gas to pay for it.

As Elastos wallet apps are not ready to execute ETH transactions yet, this sample code directly uses user's wallet address. Later on, the wallet app will be responsible to sign and broadcast ethereum transactions like this to the ETH sidechain.

 // Estimate gas cost, so we can use this estimation as max gas amount.
let method = mContract.methods.MY_WRITE_METHOD(10, otherParams...)
let gas = await method.estimateGas({

// Send the transaction to the blockchain.
    gas: gas
}).then((res) => {
}).catch((e, r) => {
    console.log(JSON.stringify(e, Object.getOwnPropertyNames(e)))