Photo by Launchpresso on Unsplash

Blockchain and the Supply Chain

Kushagra Jindal
Integraate
Published in
5 min readMay 8, 2020

--

The supply chain in today’s globalised economy is evolving as we speak, it requires technology to develop and grow, for the betterment of consumers and providers. Imagine, how wonderful it would be, when you purchase a product in a supermarket and to know the complete process cycle of the product (including the list of all the middlemen and metadata) all you need to do is scan a code.

In this blog, I will share how we can integrate the blockchain technology in supply chain, for effective management and to know its effects on the supply chain. We will further discuss a use case for tracking one apple between suppliers and consumers using the Ethereum network.

Benefits of blockchain in supply chain management

If we recall the basics of blockchain, the two of its most important characteristics are decentralisation and transparency.

These features can be used to find a solution to supply chain problems:-

(a) Decentralisation — When the complete data associated with a product is not stored at a central location, it will give rise to immutable and more accurate information about the product. Thus, it increases the reliability of the product.

(b) Transparency — The ledger of a public blockchain can be viewed by anyone and everyone, therefore it provides transparency into the process cycle of the product. This increases the audibility in the supply chain. For example, I, as a user can view every detail associated with the product before shopping that product.

Traceability as an application

As the number of consumers, who demand information about the product before buying, increases, blockchain can help them view complete details of the product. It also helps the supply chain to verify the quality of the product, with details of all the raw material used in the manufacturing process.

For example, consider a car which is made up of, thousands of small parts. Blockchain can be used to track all the individual parts involved in the manufacturing of the car. Now let’s assume the owner of the car wants to sell it after a year. Every individual who is interested in buying the car can track the complete history of the car from manufacturing, to how the car was managed in that one year.

Similarly, we can take another example of medicines. Consider a medicine being manufactured by a pharmaceutical company using various chemical compounds. Blockchain can be used to track all the information associated with its components as well as the users and middlemen can also track the same.

An Apple Use Case:-

Blockchain can be used in the apple supply chain, to track the process of an apple grown by a farmer to the end-user. Where a farmer will mint or register an apple using the public key and an end-user will ‘burn’ or consume the apple.

Development:- On Ganache-CLI network with truffle and web3.js

First of all, we will develop a smart contract for the implementation of an apple token. Here, we consider an apple as Erc721 token, whose functionality can be implemented using openzeppelin-solidity. Openzeppelin-solidity is a npm package that provides the basic functionality of ERC20, ERC721 and ERC777 tokens.

pragma solidity ^0.5.16;
import "/openzeppelin-solidity/contracts/token/ERC721/ERC721Full.sol";import "/openzeppelin-solidity/contracts/token/ERC721/ERC721Burnable.sol";contract Apple is ERC721Full, ERC721Burnable{ uint totalTokenToBeMinted = 5; constructor() ERC721Full("Apple", "APP") public {} function mint(address to, uint256 tokenId) public { _mint(to, tokenId); } function _mint(address to) public{ require((totalSupply()+1) <= totalTokenToBeMinted ,"error"); mint(to, totalSupply().add(1));

}
}

In the above smart contract, we inherit the ERC721Full and ERC721Burnable from the @openzeppelin-solidity/contracts/token/ERC721. We provide the token name as “APPLE” and Symbol as “APP”. The mint function in the smart contract is used to mint a new apple, which means, a farmer can create a token for the apple. Now, the owner of the apple is the farmers public key. A totalTokenToBeMinted variable can be used to provide the maximum limit of tokens that can be minted in our smart contract.

contract Apple is ERC721Full, ERC721Burnable{

...

function appleGone(uint256 tokenId) public{
burn(tokenId); } function setData(uint256 index, string memory text) public { _setTokenURI(index,text); }}

As we have added the functionality of ‘burning’ the apple that is used when an end-user consumes the apple, the apple token can be ‘burned’ or destroyed. The setData function is used to set the token Uri of the token. Now, we need to make a migration file so truffle knows which contract to migrate.

var Apple = artifacts.require("Apple");module.exports = function(deployer) {  deployer.deploy(Apple);};

We are ready to deploy our contract on the ganache-CLI network. Initially, compile the contract using the truffle Compile and then migrate it to the ganache-CLI using the truffle migrate command. Make sure that the ganache-CLI and truffle are working on the same port.

2_apple.js
==========
Deploying 'Apple'
-----------------
> transaction hash: 0x84fac3f21f5adebc7f17e8ad26567c542d065c8884404ad6ad426e8039a21ede
> Blocks: 0 Seconds: 0
> contract address: 0xbCe33371462f9AE3dAD88a5Ad1b166ab368389fd
> block number: 3
> block timestamp: 1588925711
> account: 0x5a4d0773D1Df9B0f99627406536d6082955C79B8
> balance: 99.93843114
> gas used: 2871927
> gas price: 20 gwei
> value sent: 0 ETH
> total cost: 0.05743854 ETH
> Saving migration to chain.
> Saving artifacts
-------------------------------------
> Total cost: 0.05743854 ETH

Open the console using truffle Console, we will mint our first apple from one of the address provided by the ganache-CLI.

truffle(development)> apple = await Apple.deployed()
undefined
truffle(development)> apple.address
'0xbCe33371462f9AE3dAD88a5Ad1b166ab368389fd'
truffle(development)> apple._mint("address1")
{
tx: '0x8dba666412f1f4ff24096db75bc7130b23b9eccfd7f3e0d5e2a54e799d2060c6',
receipt: {
transactionHash: '0x8dba666412f1f4ff24096db75bc7130b23b9eccfd7f3e0d5e2a54e799d2060c6',
transactionIndex: 0,
blockHash: '0xc51624a6ac26c1456f822c424dd598abd3f9f0482fe3b7ee862180508ed6c9c0',
blockNumber: 7,
from: '0x5a4d0773d1df9b0f99627406536d6082955c79b8',
to: '0xbce33371462f9ae3dad88a5ad1b166ab368389fd',
gasUsed: 154886,
cumulativeGasUsed: 154886,
contractAddress: null,
logs: [ [Object] ],
status: true,
logsBloom: '0x00000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000008000000000000040000000000000000000000000008000000000000000000040000000000000000000000000000020000000000000000000800000000000000000000000010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010100000000000000000000000000000001002000000000000000000000000000000000000001000000000000060000000000000000000000000000000000000000000000000000000000000000000',
rawLogs: [ [Object] ]
},
logs: [
{
logIndex: 0,
transactionIndex: 0,
transactionHash: '0x8dba666412f1f4ff24096db75bc7130b23b9eccfd7f3e0d5e2a54e799d2060c6',
blockHash: '0xc51624a6ac26c1456f822c424dd598abd3f9f0482fe3b7ee862180508ed6c9c0',
blockNumber: 7,
address: '0xbCe33371462f9AE3dAD88a5Ad1b166ab368389fd',
type: 'mined',
id: 'log_b11893a3',
event: 'Transfer',
args: [Result]
}
]
}

Now we can transfer out an apple from one address to another and trace the owner of the apple using the ownerOf() function provided by the Openzeppelin-solidity.

truffle(development)> apple.transferFrom(address1,address2,1,{from:address1})
{
tx: '0x813252278b90be7e19b48800711dd2afcb00565caa89567f673e716915ab6544',
receipt: {
transactionHash: '0x813252278b90be7e19b48800711dd2afcb00565caa89567f673e716915ab6544',
transactionIndex: 0,
blockHash: '0xe61365144fe9688e6087e6c131d48a124e98d534fd4e2bf375a29825234ab85d',
blockNumber: 8,
from: '0x5a4d0773d1df9b0f99627406536d6082955c79b8',
to: '0xbce33371462f9ae3dad88a5ad1b166ab368389fd',
gasUsed: 73528,
cumulativeGasUsed: 73528,
contractAddress: null,
logs: [ [Object] ],
status: true,
logsBloom: '0x00000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000004000000008000000000000040000000000000000000000400008000000000000000000040000000000000000000000000000000000000000000000000000000000000000000000000010000000000000000000000040000000000000000000000000000000000000000000000000000000000000000000000000000000000010100000000000000000000000000000001002000000000000000000000000000000000000001000000000000040000000000000000000000000000000000000000000000000000000000000000000',
rawLogs: [ [Object] ]
},
logs: [
{
logIndex: 0,
transactionIndex: 0,
transactionHash: '0x813252278b90be7e19b48800711dd2afcb00565caa89567f673e716915ab6544',
blockHash: '0xe61365144fe9688e6087e6c131d48a124e98d534fd4e2bf375a29825234ab85d',
blockNumber: 8,
address: '0xbCe33371462f9AE3dAD88a5Ad1b166ab368389fd',
type: 'mined',
id: 'log_09751516',
event: 'Transfer',
args: [Result]
}
]
}
truffle(development)>
truffle(development)> apple.ownerOf(1)

Conclusion

As you can see, blockchain can help in making transparent, the complexity of supply chain. In the next blog, we shall see how we can store metadata about the apple on IPFS.

--

--

Kushagra Jindal
Integraate

Blockchain | web3 | DeFi | NFT | zkSNARKs | circom