如何创建一个Node.JS函数来调用Solidity的提现函数?

huangapple go评论77阅读模式
英文:

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&#39;t get JS properly to call my solidity function withdraw funds. I have got solidity:

```javascript
function withdrawFunds() public {
    require(msg.sender == owner, &quot;You are not the owner&quot;);
    (bool success, ) = payable(owner).call {
        value: address(this).balance
    }(&quot;&quot;);
    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 () =&gt; {
    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 () =&gt; {
   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.

huangapple
  • 本文由 发表于 2023年1月6日 04:39:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/75024110.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定