1

I've been struggling for a whole day trying to figure out what's wrong with my smart contract but I couldn't sort it out. Any help is appreciated, I'm sure I'm missing something :(

Here is my contract:

pragma solidity ^0.4.2;

contract ethTransfer 
{
    address public creator;
    mapping (address => uint) public balances;

    event Sent(address from, address to, uint amount);

    constructor() public {
        creator = msg.sender;
    }

    function update(uint newBalance) public {
        balances[msg.sender] = newBalance;
    }

    function send(address receiver, uint amount) public {
        if (balances[msg.sender] < amount) return;
        balances[msg.sender] -= amount;
        balances[receiver] += amount;
        emit Sent(msg.sender, receiver, amount);
    }
}

Here is the output from Remix compiler

var ethtransferContract = web3.eth.contract([{"constant":true,"inputs":[],"name":"creator","outputs":[{"name":"","type":"address"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"name":"","type":"address"}],"name":"balances","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"newBalance","type":"uint256"}],"name":"update","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"name":"receiver","type":"address"},{"name":"amount","type":"uint256"}],"name":"send","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"inputs":[],"payable":false,"stateMutability":"nonpayable","type":"constructor"},{"anonymous":false,"inputs":[{"indexed":false,"name":"from","type":"address"},{"indexed":false,"name":"to","type":"address"},{"indexed":false,"name":"amount","type":"uint256"}],"name":"Sent","type":"event"}]);
var ethtransfer = ethtransferContract.new(
{
from: web3.eth.accounts[0],
data: '0x608060405234801561001057600080fd5b50336000806101000a815481600160a060020a030219169083600160a060020a03160217905550610244806100466000396000f3006080604052600436106100615763ffffffff7c010000000000000000000000000000000000000000000000000000000060003504166302d05d3f811461006657806327e235e3146100a357806382ab890a146100d7578063d0679d34146100f1575b600080fd5b34801561007257600080fd5b5061007b610118565b6040518082600160a060020a0316600160a060020a0316815260200191505060405180910390f35b3480156100af57600080fd5b506100c4600160a060020a0360043516610130565b6040805191825251602090910181900390f35b3480156100e357600080fd5b506100ef600435610142565b005b3480156100fd57600080fd5b506100ef60048035600160a060020a03169060200135610163565b6000809054906101000a9004600160a060020a031681565b60016020526000908152604090205481565b600160a060020a033381161660009081526001602080830191825201902055565b806001600033600160a060020a0316600160a060020a0316815260200190815260200160002054101561019557610214565b600160a060020a033381168116600081815260016020808301828152808201808520805489900390558887169096168085529290529390912080548501905560408051928352918301908152820183815290517f3990db2d31862302a685e8086b5755072a6e2b5b780af1ee81ece35ee3cd3345929190910181900390a15b50505600a165627a7a72305820a083d152312d640c0adc7dcb6854037905c2a6df29630d016337c56b1966772d0029',
gas: '4700000'
}, function (e, contract){
  console.log(e, contract);
  if (typeof contract.address !== 'undefined') {
      // I do these assignment for callback testing and other purposes
      // anyway contract gets correctly deployed on the private network
      contractABI = ethtransferContract;
      contractData = contract;
    }
  })
}

Then I invoke update() method on each one of my peers to fulfill the balances mapping ( I still haven't understood if I have to recall it each time after the use of send method) and then I call the send function in the peer I want to, as follows:

ethTransfer.update(eth.getBalance(eth.coinbase))
ethTransfer.send("0x4bba94568F38B904F631F4E0e98ce845B5c91046", 999999999999)

After that, I start mining in one of my peers and transaction get successfully mined. After that I should expect "0x4bba94568F38B904F631F4E0e98ce845B5c91046" peer to have a balance increased by 999999999999 but it's not and neither is calling peer's balance decreased. Although I correctly call the method and mine the transaction, it's like nothing has happened.

Have you got any idea why? Thanks infinitely!

P.S All the accounts I'm using for this test case are already unlocked

  • How do you check balance after this? What exactly tells you that balances don't change the way you expect them to? – Utgarda Sep 20 '18 at 20:34
  • i do use eth.getBalance(età.coinbase) both on receiver and sender peer and their balance always rest the same – Manfredi P. Sep 20 '18 at 21:16
2

You are not transferring Ethers but something which looks a bit more like tokens. And the command eth.getBalance returns Ether balance of an account. So your Ether balances stay the same because no Ether is being transferred.

I'm not quite sure if you want to transfer Ether or tokens. If you want to transfer Ethers, you have to:

1) Mark your function as payable so it is able to receive Ether

2) To transfer Ether from one account to another, use something like web3.sendTransaction({to:receiver, from:sender, value:web3.toWei("0.5", "ether")}) For more details check for example here: How to use web3 to send money from wallet A to wallet B?

If you just want to "transfer" arbitrary tokens around (not ERC20 compatible as your contract is far from being ERC20 compatible) your contract is just fine. You can use it like this:

1) Add some balance for one account with the update function. The function simply assigns an arbitrary balance to the sender.

2) Transfer some of that balance with the send function to another account. The other account now has higher balance in the balances variable.

Note that almost anything can be considered a token (ERC20 compatible tokens is another story). But tokens such as yours are useless except for internal and test usage as they don't follow any standards and nobody knows how to use them.

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.