I have a token erc20 .. and I created a method to buy this token.. and I want him to pay the GAS fees from his account using the metamask wallet

and this is my code ..

interface IERC20 {
    function balanceOf(address account) external view returns (uint256);

    function transfer(address recipient, uint256 amount) external returns (bool);

contract BuyTokens {
    IERC20 token;

    function withdraw(address beneficiary) public {

        uint256 value = balanceOf(_admin).mul(5).div(100);
        require(balanceOf(_admin) > value);
        token.transfer(beneficiary, value);


plz help me

1 Answer 1


The gas fees are by default always paid by the account that signed the Ethereum transaction (in your case probably in most cases the account that calls your contract)

An alternative to this would be adding support for OpenGSN

  • Is my code correct?
    – iyas seyam
    Dec 27, 2021 at 13:49
  • Actually didn't check :D Looking at your contract I have some questions: What is _admin? How do you wand to sell the token (e.g. for ETH)? Also you are asking multiple questions at once. I would recommend to split them. I only answered the fee related part, as this was the one from the title.
    – Richard
    Dec 27, 2021 at 16:18

Not the answer you're looking for? Browse other questions tagged or ask your own question.