Written by Edward Jackson, Founder at AlliedBlock.com
This article will take you from never having touched a Smart Contract all the way to launching your first token on the Ethereum Ropsten testnet. It sounds harder than it actually is and the guide below will take you step by step. Lets do it!
The very first thing that you need to do is get your local environment setup with the requirements. Smart contract launches on Ethereum require Node.js and the associated npm package manager to be setup on the machine you’ll be working with. So, right off the bat, if you don’t have Node on your machine head to this link: https://nodejs.org/en/download/ and download and install the latest stable version for your operating system.
Once node is setup on your system, we will use it to install the truffle package. Truffle is a tool developed by Consensys (https://consensys.net/) to act as a development environment for work with the Ethereum Virtual Machine. Use the following command:
npm install -g truffle
With truffle up and running, we can now take advantage of Truffle’s pre-built example project templates. Truffle calls them ‘truffle boxes’. Here’s a full list: https://www.trufflesuite.com/boxes. We’re going to use the metacoin truffle box which sets up a simple token that can be transferred between accounts on the Ethereum blockchain.
First thing to do is set up the local directory where you’d like to house the metacoin project. Navigate to that newly created directory and open up your command line with that new metacoin directory as your root. Use the following command to fetch the metacoin files:
truffle unbox metacoin
The metcoin box sets up several files and folders on your local machine. Here is a breakdown:
· contracts/: Directory for the actual Solidity Smart contracts
· migrations/: Directory for scriptable deployment files
· test/: Directory for test files for testing your application and contracts
· truffle-config.js: Truffle configuration file for setting requirements
The next step is to prep your local directory for npm package installs. We do this by setting up a package.json file. While pointing to your target directory folder, run the following:
npm init
This will prompt you for a few fields to fill out. For our testing purposes, the default values for ‘package name’, ‘version’, ‘entry point’, and ‘license’ can be utilized, and the remaining fields can simply be left blank.
Now that the package.json is setup in the directory, we can successfully pull in the npm package hdwallet-provider. Hdwallet-provider will allow us to sign transactions derived from our set mnemonic. Use the following command to pull the hd-wallet package into your local folder:
npm install @truffle/hdwallet-provider
Make sure in you command line you don’t see any ERROR messages from this install. Here’s the official documentation in case you hit any snags: https://github.com/trufflesuite/truffle/tree/develop/packages/hdwallet-provider.
If all went well, you’ll see a ‘node_module’ folder in your metacoin local directory. Your local directory is now primed and ready. We now need to go online to setup the access point to the Ethereum network and the wallet that’ll be used to fund metacoin’s launch onto the testnet.
Lets first tackle the access point. For this, we’ll use infura.io. Create an account on the infura site and then create a new Ethereum project. Under the ‘settings’ tab of your Ethereum project, locate the Keys section as pictured below:
Make sure to adjust your ENDPOINT to Ropsten. Copy the initial link from your page (it will be different than the one shown). Paste that link somewhere where you can easily access it later.
Next step, is to setup a new wallet. Again, this is going to serve as MetaCoin’s creator wallet. To do this we will be using MyEtherWallet (MEW) (https://www.myetherwallet.com/). Once on MEW’s site, select “Create A New Wallet”. The site will prompt you with several informational modal boxes. Just walkthrough each of the prompts until you reach the ‘Get a New Wallet’ screen. Once on the ‘Get a New Wallet’ screen, select “By Mnemonic Phrase” as below:
You’ll be presented with 12 random words. This is your hd wallet’s mnemonic phrase. Copy all 12 words and store that in the same place you stored the infura link for easy access later. The mnemonic is your wallet access point so always store it in a safe place. Once your mnemonic is noted down, go ahead and click “I Wrote Down My Mnemonic Phrase”, verify the mnemonic on the next prompt and move on to the next screen.
On the ‘Access My Wallet’ screen, select the Software option as below:
Select Mnemonic phrase and then re-input your mnemonic. After inputting your mnemonic, youll be prompted with the following screen:
The Network and HD Derivation Path will default to Ethereum Main Net, make sure you adjust both to Ropsten. Copy the very first wallet address from your list. Store that with your infura.io link and your mnemonic for easy access later.
Before we leave MEW, lets make sure we fund this address with test Ethers to use when migrating the metacoin contract to Ropsten. I’ve found this faucet is a good source of test Ether for Ropsten: https://faucet.ropsten.be/. There are others out there as well so if this one doesn’t work just run a quick Google search for “Ropsten Faucet”.
Once you submit your address to the faucet, you should see a balance of ROP in your MEW wallet. It may take a few moments for the test Ether’s to transfer. You can refresh the balance by hitting the circular arrows on the bottom of the box next to the ellipse.
Make sure it’s there because if your balance is null, the Smart Contract migration will fail.
With all of this now setup, we can move back to our local folder and adjust the ‘truffle-config.js’ file. The easiest method to get this code right is to delete the default copy in there and paste the following:
const HDWalletProvider = require("@truffle/hdwallet-provider");
const mnemonic = "wagon power south…";
module.exports = {
networks: {
development: {
host:"localhost",
port: 8545,
network_id:"*" // Match any network id
},
ropsten: {
// must be a thunk, otherwise truffle commands may hang in CI
provider:()=>
new HDWalletProvider(mnemonic, "https://ropsten.infura.io/v3/YOUR-ID",
0, 1, true, "m/44'/1'/0'/0/"
),
network_id: '3',
}
}
};
Replace the first red text area with the mnemonic you got from MEW and the second red text area with the link you copied from infura. Big note to take here as we complete this config file, it is NOT best practice to store your mnemonic in plain sight like this. This is for testing ONLY. In a real deploy, you’d store your mnemonic in a non front facing asset like an environmental variable and utilize that in the config file. But, for simplicity of testing, this will work for our purposes.
You’re now ready to compile your smart contracts. Run the following:
truffle compile
This will create bytecode versions of the metacoin contracts which can be read by the EVM. You’ll see a new ‘build’ folder in your directory containing these new files.
We’ve now reached the final step, the migration to the ropsten testnet. Run the following:
truffle migrate --network ropsten
If all goes well, you’ll be able to see your contract directly on etherscan (https://ropsten.etherscan.io/) by searching for the contract address you see in the command line output:
Congrats, you just launched your first LIVE smart contract on the Ropsten Testnet!
AlliedBlock is a global blockchain research and development firm that is focused on delivering tailored blockchain development solutions. Contact us to learn more about how we can help you tailor a blockchain solution for your business (www.alliedblock.com).
Commentaires