Compiling and deploying Ethereum Smart Contracts with pure JavaScript.

Daniel Lara
Coinmonks
7 min readJan 31, 2019

--

The development of Ethereum Smart Contracts has increased exponentially in 2018 and it seems that 2019 will follow the trend. There are some frameworks, like Truffle, that help us in the process of building and maintaining our Smart Contract but if you like to get your hands dirty, I will explain how to compile your Smart Contract and deploy it to an Ethereum network (Rinkeby, Ropsten or Mainnet) with your own scripts.

NOTE: I am going to use simple Smart Contracts because the aim of this post is just to show how to compile and deploy scripts.

Previous steps

If you get stuck, the full code is available in this Github repo.

The project has the following structure:

Project structure

The folder Contracts contains our contracts (you can have as many contracts as needed). For this post, we are going to use two simple contracts:

MyContractA.sol
MyContractB.sol

As you can see, we have two files, one of them contains two contracts, proving that we can have as many contracts as we want.

Compile script

Now, once we have our initial project structure and our contracts, is time to start building the compile script.

The aim of the script is to generate one JSON per contract (in this case we will finish the compile process with three JSON), each of them containing the compiled contract information. These JSON will be stored in an output path called build/

The steps for building our script are the following:

  1. Create the build/ directory.
  2. Get the sources of our contracts.
  3. Compile the contracts and write the output to a file.

Step 1 - Create the build/ folder.
This step is the simplest due we only need to know some basics of JavaScript (no Ethereum concepts needed here).

Step 2 - Get sources of contracts
Now it’s time to get the source of all our files inside contracts folder. In this step, like in the step one, no Ethereum concepts are needed just only JavaScript, but it is a bit more complex (don’t worry just a bit ;)).

For each file in our contracts folder, we add a new field in sources object (we will see in step three the purpose of this sources) where the key is the filename and the value is the content of the .sol file.

Step 3 - Compile and write the output
Now that we already have our output folder and the content of the contracts, it’s time to compile them (yes! finally the Ethereum part appears).

First of all, we have to define an object that will serve as an input of information for the Solidity compiler.

  • language : the programming language of our contracts, in this case is Solidity but you can choose other (Viper, assembly…)
  • sources : the content of our contracts.
  • settings : this option tells the compiler what fields of the output we want to be generated. For this example I have choose to generate the abi and the evm.bytecode for all of our files in sources. This two piece of information are the necessary in the Deploy phase.

Once we have the configuration object for the compiler we can execute it.

The first line get an object that contains our scripts compiled. The two level of for loops allows us to store in different JSON files the contracts that are in a single .sol file (like MyContractA.sol in our case).

One final step should be stick together the three parts

With this we have finished the compile script. If we run it, the project structure should look like this:

We have gitignored the build folder because has no sense to track it under Git.

Now that we have our contracts compiled is time to deploy them to the blockchain.

Click here if you want more info about the compiler.

Deploy script

It’s time to deploy our contract to the Ethereum Blockchain (in this case the deploy will be against Rinkeby so no real ether will be spent but the process is similar to other Ethereum Blockchains).

First of all we need two things to deploy a contract to the Blockchain:

  • An unlock account: due we need to spent gas to send the transaction that will create the Smart Contract.
  • A node connected to the Blockchain: we are sending a transaction to the net so we need something connected to it.

For the first point we are going to use HDWalletProvider, this tool (thanks Truffle :)) allows us to unlock accounts using a mnemonic twelve words phrase and connect to an Ethereum node (uoh! it seems what we need in point two). But… where is that node? Well we can do two things, run our own Ethereum node (which is a thing I do not recommend, at least for developing dApps for hobbie) or use Infura. Infura is a service that allows us to connect to the Ethereum network without running our own Ethereum node. So, it seems that we have our two points covered.

NOTE: In other post I will cover creation of accounts for Rinkeby testnet. But, for now, let’s continue assuming that we have at least one account in Rinkeby.

Unlocking accounts and connecting to the node.
For using Infura we need to register in its page. It will generate an API key that we need to use its services (it is completely free)

We will use Web3JS to interact with the blockchain. Web3 needs a provider to connect and interact with the node. Yeah! You are right! this provider is HDWalletProvider, so let’s configure it and Web3.

The first argument for HDWalletProvider is our twelve words mnemonic phrase. This phrase allows the provider to unlock the account (it unlocks more than one by default but we only need the first one at this moment), the second argument tells the provider where is the Ethereum node, in this case we connect via Infura (if you use your own node it should look something like ws://localhost:4535 or wherever your node is) .

Finally we set the provider inside web3.

With web3 properly configured we can finally deploy the contract.

First of all we need our compiled script (we are only to deploy one, but the process is the same for the others, or maybe you can build some logic to take all compiled and deploying one by one).

Then, in line 8 we create our contract using web3 API and passing to it the interface of the contract. Following this, we need to create a deploy transaction with the info of the contract:

  • data:the byte code of the contract. It starts with 0x to indicate that is hexadecimal.
  • arguments: the parameters of our contract constructor (no need if the constructor haven’t arguments).

Once the transaction is created, we send it to the Ethereum Blockchain so it can be processed for the nodes:

  • from: the account that signed the transaction and sends it. In line 4 we get the accounts generated and unlocked by HDWalletProvider and the twelve words phrase.
  • gas: the maximum amount of gas that we want to spend sending the transaction.

The console.log of line 18 is important in order to get the address where the contract has been deployed so we can use it in the future for our dApp, for consuming by other contracts, etc.

The last line of the script tells the provider to end the connection to the Ethereum node.

If we execute the script, after a while, we obtain a trace like the following:

Now we can go to EtherScan and see our contract deployed in the Blockchain.

Finally, we have our contract deployed and accessible to everyone. I hope this post has been helpful.

Did you like what you read? Recommend this post by clicking the heart button so others can see it!

Join Coinmonks Telegram Channel and Youtube Channel get daily Crypto News

Also, Read

--

--