JSanchezFDZ
JSanchezFDZ

Reputation: 27

How can accept other token in my Smart Contract?

The token is ERC721 by OpenZeppelin

I'm trying to add USDC payments to my Smart Contract, but I'm having problems.

This is my function to accept token.. but transfer its called by Smart Contract. Dont work...

function AcceptPayment(uint32 amount) public {
   tokenUSDC.transfer(address(this), amount * priceCapsule);
   bulkMint(_msgSender(), amount);
}

Allowance and approve its working fine, testing with Remix.

New try.. but get: Execution reverted on Remix

function AcceptPayment(uint32 amount) public {
   bool success = tokenUSDC.transferFrom(msg.sender, address(this), amount * priceCapsule);
   require(success, "Could not transfer token. Missing approval?");
   bulkMint(_msgSender(), amount);
}

I can't find a solution to make the transfer from the Smart Contract itself.

Upvotes: 1

Views: 1623

Answers (1)

Petr Hejda
Petr Hejda

Reputation: 43481

The user needs to approve() your contract address to pull their tokens. This action needs to be called directly from the user address (on the token contract).

Then you can transferFrom() their address to yours.

function AcceptPayment(uint32 amount) public {
   bool success = tokenUSDC.transferFrom(msg.sender, address(this), amount * priceCapsule);
   require(success, "Could not transfer token. Missing approval?");
   bulkMint(_msgSender(), amount);
}

Upvotes: 1

Related Questions