Interacting with Smart Contracts in EW-DOS

Using smart contracts on the Ethereum Virtual Machine

EW-DOS utility layer packages and SDKs interact with smart contracts that are deployed on the Energy Web Chain. To do this, they use API Client Libraries to connect to the blockchain and create instances of smart contracts to access their public functions.

If you're not familiar with what a smart contract is, you can read Ethereum's introduction to smart contracts here.

Below we provide an overview of API Client libraries and a some code examples. Note that exact implementation varies in each package or SDK, but the fundamental concepts remain the same.

API Client Libraries

You can interact with smart contracts (i.e. call their public read and write functions) on the the Energy Web Chain and the Volta Test Network using any Ethereum API client library. Client libraries allow you to connect to and interact with the blockchain network. You can read about all the functionalities of Ethereum API client libraries in the Ethereum documentation here.

The most popular JavaScript libraries for interacting with Ethereum networks are ethers.js and web3.js. EW-DOS uses ethers.js, so below we will provide ethers.js examples, however concepts will be similar among different client libraries.

When interacting with a contract, you use a library to first create a Provider, which is a connection the blockchain. You then use the provider to create an instance of the smart contract that you want to interact with. Once this instance is created, you can call the public methods that are exposed in the smart contract.

1. Instantiate a Provider

A provider is a connection to the blockchain. It gives you an interface to interact with the network. To create a JSONRpcProvider, you will need the JSON RPC Url. To create an IPCProvider, you will need the path local IPC filename (this is only available in node.js).

You can see the full ethers.js documentation on Providers here.

import { providers, Signer, utils, errors, Wallet } from "ethers";
const { JsonRpcProvider } = providers;

this._provider = new JsonRpcProvider({ url: rpcUrl });

See source code here

2. Instantiate a Contract

To create a new instance of a contract that is already deployed on the blockchain, you need three pieces of information:

  1. The contract's ABI- the ABI (Application Binary Interface). The ABI is a JSON object that provides the interface to your smart contract. It defines the contract's constructor, events and the behavior of its public functions. The contract's ABI is generated with the smart contract is compiled.

  2. The contract's address - every smart contract has an address when it is deployed to the blockchain. You can deploy the same contract on multiple blockchains, but each contract will have a different address.

  3. The provider

You can see the ethers.js documentation **** for creating a new instance of a deployed Contract here.

//pass in the smart contract address, smart contract ABI, provider:
this._contract = new Contract(settings.address, this.settings.abi, this._provider);

See source code here

2. Implement Contract Methods

The Contract method returns a new instance of that contract at the address passed in. Now that we have an instance of this contract, we can interact with its public methods:

    try {
      valid = await this._contract.validDelegate(identityAddress, bytesType, delegateAddress);
    } catch (error) {
      throw new Error(error);
    }

See source code here

EW-DOS Smart Contracts

Smart Contracts

Link to Source Code

IAM Smart Contracts

For a tutorial on how to deploy a smart contract on the Volta Test Network or Energy Web Main Network, go here.

Last updated