1

I am learning ERC20 contract and stuck with allowance and transferFrom part.

Approve function approves an allowance for an address (spender) to transfer tokens from owner's contract to another address. After approval, the address (spender) could call transferFrom to send to other address from owner's contract, any amnt within allowances approed.

The point I am stuck is, how do I test this. I tried both Remix and deploying in Ethereum Wallet with test node but in vain.

Q1: In remix, I could create owner's contract and then call its functions. But how do I call contract function's from another address? For eg, owner's contract address: 0xca35b7d915458ef540ade6068dfe2f44e8fa733c spender address: 0x1111111111111111111111111111111111111111

how do I call (0x1111111111111111111111111111111111111111).approve or transferFrom to carry out this operation?

Q2: I tried deploying in Ethereum wallet in test node, but in vain. Contract created successfully with tokens, function calls shown as success for transfer, approve, transferFrom but no value shows up either reduced from owner's contract or as received in recipient account.

Can anyone you please check my code below and help? (I could not paste entire code here with proper formatting, so giving a link) https://gist.github.com/parthi2929/f2d535253fcb0b9ebccbfb980ff52c1d

2 Answers 2

1

below is my humble advice. I made these changes to your contract.

  1. (line3) change 'interface' into 'contract'
  2. (line69) into '__allowances[_from][msg.sender] -= _value;'
  3. you didn't initialize '__totalSupply', add '__totalSupply = initialSupply' to the constructor.

After these changes, I have deployed the contract to Ropsten testnet and it works fine with all functions. I tested the functions through geth console client. Instructions are below.

  1. var abi = [...];
  2. var MyContract = web3.eth.contract(abi);
  3. var myContractInstance = MyContract.at(address of contract);
  4. myContractInstance.totalSupply.call();
  5. myContractInstance.balanceOf.call(address);
  6. myContractInstance.transfer(address, token);
  7. myContractInstance.transferFrom(address1, address2, token);
  8. myContractInstance.approve(address, token);
  9. myContractInstance.allowance(address1, address2);

Address needs quotes. It's also noted that '.call()' doesn't send transaction, therefore, blockchain states won't change even though you can receive right return values.

I hope these will shed some light on your question.

2
  • Thank you very much for your time. I still could not get approve and transferFrom working in actual Ethereum wallet. What good it is if its working in geth (am yet to test as that is another learning curve), and not in actual ethereum wallet? I have Main Account (Acnt 1), and 2 normal accounts (Acnt 2, Acnt 3). I deploy contract in Acnt 1. Then approve Acnt 2 to transfer some MFT (token) from Acnt 1. Then I call transferFrom (Acnt 1, Acnt 3, some MFT). Both functions executed. I even got transaction sent pop up in ethereum wallet. But I dont see any balance changes in all 3 Acnts.
    – parthi2929
    Commented Dec 1, 2017 at 2:55
  • I have updated the gist also with changes, which still resulted in nothing for approve and transferFrom though wallet says transaction sent, no change in balances. gist.github.com/parthi2929/f2d535253fcb0b9ebccbfb980ff52c1d Also why interface shoudl be changed to contract?
    – parthi2929
    Commented Dec 1, 2017 at 3:17
0

Finally found out a bug not in the code (thanks to Jeff Liu, that was corrected), but in my workflow.

Apparently my test accounts apart from Main account did not have ether at all. Being newbie I did not realize, they too should have ether in first place to carry out operation to transfer my tokens. Wallet also did not warn anything about it.

Out of slight doubt, I first transferred few test ethers to these accounts and then all are now working fine.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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