Last active
June 29, 2021 09:05
-
-
Save czhc/d6d22835c0215018f43f747d12a78bc7 to your computer and use it in GitHub Desktop.
Interacting with contracts with web3.js and ethers.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //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