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,
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;
Best Answer
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 thereceiveApproval()
function of contracttokenRecipient
.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 functionreceiveApproval
of contracttokenRecipient
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