# Truffle

Quickly build, test, compile and debug using Truffle. Write scripts and plugins to automate common processes.

* Truffle Storage - <https://github.com/trufflesuite/truffle>
* Truffle Docs - <https://trufflesuite.com/docs/>

## 1. Install Truffle

Install command

```shell
$ npm install truffle -g
```

## 2. Create Project Directory <a href="#deploying-a-smart-contract-using-truffle" id="deploying-a-smart-contract-using-truffle"></a>

First, choose directory to place source code.

```shell
$ mkdir meverse-test
$ cd meverse-test
```

## 3. Initialize Truffle

To deploy contract, initialize Truffle.

```shell
$ truffle init
```

## 4. Write Simple Smart Contract in Solidity&#x20;

Create `MEVerseGreeter.sol` in `meverse-test/contracts` directory.

```shell
$ cd contracts
$ touch MEVerseGreeter.sol
$ vi MEVerseGreeter.sol
```

Write the following code in `MEVerseGreeter.sol`

```solidity
// SPDX-License-Identifier: MIT
pragma solidity ^0.5.6;
contract Mortal {
    /* Define Variable owner of the type address */
    address payable owner;
    /* This function is executed at initialization and sets the owner of the contract */
    constructor () public { owner = msg.sender; }
    /* Function to recover the funds on the contract */
    function kill() public payable { if (msg.sender == owner) selfdestruct(owner); }
}

contract MEVerseGreeter is Mortal {
    /* Define variable greeting of the type string */
    string greeting;
    /* This runs when the contract is executed */
    constructor (string memory _greeting) public {
        greeting = _greeting;
    }
    /* Main function */
    function greet() public view returns (string memory) {
        return greeting;
    }
}
```

## 5. Modify Migration Script

Deploy contract with hdwallet-provider. Install the library first.

```shell
$ npm install @truffle/hdwallet-provider
```

* *you can also deploy contracts written via Truffle's **Dashboard**, please refer to the Dashboard for more details.*

> **Use Truffle Dashboard** : <https://trufflesuite.com/docs/truffle/how-to/use-the-truffle-dashboard/>

Modify **MEVerse Testnet network** information at truffle-config.js

```javascript
// truffle-config.js
const HDWalletProvider = require("@truffle/hdwallet-provider");
...
networks: {
    MEVerseTestnet: {
      provider: () =>
        new HDWalletProvider({
          privateKeys: [
            "adf...583a9", // 컨트랙트를 배포할 계정 private key
          ],
          providerOrUrl: "https://rpc.meversetestnet.io",
        }),
      network_id: 4759, // MEVerse testnet chain id
    }
}
```

## 6. Deploy Smart Contract with Truffle <a href="#deploying-a-smart-contract-using-truffle" id="deploying-a-smart-contract-using-truffle"></a>

```
$ truffle migrate --network MEVerseTestnet
Compiling your contracts...
===========================
> Compiling ./contracts/MEVerseGreeter.sol
> Artifacts written to /truffle_sample/build/contracts
> Compiled successfully using:
   - solc: 0.5.16+commit.9c3226ce.Emscripten.clang



Starting migrations...
======================
> Network name:    'MEVerseTestnet'
> Network id:      4759
> Block gas limit: 30000000 (0x1c9c380)


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

   Deploying 'MEVerseGreeter'
   --------------------------
   > transaction hash:    0xe1f6fad3f5121cb1448750113f25e20228c4b9e800d0a7cda8ca04ff464257f2
   > Blocks: 0            Seconds: 0
   > contract address:    0xeb7A1483E3408Af19231262643672719CcFc6E17
   > block number:        27778152
   > block timestamp:     1668760128889501
   > account:             0x19368fCd2F0EE8d2C788b754F6504FD49de449E7
   > balance:             191907.1
   > gas used:            223381 (0x36895)
   > gas price:           1000 gwei
   > value sent:          0 ETH
   > total cost:          0.223381 ETH


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


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

## 7. Check Deployment

You can check the deployment by running deployed contract method.

you can check the deployed contract's network with truffle networks command

```shell
$ truffle networks
Network: MEVerseTestnet (id: 4759)
  MEVerseGreeter: 0xeb7A1483E3408Af19231262643672719CcFc6E17
  Migrations: 0xD542f454EdEb2CebeDF68a2B6176951c19dff9ed
```

To connect the deployed contract with network, please use the following command

```shell
$ truffle console --network MEVerseTestnet  
truffle(MEVerseTestnet)> 
```

create instance and check if it runs well by calling method in instance

```bash
truffle(development)> let mev = await MEVerseGreeter.deployed()
undefined
truffle(MEVerseTestnet)> mev.greet()
'Hello, MEVerse'
truffle(MEVerseTestnet)> 
```

Finally, check the Contract Address in Console and search tx hash in [MEVerse Testnet Scan](https://testnet.meversescan.io/)

<figure><img src="/files/bijopjcjampnztJMpYW0" alt=""><figcaption><p>Testnet Scan - view transaction with tx hash</p></figcaption></figure>


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://meversedex.gitbook.io/meverse-dev-docs/smart-contract/ide-and-tools/truffle.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
