GodeChain Docs
Search
K
Comment on page

Using Truffle for Deploying Smart Contracts on GODE

In this tutorial, you will learn to create, compile nad deploy smart contract on the GODE Smart Chain network using the Truffle IDE.

Setting up the development environment

There are a few technical requirements before we start. Please install the following:
Recommendations for Windows If you're running Truffle on Windows, you may encounter some naming conflicts that could prevent Truffle from executing properly. Please refer to the official Truffle Documentation to find solutions for resolving naming conflicts.

Installing Truffle

Once the above mentioned softwares are installed, we only need one command to install Truffle:
npm install -g truffle
To verify that Truffle is installed properly, type truffle version on a terminal. If you see an error, make sure that your npm modules are added to your path.
If you're new to Truffle then please follow the Getting Started by truffle, To setup the truffle environment.

Project Creation, Compilation, and Configuration

To use most Truffle commands, you need to run them against an existing Truffle project. So the first step is to create a Truffle project.

Creation

For creating a scaffold project for getting started, you can use the Truffle Boxes, GODE Starter Box for an eample template to start devloping on GODE Smart Chain.
For this tutorial, we have used the MetaCoin box as an example, which creates a token that can be transferred between accounts. Use the command truffle unbox metacoin to unbox the metacoin box.
NOTE: You can use the truffle unbox <box-name> command to download any of the other Truffle Boxes. Another alternative is to create a bare Truffle project with no smart contracts included using the truffle init command.
Once this operation is completed, you'll now have a project structure with the following items:
  • contracts/: Directory for Solidity contracts
  • migrations/: Directory for scriptable deployment files
  • test/: Directory for test files for testing your application and contracts
  • truffle-config.js: Truffle configuration file

Create Contract

You can write your own smart contract or download the GRC20 token smart contract template and place it in the contracts directory.

Compile Contract

To compile a Truffle project, change to the root of the directory where the project is located and then type the following into a terminal:
truffle compile

Configuring Truffle for GODE

  • Go to truffle-config.js
  • Update the truffle-config with gode-network-crendentials.
const HDWalletProvider = require('@truffle/hdwallet-provider');
const fs = require('fs');
const mnemonic = fs.readFileSync(".secret").toString().trim();
module.exports = {
networks: {
development: {
host: "127.0.0.1", // Localhost (default: none)
port: 8545, // Standard GODE port (default: none)
network_id: "*", // Any network (default: none)
},
testnet: {
provider: () => new HDWalletProvider(mnemonic, `https://rpctest.godechain.com`),
network_id: 5566,
confirmations: 10,
timeoutBlocks: 200,
skipDryRun: true
},
gode: {
provider: () => new HDWalletProvider(mnemonic, `https://rpc.godechain.com`),
network_id: 5500,
confirmations: 10,
timeoutBlocks: 200,
skipDryRun: true
},
},
// Set default mocha options here, use special reporters etc.
mocha: {
// timeout: 100000
},
// Configure your compilers
compilers: {
solc: {
version: "^0.8.0", // A version or constraint - Ex. "^0.5.0"
}
}
}
NOTE: Notice, it requires mnemonic to be passed in for Provider, this is the seed phrase for the account you'd like to deploy from. Create a new .secret file in root directory and enter your 12 word mnemonic seed phrase to get started. To get the seedwords from metamask wallet you can go to Metamask Settings, then from the menu choose Security and Privacy where you will see a button that says reveal seed words, refer here for more details.

Deploying on Gode Network

Run this command in root of the project directory:
$ truffle migrate --network testnet
Contract will be deployed on Binance Chain Chapel Testnet, it look like this:
1_initial_migration.js
======================
Deploying 'Migrations'
----------------------
> transaction hash: 0xaf4502198400bde2148eb4274b08d727a17080b685cd2dcd4aee13d8eb954adc
> Blocks: 3 Seconds: 9
> contract address: 0x81eCD10b61978D9160428943a0c0Fb31a5460466
> block number: 3223948
> block timestamp: 1604049862
> account: 0x623ac9f6E62A8134bBD5Dc96D9B8b29b4B60e45F
> balance: 6.24574114
> gas used: 191943 (0x2edc7)
> gas price: 20 gwei
> value sent: 0 GODE
> total cost: 0.00383886 GODE
Pausing for 5 confirmations...
------------------------------
> confirmation number: 2 (block: 3223952)
> confirmation number: 3 (block: 3223953)
> confirmation number: 4 (block: 3223954)
> confirmation number: 6 (block: 3223956)
> Saving migration to chain.
> Saving artifacts
-------------------------------------
> Total cost: 0.00383886 GODE
Summary
=======
> Total deployments: 1
> Final cost: 0.00383886 GODE
Note Remember your address, transaction_hash and other details provided would differ, Above is just to provide an idea of structure.
🎉 Congratulations! You have successfully deployed GRC20 Smart Contract. Now you can interact with the Smart Contract.
You can check the deployment status here: https://explorer.godechain.com/ or https://explorertest.godechain.com/

Verify Your Contract on GodeScan

The recommended way to verify a smart contract is using the plugin. It is easier to read and imports and licenses are maintained.
Verified using Truffle

GodeScan plugin for Truffle

Truffle has a GodeScan plugin: truffle-plugin-verify
You need to deploy with Truffle to verify with the Truffle verify plugin.
Get API key: https://explorer.godechain.com/myapikey

Install the plugin

npm install -D truffle-plugin-verify

Configure the plugin

Configure the plugin in truffle-config.js using the following command
const HDWalletProvider = require("@truffle/hdwallet-provider");
// const infuraKey = "fj4jll3k.....";
//
const { mnemonic, GODESCANAPIKEY} = require('./env.json');
module.exports = {
plugins: [
'truffle-plugin-verify'
],
api_keys: {
godescan: GODESCANAPIKEY
},
networks: {
testnet: {
provider: () => new HDWalletProvider(mnemonic, `https://rpctest.godechain.com`),
network_id: 5566,
timeoutBlocks: 200,
confirmations: 5,
production: true // Treats this network as if it was a public net. (default: false)
}
},
// Set default mocha options here, use special reporters etc.
mocha: {
timeout: 100000
},
// Configure your compilers
compilers: {
solc: {
version: "0.5.16", // 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: false,
runs: 200
},
evmVersion: "byzantium"
}
},
},
};

Verification Command

truffle run verify GRC20Token@{contract-address} --network testnet
You should see the following output:
Verifying GRC20Token@0x68D2E27940CA48109Fa3DaD0D2C8B27E64a0c6cf
Pass - Verified: https://explorertest.gochain.com/address/0x68D2E27940CA48109Fa3DaD0D2C8B27E64a0c6cf#contracts
Successfully verified 1 contract(s).