4

I'm taking my first steps in Ethereum development.

What I'm trying to do is write a function that accepts an address and send it 1 ETH. I don't understand how to check if the contract has sufficient funds, tx.origin.balance is in wei format, how can I convert wei into eth.

function refillAccount(address receiver) payable returns(bool isRefilled) {
    if (tx.origin.balance < [1 eth]) {
        refilledAccounts[receiver] = false;
        return false;
    }

    if ( !refilledAccounts[receiver] ) {
        refilledAccounts[receiver] = true;
        if( !receiver.send(1 ether) ) 
        throw;
        return true;
    }
}
6

Totally agree with Richard. Contracts usually work best in Wei using integers. Conversion to/from Eth should usually be done on the client side, when communicating with users.

The value you're looking for is called msg.value - the amount received into the contract with the transaction. The code below checks it's the right amount and only forwards funds it actually received. In the unlikely event that something goes haywire with the funds forwarding, the funds will be returned to the sender.

tx.origin is dangerous. msg.sender is almost always preferred.

It's not thoroughly tested but hopefully helps you get where you're going:

pragma solidity ^0.4.6;

contract Practice {

    function refillAccount(address receiver) payable returns(bool isRefilled) {

        if(msg.value != 1 ether) throw; // accept exactly 1 ether and nothing else
        // the sender has sent us 1 ether, so we can pass it on.
        if(!receiver.send(msg.value)) {
            throw; // if we can't pay it forward, send it back
        }
        // it happened
        return true;
    }
}
  • Hi, I want to send a fix amount of eth (1) for every new user. The concept is: If you are a new user I give you 1 eth (or less) to let you interact with my contracts. So the idea to set the value into the contract end not from client. – underdog Jan 5 '17 at 16:47
  • You can do if(!receiver.send(1 ether)) ... it's essentially the same thing because of the earlier check to make sure you sent precisely 1 ether with the transaction. The foregoing process assumes that the source of the one ether is the message itself, i.e. the user (admin?) provides the 1 ether to forward. Upon rereading your question, I see you want to draw from the contract. I would suggest caution around a function that draws funds from the contract itself. Having said that, you can check the contract balance with this.balance, e.g. if(this.balance < 1 ether) throw; – Rob Hitchens - B9lab Jan 20 '17 at 19:15
4

The base unit for currency in Solidity is Wei. That means if you're dealing with balances, and you don't specify a unit, then it defaults to Wei.

That said, you can specify units explicitly: wei, finney, szabo and ether. As long as you use ether (not "eth") in your contract, the comparison will work fine.

For the actual conversion between denominations, this previous answer has a comprehensive list. There is also a conversion tool, here: https://etherconverter.online/

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.