Migrating all the contracts to the blockchain 

Now that we have written the contract, we need to migrate the contracts to our blockchain where we can execute them:

  1. Copy and paste the orderbook smart contract file to the contracts folder in your Truffle workspace.
  2. Navigate to the Truffle console Terminal that you had opened earlier for compiling the USD and Gold token contracts. If you closed the console, you can bring it online again by entering truffle console in your Truffle workspace:

  1. Compile all the contracts by entering the compile command in the Truffle command line.
  2. Provided there are no errors, your contracts should now be compiled and ready to migrate.
  3. Now, create a migration file for the orderbook contract. Enter the following command in the Truffle console:
create migration orderbook
  1. Navigate to the migration folder in your Truffle workspace. A new migration file should have been created for your Orderbook contract.
  2. Open the file and replace the contents of the file with the following code:
const Orderbook = artifacts.require("Orderbook");
const Gold = artifacts.require("Gold");
const USD = artifacts.require("USD");

module.exports = function(deployer) {

deployer.deploy(USD).then(function(){
return deployer.deploy(Gold);}).then(function(){
return deployer.deploy(Orderbook,USD.address,Gold.address);})
};

Let's understand the migration script:

  • The preceding migration script deploys three contracts back to back. It first deploys the USD token contract, followed by the gold token contract.
  • It uses the blockchain addresses of the USD and Gold tokens as input parameters to the constructor for the orderbook contract.
  1.  This is required because our Orderbook contract constructor takes the address of the Gold and the USD contract as input and uses it to instantiate the Gold and USD contract:
//orderbook smart contract constructor.

constructor (address Base,address Counter) public
{
ERC20Base = ERC20(Base);
ERC20Counter = ERC20(Counter);
owner = msg.sender;
}
  1. Now, navigate back to the Truffle console and enter migrate in order to migrate all the contract to the blockchain.
  1. Make sure you note down the contract address for the USD token, the Gold token, and the Orderbook contract, because we'll require these again soon enough:

  1.  Copy the contract address for all the contracts:

That brings us to the end of our smart contracts. Now, let's build the browser app that the user will interact with.

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

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