Payable

What did you first heard about blockchain? I would bet it is cryptocurrency (if it was a recommendation of this incredibly well written tutorial then congrats, the world is not as wild as expected).

ETH currency

Blockchain is a so-called decentralized ledger. While being decentralized allows anyone to analyze it, adding new data in the ledger is still a costly operation. And all operations comes with a cost. This cost is mesured and paid in a currency called ETH. You have been using it since the beginning of this tutorial to complete exercises. This currency is secured using a private/public key model, each pair representing a unique wallet.

Amiral tip Metamask is an ETH wallet. It holds your fund safe, like a regular wallet

When you have configured Metamask, it has generated such a wallet for you. It holds your funds. If you click on its icon, you will see how much ETH you hold.

Payable

Like you pay the store and not the seller, in Solidity you pay the function and not the contract.

In your message msg, there will be a special attribute called value which represents the amount of ETH you have sent to the contract.

Before going further into details, let's get back to coding (I now you ️ it)

Exercise
Correct!
False!

Loading...

Because it has launched its twitch channel, people want to tip the SpaceMuffin. A tip is a msg.value above 0
pragma solidity ^0.4.24; contract SpaceMuffin { uint public boughtMuffins = 0; uint public lastTip = 0; function tip() public payable { require(/* put the tipping requirement here */); lastTip = msg.value; } }

Balance

Sending ETH is great. But what is cooler is to know the amount you and other addresses are holding.

This is the specific purpose of the balance. On an address variable, access .balance to know how much the address holds. A contract is convertible to an address using the address() function.

// Address balance
address a = 0x0;
uint balanceA = a.balance;

// Contract address from within the contract
address c = address(this);
uint balanceC = c.balance;

Units

ETH main unit is ether. But as for regular currencies, it can also be subdivided into smaller amounts.

Unit Value (in ether)
ether 1
finney 10^(-3)
szabo 10^(-6)
wei 10^(-18)

Those units are compatible with uint directly

uint w = 1 wei    // == 1
uint s = 1 szabo  // == 1000000000000
uint f = 1 finney // == 1000000000000000
uint e = 1 ether  // == 1000000000000000000

Payback

User are now able to offer SpaceMuffin their money. But as a cult master, you might want to get the money back at some point. To make the cult even bigger because the SpaceMuffin deserves it obviously.

Actually, msg has another attribute called sender. msg.sender is an address associated to the user that sent the message.

To transfer funds to msg.sender (or to any other address), you should use <address>.transfer(<amount>). This will check if the contract has enough funds and send those funds to msg.sender (or an other address).

Exercise
Correct!
False!

Loading...

If the sender of the message has the correct password, you should transfer all contract balance
pragma solidity ^0.4.24; contract SpaceMuffin { uint public lastTip = 0; modifier securityCheck(bytes32 _password) { require(_password == "Super Super Muffin"); _; } function tip() public payable { require(msg.value > 0); lastTip = msg.value; } // I helped you by already checking if the password is valid function retrieve(bytes32 _password) public securityCheck(_password) { // you just have to send all contract funds to msg.sender } }

results matching ""

    No results matching ""