# Using Truffle

## Using Truffle

### What is Truffle ?

Truffle is a development framework and suite of tools primarily used for building, deploying, and managing smart contracts and decentralized applications (DApps) on blockchain platforms. It provides developers with a set of utilities and commands to streamline the development process and make it easier to work with blockchain technologies.

### Configuring the Development Workspace

Before we dive in, ensure you have the following essentials installed: Requirements:

* Windows, Linux or Mac OS X
* [Node.js v8.9.4 LTS or later](https://nodejs.org/en/)

### Installing Truffle

After we've set up those tools, we just need a single command to get Truffle:

```
npm install -g truffle
```

Let's ensure Truffle is correctly installed. Open your terminal and type `truffle version`. If some error shows up, make sure your computer knows where to find the npm modules.

### Create a Project <a href="#create-a-project" id="create-a-project"></a>

Our first task is to set up a Truffle project. We'll use `TokenDeelance.sol` as our guide, demonstrating how to create a token that can be shared between accounts:

* Create a new directory for your Truffle project

```
mkdir PepeDeelance
cd PepeDeelance
```

* Initialize your project:

```
truffle init
```

After completing this step, you'll have a project organized with these folders:

* 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 <a href="#create-contract" id="create-contract"></a>

* Create a `PepeDeelance.sol` file within the `contracts/:` directory.
* Copy the below code and place it into the `PepeDeelance.sol` file:

{% code title="PepeDeelance.sol" %}

```solidity
// SPDX-License-Identifier: MIT
pragma solidity >=0.4.22 <0.9.0;

import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
import "@openzeppelin/contracts/access/Ownable.sol";

contract PepeDeelance is ERC20, Ownable {
    constructor() ERC20("PepeDeelance", "PEPEDLANCE") {
        _mint(msg.sender, 1000000000 * 10 ** decimals());
    }

    function mint(address to, uint256 amount) public onlyOwner {
        _mint(to, amount);
    }
}
```

{% endcode %}

{% hint style="info" %}
Ensure you have the [`@openzeppelin/contracts`](https://www.npmjs.com/package/@openzeppelin/contracts) package since our Solidity code relies on it. To get it, execute: `npm i @openzeppelin/contracts` in terminal.
{% endhint %}

&#x20;

* Create a `1_deploy_contracts.js` file within the `migrations/:` directory and insert the following code:

{% code title="1\_deploy\_contracts.js" %}

```javascript
const PepeDeelance = artifacts.require("PepeDeelance");

module.exports = function (deployer) {
  deployer.deploy(PepeDeelance);
};

```

{% endcode %}

* Next, paste the following code into your `truffle-config.js` file:

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

```javascript
const HDWalletProvider = require("@truffle/hdwallet-provider");

module.exports = {
  networks: {
    development: {
      host: "127.0.0.1",
      port: 9545,
      network_id: "*",
    },
    testnet: {
      provider: () =>
        new HDWalletProvider(
          "<YOUR_WALLET_PRIVATE_KEY>",
          `https://testnet-rpc.deelance.com`
        ),
      network_id: 455214,
      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"
    },
  },
};

```

{% endcode %}

{% hint style="info" %}
Ensure you replace `<YOUR_WALLET_PRIVATE_KEY>` with the actual private key of your wallet.
{% endhint %}

{% hint style="info" %}
Ensure you've set up [@truffle/hdwallet-provider](https://www.npmjs.com/package/@truffle/hdwallet-provider), as mentioned in the file. To get this package, type `npm i @truffle/hdwallet-provider` in your terminal.
{% endhint %}

* #### Compile Contract <a href="#compile-contract" id="compile-contract"></a>

To compile your Truffle project, execute the following command:

```
truffle compile
```

### Deploying DRC20 Contract on Deelance Network <a href="#compile-contract" id="compile-contract"></a>

Execute the given command at the base of the project folder:

```
truffle migrate --network testnet
```

The contract will deploy on the Deelance Testnet and will appear in your terminal as follows:

```bash
1_deploy_contracts.js
=====================

   Deploying 'PepeDeelance'
   ------------------------
   > transaction hash:    0x42a4fe45d66cdc7648d9a8607f4f8c0044946074c1bf07963cf501bf14a8f493
   > Blocks: 2            Seconds: 8
   > contract address:    0xE6D82514Fe6A796D947b5580Fc26090Dd818412e
   > block number:        37296
   > block timestamp:     1692550864
   > account:             0x2bd250B5e8eEeB4e0E9fC6023f525Ac62b72c0ae
   > balance:             9.984142327455598517
   > gas used:            1491671 (0x16c2d7)
   > gas price:           2.500000007 gwei
   > value sent:          0 ETH
   > total cost:          0.003729177510441697 ETH

   Pausing for 10 confirmations...

   --------------------------------
   > confirmation number: 1 (block: 37297)
   > confirmation number: 2 (block: 37298)
   > confirmation number: 3 (block: 37299)
   > confirmation number: 4 (block: 37300)
   > confirmation number: 5 (block: 37301)
   > confirmation number: 6 (block: 37302)
   > confirmation number: 7 (block: 37303)
   > confirmation number: 8 (block: 37304)
   > confirmation number: 9 (block: 37305)
   > confirmation number: 10 (block: 37306)
   > Saving artifacts
   -------------------------------------
   > Total cost:     0.003729177510441697 ETH

Summary
=======
> Total deployments:   1
> Final cost:          0.003729177510441697 ETH
```

> Keep in mind that your address, transaction\_hash, and other details will vary. The information above is simply to give you a sense of the format.

Well done! You've successfully launched the DRC20 Smart Contract on Deelance Network. You can now engage with the Smart Contract.
