> For the complete documentation index, see [llms.txt](https://meversedex.gitbook.io/meverse-dev-docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://meversedex.gitbook.io/meverse-dev-docs/get-started/deploy-smart-contract.md).

# Deploy Smart Contract

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

First, create a directory to store the source code

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

## Initialize Truffle

To deploy contract, initialize Truffle

```shell
$ truffle init
```

## Write simple solidity contract

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

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

Write following code in `MEVerseGreeter.sol`

```javascript
// 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;
    }
}
```

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

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