# 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+](https://nodejs.org/en/) installed before we get started. And you can choose a preferred package management tool to begin with:

{% tabs %}
{% tab title="npm" %}

```bash
# 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
```

{% endtab %}

{% tab title="Yarn" %}

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

# Install truffle and wallet-provider to your project
$ yarn add --dev truffle @truffle/hdwallet-provider

# Generate truffle configuation file and default folders
$ yarn truffle init
```

{% endtab %}
{% endtabs %}

{% hint style="info" %}
You can install global Truffle to your environment:

```bash
$ npm i -g truffle
```

Then, you can ignore `npm exec --` or `yarn` commands before `truffle`
{% endhint %}

#### Setup Truffle config file

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

{% code title="truffle-config.js" %}

```git
- // const HDWalletProvider = require('@truffle/hdwallet-provider');
+ const HDWalletProvider = require('@truffle/hdwallet-provider');
```

{% endcode %}

Then, we add ThunderCore network settings into your project.

{% code title="truffle-config.js" %}

```git
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
  }
}
```

{% endcode %}

{% hint style="danger" %}
Never store your private key directly in `truffle-config.js`
{% endhint %}

#### Deploy a Contract

{% hint style="info" %}
You'll need gas tokens to deploy contracts. To test on ThunderCore testnet, you can get testnet tokens from [ThunderCore testnet Faucet](https://faucet-testnet.thundercore.com)
{% endhint %}

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

{% tabs %}
{% tab title="Unix-like system" %}

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

{% endtab %}

{% tab title="Windows CMD" %}

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

{% endtab %}

{% tab title="Windows Powershell" %}

```shell
# Using NPM
$env:KEY='0xPrivateKey';npm exec -- truffle migrate --network thunder-testnet --reset
# or using Yarn
$env:KEY='0xPrivateKey';yarn truffle migrate --network thunder-testnet --reset
```

{% endtab %}
{% endtabs %}

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:

* [**ThunderCore mainnet:**](https://viewblock.io/thundercore)
* [**ThunderCore testnet:**](https://explorer-testnet.thundercore.com/)
