遵循ERC20規(guī)則,
錢包里可以有很多種類型的token:USDT,DFI,DFA
1.合約交互之前需要先判斷是否仍允許purchasePool合約從賬戶(錢包里的賬戶) 中提取的ERC20dfaContract 這本合約里(DFA)的金額
await this.contract.ERC20dfaContract.methods.allowance(this.address,this.addressMap.AcceleratePool).call();
ERC20dfaContract:這個是dfa合約地址
ERC20dfaContract:new window.web3.eth.Contract(ERC20Token,address.DFAToken)
purchasePool:這個是池子的合約地址
purchasePool:new window.web3.eth.Contract(DistributionInAbi,address.AcceleratePool),
ERC20Token:是ERC20的abi? ,? address.DFAToken:是創(chuàng)建合約的token地址,
this.address這個是錢包的賬戶地址
this.addressMap.AcceleratePool:這個是池子的token地址
注:如果返回值是0,可以直接授權(quán),如果不為0,就不可以授權(quán),如果一定要授權(quán)需要將返回值置為0,再授權(quán)。
2.授權(quán)(ERC20dfaContract這個合約允許這個池子(AcceleratePool)從它這里轉(zhuǎn)走這么多錢)
this.contract.ERC20dfaContract.methods.approve(this.addressMap.AcceleratePool,"轉(zhuǎn)多少錢")
.send({ from: this.address, gas: approveGas })
.then((result) => {}).catch(() => {});
3.充值(池子里的合約方法,自己寫的合約,根據(jù)合約接口操作)?
this.contract.purchasePool.methods.purchase(input).send({from:this.address,gas:getPurchaseGas}).then((result)=>{}).catch(()=>{})
purchase:這個是合約里的充值的方法
4.贖回?(池子里的合約方法,自己寫的合約,根據(jù)合約接口操作)
this.contract.purchasePool.methods.claim().send({ from: this.address, gas: claimGas }).then((result) => {}).catch(() => {});
注:充值,贖回這些不是查詢的,是需要消耗gas的,所以每次轉(zhuǎn)賬,贖回時需要查詢消耗的gas?(上文中的 getPurchaseGas)
async getPurchaseGas(){return await this.contract.purchasePool.methods.purchase(this.input).estimateGas({from:this.address})},文章來源:http://www.zghlxwxcb.cn/news/detail-470482.html
*這個是充值消耗gas例子文章來源地址http://www.zghlxwxcb.cn/news/detail-470482.html
到了這里,關(guān)于區(qū)塊鏈合約交互 web3的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!