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;

Devendra Chouhan
  • 391
  • 1
  • 3
  • 10
  • 1
    The is a example showing how the token transfer(...), approve(...) and transferFrom(...) functions work at https://theethereum.wiki/w/index.php/ERC20_Token_Standard#How_Does_A_Token_Contract_Work.3F . – BokkyPooBah Mar 07 '17 at 10:59
  • 1
    @BokkyPooBah Thanks for the link. Understood it better. – Devendra Chouhan Mar 08 '17 at 06:09

1 Answers1

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

Aniket
  • 3,545
  • 2
  • 20
  • 42
  • 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 07 '17 at 15:03
  • Could somebody help me with http://ethereum.stackexchange.com/questions/12848/error-base-fee-exceeds-gas-limit-when-creating-new-contract-instance-using-t doubt of mine? – Devendra Chouhan Mar 08 '17 at 06:13
  • can you explain the bytes _extraData parameter from approveAndCall ? – iamsujit Mar 21 '19 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 '19 at 06:05