简体   繁体   English

如何使用 Web3.js 批准一个 Token 的花费?

[英]How to Approve spend of one Token with Web3.js?

I seek via my web page of test to create a button which allows to authorize the expenditure of a Contract (Token).. If I go to the page and I click I would like the script to load web3 (it loads well) then if I press the button that Metamask authorizes the spending of the contract.我通过我的 web 测试页面寻求创建一个按钮,该按钮允许授权合同(令牌)的支出。如果我将 go 到页面并单击我希望脚本加载 web3(它加载良好),那么如果我按下 Metamask 授权合约支出的按钮。

Metamask opens fine and does request the connection for my test site on the BSC in Web3js. Metamask 可以正常打开并请求连接我在 Web3js 中的 BSC 上的测试站点。 However I can't find the exact code for the approve function.但是我找不到批准 function 的确切代码。

Here is the code:这是代码:

<head>
<script src='https://cdnjs.cloudflare.com/ajax/libs/web3/1.7.0/web3.min.js'></script>
</head>
 
<button onclick="approvebutton();">Approve button to authorize tokens to be spent</button>

<script type="text/javascript">
if (typeof window.ethereum !== 'undefined') {
    ethereum.request({ method: 'eth_requestAccounts' });
} else {
    alert('Please install metamask')
}

var Web3 = require('web3');
const web3 = new Web3('https://bsc-dataseed1.binance.org:443');

 async function approvebutton() {

 /// APPROVE FUNCTION WITH THE CONTRACT 
 
 }
        
        
  </script>

I tried this approach but it doesn't work (metamask confirmation won't show up):我尝试了这种方法,但它不起作用(元掩码确认不会出现):

if (typeof window.ethereum !== 'undefined') {
    ethereum.request({ method: 'eth_requestAccounts' });
} else {
    alert('Please install metamask')
}

var Web3 = require('web3');
const web3 = new Web3('https://bsc-dataseed1.binance.org:443');
const Contract = ('0xContractAddress');
const spenderAdr = ('0xSpenderAddress');
const amount = ('AmountTokensNumber')


async function approvebutton(Contract,spenderAdr){
  Contract.methods.approve(spenderAddr, amount).send({
   from: ownerAddr
})
}

Metamask won't show up to confirm the TX. Metamask 不会出现以确认 TX。

First of all, the approve method takes 2 parameters, the spender and the amount so it will be something like this:首先, approve方法有 2 个参数, spenderamount ,所以它是这样的:

Contract.methods.approve(spenderAddr, amount).send({
   from: ownerAddr
})

The gas parameter is optional. gas参数是可选的。

From the example code, I think you're missing the ABI (or Json Interface) for the contract and the instantiation of the contract via web3.eth.Contract() with the ABI and the contract address.从示例代码中,我认为您缺少合约的 ABI(或 Json 接口)以及通过 web3.eth.Contract() 与 ABI 和合约地址进行合约的实例化。

var Contract = new web3.eth.Contract(jsonInterface[, address][, options])

From that Contract instance you can then call the methods in the ABI, and that should trigger the Metamask modal.然后,您可以从该 Contract 实例调用 ABI 中的方法,这应该会触发 Metamask 模式。

From the docs:从文档:

Contract.methods.myMethod(123).send({from: '0xde0B295669a9FD93d5F28D9Ec85E40f4cb697BAe'})
.then(function(receipt){
    // receipt can also be a new contract instance, when coming from a "contract.deploy({...}).send()"
});

Or in your case, something along the lines of:或者在您的情况下,类似于以下内容:

await Contract.methods.approve(spenderAddr, amount).send({ from: ownerAddr })

I also think that you're missing an await inside the approvebutton() function, to await the "promisevent" that the method call on the contract returns.我还认为您在approvebutton() function 中缺少等待,以等待合约上的方法调用返回的“承诺事件”。

( Source https://web3js.readthedocs.io/en/v1.2.11/web3-eth-contract.html# , https://web3js.readthedocs.io/en/v1.2.11/web3-eth-contract.html#id26 ) 来源 https://web3js.readthedocs.io/en/v1.2.11/web3-eth-contract.html#,https ://web3js.readthedocs.io/en/v1.2.11/web3-eth-contract.html #id26 )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM