The goals of this project are as follows:
- Plan the project with write-ups.
- Write smart contracts.
- Test smart contract code coverage
- Deploy smart contract on public test network.
- Create web client to interact with smart contract
This project is a Distributed Application (DApp) which includes an Ethereum smart contract and a web front-end. The smart contract represents a pharmaceutical supply chain implemented with Solidity .
The Supply Chain smart contract (SupplyChain.sol) is implemented using Solidity. The contract inherits from a contract that provide ownership (Ownable.sol) functions. And contracts based on the Role contract (Roles.sol) that implement role-specific functions (ManufacturerRole.sol, DistributorRole.sol, RetailerRole.sol, and ConsumerRole.sol).
UML diagrams describing the contracts and their interactions are listed below.
The Supply Chain contract includes a struct
named Item
(SupplyChain.sol lines -38-54) which holds item information including the current owner, manufacturer, distributor, retailer, and consumer information.)
Unit tests are located within TestSupplychain.js. They are implemented in JavaScript using the Truffle framework. The tests can be run by starting Ganache and executing the following commands (from a Windows Command prompt) from the main project folder.
C:\truffle.cmd develop
truffle(develop)>test
Truffle will compile the solidity contracts located under contracts
, then deploy the contracts to Ganache and execute the tests located under test
. An image of the truffle test execution is below.
Truffle is used to deploy the smart contracts to a target network. The truffle configuration file (truffle-config.js) controls where Truffle deploys the project's contracts. A screenshot of the truffle-config.js file is shown below. The config file contains deployment parameters for two networks: development and rinkeby.
require('dotenv').config();
var HDWalletProvider = require('truffle-hdwallet-provider');
var infuraUrl = 'https://rinkeby.infura.io/v3/' + process.env.INFURA_API_KEY;
module.exports = {
networks: {
development: {
host: '127.0.0.1',
port: 7545,
network_id: "*"
},
rinkeby: {
provider: function() {
return new HDWalletProvider(process.env.MNEMONIC, infuraUrl)
},
network_id: 4,
gas: 4500000,
gasPrice: 10000000000,
}
}
};
The rinkeby network configuration tells Truffle to deploy the smart contracts to the RINKEBY test network through the Infura blockchain infrastructure. The wallet mnemonic and RINKEBY API key values are retrieved from the .env file using the dotenv node.js module.
Running the following commands will initiate the deployment to the RINKEBY test network.
cd contracts
truffle migrate --network rinkeby
The project includes an HTML test client (index.html) that interacts with the contract on the Ethereum RINKEBY test network. The front-end uses the Web3 Ethereum JavaScript API to interact with the supply chain contract on the RINKEBY test network through the MetaMask browser plug-in.
Here is a screen shot of the web client.
The interface interacts with the smart contract to move a drug through the supply chain process; from manufacture to purchase by the end consumer.
The following software must be installed on the host machine.
-
Install Ganache
Download and install the version of Ganache for your operating system.
-
Install Truffle
Run the following command
npm install truffle -g
-
Install MetaMask
Install the MetaMask Chrome extension.
-
Install Node.js dependencies
The following node installation command should be executed from the project root directory.
npm install
The project requires an Infura account. You can create one here.
The truffle test and migration process requires a file named .env to be located in the root folder. This file must contain your wallet mnemonic and Infura API URL.
INFURA_API_KEY=<INFURA API KEY GOES HERE>
MNEMONIC="<WALLET MNEMONIC GOES HERE"