When I use web3.py to send some coins from account1 to account2, account2 does not see a balance increase.

from web3 import Web3 ,HTTPProvider, IPCProvider
from solc import compile_source

addr = ''
web3 = Web3(HTTPProvider(addr))

source_code = ' contract token { mapping (address => uint) public coinBalanceOf; event CoinTransfer(address sender, address receiver, uint amount);  function token(uint supply) { coinBalanceOf[msg.sender] = supply; } function sendCoin(address receiver, uint amount) returns(bool sufficient) { if (coinBalanceOf[msg.sender] < amount) return false; coinBalanceOf[msg.sender] -= amount; coinBalanceOf[receiver] += amount; CoinTransfer(msg.sender, receiver, amount); return true; } }'

web3.personal.unlockAccount('<insert account 1 here>','password!')

compile_sol = compile_source(source_code)
contract_address = '0xC7c1F1129726df4148B756c356Dc64cb27d763AC' #test-net 

Mycontract = web3.eth.contract(abi=compile_sol['<stdin>:token']['abi'], bytecode = compile_sol['<stdin>:token']['bin'],bytecode_runtime = compile_sol['<stdin>:token']['bin-runtime'])
my_contract = Mycontract(contract_address)

print my_contract.call().coinBalanceOf(Account1)
hashs = my_contract.transact({
    'from': web3.eth.coinbase,
    'to': '<insert account 2 here>',
    'value': 231,
    'gas': 702438
}).sendCoin('<insert account 2 here>', 231)

print web3.eth.getTransaction(hashs)
print "----------------------------------\n"
print web3.eth.getTransactionReceipt(hashs)
print  hashs+"\n"
print web3.eth.getTransactionCount(web3.eth.coinbase)

print my_contract.call().coinBalanceOf('<insert account 2 here>')

Account2 doesn't have any tokens. What do I have wrong?

1 Answer 1


When sending a token, do not set the to or value fields in the transact() method.

Original Code

hashs = my_contract.transact({
    'from': web3.eth.coinbase,
    'to': '<insert account 2 here>',
    'value': 231,
    'gas': 702438,
}).sendCoin('<insert account 2 here>', 231)

New Code

hashs = my_contract.transact({
    'from': web3.eth.coinbase,
    'gas': 702438,
}).sendCoin('<insert account 2 here>', 231)

Note that both of the removed values are specified in the contract method call sendCoin().


The to key

When you interact with a contract, you send a transaction to it. The receiving account is only mentioned in the call to the contract function.

This is an interesting point that many people don't understand at first: Account 2 does not have a record that it owns the new token. It must query the contract to discover its own token balance.

The value key

Value is always about transferring ether, denominated in wei. When you send the token, the only cost in ether will be the gas paid to run the contract.

Other Issues?

This is a long enough example that there may be other bugs hiding in there. I am not sure I found all the problems. If there are more, feel free to comment on my answer and add more details to your question about what's going on.

  • Based on your line web3.eth.getTransactionReceipt(hashs) I assume that you confirmed that the transaction got mined. Please confirm that it has when retesting with my answer.
    – carver
    Sep 8, 2017 at 15:36
  • Thanks a lot. The answer perfect. I send my token to Account2. This answer save my all day. 😄
    – puluo
    Sep 9, 2017 at 2:40

Your Answer

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

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