Deploying your first LLL contract — Part 2

In which we get down and dirty with testrpc and web3.js

Daniel Ellison
ConsenSys Media

--

In the first part of this article I described how to set up your environment to enable you to deploy contracts to an Ethereum blockchain. Now we’re going to complete that deployment. If you prefer a video demonstration, please watch this article’s companion screencast.

Setup

Ok, let’s get started. Make sure you have testrpc running in a terminal. When you start it, you should see a banner with the testrpc version number, then a bunch of hexadecimal strings for Available Accounts and Private Keys, then an HD Wallet section. Your blockchain is ready. Open a new terminal and type node. You’ll be left at a simple > prompt.

We’ll start by “requiring” web3. This JavaScript library has the functionality necessary to talk to the testrpc blockchain. In fact, it’s the library used by dApp developers to interact with the public blockchain from their code. In order to use web3 here, we type the following at the node prompt:

> var Web3 = require(‘web3’);

Now we want to connect web3 to testrpc:

> var web3 = new Web3(new Web3.providers.HttpProvider(“http://localhost:8545"));

The word wrap is a bit annoying, but copying the text should restore its one-linedness. Be careful not to copy the > prompt.

Contract ABI

The next step will require a bit of an explanation. In order for web3 to deploy your contract it needs to know the contract’s ABI, which stands for Application Binary Interface. From Wikipedia:

In contrast to an API, which defines structures and methods one can use at software level, an ABI defines the structures and methods used to access external, already compiled libraries/code at the level of machine code.

A contract on the blockchain is stored as EVM bytecode, which is effectively its machine code. In our current case, in order to create a JavaScript object with which to interact with our contract, we need to supply the contract’s ABI. An ABI definition for our contract–if it were written in Solidity–would look like this:

pragma solidity ^0.4.0;
contract IdentityFunction {
function identity (uint256 input) constant returns (uint256);
}

This is similar to an interface in some other languages. An interface in our case is simply the definition of any functions in a contract minus the function bodies. Here we have only one function, but there are usually several function definitions as well as event definitions. EVM events will be covered in a future tutorial.

The Solidity interface above can actually be used to provide the ABI for our LLL contract. The function definition represents the Solidity equivalent to the function we defined in our contract. That’s the beauty of an ABI: We don’t have to care what language the contract was written in; if it follows the ABI specification it’s available in exactly the same way as a Solidity contract’s functions.

Since web3 requires the interface in JSON format we have to convert the solidity definition to JSON. The easiest way to do that is with remix, a browser-based Solidity compiler. Paste the preceding code into a new document in remix. To the right you’ll see a column with a set of tabs at the top. Switch to the Contract tab. At the bottom of the column you’ll find a link that says Contract details (bytecode, interface etc.). Click that link to reveal the details. You’ll see an Interface heading and some JSON text to the right. That JSON is your contract’s ABI in JavaScript. When lightly formatted it looks like this:

[{
"constant":false,
"inputs":[{
"name":"value",
"type":"uint256"
}],
"name":"identity",
"outputs":[{
"name":"",
"type":"uint256"
}],
"payable":false,
"type":"function"
}]

Our JavaScript object

We’re now ready to continue with the deployment process. Copy the JSON interface and type the following at the node prompt, pasting the JSON accordingly:

> var abi = [{"name":"identity","type":"function","constant":true,"payable":false,"inputs":[{"name":"input","type":"uint256"}],"outputs":[{"name":"","type":"uint256"}]}]

This simply defines a JSON string that represents our ABI for use in the next command:

> var Tutorial = web3.eth.contract(abi);

This creates an object we’ll use to interact with the testrpc blockchain.

Bytecode

Now we need the bytecode for our contract. We executed this step in my previous article on compiling an LLL contract. If you don’t have the bytecode, don’t worry: It’s in the following text that you should paste at the node prompt:

> var bin = '601f80600c6000396000f30063ac37eebb60e060020a600035041415601e5760043560005260206000f35b';

Sender account

To deploy a contract on the blockchain we need to specify the account that is sending the transaction. The default is defined as web3.eth.coinbase. Type the following:

> var account = web3.eth.coinbase;

Deploying

We’re almost there! This all may seem a bit daunting but it gets easier with repetition. It definitely takes longer to explain than it does to execute. The next line deploys the contract’s bytecode to testrpc.

> var tut = Tutorial.new({data: bin, from: account, gas: 100000});

Again though, this is legitimate for any Ethereum blockchain. Let’s unravel that a bit. We’re creating a new Tutorial object providing the following parameters:

  1. data: this specifies the bytecode to deploy, which in our case is stored in the bin variable.
  2. from: this specifies the account that is sending the transaction. This is stored in the account variable.
  3. gas: This is the amount of gas you want to send with the deployment, here 100000.

Without going into extreme detail, gas is used in Ethereum to pay for the execution of the deployment. For more details, please see Joseph Chow’s excellent article on the subject. For this exercise we supply 100000 gas to the transaction, which covers its cost.

Invoking

Our contract has been deployed! From this point on it is the exact equivalent to a contract written in any EVM language, including Solidity. This is due to the fact that we followed the ABI spec.

The tutorial object has several attributes, one of which is its ABI. To see what it looks like in context of the object, type this:

> tut.abi
[ { name: 'identity',
type: 'function',
constant: true,
payable: false,
inputs: [ [Object] ],
outputs: [ [Object] ] } ]

Let’s invoke the identity function we defined previously. Recall that an identity function simply returns any value passed in. In our case we’re using a uint256 value as this is what we specified as the input type in the ABI. Type the following:

> tut.identity(42)
{ [String: '42'] s: 1, e: 1, c: [ 42 ] }

This calls the identity function, providing the number we wish to send into the function–in this case 42. You can try this with any value you like. The result of the call is a JavaScript BigNumber object representing 42. According to the Truffle documentation

[t]his is because Ethereum can represent larger numbers than those natively represented by Javascript, and so we need an abstraction in order to interact with them.

This BigNumber object was created by the web3 library from the actual response provided by the EVM.

Conclusion

Congratulations! You’ve successfully deployed an LLL contract to an Ethereum blockchain. Very few people have done so to this point. I hope my articles help people get a handle on LLL contract compilation and deployment. These are all the tools you need to start developing LLL contracts and reaping the rewards of reduced binary size and gas cost.

Read More:

An Introduction to LLL for Ethereum Smart Contract Development
Building and Installing Ethereum Compilers

Compiling an LLL Contract for the First Time
Deploying Your First LLL Contract — Part 2
The Structure of an LLL Contract — Part 1
The Structure of an LLL Contract — Part 2
The Structure of an LLL Contract — Part 3

Like this piece? Sign up here for the ConsenSys weekly newsletter.

Disclaimer: The views expressed by the author above do not necessarily represent the views of Consensys AG. ConsenSys is a decentralized community with ConsenSys Media being a platform for members to freely express their diverse ideas and perspectives. To learn more about ConsenSys and Ethereum, please visit our website.

--

--