我部署了一个ERC20令牌智能契约和一个exchange智能契约,它允许与以太一起购买令牌。
我将它们部署到主网,使用英弗拉和硬帽子,并能够购买令牌并使用MetaMask查看它们。
我如何才能在智能合同上调用公共方法?
发布于 2021-07-02 07:26:19
您必须使用abi初始化您的合同,并使用web3初始化合同地址,然后调用该函数
const mycontract = new web3.eth.Contract(abi , contractAddress)
mycontract.methods.withDraw("arguments").send({
fom:fromAddress, gas:3000000})
.on('confirmation', (confirmations, receipt) => {
console.log('CONFIRMATION');
console.log(confirmations);
console.log(receipt);
}发布于 2021-07-01 19:54:34
在您的网站上,您可以安装允许您与合同交互的web3。您需要声明为常量:
然后,使用web3,您可以调用任何方法。您可以在您的web3变量中添加一个私钥来对事务进行签名,但也可以只请求一个帐户,这样您就可以使用元问询或其他钱包签署事务。web3.js方法的可测试性:
async function getCurrentAccount() {
const accounts = await ethereum.request({ method: 'eth_requestAccounts' });
return accounts[0];
}
async function withdraw() {
const account = await getCurrentAccount();
const doMethod = await MyContract.methods.withdraw().send({ from: account })
}https://ethereum.stackexchange.com/questions/102810
复制相似问题