Skip to content

Instantly share code, notes, and snippets.

@czhc
Last active June 29, 2021 09:05
Show Gist options
  • Select an option

  • Save czhc/d6d22835c0215018f43f747d12a78bc7 to your computer and use it in GitHub Desktop.

Select an option

Save czhc/d6d22835c0215018f43f747d12a78bc7 to your computer and use it in GitHub Desktop.
Interacting with contracts with web3.js and ethers.js
const ethers = require('ethers');
const provider = new ethers.providers.JsonRpcProvider('http://localhost:7545'); //using Ganache
const network = provider.getNetwork();
const signer = provider.getSigner();
const artifact = require('./client/src/contracts/Item.json');
const contractAddress = '0xE0dc8879F4CF0c2273DF277A570F3884B98A692C';
const contract = new ethers.Contract(contractAddress, artifact.abi, signer);
let paidWei = (await contract.paidWei()).toString()
//from truffle console
//Item.sol is a child contract created from a parent contract ItemManager. Item addresses are created per ItemManager.creatItem call
const Web3 = require('web3');
const web3 = new Web3('http://localhost:7545');
const Abi = require('./client/src/contracts/Item.json');
const instance = new web3.eth.Contract(Abi.abi, "0xE0dc8879F4CF0c2273DF277A570F3884B98A692C"); //replace with contract address
const params = { from: accounts[0] };
// Invoke contract methods
instance.methods.paidWei().call(params);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment