Truffle

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

1. Install Truffle

Install command

$ npm install truffle -g

2. Create Project Directory

First, choose directory to place source code.

$ mkdir meverse-test
$ cd meverse-test

3. Initialize Truffle

To deploy contract, initialize Truffle.

$ truffle init

4. Write Simple Smart Contract in Solidity

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

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

Write the following code in MEVerseGreeter.sol

// 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.

$ 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

// 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

$ 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

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

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

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

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

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

Last updated