# 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="/files/I3k6mW598UW8Y7tjvUNt" 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/)


---

# 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://docs.xenea.io/testnet-archive/how-to-deploy-to-smart-contracts/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.
