我创建了一个简单的dapp,它包含了function /块菌/web3,允许用户输入多个令牌并将其提交到指定位置,问题是当单击submit按钮时,Metamask弹出以确认事务(Approuve),但我没有第二个弹出来确认stakeTokens函数。
注:(在默认情况下,我将100枚代币存入他们的账户中)
实心木桩标志
function stakeTokens(uint _amount) public {
// Require amount greater than 0
require(_amount > 0, "amount cannot be 0");
// Transfer tokens from the investor wallet to "this" farm
daiToken.transferFrom(msg.sender, address(this), _amount);
// Store deposit balance
stakingBalance[msg.sender] = stakingBalance[msg.sender] + _amount;
// To store inside the array *Only* who haven't stake yet(The new investors);
// if hasStacked is false then will be true ! then will execute the code
if(!hasStaked[msg.sender]){
stakers.push(msg.sender);
}
//update staking status
isStaking[msg.sender] = true;
hasStaked[msg.sender] = true;
}桩标反应
const stakeTokens = async (amount) => {
await dTokens.methods.approve(FarmAddress, amount).send({ from : investorAccount}).on("transitionsHash", (hash)=> {
fTokens.methods.stakeTokens(amount).send({from : investorAccount})
}).on("transitionsHash", (hash)=> {
console.log("stacking has been validated")
});
};提交功能
const stackHandler = (e) => {
e.preventDefault();
const amount = refInput.current.value;
const newAmount = amount.toString();
const myAmount = web3.utils.toWei(newAmount, "ether");
stakeTokens(myAmount);
}web3 js
import Web3 from "web3";
window.ethereum.request({method: "eth_requestAccounts" });
const web3 = new Web3(window.ethereum);
export default web3;发布于 2022-01-08 14:45:06
你的交易“链接”可能有点离题。您的第二个transitionsHash侦听器仍然链接到第一个tx。尝试以下几点:
const stakeTokens = (amount, onSuccess) => {
dTokens.methods
.approve(FarmAddress, amount)
.send({ from: investorAccount })
.on('transitionsHash', hash => {
fTokens.methods
.stakeTokens(amount)
.send({ from: investorAccount })
.on('transitionsHash', hash => {
console.log('stacking has been validated')
onSuccess(); // Example of where to use a callback
})
})
}建议:
https://ethereum.stackexchange.com/questions/118323
复制相似问题