Logo

dev-resources.site

for different kinds of informations.

Avoiding Call Revert Exception Error when accessing Truffle Ganache via Ethers in Node

Published at
7/20/2020
Categories
ethereum
truffle
blockchain
node
Author
preciouschicken
Categories
4 categories in total
ethereum
open
truffle
open
blockchain
open
node
open
Author
15 person written this
preciouschicken
open
Avoiding Call Revert Exception Error when accessing Truffle Ganache via Ethers in Node

Introduction

My default way of connecting to a local instance of the Ethereum blockchain using Truffle Ganache is via the browser using Metamask. Using ethers to connect via node.js is however a little different. As the Ethers documentation, at time of writing, contains few specific mentions of Truffle (primarily I suspect as the lead developer doesn't use it), I got a couple of call revert exception errors before I figured out what I was doing wrong.

Setup

Just in case you are completely new to this, some initial set up steps at the terminal:

mkdir justsayhi
cd justsayhi
truffle init
npm init -y
npm install ethers
Enter fullscreen mode Exit fullscreen mode

I am assuming you have already installed Node, Truffle and Ganache, if not see my earlier post PreciousChickenToken: A guided example of OpenZeppelin's ERC20 using Ethers, Truffle and React.

Now would also be a good time to fire up Ganache, selecting the Quickstart Ethereum option when offered.

The JavaScript

Create a file node_server.js and copy and paste:

var ethers = require('ethers');
var JustSayHi = require('./build/contracts/JustSayHi.json');

// This is the localhost port Ganache operates on
const url = "http://127.0.0.1:7545";
const provider = ethers.providers.getDefaultProvider(url);

const contractAddress ='0x02e68a4a2B539451F7d02b166B3376DBc7473F75';

// Connect to the network
// We connect to the Contract using a Provider, so we will only
// have read-only access to the Contract
let contract = new ethers.Contract(contractAddress, JustSayHi.abi, provider);

try {
    contract.sayHi().then(msg => console.log(msg));
} catch (e) {
    console.log(e);
}
Enter fullscreen mode Exit fullscreen mode

The SmartContract

Create a file contracts/JustSayHi.sol and copy and paste:

// SPDX-License-Identifier: Unlicencse
pragma solidity ^0.5.1;

contract JustSayHi {
    function sayHi() public pure returns (string memory) {
        return "Hi";
    }
}
Enter fullscreen mode Exit fullscreen mode

Create a file migrations/2_deploy_contract.js and copy and paste:

var JustSayHi = artifacts.require("JustSayHi");

module.exports = function(deployer) {
  // Arguments are: contract
  deployer.deploy(JustSayHi);
};
Enter fullscreen mode Exit fullscreen mode

Deploy the contract

From the terminal run:

truffle deploy
Enter fullscreen mode Exit fullscreen mode

If all good we should get a screen that looks like:

Truffle deploying JustSayHi

Copy the highlighted contract address above and paste it into the contractAddress variable line in node_server.js, which is this line in my example above:

const contractAddress ='0x02e68a4a2B539451F7d02b166B3376DBc7473F75';
Enter fullscreen mode Exit fullscreen mode

Run using Node.js

From the terminal:

node node_server.js
Enter fullscreen mode Exit fullscreen mode

And if all is well you should see a nice, friendly Hi in response.

Configuration

At time of writing I'm using: Truffle v5.1.34 (core: 5.1.34), Solidity v0.5.16 (solc-js), Node v14.4.0, Web3.js v1.2.1, ethers v5.07 and Ubuntu 20.04 LTS.

truffle Article's
30 articles in total
Favicon
Hardhat vs Truffle: Which One Is the Best for Developing Ethereum dApps?
Favicon
Help Me With Ubuntu Terminal...
Favicon
How to write, test and deploy Ethereum smart contracts using Truffle
Favicon
Create & deploy an ERC-20 token in 15 minutes (Truffle, OpenZeppelin, Goerli)
Favicon
How to Run Ganache in a Browser
Favicon
121 ethereum truffle : Writing automated smart contract tests
Favicon
11Z ethereum truffle : Deploying and interacting with smart contracts
Favicon
11X ethereum truffle : Developing smart contracts
Favicon
how to use web3.js instead of Ethers in react Dapp and connect to hardhat node
Favicon
Truffle React box using functional Components
Favicon
Build a simple dApp using truffle, ganache, Ethers.js and React(1)
Favicon
How To Mint an NFT on Polygon
Favicon
Hardhat vs Truffle: Which one is better for writing Smart Contracts?
Favicon
Interactuar con contratos en Ethereum
Favicon
Tutorial: Play with Truffle & Ganache
Favicon
has no network configuration for its current network id (5777).
Favicon
Roadmap to become a Blockchain developer
Favicon
How to Fork Ethereum and Replay Historical Transactions with Ganache 7 Archive Support
Favicon
Deploy your smart contracts on any Ethereum network
Favicon
TruffleでGoerliγƒγƒƒγƒˆγƒ―γƒΌγ‚―γ«γ‚³γƒ³γƒˆγƒ©γ‚―γƒˆγ‚’γƒ‡γƒ—γƒ­γ‚€γ™γ‚‹
Favicon
A Guide to Building, Testing, and Deploying your First DApp with Truffle, Ethers.js, Ganache, and React.
Favicon
Introduciendo Web3 con Angular usando Truffle Box β€” Aplicaciones Descentralizadas al alcance de todos
Favicon
HardHat: "Hola Mundo!" en un Blockchain de Prueba
Favicon
Using React with Truffle
Favicon
Interacting with Truffle
Favicon
Introducing Truffle
Favicon
How to configure Truffle to connect to RSK
Favicon
Avoiding Call Revert Exception Error when accessing Truffle Ganache via Ethers in Node
Favicon
Short, sharp Solidity: pure vs view vs call
Favicon
Decomposing a BigNumber in Truffle Console

Featured ones: