1

I am building an escrow smart contract, i would like the escrow to be able to receive ERC20 tokens preferably stable coins like USDT OR USDC and not ether.

Is there a way to implement this to my contract please ?

2 Answers 2

2

Yes, it is possible to implement the ability to receive ERC20 tokens in an escrow contract. Here is an example of how this could be done:

Import the ERC20 contract:

import "https://github.com/OpenZeppelin/openzeppelin-solidity/blob/v3.0.0/contracts/token/ERC20/SafeERC20.sol";

Define the ERC20 token you want to receive in the contract

contract Escrow {
    SafeERC20 public token;

    constructor(SafeERC20 _token) public {
        token = _token;
    }
}

Add a function to the contract to allow the user to deposit ERC20 tokens:

function deposit(uint256 _amount) public {
    // Transfer the tokens from the user's account to the contract
    require(token.transferFrom(msg.sender, address(this), _amount), "Transfer failed");

    // Store the deposited tokens in a mapping
    // You can use a struct or any other data structure to store the deposited tokens
    depositedTokens[msg.sender] += _amount;
}

Add a function to the contract to allow the user to withdraw the deposited tokens:

function withdraw(uint256 _amount) public {
    // Check if the user has enough deposited tokens
    require(depositedTokens[msg.sender] >= _amount, "Not enough deposited tokens");

    // Transfer the tokens from the contract to the user's account
    require(token.transfer(msg.sender, _amount), "");
1

I am trying to implement a similar function. Try this. I am not sure if it'll work but give it a try.

pragma solidity ^0.8.0;

interface IERC20 {
    function transferFrom(address _from, address _to, uint256 _amount) external returns (bool);
}

contract ABC{
    
    function enter() public payable {
    
    IERC20 token = IERC20(`0x123`); // Insert the token contract address instead of `0x123`
    require(token.transferFrom(msg.sender, address(this), .01 ether));
}


}
2
  • Hello, how exactly do i import this into the code? let's say i want to use only usdc erc20 Dec 4, 2022 at 19:39
  • just replace '0x123' with the USDC contract address. Dec 5, 2022 at 13:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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