0

i am trying to send tokens out from escrow smartcontract to seller's address and for tokens i am using different ERC2O standard smart contract. so i can send like buyer can send from ERC20 smartcontract to escrow. but how can i send those token from escrow smartcontract to seller's wallet

suppose buyer send tokens to escrow and after condition escrow send to seller then how to code that same transfer method in solidity and i am using ERC20 standard.

1
  • 1
    That depends on the escrow smart contract. Impossible to say without seeing it. Apr 18, 2018 at 12:37

2 Answers 2

1

Escrowing ERC20 tokens:

Your escrow contract needs to know when it received new tokens, from whom, and how many.

This is achieved by having the "seller" create two transactions:

  1. Approval: First transaction calls token contract
// tracker_0x_address is the address of the ERC20 contract they want to deposit tokens from
// spender is your deployed escrow contract address

ERC20(tracker_0x_address).approve(address spender, uint tokens)
  1. Deposit: Second transaction calls a method in the escrow contract
mapping ( address => uint256 ) public balances;
deposit(uint tokens) {

  // add the deposited tokens into existing balance 
  balances[msg.sender]+= tokens;

  // transfer the tokens from the sender to this contract
  ERC20(tracker_0x_address).transferFrom(msg.sender, address(this), tokens);
}

This updates the sender's balance, and then transfers the tokens from sender to escrow.

Releasing escrowed tokens:

All your smart contract has to do is call the

ERC20(tracker_0x_address).transfer(msg.sender, balances[msg.sender]);

on the token tracker address in order to transfer those tokens to an address.

If you're doing multiple tokens and multiple users in one contract you're going to have to implement a second layer mapping for the balances as well as support for a tracker variable in the deposit method.

// later edit: updated second transaction to "transferFrom" as it should be.

2
  • first of all thanks for help! and is there any tutorial or anything about escrow where i can learn more about escrow? Apr 18, 2018 at 16:25
  • Haven't really found any tutorials that go in depth about this specifically, but you're free to take a look at how i deal with both ethereum and token escrows in my project. Check the FundingVault and FundingManager. Apr 19, 2018 at 6:48
0

Token recovery

Generally this is done through a token recovery function in the smart contract.

For example in your contract you will need a function like the following, which will allow the owner to recover a specified _value of units of the given token at _token and transfer them to _to:

Example:

/**
 * @notice Recover ERC20 tokens sent to this contract.
 * @param _token The address of the ERC20 token that you wish to transfer.
 * @param _to Address to send the tokens to.
 * @param _value The number of token units to send.
 */
function recoverTokens(address _token, address _to, uint _value) external ownerOnly {
    return ERC20(_token).transfer(_to, _value);
}

Note: This requires an interface ERC20 that contains at minimum the transfer implementation, and a modifier ownerOnly that will ensure only an authorised address can call the recoverTokens function.

2
  • my token name is catchcoin so i have to use deployed betacoin's address as a_token value? or should i add this code into my catchcoin token smartcontract Apr 18, 2018 at 13:01
  • This function is able to recover any token transferred to the escrow contract, but you need to supply the address of the token you wish to recover. So for example if the buyer transferred some "catchcoin" token to your "betacoin" escrow contract, then you would add this to your betacoin contract and would need to call betacoin.recoverTokens, supplying the catchcoin contract address as _token, and the seller's address as _to. (You'll probably also want to rename recoverTokens to something more appropriate).
    – supakaity
    Apr 18, 2018 at 13:13

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.