简体   繁体   English

Solidity 交易问题

[英]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.

相关问题 带有未签名交易 object 的 Solidity 交易 - Solidity transaction with an unsigned transaction object Solidity 转移问题 - Solidity Transfer Issue Solidity Remix 交易恢复到初始状态 - Solidity Remix transaction reverted to initial state 找出一个可靠的接口问题 - Figuring out a solidity Interface issue 使用 Remix IDE 在 Solidity 中购买代币交易时出现“资金不足” - "Not Enough Fund" when doing buying token transaction in Solidity with Remix IDE Web3功能的回拨问题-牢固性 - Web3 Call Back Issue for Function - Solidity 进行交易时的 Solidity 错误 - 无法估算气体; 交易可能会失败或可能需要手动限制气体 - Solidity Error when doing a transaction - cannot estimate gas; transaction may fail or may require manual gas limit 当我测试我的可靠性代码时,我收到“AssertionError: Expected transaction to be reverted with Username” - I am getting "AssertionError: Expected transaction to be reverted with Username " when I am testing my solidity code 嗨,我有一个 Solidity 初学者问题:我正在尝试部署带有已签名交易的合同 - Hi, I have a Solidity beginner question: I am trying to deploy a contract with signed transaction 我们是否可以使用智能合约中的Solidity获取过去区块中记录的交易信息? - Can we get transaction information recorded in the past block using Solidity in the Smart contract?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM