合约部署在bsc测试网。前端使用ethers.js调用approve已经成功授权了金额。在后端使用webp/web3.php 操作合约函数transferFrom,一直提示我Error: execution reverted: BEP20: transfer amount exceeds allowance。approve
也授权成功了。bsc浏览器上可以查询到授权金额,
合约代码如下:
'constructor()',
'event Approval(address indexed owner, address indexed spender, uint256 value)',
'function approve(address spender, uint256 amount) returns (bool)',
'function burn(uint256 amount) returns (bool)',
'function decreaseAllowance(address spender, uint256 subtractedValue) returns (bool)',
'function increaseAllowance(address spender, uint256 addedValue) returns (bool)',
'function mint(uint256 amount) returns (bool)',
'event OwnershipTransferred(address indexed previousOwner, address indexed newOwner)',
'function renounceOwnership()',
'function transfer(address recipient, uint256 amount) returns (bool)',
'event Transfer(address indexed from, address indexed to, uint256 value)',
'function transferFrom(address sender, address recipient, uint256 amount) returns (bool)',
'function transferOwnership(address newOwner)',
'function _decimals() view returns (uint8)',
'function _name() view returns (string)',
'function _symbol() view returns (string)',
'function allowance(address owner, address spender) view returns (uint256)',
'function balanceOf(address account) view returns (uint256)',
'function decimals() view returns (uint8)',
'function getOwner() view returns (address)',
'function name() view returns (string)',
'function owner() view returns (address)',
'function symbol() view returns (string)',
'function totalSupply() view returns (uint256)'
/**
* @dev See {BEP20-transferFrom}.
*
* Emits an {Approval} event indicating the updated allowance. This is not
* required by the EIP. See the note at the beginning of {BEP20};
*
* Requirements:
* - `sender` and `recipient` cannot be the zero address.
* - `sender` must have a balance of at least `amount`.
* - the caller must have allowance for `sender`'s tokens of at least
* `amount`.
*/
function transferFrom(address sender, address recipient, uint256 amount) external returns (bool) {
_transfer(sender, recipient, amount);
_approve(sender, _msgSender(), _allowances[sender][_msgSender()].sub(amount, "BEP20: transfer amount exceeds allowance"));
return true;
}
WEB3 操作函数如下:
public static function transferFrom_bep20($from_address, $to_address, $amount){
/*
发送erc20代币给指定地址指定数量
from_address:发送方地址
to_address:接收方地址
amount:发送erc20代币数量
*/
$abi = Config::get('abi.bsc')['abi'];
$bytecode = Config::get('abi.bsc')['bytecode'];
$contractaddress = Config::get('abi.bsc')['contract_address'];
$rpcurl = Config::get('abi.bsc')['rpcurl'];
$Web3 = new Web3($rpcurl , 60);
$contract = new Contract($Web3->getProvider(), $abi);
$amountInWei = Utils::toWei($amount, 'ether'); // 将ETH单位转换为wei单位
$contract->at($contractaddress)->call('transferFrom', $from_address , $to_address, $amountInWei, function($err, $result) {
print_r($result);
if ($err !== null) {
echo 'Error: ' . $err->getMessage();
return;
}
print_r($result);
});
}
一直报错: Error: execution reverted: BEP20: transfer amount exceeds allowance .
这个问题纠结我两天了。
相似问题