3 Things I Learned This Week Using Solidity, Truffle, and Web3

Thomas Shallenberger
Coinmonks
Published in
3 min readJun 25, 2017

--

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:

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

Get Best Software Deals Directly In Your Inbox

r 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).

How do I generate a random number in a smart contract?

Since the blockchain is deterministic, random numbers are practically impossible to create. Industrial strength solutions implemented in most gambling contracts where a lot of money relies on RNG typically rely on Oracles or ticket based solutions where multiple players submit a hash, and then all submissions are hashed and then revealed for a strong pseudorandomly generated number.

I personally went the easy, one liner route, at least until my application evolves to handle more Ether:

Solidity: // Generate a random number from 1-MAX_VALUE (MAX_VALUE is set 
// arbitrarily by the developer).
uint random_number = uint(block.blockhash(block.number-1))% MAX_VALUE + 1;

How do I handle events in a smart contract?

Events are a notoriously tricky, convoluted subject in Ethereum development. I figured out how to handle an error condition as well as logging an event in the following manner.

First, define an event in your Solidity smart contract:

event Error(address indexed _sender, bytes32 error);

the indexed keyword is important, as that’s how we track events by the accounts who called the contract function.

Here is how the event is called in the contract function:

Error(msg.sender, "ERROR_DETAILS_HERE");

Here is how we use web3 in the Truffle console to track events:

truffle(development)> var errorEvent = contract.Error({_sender: web3.eth.coinbase})truffle(development)> errorEvent.watch(function(err, result){ console.log(result.args) })

Then, when we call the function and an event is caused, our watcher can grab it, read it, and respond accordingly.

Hope this helped! I’m putting together a more comprehensive article on development, but will be releasing things that I learned along the way.

If you liked this article, please recommend it and share it with your friends!

Hit me up on Twitter: @valkn0t

--

--