0

I have a smart contract deployed at a particular address(say, 0xABC). This contract is ERC20 token(say, ABC token) smart contract. This contract was deployed on **Nov 2017.**

Now I want to have another smart contract that can act as an Escrow, that can hold ABC token until a specific condition is met. This contract is yet to be deployed.

So, how the Escrow contract can accept the ABC token and and release the ABC token to the valid user after the condition is met? I want to call .transfer() method of ABC token contract from Escrow contract, To get and release funds from Escrow.

Note: This both smart contract are deployed at different address, since they are deployed at different time.

2 Answers 2

0

Something like this should work

contract Escrow {

    ERC20 token = ERC20(0xABCDEF....);

    event Deposit(address sender, uint amount);
    event Withdraw(address sender, uint amount);

    mapping (address => uint) deposits;

    // The user should approve the contract to retrieve tokens
    function deposit(uint amount) public {
        // Accept tokens in deposit
        token.transferFrom(msg.sender, amount);
        deposits[msg.sender] += amount;

        emit Deposit(msg.sender, amount);
    }

    function withdraw(uint amount) public {
        // Cannot withdraw more tokens than deposited
        require(deposits[msg.sender] >= amount);

        // Verify condition enable withdraw
        if (condition) {
            // Transfer tokens to sender
            deposits[msg.sender] -= amount;
            token.transfer(msg.sender, amount);

            emit Withdraw(msg.sender, amount);
        } else {
            revert();
        }
    }
}

The user has to approve your contract so it can transfer tokens with transferFrom. In javascript is something like ERC20Token.approve(Escrow.addrss, amount, { from: "0xTokenOwnerAddress" }), and then call the Escrow to make a deposit EscrowContract.deposit(amount, { from: "0xTokenOwnerAddress"}).

0

This can be done. However, if the users send the tokens to the escrow contract using transfer, the escrow contract will not get notified. you need to use approveAndCall in the ERC20 token contract (assuming that was implemented)

approveAndCall will approve your contract to move tokens from the user to your contract and then you can apply the logic you need about how to allows for the tokens to be withdrawn.

In order to be able to use the functions of the ERC20 token contract you need to create an instance of ERC20TokenContract inside your contract.

Then you can do:

ERC20TokenContract myInstance = ERC20TokenContract('contractAddress')

this allows you to do things like: myInstance.transfer(to,value) where transfer is a function fo the ERC20TokenContract.

You need to import the token contract into your escrow contract. Here I am assuming that the token contract name is ERC20TokenContract.

For the details of the implementation take a look at this answer

Note that if your ERC20 contract does not implement approveAndCall, the only alternative is to ask the users to first send the tokens to your contract address and then to execute the function in your contract that locks the tokens.

2
  • how to create an instance of my token Contract inside escrow contract?? by using abi of token contract or just the address or something else? please help me because this is the only way i can use escrow.. if you can give any example that will be very very helpful.. thanks! Apr 19, 2018 at 14:47
  • It is in the answer. you do ERC20TokenContract myInstance = ERC20TokenContract('contractAddress') you do not need the ABI but you need to either copy the contract of the token in the same file as your escrow contract or to import it using 'import filename.sol'. You can also create an interface to the token contract, any of these three things will work.
    – Jaime
    Apr 19, 2018 at 14:55

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.