8

I would like to know how to identify units of specific tokens received in a Smart Contract.

I am building a Smart Contract that can receive ether or other ethereum tokens and in exchange for the tokens received another token will be issued.

Which code can I use in the Smart Contract to identify how many ether or any other token are received?

And in which function should it be used?

4

To know how much ether are you receiving, you can use msg.value as usual. Just don't forget to include the payable modifier.

About the tokens, the standard is:

  1. The user executes approveAndCall in the token contract to allow your contract to move his tokens.
  2. In the same transaction, your contracts executes receiveApproval and moves the tokens while executing any other function if needed. To know how many tokens has your contract access to, you can execute from you contract tokenContract.allowance(msg.sender, this)
2

The answers given so far do not address the question. The Idea is that you create a smart contract. This smart contract can receive ether or other tokens. The question is, how can be checked how many tokens of a particular 'type' were received?

In general, a token (ERC20 like) is just a balance. When you see tokens in your wallet, this is the wallet accessing the balance in the contract (because the contract for that token has issued an event indicating a change in the balance). This mean, you do not really receive the tokens in your wallet.

Now, assume you created the token_1 using a contract. Another contract has created the token_2. If I send the token_2 to your contract address, what happens is that the balance of the contract that created Token_2 updates the balance and adds your contract's address to the bookkeeping. Your contract will not react to this, and this is why usually sending tokens to wrong contract's address results in lost funds.

You will need to listen to the events of the other contract to know who and how much of a particular token has been sent to you, but this can not be done in the smart contract, you will need an off-chain code that read the events.

I hope this help.

  • Contracts cannot access the events log. – Rick Park Nov 12 '18 at 14:00
  • I clarified that contracts cannot access the logs, this in the last paragraph. – Jaime Nov 12 '18 at 14:27
0

Contracts cannot receive tokens like they can receive ETH in a transaction. Tokens are managed (created/transfered/burned/etc.) by the issuing contract.

So if you own contract A that has method receivePayment(), as @adrianclv mentions, you can access the msg.value member to see how much ETH was sent with the method call transaction.

But if someone were to "send" contract A some other token, they would need to transfer ownership of those tokens to contract A via whatever interface the issuing contract, say B, provided.

If the issuing contract B was one like the sample, then the sender would call B.transfer(A, amount) which would grant contract A "ownership" of amount of the token.

(Unfortunately, without running a node and watching for the Transfer events from B, your contract A will never know that such a transfer occurred.)

  • Can you show me an example of the receivepayment function? I don't seem to find it anywhere online. Is this function triggered automatically when a Smart Contract receives ether? – Resten May 3 '17 at 2:25
  • No, it was an example - a payable method you would write on your contract. Alternatively, there's the default unnamed method that automatically gets called if your contract gets paid without a method call. Details here: solidity.readthedocs.io/en/develop/… – Jamie Hale May 3 '17 at 13:12

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.