Using Truffle

Truffle is a development environment, testing framework and asset pipeline for Ethereum, aiming to make life as an Ethereum developer easier.

Adding Truffle to your project

You'll need Node.js v8+ installed before we get started. And you can choose a preferred package management tool to begin with:

# To use truffle locally in your project, you must init a package.json file
$ npm init

# Install truffle and wallet-provider to your project
$ npm i -D truffle @truffle/hdwallet-provider

# Generate truffle configuation file and default folders
$ npm exec -- truffle init

You can install global Truffle to your environment:

$ npm i -g truffle

Then, you can ignore npm exec -- or yarn commands before truffle

Setup Truffle config file

We are going to edit truffle-config.js. First, we uncomment this line to import @truffle/hdwallet-provider:

truffle-config.js
- // const HDWalletProvider = require('@truffle/hdwallet-provider');
+ const HDWalletProvider = require('@truffle/hdwallet-provider');

Then, we add ThunderCore network settings into your project.

truffle-config.js
networks: {
  "thunder-mainnet": {
    provider: () => new HDWalletProvider([process.env.KEY], "https://mainnet-rpc.thundercore.com"),
    network_id: "108",
    gas: 90000000,
    gasPrice: 15e9
  },
  "thunder-testnet": {
    provider: () => new HDWalletProvider([process.env.KEY], "https://testnet-rpc.thundercore.com"),
    network_id: "18",
    gas: 90000000,
    gasPrice: 15e9
  }
}

Never store your private key directly in truffle-config.js

Deploy a Contract

You'll need gas tokens to deploy contracts. To test on ThunderCore testnet, you can get testnet tokens from ThunderCore testnet Faucet

Now, we can deploy the Migrator contract in the template to ThunderCore testnet.

# Using NPM
KEY=0xPrivateKey npm exec -- truffle migrate --network thunder-testnet --reset
# or using Yarn
KEY=0xPrivateKey yarn truffle migrate --network thunder-testnet --reset

When it's done, should show messages like this:

Starting migrations...
======================
> Network name:    'thunder-testnet'
> Network id:      18
> Block gas limit: 100000000 (0x5f5e100)


1_initial_migration.js
======================

   Deploying 'Migrations'
   ----------------------

   ...
   ...
   ...

   > Saving migration to chain.
   > Saving artifacts
   -------------------------------------
   > Total cost:         0.000266154 ETH


Summary
=======
> Total deployments:   1
> Final cost:          0.000266154 ETH

Congratulations! You have successfully deployed a smart contract onto the ThunderCore network.

You can check the deployment status at:

Last updated