arrow-left

All pages
gitbookPowered by GitBook
1 of 1

Loading...

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.

circle-info

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

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.

hashtag
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 .

The most popular JavaScript libraries for interacting with Ethereum networks are and . 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.

hashtag
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 , you will need the JSON RPC Url. To create an , 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 .

See source code

hashtag
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 .

See source code

hashtag
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:

See source code

hashtag
EW-DOS Smart Contracts

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

Energy Web

Contracts

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

this._provider = new JsonRpcProvider({ url: rpcUrl });
//pass in the smart contract address, smart contract ABI, provider:
this._contract = new Contract(settings.address, this.settings.abi, this._provider);
    try {
      valid = await this._contract.validDelegate(identityAddress, bytesType, delegateAddress);
    } catch (error) {
      throw new Error(error);
    }

Smart Contracts

Link to Source Code

IAM Smart Contracts

https://github.com/energywebfoundation/iam-contractsarrow-up-right

Origin Smart Contracts

https://github.com/energywebfoundation/origin/tree/master/packages/traceability/issuer/contractsarrow-up-right

Volta System Contracts

https://github.com/energywebfoundation/volta-system-contractsarrow-up-right

herearrow-up-right
ethers.jsarrow-up-right
web3.jsarrow-up-right
JSONRpcProviderarrow-up-right
IPCProviderarrow-up-right
herearrow-up-right
herearrow-up-right
herearrow-up-right
herearrow-up-right
herearrow-up-right
here
Deploy a Smart Contract on Volta with Remixchevron-right
System Contracts
https://github.com/energywebfoundation/ewc-system-contractsarrow-up-right
Staking
https://github.com/energywebfoundation/staking-contractsarrow-up-right