Deploying Smart Contracts To An Azure Blockchain Using Truffle

In this tutorial, I’ll talk about how to migrate Solidity smart contracts to your own personal blockchain. I’ll be using the standard “MetaCoin” contract that comes included when creating a new Truffle project.

If you haven't already, make sure you complete the Deploying An Ethereum Consortium Blockchain on Azure tutorial first, as it goes over creating an Ethereum network and logging in with Geth.

We’ll be using the Truffle Framework to migrate our smart contracts onto Azure. Make sure you have Node installed, and run:

$ npm install -g truffle

Starting A Project

$ mkdir truffleProject
$ cd truffleProject
$ truffle init

This initializes a Truffle project within the truffleProject folder. If we open our folder, we’ll see folders for contracts, migrations, and test, along with a truffle.js file.

Truffle Project directory

The truffle.js file contains the information for deploying smart contracts onto a blockchain network. Currently, the configuration is set so that contracts are deployed to a local network, for use with testrpc. We need to add the endpoints for connecting to our Ethereum network on Azure.

Default settings: deploys to testrpc network

In Azure, go to your deployment configuration screen and grab the “ADMIN-SITE” and “ETHEREUMNETWORKID” fields. (If you need help finding this screen, see the previous tutorial.)

Deployment configuration screen

Now, in the truffle.js file, add a new network:

// Truffle.js file 
module.exports = {
networks: {
development: {
host: "localhost",
port: 8545,
network_id: "*" // Match any network id
azureNetwork: {
host: "",
network_id: 10101010,
port: 8545

You can name it whatever you want. In my case, I named it “azureNetwork”, and added the host from the “ADMIN-SITE” output and the network ID from the “ETHEREUMNETWORKID” field.

We’re almost ready to deploy. There’s just one more step: we need to unlock our Ethereum account. By default, accounts in Geth are “locked,” meaning that sending transactions is not allowed. Creating a smart contract on the blockchain is a transaction and requires gas, and so we need to unlock our default account.

Log on to your Ethereum network on Azure using SSH. Once you’re logged in, run:

$ geth attach 
// Opens the Javascript console
> personal.unlockAccount(personal.listAccounts[0], "MyPassword20!", 0)
// Unlock 1st account using your password, indefinitely

Note: Using the Javascript console to unlock an account will log your password. If you don’t want your password to be logged, use Geth instead.

geth --unlock <YOUR_ACCOUNT_ADDRESS> --password <YOUR_PASSWORD>

Now, we’re ready to deploy from our computer to the cloud!

Back in your console, from the truffleProject directory, run

$ truffle migrate --network azureNetwork

This deploys your contracts to that network, from Account #0 on your network:

Truffle deployment

We see that the Metacoin contract is deployed at address “0xc59cc5b5eecc60483437031d1c807ba3127e37f2.”

We can run

$ truffle console --network azureNetwork 

to open up the truffle console, which connects automatically to our blockchain network. With truffle, you get access to the web3 library, and to interacting with your contracts as if you were writing tests for your front end.

truffle(azureNetwork)> MetaCoin.deployed()    
// Returns deployed instance of MetaCoin contract
truffle(azureNetwork)> web3.eth.accounts
[ '0xec39bea3e66a7e9064af10e3990760dff9449e01',
'0xae3ad20bd7225e121ec66892a03f209871c52c2d' ]
// Returns an array of accounts that available within the Ethereum network we're in
truffle(azureNetwork)> MetaCoin.deployed().then((instance) => { return instance.sendCoin(web3.eth.accounts[1], 5, {from: web3.eth.accounts[0]})})
// Uses the sendCoin function from that contract to send 5 MetaCoins to Account #1 from Account #0
// Returns a transaction receipt
truffle(azureNetwork)> MetaCoin.deployed().then((instance) =>[1], {from: web3.eth.accounts[1]}))
{ [String: '5'] s: 1, e: 0, c: [ 5 ] }
// Uses the getBalance function from the MetaCoin contract
// We can use .call because the function does not mutate the state of the blockchain
// Returns a BigNumber in Javascript

And that’s it! You now have smart contracts that live on your private blockchain. You can interact with them using Geth or the Truffle Console, and start writing some front-end code using web3.