Solidity - 转账金额超过消费限额 [英] Solidity - transfer amount exceeds spender allowance

查看:233
本文介绍了Solidity - 转账金额超过消费限额的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

代码看起来正确,在转移前批准令牌,但失败并出现错误:转移金额超过了消费者限额";发件人帐户的令牌余额超过了要求.

有什么想法吗?合约:

解决方案

当你的合约执行 tokenAddress 函数 approve() 时,msg.sender<approve() 函数中的/code> 是您的合同(不是发起交易的用户).

有效地批准了您的合同";(第一个参数 address(this)) 花费你的合约"的 (msg.sender) 资金.

然后它试图transferFrom()用户地址,但它没有获得批准.


如果您希望您的合约花费属于另一个用户的代币,他们需要从他们的地址直接执行approve(, )强>(不是通过合同).

注意:请参阅此答案中的 TLDR,它显示了此要求的用途.

The code looking correct,approving token before transfering, but failing with error: "transfer amount exceeds spender allowance" Sender account is having more than required token balance.

Any ideas? Contract: https://rinkeby.etherscan.io/address/0x2a855c479b14443489963eef90ad449ecdf40cf5#writeContract

pragma solidity ^0.7.0;  


interface IERC20 {
   function transferFrom(address sender, address recipient, uint256 amount) external returns (bool);
   function approve(address spender, uint256 amount) external returns (bool); 
   function transfer(address recipient, uint256 amount) external returns (bool);
} 
 
contract FundTokenContract { 
    address public owner;   
     
    constructor(){ 
        owner = msg.sender;
    }   
    
    modifier OnlyOwner() {
        require(msg.sender == owner, "Only owner can call this function.");
        _;
    } 
    
     function fundToken(address tokenAddress, uint amount ) external {  
        require(amount > 0);  
        IERC20(tokenAddress).approve(address(this), amount); 
        IERC20(tokenAddress).transferFrom(msg.sender ,address(this), amount);  
    }  
      
      
    function withdrawToken(address _tokenAddress, address  _recipient, uint _amount) public  OnlyOwner returns (bool){  
        IERC20(_tokenAddress).transfer(_recipient, _amount);
        return true;
    }  
}

解决方案

When your contract executes the tokenAddress function approve(), the msg.sender within the approve() function is your contract (not the user initiating the transaction).

Which effectively approves "your contract" (first argument address(this)) to spend "your contract"s (msg.sender) funds.

Then it's trying to transferFrom() the user address, but it doesn't have the approval.


If you want your contract to spend tokens that belong to another user, they need to execute the approve(<yourContract>, <amount>) from their address directly (not through a contract).

Note: See the TLDR in this answer, it shows how this requirement is useful.

这篇关于Solidity - 转账金额超过消费限额的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆