> For the complete documentation index, see [llms.txt](https://docs.xenea.io/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.xenea.io/testnet-archive/how-to-deploy-to-smart-contracts/truffle.md). Agents can also search and read this documentation through its [MCP server](https://docs.xenea.io/~gitbook/mcp).

# Truffle

## Truffle <a href="#what-is-truffle" id="what-is-truffle"></a>

A world class development environment, testing framework and asset pipeline for blockchains using the Ethereum Virtual Machine (EVM), aiming to make life as a developer easier.&#x20;

This page will take you through the basics of creating a Truffle project and deploying a smart contract to a blockchain.

<mark style="background-color:yellow;">**Note:**</mark> <mark style="background-color:yellow;"></mark><mark style="background-color:yellow;">This page is just a quick start based on a sample program; please refer to</mark> [<mark style="background-color:yellow;">Truffle's documentation</mark>](https://archive.trufflesuite.com/docs/truffle/quickstart/) <mark style="background-color:yellow;">for more information on Truffle installation.</mark>

## Install Truffle

### Requirements <a href="#requirements" id="requirements"></a>

* [Node.js v14 - v18](https://archive.trufflesuite.com/docs/truffle/how-to/install/#install-nodejs)
* Windows, Linux, or macOS

### Install Truffle

<mark style="background-color:yellow;">**Warning**</mark><mark style="background-color:yellow;">: Avoid using the</mark> <mark style="background-color:yellow;"></mark><mark style="background-color:yellow;">`sudo`</mark> <mark style="background-color:yellow;"></mark><mark style="background-color:yellow;">command when installing Truffle, this can cause permission errors.</mark>

In a terminal, use NPM to install Truffle:

```bash
npm install -g truffle
```

You may receive a list of warnings during installation. To confirm that Truffle was installed correctly, run:

```bash
truffle version
```

## Creating a Project

After ensuring Truffle is installed successfully, create your own project and name it something like "truffleApp".

### Create a new directory for your Truffle project

```bash
mkdir truffleApp
cd truffleApp
```

### Initialize your project

```bash
truffle init
```

Once this operation is complete, you’ll now have a project structure with the following items:

1. contracts/: Directory for Solidity contracts
2. migrations/: Directory for scriptable deployment files
3. test/: Directory for test files for testing your application and contracts
4. truffle-config.js: Truffle configuration file
5. build (visible after compile): Compiled Solidity contracts with bytecode and ABI

### Create Contract

Make sure you install HDWalletProvider which we will use later:

```bash
npm install @truffle/hdwallet-provider --save
```

we create a file for smart contract called HelloKura.sol inside the contracts directory:

```solidity
pragma solidity ^0.8.0;

contract HelloKura {
   string public greet = "Hello Kura!";
}
```

### Coding Migrations

To deploy our HelloKura contract on Kura Testnet, we have to create a migration to get the contract on the network. Create a file in the migrations folder named “1\_deploy\_contract.js”.

```javascript
var HelloKura = artifacts.require("HelloKura");

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

### Configuring Truffle For Kura Testnet

1. Go to truffle-config.js (located in root directory)
2. Update with Kura Testnet details

```javascript
require('dotenv').config();
const mnemonic = process.env.MNEMONIC;
const HDWalletProvider = require('@truffle/hdwallet-provider');

module.exports = {
  networks: {
    kura: {
      provider: () => new HDWalletProvider(mnemonic, `https://rpc-kura.cross.technology/`),
      network_id: 5555,
      confirmations: 10,
      timeoutBlocks: 200,
      skipDryRun: true,
      from: "0x(your kura testnet account address)"
    }
  },
  compilers: {
    solc: {
      version: "0.8.0"
    }
  }
};
```

Be aware that we need to declare mnemonic which is used by **HDWalletProvider** in the **truffle-config.js** file to verify the account supplying funds during contract deployment. To set mnemonic variable, you would set it as an environment variable in **.env** file in the root directory.

```bash
npm i dotenv
```

```
MNEMONIC="(Your secret recovery phase)"
```

We can find our secret recovery phase for our account in the Metamask by going through **Settings**, **Security & Privacy**, and then **Reveal Secret Recovery Phrase**.

## Deployment <a href="#step-4-deployment" id="step-4-deployment"></a>

Finally, we have everything ready and we can compile the smart contract we made:

```bash
truffle compile
```

```bash
truffle migrate --network kura
```

We would see something like below to confirm our smart contract is deployed on Kura testnet.<br>

<figure><img src="https://2276277239-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FV0mW3n7q1sGjuyyHSxIb%2Fuploads%2FLERLtVn9iYd17NgmBkuo%2F%E3%82%B9%E3%82%AF%E3%83%AA%E3%83%BC%E3%83%B3%E3%82%B7%E3%83%A7%E3%83%83%E3%83%88%202024-05-02%2022.43.43.png?alt=media&amp;token=0ccb0897-a47b-4322-8fca-971cec7a6964" alt=""><figcaption></figcaption></figure>

We can confirm this also by looking at the explorer [Subscan](https://testnet.crossvaluescan.com/).

## Reference[​](https://docs.astar.network/ja/docs/build/builder-guides/astar_features/truffle/#reference) <a href="#reference" id="reference"></a>

* [Official Document for Truffle](https://trufflesuite.com/docs/)
