参考文档:https://usedapp.readthedocs.io/en/latest/guide.html#custom-hooks
Package.json中的usedapp版本:"@usedapp/core":"^0.11.0“
我使用useCall钩子调用我的函数之一,该函数返回一个显示总供应量的整数。这是我的定制钩子:
import { useCall } from "@usedapp/core"
import { Contract } from '@ethersproject/contracts'
const useTotalSupply = (contractAddress, ethInterface) => {
const ethContract = new Contract(contractAddress, ethInterface)
console.log("Found address for contract object: " + ethContract.address)
const { value, error } = useCall(
ethContract,
'getTotalCurrentSupply',
[]) ?? {}
if (error) {
console.error(error.message)
return -1
}
return value?.[0]
}
export default useTotalSupply这就是我在组件中使用钩子的方式
const MintCounter = () => {
const totalSupply = useTotalSupply(ethContractAddress, ethInterface)
return (
<Box>
<Stat>
<StatLabel>Total Mints</StatLabel>
<StatNumber>{totalSupply}</StatNumber>
</Stat>
</Box>
)
}在构建我的应用程序之后,我看到了以下错误:错误截图
我对这个错误感到有点困惑,因为我正在传递契约实例,我可以看到contract.address值已经设置,并且在控制台输出中看到了地址字符串。我在使用这个钩子时有没有遗漏什么?提前感谢
发布于 2022-04-02 10:24:30
我能够展示替代useCall部件的供应:
const {value: message} = useCall({
contract: ethContract,
method: "totalSupply",
args: []
}) ?? {}
return String(message)环境: ERC721契约,本地硬帽子节点(31337),usedapp 0.12
https://stackoverflow.com/questions/71154740
复制相似问题