Eye-candy so you’ll like my article ❤

As I began developing it became really hard to find answers to simple questions I had. Here, I lay out the questions I had and their answers.

In this article, I cover these questions:

How do I send Ether to a smart contract function? How do I generate a random number? How do I log events in a smart contract, and then read them as they occur?

How do I send Ether to a smart contract function?

There are some really good documentation on sending Ether to smart contracts, but it’s gone through some iterations. Also, how you go about delivering funds to contracts depends on the medium you are using.

If you are trying to send Ether to a smart contract via another smart contract, there is a really good write up of this process dated August 22, 2016 on the Ethereum Stack Exchange.

If you are attempting to send Ether via web3, or a Truffle console, here’s how I went about it:

First, your smart contract must contain a payable function.

This is an example of a constructor where you can initialize a smart contract with Ether:

Solidity: function Contract() payable { }

Here are some examples of payable functions in a smart contract:

Solidity: function payableFunction() payable { } function payableFunction(uint randomVal) payable returns (bool) { }

Here is how you call these functions from a web3 or Truffle console:

truffle(development)> contract.payableFunction({from: address, value: etherAmount}) truffle(development)> contract.payableFunction(randomVal, {from: address, value: etherAmount})

What was stumping me was how I called the function to send Ether and pass it an arbitrary value as well. Turns out you pass your function parameters first, then a dictionary containing your from and value variables (which map to msg.sender and msg.value in Solidity).