0

I know that a smart contract address can accepts a ERC20 token sent to it.

Let’s say there are two smart contracts A and B. A is a deployed smart contract implementing a ERC20 token and B is the contract I want to write.

I would like to know can I write a method in the contract B so that, on trigger by sending a transaction to call its method, it sends a certain amount of the A token owned by the contract B address to another address I specified?

A follow up question: In a manual transaction sender has to sign the raw ERC20 token transaction and send it to network. For smart contract B, is it as simple as calling the target ERC20 smart contract A’s method and it can use the tokens owned by B’s address? Is there any steps needed to "sign" the transfer?

Thank you.

1

1) Depends on what you mean with a "trigger". Smart contracts can never do anything without someone/something issuing them a transaction. So contracts can't "monitor" things by themselves or run in the background in that sense. Otherwise, yes, a smart contract can transfer tokens to another contract/address.

2) Because all action is initiated through a transaction, the original transaction sender is the "signer". The one who issues the original transaction also has to pay (with gas) for all the transactions that occur in the possible chain of contracts.

EDIT After the asker added more information about the problem:

Sending tokens to another address is done by calling the transfer method in the ERC20 token contract. That method should actually just modify the contract A's inner state, so transferring tokens from contract B only requires B to call A's transfer function. B does not need to "send" any tokens itself - it only needs to call the transfer function to change the ownership of said tokens.

  • When I say trigger I mean to send the contract a transaction with method call like send_someone_token('0x123123123', 10). And I won't need the contract to monitor anything. The core question I have is actually whether I can tell the smart contract to send the ERC20 token owned by the contract address itself. And of course given I have sent enough ERC20 token to the contract address before. – Calvin Lau Mar 27 '18 at 10:07
  • Edited answer to answer your clarified question – Lauri Peltonen Mar 27 '18 at 10:18
  • I must apologise It appears my extra information actually confuse you. I have updated the question to further clarify it. – Calvin Lau Mar 27 '18 at 10:33
  • edited answer again – Lauri Peltonen Mar 27 '18 at 10:37
1

Concerning your first question : yes. Contract B needs to know how the methods of Contract A behave (at least, the methods you need to use), hence using an abstract contract (the interface part of the code below). Since it's usually needed to update storage in your contract, but if you don't need it, you can delete that part, You will need to first retrieve the amount of tokens your contract own, since it cannot know by magic how much tokens it has, right? It has to call the balanceOf method of the token contract. Once it's done, you can send the amount you want to the address you want by using the transfer method

interface ERC20 {
  function transfer(address _to, uint256 _value) public returns (bool success);
  function balanceOf(address _owner) public constant returns (uint256 balance);
}

function get_all_tokens(address token_address, uint256 amount) {
  ERC20 token = ERC20(token_address);
  uint256 contract_token_balance = token.balanceOf(address(this));
  require(contract_token_balance != 0);
  //Eventually do some updates of balances in your contract
  require(token.transfer(msg.sender, amount));
}

Now, you can adapt it to fit your need, your imagination is your only limit. :)

For the second question, I believe it was already answered (above or below my answer).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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