9

I am new to ethereum & blockchain technology and trying to understand somethings(Etheruem, Truffle, DApps etc.) here and there from the internet. From Ethereum official website,

Create a cryptocurrency contract in Ethereum

I am trying to specifically understand these functions

approve(address _spender, uint _value) returns (bool success) {...}

approveAndCall(address _spender, uint _value, bytes _extraData) returns (bool success) {...}

transferFrom(address _from, address _to, uint _value) returns (bool success) {...}

and lastly this function() {throw;}

This contract code

contract tokenRecipient {
  function receiveApproval(address _from, uint _value, address _token, bytes _extraData);
}

and this declaration mapping (address => mapping (address => uint) ) public allowance;

9

approve(address _spender, uint _value) returns (bool success) {...}

This function is just being used to make an entry to the allowance array when another contract want to spend some tokens. _ spender is the address of the contract which is going to use it. _value denotes the number of tokens to be spend.

approveAndCall(address _spender, uint _value, bytes _extraData) returns (bool success) {...}

if approve() function returns true, it will invoke the receiveApproval() function of contract tokenRecipient.

transferFrom(address _from, address _to, uint _value) returns (bool success) {...}

This function will be used to transfer tokens from one address to another. Variables are self-explanatory.

function() {throw;}

This is fallback function. According to its functionality, this function will be executed when someone tries to send the ether to the contract. throw; prevents accidental sending of ether.

contract tokenRecipient { function receiveApproval(address _from, uint _value, address _token, bytes _extraData); }

This tells the MyToken contract that the function receiveApproval of contract tokenRecipient can be invoked somewhere in this code.

mapping (address => mapping (address => uint) ) public allowance;

And lastly this line is a way to declare an array by defining the type of key and value. Value further can contain an array. You can assume it a 2D array. This is being used here to keep the record of users asking for the approval of spending the token for the other contracts.

This contract is ERC20-compliant as it implements the approve(...), transfer(..) and transferFrom(...) functions.
For more detail : https://theethereum.wiki/w/index.php/ERC20_Token_Standard#How_Does_A_Token_Contract_Work.3F

  • 3
    I think the answer would be improved if you mentioned that the approve, transfer and transferFrom functions are part of the ERC 20 token standard. That's a very important thing to note, I think. – Thomas Jay Rush Mar 7 '17 at 15:03
  • Could somebody help me with ethereum.stackexchange.com/questions/12848/… doubt of mine? – Devendra Chouhan Mar 8 '17 at 6:13
  • can you explain the bytes _extraData parameter from approveAndCall ? – iamsujit Mar 21 at 17:43
  • That is an optional data that you can send to the contract who is being approved as a spender of token. – Aniket Mar 25 at 6:05

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.