[英]Solidity Transaction Issue
i'm facing some weird issues, or well, things i'm not understanding.我面临一些奇怪的问题,或者是我不理解的事情。 I'm still pretty new with solidity tho, anyway, i'm trying to create a staking contract, based on an ERC20 token i created.
无论如何,我还是很新的solidity,我正在尝试基于我创建的ERC20令牌创建一个质押合约。 I call the stake function with ethers.js and pass the amount with it.
我用 ethers.js 调用权益 function 并传递金额。 The staking contract saves some info and forwards receiver address and amount to the ERC20 transfer function.
质押合约保存一些信息并将接收方地址和金额转发到 ERC20 转账 function。
async function stake () {
await stakeContract.stake(1);
}
function stake (uint256 _amount) public {
require(_amount > 0, "You must stake more than 0");
require(_amount < ercToken.balanceOf(msg.sender), "The amount exceeds your balance");
addressToStaked[msg.sender].push(Stakes(block.timestamp, _amount));
totalStakes[msg.sender] += 1;
ercToken.transfer(address(ercToken), _amount);
}
The transfer function then forwards the data to the internal _transfer function shown below.传输 function 然后将数据转发到内部 _transfer function 如下所示。 The problem is that, even if i do have enough tokens in my wallet, the _transfer function still fails with error: Amount exceeds balance.
问题是,即使我的钱包里确实有足够的代币,_transfer function 仍然会失败并出现错误:金额超过余额。
I've double checked the mapping saving the balance and it works.我已经仔细检查了保存余额的映射并且它有效。
function _transfer(
address from,
address to,
uint256 amount
) internal virtual {
require(from != address(0), "ERC20: transfer from the zero address");
require(to != address(0), "ERC20: transfer to the zero address");
_beforeTokenTransfer(from, to, amount);
uint256 fromBalance = _balances[from];
require(fromBalance >= amount, "ERC20: transfer amount exceeds balance");
unchecked {
_balances[from] = fromBalance - amount;
_balances[to] += amount;
}
emit Transfer(from, to, amount);
_afterTokenTransfer(from, to, amount);
}
It seems you made a mistake on this line of the staking contract.看来你在质押合同的这条线上犯了一个错误。
function stake (uint256 _amount) public {
...
ercToken.transfer(address(ercToken), _amount);
}
The above line means, transfer "_amount" token to "ercToken" address from staking contract.上述行的意思是,将“_amount”代币从质押合约转移到“ercToken”地址。 So there should be some amounts ( >_amount ) of tokens on staking contract.
因此,在质押合约上应该有一定数量( >_amount )的代币。 But as you mentioned, you only have enough tokens on your wallet, not on staking contract.
但正如你所提到的,你的钱包里只有足够的代币,而不是 Staking 合约。
Plz use "transferFrom" function or "safeTransferFrom" function of "SafeERC20" library to solve the issue.请使用“SafeERC20”库的“transferFrom”function 或“safeTransferFrom”function 来解决问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.