[英]ERC20 Payment processing
What's wrong with my smart contract, because I get "Error: cannot estimate gas; transaction may fail or may require manual gas limit".我的智能合约出了什么问题,因为我收到“错误:无法估算气体;交易可能失败或可能需要手动气体限制”。 On frontend I am calling approveTokens() first and acceptPayment() later在前端,我先调用approveTokens(),然后再调用acceptPayment()
pragma solidity ^0.8.11;
import '@openzeppelin/contracts/token/ERC20/IERC20.sol';
contract PaymentProcessor {
address public owner;
IERC20 public token;
constructor(address _owner, address _token) {
owner = _owner;
token = IERC20(_token);
}
event PaymentCompleted(
address payer,
uint256 amount,
uint paymentId,
uint timestamp
);
function approveTokens(uint256 amount) public returns(bool){
token.approve(owner, amount);
return true;
}
function getAllowance() public view returns(uint256){
return token.allowance(msg.sender, owner);
}
function acceptPayment(uint256 amount, uint paymentId) payable public {
require(amount > getAllowance(), "Please approve tokens before transferring");
token.transfer(owner, amount);
emit PaymentCompleted(msg.sender, amount, paymentId, block.timestamp);
}
}
Users need to call approve()
directly on the token
address - not through your contract.用户需要直接在代token
地址上调用approve()
——而不是通过你的合约。
Your current implementation approves owner
to spend PaymentProcessor
's tokens because PaymentProcessor
is the msg.sender
in the context of the token
contract.您当前的实现批准owner
使用PaymentProcessor
的代币,因为PaymentProcessor
是代token
合约上下文中的msg.sender
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.