1

I'm a beginner and I have a smart contract:

pragma solidity ^0.4.0;

contract Coin {
    address public minter;
    mapping (address => uint) public balances;

    // Events allow light clients to react on
    // changes efficiently.
    event Sent(address from, address to, uint amount);

    // This is the constructor whose code is
    // run only when the contract is created.
    function Coin() {
        minter = msg.sender;
    }

    function mint(address receiver, uint amount) {
        if (msg.sender != minter) return;
        balances[receiver] += amount;
    }

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

I ran on local console: testrpc -u 0 -u 1

I ran on Chrome console (result of compilation with https://ethereum.github.io/browser-solidity/)

var ballot_sol_coinContract = web3.eth.contract([{"constant":true,"inputs":[],"name":"minter","outputs":[{"name":"","type":"address"}],"payable":false,"type":"function"},{"constant":true,"inputs":[{"name":"","type":"address"}],"name":"balances","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"receiver","type":"address"},{"name":"amount","type":"uint256"}],"name":"mint","outputs":[],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"receiver","type":"address"},{"name":"amount","type":"uint256"}],"name":"send","outputs":[],"payable":false,"type":"function"},{"inputs":[],"payable":false,"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 ballot_sol_coin = ballot_sol_coinContract.new(
   {
     from: web3.eth.accounts[0], 
     data: '0x6060604052341561000c57fe5b5b33600060006101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff1602179055505b5b6104188061005f6000396000f30060606040526000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff168063075461721461005c57806327e235e3146100ae57806340c10f19146100f8578063d0679d3414610137575bfe5b341561006457fe5b61006c610176565b604051808273ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200191505060405180910390f35b34156100b657fe5b6100e2600480803573ffffffffffffffffffffffffffffffffffffffff1690602001909190505061019c565b6040518082815260200191505060405180910390f35b341561010057fe5b610135600480803573ffffffffffffffffffffffffffffffffffffffff169060200190919080359060200190919050506101b4565b005b341561013f57fe5b610174600480803573ffffffffffffffffffffffffffffffffffffffff16906020019091908035906020019091905050610262565b005b600060009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1681565b60016020528060005260406000206000915090505481565b600060009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff161415156102105761025e565b80600160008473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020600082825401925050819055505b5050565b80600160003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000205410156102ae576103e8565b80600160003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000206000828254039250508190555080600160008473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020600082825401925050819055507f3990db2d31862302a685e8086b5755072a6e2b5b780af1ee81ece35ee3cd3345338383604051808473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020018373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001828152602001935050505060405180910390a15b50505600a165627a7a72305820fd2bce60be92e7948de9f782738870dae224ac0ade9d0fa889b425bf50f2e9340029', 
     gas: '4700000'
   }, function (e, contract){
    console.log(e, contract);
    if (typeof contract.address !== 'undefined') {
         console.log('Contract mined! address: ' + contract.address + ' transactionHash: ' + contract.transactionHash);
    }
 })

And I tried:

ballot_sol_coin.send(web3.eth.accounts[1], web3.toWei(10, "ether"), {from: web3.eth.accounts[0]})

But nothing appends :'( And I have no idea of why!

ballot_sol_coin.minter()

This works!! (Return @account[0])

Could someone write me the lines to copy paste into the console to execute an ether transfer with this smart contract?

2 Answers 2

0

Did you invoke mint with account[0] as the receiver to increase its balance? otherwise your first if check in the send function will return without doing anything.

Also not sure why you are using web3.toWei(10, "ether") in your send when you are sending custom tokens - why not just use the number of tokens you want to transfer?

1
  • I try ballot_sol_coin.mint(web3.eth.accounts[0], 10000000000000, {from: web3.eth.accounts[1]}) !? Can you illustrate with code please ? And i use web3.toWei(10, "ether") because I was thinked thought I would not see the difference if i used a too small number Commented May 11, 2017 at 8:29
0

You have to receive ether in contract while sending ether from web3js. You must have payable function to receive an ether. Lets see example below :

In Web3js :

your_contractInstance.yourReceiverFunction(     
             your_input_data, // this is optional for sending ether. You can send ether without passing this input variable. 
            {  
            from:   web3.eth.defaultAccount,  
            to:     your_contract_address,  
            value:  web3.toWei(1, "ether")              
    });    

In solidity :

 function yourReceiverFunction(datatypes your_input_data)  
       payable   
       public {  
    //do anything here with your input data 
    // to receive ether it is not necessary to be any input variable , you    just
    //need to send ether from web3 and receive here. 
    //the key point here is to just receive ether and use it   
    //msg.value holds the ether that sends from web3 in wei.

  uint myethervalue = msg.value;      
    } 

Take a refrence of this example. EDIT:


In your case, you are sending ether from one account to another from contract address to externally owned account. Above there is receiving ether from externally owned account to contract account. Lets see how to send ether from CA to EOA.

 function send(address receiver)  
  payable {  
            uint amount = msg.value;
            if (balances[msg.sender] < amount) return;
            balances[msg.sender] -= amount;
            balances[receiver] += amount;
            receiver.transfer(msg.value);
    //below Sent() is calling to another function but transfered already to receiver account using `receiver.transfer`
            Sent(msg.sender, receiver, amount);
        }

This might be helpful !!

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.