簡體   English   中英

ERC20 支付處理

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

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM