'Issue with Web3.js, try to call he contract to see the state
Hi everyone!
I tried to make a call to the state of a contract developed on truffle, using web3.js in React, the problem is this, when I try to see the price of the token (already defined) it throws an error:
This is the code of my App.js:
const [AccountData, setAccountData] = useState({
web3: 'undefined',
account: '',
balance: 0
})
const [Contracts, SetContracts] = useState({
Token: '',
Sale: ''
})
const PreLoad =async()=>{
if (typeof window.ethereum !== 'undefined') {
const web3 = new Web3(window.ethereum)
const netId = await web3.eth.net.getId()
const accounts = await web3.eth.getAccounts()
if (typeof accounts[0] !== 'undefined') {
const balance = await web3.eth.getBalance(accounts[0])
setAccountData({ account: accounts[0], balance: web3.utils.fromWei(balance) })
} else {
window.alert('Please Login with MetaMask')
}
try{
const Token = new web3.eth.Contract(SuperToken.abi, SuperToken.networks[netId.address])
const Sale = new web3.eth.Contract(TokenSale.abi, TokenSale.networks[netId.address])
SetContracts({Token, Sale})
}
catch(err){
console.log(err)
alert('Error, see the console')
}
}
else{
alert('please install MetaMask or any provider!!')
}
}
useEffect( () => {
PreLoad()
}, [])
const BuyToken = async(e)=>{
e.preventDefault()
console.log('Buying')
let price = await Contracts.Sale.methods.tokenPrice().call()
console.log(price)
}
I think I'm following good the documentation from web3, and loading good the contracts on the useEffect, so I don't really know why this is happening. If anyone could help me will be great!!
PS: if you need more info just let me know!
Solution: There was a error when i called the contract with web3, this is the correct wey:
SuperToken.abi, SuperToken.networks[netId].address
Amateur problem, sorry and thanks!
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|