Meterify DApp Tutorials

DApps are decentralized applications that utilize blockchains to store a history of operations (e.g. transactions). They can also consist of functional code, called smart contracts, that are published to the blockchain. DApps interfaces can be graphical, console-based, or APIs.

Popular categories for DApps include finance, exchanges, and gambling, but social applications and games also exist.

One of the primary use cases for a DApp is sending cryptocurrency between two accounts. For example, when Alice wishes to send some MTR and MTRG to Bob's account. Application developers can create methods for this on the Meter blockchain using meterify, an extended version of Web3 (AKA the Ethereum JavaScript API library).

The final project files can be found here:

Prerequisites

Meterify requires that Node.js version 10.15.1 or above is installed, as well as npm.

$ node --version

v10.16.3

$ npm --version

6.9.0

Example DApp

Step 1 - Project Setup

Create a directory (e.g. meter-dapp) and initialize a new project using npm. Then install the app prerequisites meterify and web3, plus the JavaScript bindings for the Solidity compiler (solc).

Step 2 - Test the Connection

Create a file called index.js, and include the meterify and web3 requirements, then call the file with node to test the connection to the testnet.

index.js:

If the code runs without any errors the connection was successful.

Step 3 - Creating an Account-Generating Function

The meterify.eth.accounts package contains functions for generating accounts and signing transactions and data. Create an object, using Alice's and Bob's names as keys. Perform a loop on the object, calling the create function. This will generate new local accounts that contain both a private key and a public key. Add each account to the object as is created.

Step 4 - Add Accounts to a Wallet.

meterify.eth.accounts also contains an in memory wallet to store multiple accounts. Loop through the object returned by the createAccounts function, and add Alice's and Bob's accounts to the wallet, using each account's private key.

Step 5 - Send MTR to an Account

Use the eth package's sendTransaction method to create a function for sending some MTR and MTRG from Alice to Bob.

Units in meterify are Wei, where 1 MTR = 10e18 Wei. Note that the identifier code for MTR is 0000000000, while MTRG is 0000000001. sendTransaction returns a promiEvent that is considered resolved once the receipt becomes available.

Step 6 - Run the application.

Load, Deploy, and Test a Smart Contract

The following modifications to the example demonstrate the use of a sample smart contract on the Meter blockchain. Again, follow the steps to add code snippets to the existing index.js file.

Step 1 - Load a Smart Contract.

The file can be found here: sample_token.sol

Step 2 - Deploy a Smart Contract

Step 3 - Register Contract Events

Additionally, call some example functions within contractReady when the contract is ready.

Step 4 - Transfer Between Accounts.

Step 5 - Get a Balance.

Step 6 - Mint Some Coins.

Using Docker

Step 1 - Ensure docker-compose is Installed.

Step 2 - Create docker-compose.yml

A copy of this file can be found here: docker-compose.yml

Step 3 - Run docker-compose

Initiate the test application with docker-compose.

If there are no errors the connection was successful.

Stop Docker using Ctrl+C.

Step 4 - Comment out dependency installation.

After the first run in docker-compose.yml, stop repeated initialization and dependency installation by commenting out the following lines.

Last updated

Was this helpful?