Migrating the smart contract code using Truffle

To work with our contracts, we first need to migrate these contracts to our test blockchain. Migrations carry out the following tasks: 

  • They deploy the compiled contract code to the blockchain.
  • They establish interlinking between dependent contracts.
  • They initialize the initial values through the constructor.
  • Lastly, and most importantly, they manage the different versions of the contracts deployed. In the traditional model, every time a contract is deployed, a new Ethereum address is generated that then needs to be updated to the code of the blockchain application. Truffle allows us to abstract this concept and invoke the contract directly through a contract object instead of the address.

To deploy the smart contracts, first bring your Ganache blockchain online. Make sure your Ganache test server is running on localhost:8545. To do so, select the New Workspace option from the Ganache launch screen. Click on the SERVER tab on the Workspace screen. Set the port number to 8545:

Click on Save Workspace in the upper-right corner. A blockchain network will be started as follows:

Now let's migrate the contracts we wrote earlier to our Ganache blockchain network:

  1.  We start with creating migrations for the two token contracts using the Truffle command line, as shown here:
truffle console
truffle(development)> create migration condos_migration
truffle(development)> create migration moolahcoin_migration

Two migration files will be created with the following nomenclature:

  • *{timestamp}_moolahcoin_migration.js
  • *{timestamp}_condos_migration.js
  1. Replace the content of the *{timestamp}_moolahcoin_migration.js file with the following code:
let MoolahCoin = artifacts.require("MoolahCoin.sol"); 
module.exports = function(deployer) {
deployer.deploy(MoolahCoin);
};
  1. Replace the content of the *{timestamp}_condos_migration.js file with the following code:
let Condos = artifacts.require("Condos.sol"); 
module.exports = function(deployer) {
deployer.deploy(Condos);
};
  1. In the Truffle console, enter migrate to migrate the contracts to the blockchain, as shown in the following code:
truffle(development)> migrate
  1. Now, check the Ganache UI. It will show that some blocks have been mined and the transactions have been created:

Note down the contract addresses from the console. We'll need this later. Now, let's create a token wallet frontend using ReactJS in the next section.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.187.103