I created a simple dapp with react/truffle/web3 that allows users to enter a number of tokens and submit it to stake it, the problem that I got is that when clicked on submit button, Metamask popup to confirm the transaction(approuve) but I don't get the second popup to confirm stakeTokens function.
N.b(by default a give I put 100 tokens in their account to stake)
stake Token in solidity
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;
}
stake token in React
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")
});
};
submit function
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;