# Asset tokenization with Embark

This tutorial will guide you through creating a tokenized asset contract and deploying it on Ropsten testnet.

For illustration purposes, this contract does the following:

  • Creates a total supply of 1000 tokens specific to the asset.
  • Sets the token price to 0.1 ether.
  • Lets anyone exchange ether for the asset tokens.

This tutorial uses Embark to test and deploy the contract.

# Prerequisites

  • Chainstack account to deploy a Ropsten testnet node.
  • Embark to test and deploy the contract.
  • Geth to create an Ethereum account that will deploy the contract.
  • MetaMask to interact with the contract.

# Overview

To get from zero to an asset tokenization contract running on Ropsten, do the following:

  1. With Chainstack, create a public chain project.
  2. With Chainstack, deploy a Ropsten testnet node.
  3. With Embark, create a project and the contract.
  4. With Geth, create a new account.
  5. Import the account in MetaMask and fund the account with Ropsten ether.
  6. With Embark, deploy the contract through the Ropsten node.
  7. With Embark, check the contract with Cockpit.
  8. Interact with the contract through MetaMask.

# Step-by-step

# Create a public chain project

See Create a project.

# Deploy a Ropsten node

See Join a public network.

# Create an Embark project and the contract

  1. Create a new Embark project:

    embark new asset
    

    This will create an Embark directory called asset.

  2. Change to the contracts directory of the Embark project.

  3. Create an AssetTokenized.sol file in the contracts directory:

    pragma solidity = 0.5.0;
    
    contract AssetTokenized{
    uint public supply;
    uint public pricePerEth;
    mapping( address => uint ) public balance;
    
    constructor() public {
        supply = 1000;                    // There are a total of 1000 tokens for this asset
        pricePerEth = 100000000000000000; // One token costs 0.1 ether
      }
    
      function check() public view returns(uint) {
        return balance[msg.sender];
      }
    
      function () external payable {
        balance[msg.sender] += msg.value/pricePerEth; // adds asset tokens to how much ether is sent by the investor
        supply -= msg.value/pricePerEth;              //subtracts the remaining asset tokens from the total supply
      }
    }
    

# Create an Ethereum account

You will use this account to deploy the contract.

  1. Create the account:

    geth account new
    
  2. Check the path to the keystore file created for the new account:

    geth account list
    

# Import the account in MetaMask and fund the account

  1. In MetaMask, click Import Account > JSON FIle.
  2. Select the keystore file that you created earlier.
  3. Fund the account with Ropsten ether at Ropsten Ethereum Faucet.

# Deploy the contract

  1. In your Embark project directory, change to config.

  2. Append contracts.js with the following configuration:

    chainstack: {
        deployment:{
          accounts: [
              {
                privateKeyFile:"PATH_TO_KEYSTORE",
                password:"PASSWORD"
              }
          ],
          host:"RPC_ENDPOINT",
          port:false,
          protocol:"https",
          type:"rpc"
        },
        dappConnection: [
          "$WEB3",  // uses pre existing web3 object if available (e.g in Mist)
          "ws://localhost:8546",
          "http://localhost:8545"
        ],
        gas: "auto",
      }
    

    where

    • PATH_TO_KEYSTORE — the location of the keystore file.
    • PASSWORD — the password you provided when creating the Ethereum account with Geth.
    • RPC_ENDPOINT — your Ropsten node RPC endpoint with username and password. The format is user-name:pass-word-pass-word-pass-word@nd-123-456-789.p2pify.com. See also View node access and credentials.

    Contract example:

    chainstack: {
        deployment:{
          accounts: [
              {
                privateKeyFile:"//root/.ethereum/keystore/UTC--2019-08-01T07-24-17.754471456Z--73236c8d8aaee5263e8a32c71171030dd7a3e8e6",
                password:"123456"
              }
          ],
          host: "user-name:pass-word-pass-word-pass-word@nd-123-456-789.p2pify.com",
          port:false,
          protocol:"https",
          type:"rpc"
        },
        dappConnection: [
          "$WEB3",  // uses pre existing web3 object if available (e.g in Mist)
          "ws://localhost:8546",
          "http://localhost:8545"
        ],
        gas: "auto",
      }
    
  3. Deploy the contract with Embark:

    embark run chainstack
    

    where

    • chainstack — the argument from the configuration file contracts.js.

This will deploy the contract on Ropsten.

# Check the contract with Cockpit

On contract deployment, Embark runs Cockpit which is a front-end application to test the contract.

In your browser, open:

http://localhost:55555/explorer/contracts/CONTRACT_NAME

where

  • CONTRACT_NAME — the name of your contract. In this tutorial, the path is http://localhost:55555/explorer/contracts/AssetTokenized.

This will also display the contract address in the Deployed at line.

Test the contract by calling:

  • supply() — to check the remaining supply of tokens on the contract.
  • check() — to check the amount of tokens owned by the Ethereum address you are using to call the contract.
  • pricePerEth() — to check the token price in wei.

# Interact with the contract

  1. In MetaMask, send an amount of Ropsten ether to the contract address.
  2. In Cockpit, call the contract functions supply() and check() after a few seconds to see a change in values returned.