Ethereum dapp development 101 — How I built BiggestG.com

0
60

Deploying the Contract

Now that we’ve written and tested our contract, we need to deploy it.

Truffle handles the problem of deploying smart contracts via a concept called “migrations” which are kind of like database migrations, but different enough to be confusing. When you initialize a truffle project using truffle init , truffle will automatically create your first default migration:

This migration is confusing because its not immediately clear what it’s doing (it’s actually deploying its own “Migrations” contract!). If you want the details, you can read about them here, but for now don’t worry about it, just know that it will be the first step every time you deploy your contract (its also sometimes worth commenting out line 4 when deploying to the production Ethereum network due truffle timing out in-between migration steps).

To deploy OUR contract we will need to create a new migration. You can do this by running truffle create migration which will create a new .js file in your migrations directory.

The migration script for my SingleMessage contract looks like this:

We import our contract using artifacts.require , instantiate some constants, log what they are, and then use the deployer object that is automatically injected by truffle to deploy our contract.

The function signature for the deploy contract is that the first argument is the Contract that you’re trying to deploy. THE subsequent arguments are the values you want to pass to the contracts constructor function. The number of arguments you pass must match the number of arguments that the constructor accepts or you’ll spend hours debugging why your contract exhibits undefined behavior when you try and interact with it

Inside of the asynchronous callback of the deploy function we await for the contract to be deployed, then we call the getter functions for the state variables that we defined in the contracts. If you remember the contract from earlier, we never actually wrote any getter functions. Solidity just auto-generates getter functions for any public state variables.

So in summary this script: prints out the arguments we’re passing to the contract constructor, deploys the contract (and passes the arguments to the constructor), waits for the contract to be deployed, and then prints out the live state of the contract on the chain so that we can verify it was initialized properly.

Now we’re ready to run the migration (I.E deploy the contract), but first we have to pick a network to deploy it too. If you’re using the Parity / Truffle toolchain you have a few options:

  1. Truffle development network
  2. Kovan network
  3. Production Ethereum network

Lets start with the first one. The Truffle development chain is basically a slightly-modified Ethereum blockchain built into the truffle tool thats designed to make testing your contracts really easy.

To get started, run truffle develop

This will start a command-line prompt with the Truffle development chain running in the background. Once you’re in this command prompt, you can run any valid truffle command (without the Truffle prefix) AND you can use the Web3 Javascript API to interact with your contract.

Start by typing migrate , you should see output like:

truffle(develop)> migrate
Using network 'develop'.
Running migration: 1_initial_migration.js
Saving successful migration to network...
...0xaba9cf5e2d46db92655bf1170146029cc34055babd30fc95000663d8b5463d6a
Saving artifacts...
Running migration: 1512863228_initial.js
Contract arguments: {
initialMessage: 'Hello world!',
initialPriceInWei: 1,
maxLength: 200 }
Replacing SingleMessage...
...0x51eae26485e83ef5eee593425d7d163f286544841c80007e8f929fff1680630d
SingleMessage: 0xf12b5dd4ead5f743c6baa640b0216200e89b60da
public vars from contract: { message: 'Hello world!',
priceInWei: { [String: '1'] s: 1, e: 0, c: [ 1 ] },
maxLength: { [String: '200'] s: 1, e: 2, c: [ 200 ] } }
Saving successful migration to network...
...0x0a0e316370fb4e3442a34b7dcd221d102ee4eb07df50da6a72b9027c41b2663a
Saving artifacts...

This means that the contract was successfully deployed to the Truffle development chain!

Try interacting with it by running the following commands:

var contract = SingleMessage.at('0xf12b5dd4ead5f743c6baa640b0216200e89b60da');

Note: Replace the address above with the address that was provided to you in the terminal.

contract.message()

contract.message.call()

contract.priceInWei()

contract.set('New message!', {value: 0}) Why did this fail? (You didn’t pay enough Ether)

contract.message()

contract.set('New message!', {value: 1}) Why did this succeed? (You paid enough Ether)

contract.message()

Now that we’ve verified on our local Truffle development environment, we’re ready to test our contract in a more formal environment, so lets try the Kovan network next.

You can think of the Kovan network as behaving exactly like the production Ethereum blockchain, with a few minor modifications to make testing easier.

First, you’ll need a wallet for the Kovan network. You can get one by running parity account new --chain kovan which will prompt you for a password and then print out your public key, as well as store your private key (encrypted by your password!) on disk.

Next, you’ll need to get some Kovan Ether into the wallet you just created. The only way to do that is via Kovan faucets.

Once you’ve acquired some Kovan Ether, you’re ready to deploy the contract to the network!

The first step is to boot up the parity client so that its connected to the Kovan network. You can try and do this on your computer if you have a lot of disk space, but I recommend renting a dedicated host on a cloud provider to make your life (I’m using a DigitalOcean droplet).

To start parity on the Kovan chain, run parity --network kovan —-unlock <YOUR_KOVAN_PUBLIC_KEY> --password <PATH_TO_FILE_THAT_HAS_YOUR_PASSWORD>

Then add the following configuration to your truffle.js file

If you’re curious about the configuration in the truffle.js file, the host / port portions are pretty self-explanatory, and the network_id: * simply tells Truffle to accept any network ID it finds at that local. The gas field specifies the gas_limit for the migration and is basically an upper bound on how much Ether ( gas_limit * gas_price — you read the white paper right?) you’re willing to spend for the transaction that deploys your contract. If you’re feeling fast and loose with your money, 4700000 is a good default because its the maximum that the network allows currently and any excess gas will be refunded to you at the end of the transaction.

Once you’re ready to rock and roll, run truffle migrate --network kovan and you should see similar output as before, but this time your contract will be deployed on the Kovan chain instead.

Once its deployed, you should be able to use the address in the terminal to look up your contract on https://kovan.etherscan.io (Etherscan is a web UI for inspecting the Ethereum block chain in real time. The Kovan prefix makes Etherscan inspect the Kovan chain instead of the default production chain)

For example, here is my SingleMessage contract deployed to the Kovan chain: https://kovan.etherscan.io/address/0x6c273582e2f2f34dc0442eb00883397f18cb4a2e

Source

LEAVE A REPLY

Please enter your comment!
Please enter your name here