Sample Contracts
This chapter describes creating and deploying smart contracts using the Truffle development environment.
Install Truffle development environment
Truffle is an environment where smart contracts can be developed and tested on blockchains that use Ethereum Virtual Machine (EVM) like Metadium.
Requirements
To use Truffle, Node.js v14 - v18 must be installed in Windows, Linux, or macOS environments.
Install Truffle
Install Truffle using NPM in your terminal.
$ npm install -g truffle
To check the installation of Truffle, run the following command:
$ truffle version
Truffle v5.11.5 (core: 5.11.5)
Ganache v7.9.1
Solidity - 0.8.21 (solc-js)
Node v18.19.0
Web3.js v1.10.0
Create project
Create a new directory for your new Truffle project. We will create a smart contract that implements key/value storage in the example. To do this, we create a directory called KVStore.
$ mkdir KVStore
$ cd KVStore
Initialize the Truffle project.
$ truffle init
Starting init...
================
> Copying project files to /Users/trident/Projects/DApp/KVstore/Temp
Init successful, sweet!
Try our scaffold commands to get started:
$ truffle create contract YourContractName # scaffold a contract
$ truffle create test YourTestName # scaffold a test
http://trufflesuite.com/docs
The following directories and files are created.
$ tree
.
├── contracts
├── migrations
├── test
└── truffle-config.js
4 directories, 1 file
Create a KVStore contract with the following command.
$ truffle create contract KVStore
The KVStore.sol file is created as shown below.
// SPDX-License-Identifier: MIT
pragma solidity >=0.4.22 <0.9.0;
contract KVStore {
constructor() public {
}
}
Writing a smart contract
Create a set function that stores Key/Value and a get function that uses Key to read the stored values as follows.
// SPDX-License-Identifier: MIT
pragma solidity >=0.4.22 <0.9.0;
contract KVStore {
mapping(string => string) store;
function get(string memory key) public view returns(string memory) {
return store[key];
}
function set(string memory key, string memory value) public {
store[key] = value;
}
}
Setup
To compile and deploy, modify the truffle-config.js file.
First, wallet configuration is required. To avoid exposing the Private Key in the code, use dotenv and store the PRIVATE_KEY in the .env file as follows. If you use dotenv, install it using the npm i dotenv command to avoid compilation errors.
PRIVATE_KEY=["YOUR_PRIVATE_KEY"]
Second, proceed with network configuration. To use the Metadium Testnet, add the network configuration as follows.
metadium_testnet: {
provider: ()=> new HDWalletProvider(PRIVATE_KEY, "https://api.metadium.com/dev"),
network_id: 12,
gasPrice: 80000000000
}
Finally, proceed with compiler configuration. In the example, version 0.8.21 is used, and the optimizer is configured.
/**
...
*/
require('dotenv').config();
// const { MNEMONIC, PROJECT_ID } = process.env;
const { PRIVATE_KEY } = process.env;
const HDWalletProvider = require('@truffle/hdwallet-provider');
module.exports = {
...
networks: {
...
metadium_testnet: {
provider: ()=> new HDWalletProvider(PRIVATE_KEY, "https://api.metadium.com/dev"),
network_id: 12,
gasPrice: 80000000000
}
},
...
compilers: {
solc: {
version: "0.8.21", // Fetch exact version from solc-bin (default: truffle's version)
// docker: true, // Use "0.5.1" you've installed locally with docker (default: false)
settings: { // See the solidity docs for advice about optimization and evmVersion
optimizer: {
enabled: true,
runs: 200
},
evmVersion: "byzantium"
}
}
},
Smart contract compile
truffle compile
Compiling your contracts...
===========================
> Compiling ./contracts/KVStore.sol
> Artifacts written to /Users/trident/Projects/DApp/KVstore/Temp/build/contracts
> Compiled successfully using:
- solc: 0.8.21+commit.d9974bed.Emscripten.clang
Smart contract deployment
Before deploying smart contracts, create a script file named 1_deploy_contracts.js in the migrations directory as follows:
ar KVStore = artifacts.require("KVStore");
module.exports = function(deployer) {
deployer.deploy(KVStore);
};
Deploy the contract with the truffle migrate command.
truffle migrate --network metadium_testnet
Starting migrations...
======================
> Network name: 'metadium_testnet'
> Network id: 12
> Block gas limit: 105000000 (0x6422c40)
1_deploy_contracts.js
=====================
Deploying 'KVStore'
-------------------
> transaction hash: 0x1039d819b825a80281d27ae737858579752192b642e0678902570c64791739b0
> Blocks: 2 Seconds: 4
> contract address: 0x4981bEF8190D4CC385e4b0DcE67Faf24347dbB87
> block number: 50419826
> block timestamp: 1707187839
> account: 0xcf3C2104DF83d92C845d71Be6354266b222295aD
> balance: 10.443230699997734106
> gas used: 322126 (0x4ea4e)
> gas price: 80 gwei
> value sent: 0 ETH
> total cost: 0.02577008 ETH
> Saving artifacts
-------------------------------------
> Total cost: 0.02577008 ETH
Summary
=======
> Total deployments: 1
> Final cost: 0.02577008 ETH
Last updated