Nodeless Ethereum Smart Contracts Development with Infura

The Ethereum Blockchain is on the verge of bringing the Decentralized Web to the masses, disrupting several Industries along the way.

Considering that most of Blockchain projects right now are Ethereum-based, and the reported level of adoption of the World’s most popular Blockchain platform, it feels natural for a developer willing to explore the space, to give Solidity a try.

To get started with your Smart Contracts, you will need a development environment and proper access to an Ethereum node.

Show me the tools

Similar to what the Node.js, Webpack and Karma/Mocha stack can bring to the local development lifecycle of frontend developers, Truffle is a development environment and testing framework aiming to make life easier for Ethereum developers.

If you plan on building token sale contracts, a popular option is to combine Truffle with the (almost) ready-to-use Smart Contracts of the OpenZeppelin framework. It’s one of the easiest way to produce consistent code using existing boilerplate that you can inherit from.

The Smart Contract Development Lifecycle

Code auditing aside, a smart contract development process can be simplified into these 3 majors steps:

  1. Local development
  2. Staging (QA) on Testnet
  3. Launch on MainNet

While it’s easy to spoof local RPC calls using ganache, connecting to the staging and main Ethereum networks would normally require you to provision, run and maintain your own Ethereum node.

But do we really want to potentially spend hours on infrastructure management matters? What if your machine also lacks the processing power to do operate a node in acceptable performance conditions?

That’s where Infura could come in handy.

Your access to the Ethereum network

Infura provides an infrastructure for your decentralized applications so you can focus on development and nothing else.

It basically enables you to interact with Smart Contracts on the Ethereum Blockchain through an API. A simple, yet a great way to speed up your development workflow and give your machine an easier time.

Let’s see how.

Requirements

  • A Metamask Wallet with some ETH

(You can get some for free on the Ropsten TestNet faucet)

  • Node (=> 10.x)
curl -sL https://deb.nodesource.com/setup_10.x | sudo -E bash -
sudo apt-get install -y nodejs
  • The Truffle framework
npm install -g truffle

For the purpose of this test, we’ll be calling the functions of Stacktical DSLA token sale Smart Contracts, available at this address.

Time to set things up.

Setup

Metamask

Head over to https://metamask.io/ and install the browser extension.

Double check you are on the right website, on a secure connection.

Once installed, switch Metamast to the Ropsten Test Network (TestNet):

Go to Settings > Reveal Seed Words.

Your Mnemonic Seed Words are displayed. Keep them handy.

Now,
Go to … > Copy Address to clipboard

You should now have in your possession:

✅ Your Mnemonic Seed Words
✅ The address of the account that will deploy the Smart Contracts

Infura

Head over to infura.io, sign up and create a new project from your dashboard.

I’d advise creating 2 projects from the start, one for Development and another one for Production — less room for mistakes.

We will be using the Ropsten TestNet for Development purposes, deploying and whitelisting the smart contract in there.

Make sure your take good note of your API Key .

At the time of writing the V3 auth flow (involving the API Secret) is not mandatory yet.

Getting your local development ready

Clone the token sale contracts repository with the following command:

git clone git@github.com:Stacktical/stacktical-contracts-token-sale.git \
cd stacktical-contracts-token-sale

Then install the Smart Contract dependencies:

npm install

From there, you can edit the code to suit your needs* and start playing with the available Smart Contracts functions using ganache (make sure your instance is up).

Let’s deploy the contracts on a real Blockchain now.

*PRs and Bug reports will be rewarded according to the upcoming OSS Development Bounty Campaign.

Deploying the Smart Contracts to the Testnet

Testnet environments work like the real deal, minus the gas fees.

They’re the perfect playground for repeatedly interacting with Smart Contracts and improve the quality of developments before the official availability of contracts on the Ethereum Blockchain.

We are now going to use Infura to connect to the Ropsten Testnet without installing an Ethereum node locally.

Set your deployment parameters according to the repository README:

export DSLA_INFURA_APIKEY_DEV=<your Infura API Key> 
export DSLA_MNEMONIC_DEV=<your dev mnemonic>

Make sure you replace <your Infura API Key> with your actual Infura API key (ndlr: on your Infura dashboard) and <your dev mnemonic> with the Metamast mnemonic you had previously saved.

Now proceed with the contracts deployment using the following command:

truffle migrate --network ropsten
The DSLA and DSLAPrivateSale contracts are successfully deployed to Ropsten.

Check the status of your deployment on Etherscan:

https://ropsten.etherscan.io

Once deployed, you will be able to whitelist your Smart Contract address in your Infura dashboard:

Giving the deployed Smart Contracts a try

truffle console --network ropsten

Now let’s execute some function of the Smart Contract:

$ truffle console --network ropsten
truffle(ropsten)> participant = "0x556249c7E5B660CCe504a7e5EbA021F32C6216Df"
'0x556249c7E5B660CCe504a7e5EbA021F32C6216Df'
truffle(ropsten)> crowdsale.addToWhitelist(participant)
{ tx:
'0x582f2ea7f2988a214a72271adc2dc5ccd1be35eedd98af3877f1e29c343afb4a',
receipt:
{ blockHash:
'0xe51a8b35f52cc00ce1e73b763e884a6c693a7d8f685b5558bf9bc4def5993929',
blockNumber: 3715376,
contractAddress: null,
cumulativeGasUsed: 122286,
from: '0x87af48ad0668de43d689da0c33899a2d3aa1d86b',
gasUsed: 44104,
logs: [],
logsBloom:
'0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000',
status: '0x1',
to: '0x9f44261d94bcb279c9682932990b62adb8c392a1',
transactionHash:
'0x582f2ea7f2988a214a72271adc2dc5ccd1be35eedd98af3877f1e29c343afb4a',
transactionIndex: 2 },
logs: [] }
truffle(ropsten)> crowdsale.whitelist(participant)
true

Et voilà!

Infrastructure management can be daunting for developers that are not in full DevOps capacity, especially when it comes to Blockchains, DAGs and other Ledger-based technologies.

I hope you have a better idea on how Infura makes it easier to focus on your Solidity developments and (nearly) forget about the rest.

Upon successful testing and auditing the Smart Contracts, they can be deployed to the Mainnet using the following Truffle command:

truffle migrate --network production

And the Token Sale can start

Obviously, you’ll want to deploy your own token on the Mainnet, not ours!

About Stacktical

⚡️ Stacktical combines the power of predictive and blockchain technologies to help companies indemnify their customers for performance failures, while rewarding their support teams for operational excellence.

Powered by DSLA, the Decentralized Service Level Agreement ERC-20 token.

📣 To learn more about the DSLA token sale, go to stacktical.com and join our Telegram community chat.