[英]ERC20 Payment processing
我的智能合約出了什么問題,因為我收到“錯誤:無法估算氣體;交易可能失敗或可能需要手動氣體限制”。 在前端,我先調用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);
}
}
用戶需要直接在代token
地址上調用approve()
——而不是通過你的合約。
您當前的實現批准owner
使用PaymentProcessor
的代幣,因為PaymentProcessor
是代token
合約上下文中的msg.sender
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.