英文:
How to make Node.JS function that calls solidity withdraw function?
问题
以下是翻译好的部分:
我无法正确地让JS调用我的Solidity函数来提取资金。我已经有了Solidity代码:
```javascript
function withdrawFunds() public {
require(msg.sender == owner, "You are not the owner");
(bool success, ) = payable(owner).call {
value: address(this).balance
}("");
require(success);
}
它可以工作,但我无法弄清楚如何在JS中正确调用它。
到目前为止,我在JS中有以下代码:
const withdrawFunds = async () => {
try {
await Contractoftickets.methods.withdrawFunds().call({
from: address,
})
setSuccessMsg(`Funds withdrawn!`)
}
}
它执行了,但当前没有提取资金。
<details>
<summary>英文:</summary>
I can't get JS properly to call my solidity function withdraw funds. I have got solidity:
```javascript
function withdrawFunds() public {
require(msg.sender == owner, "You are not the owner");
(bool success, ) = payable(owner).call {
value: address(this).balance
}("");
require(success);
}
And it works, but I cant figure out how to properly call it in JS.
So far I have this in JS:
const withdrawFunds = async () => {
try {
await Contractoftickets.methods.withdrawFunds().call({
from: address,
})
setSuccessMsg(`Funds withdrawn!`)
}
}
It executes but it currently does not withdraw funds.
答案1
得分: -1
使用ethers.js,你可以这样做:
const withdrawFunds = async () => {
try {
const Contractoftickets = ethers.getContract(contractAddress, contractInterfaceOrABI)
await Contractoftickets.withdrawFunds()
setSuccessMsg(`Funds withdrawn!`)
}
}
我建议你查看ethers文档以获取更多详细信息。简单提醒,在上述使用之前,你必须安装ethers并连接你的钱包。
英文:
With ethers.js you can do it like this:
const withdrawFunds = async () => {
try {
const Contractoftickets = ethers.getContract(contractAddress, contractInterfaceOrABI)
await Contractoftickets.withdrawFunds()
setSuccessMsg(`Funds withdrawn!`)
}
}
I'll recommend you to go through ethers docs for more details. A simple note, you've to install ethers and connect your wallet with it before using it like above.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论