Skip to main content

Using Truffle

This page outlines a methodology for deploying and testing contracts in Neon EVM using the Truffle tool. This technique can be useful for personnel involved in the development and maintenance of Neon EVM.

It should be noted that Ethereum contracts can also be successfully deployed in Neon EVM using Remix in manual mode. However, since Remix does not have as many capabilities, it turned out to be not as convenient for software development, and therefore we wanted to provide developers with a more advanced methodology using Truffle.

The Goal

Our main goal with Truffle is to make it easier for developers to deploy and debug contracts in Neon EVM.

With Truffle, you get:

  • A simple setting of configuration parameters.
  • An easy process of deploying and debugging contracts in the network.
  • Automated deployment and running of tests.

Installation

Requirements for your device

The following software must be installed on your device:

  • NodeJS v8.9.4 or later
  • Web3 v1.2.0 or later

Note: Although this tutorial uses the Ubuntu platform, the instructions provided can be applied to other Linux platforms.

Installing Truffle

If Truffle is already installed on your device, you can skip this section and move on to the next one. For those just getting started, you need to go through this section.

Note: This page is just a quickstart. To go into more detail, you can read the Truffle documentation.

Create a new directory for your Truffle project:

$ mkdir <project name>
$ cd <project name>

Install Truffle:

$ npm install truffle

Initialize the project directory by running the following command:

$ truffle init

Once this operation is completed, you will have a project structure with the following items:

  • contracts/ — Directory for Solidity contracts
  • migrations/ — Directory for scriptable deployment files
  • test/ — Directory for test files for testing your contracts
  • truffle-config.js — Truffle configuration file

You can run truffle compile, truffle migrate and truffle test to compile your contracts, deploy them to the network, and run their associated unit tests.

Installation of the HDWalletProvider library

HD Wallet-enabled Web3 provider (HDWalletProvider) is a standalone library. One of its functions is signing transactions with private keys. Since the Neon EVM proxy does not store private keys, it cannot sign transactions. Therefore, during debugging contracts, the HDWalletProvider library is used to sign transactions for addresses derived from a 12 or 24 word mnemonic.

By default, the Truffle installation does not provide the HDWalletProvider library. If during the installation of Truffle none of the applications required the HDWalletProvider library to be installed, you need to install it separately.

Install the HDWalletProvider library:

$ npm install @truffle/hdwallet-provider

Note: To go into much detail here, you can see the official documentation.

Connecting Truffle to a Proxy

To connect Truffle to a proxy on node.js, the eth_accounts method from the Ethereum JSON RPC API set is required. This method allows serving a list of 20 byte addresses owned by a client. Since the Neon EVM proxy does not support the eth_accounts method required to connect Truffle, the HDWalletProvider library is used to function as this method. The connection is configured in truffle-config.js.

The configuration file is publicly available, and therefore the word mnemonic and private key contained in the file are also publicly available. This makes it possible for the library to use this data. HDWalletProvider obtains word mnemonic or private key from the configuration file and uses this data to sign transactions before sending them to the proxy.

This method of configuration is convenient for debug mode, but not suitable for work in real conditions. Since the development process uses "test" wallets, this data is not of any value.

Note: We strongly recommend using Truffle in Neon EVM only for developing or testing contracts.

Configuration

Your configuration file is called truffle-config.js and is located at the root of your project directory. This file is a JavaScript file and can execute any code necessary to create your configuration.

Compiling Contracts

All of your contracts are located in your project's contracts/ directory. To compile a Truffle project, change to the root of the directory where the project is located and run the following command:

$ truffle compile

Upon first run, all contracts will be compiled. Upon subsequent runs, only contracts that have changed since the last compilation will be compiled again.

If you want to re-compile all contracts, run the above command with the --all option:

$ truffle compile --all

Running Migrations

Migration is used to deploy your contracts to the network. This operation is performed using JavaScript files contained in the migration/ directory. Migrations are simply a set of managed deployment scripts.

Run migrations to deploy contracts:

$ truffle migrate

This will run all migrations located within the migrations/ directory. If your migrations were previously run successfully, truffle migrate will start execution from the last migration that was run, running only newly created migrations. If no new migrations exist, truffle migrate won't perform any action.

If you need to run all migrations from the beginning, instead of running from the last completed migration, you can use the --reset option:

$ truffle migrate --reset

The full set of options that you can use during running migrations are listed in the page with truffle migrate command.

Testing Contracts

All test files should be located in the test/ directory.

To run all tests by default, simply run:

$ truffle test

To run only one file from the entire test suite or a specific file that is not in test/, you need to specify the full name of that file:

$ truffle test <./path/file.js>

The full set of options that you can use during testing are listed in the page with the truffle test command.

Example of Configuration File Settings

The example of the configuration file for connecting Truffle to a devnet-proxy using the one-way library on Node.js:

const Web3 = require("web3");
const HDWalletProvider = require("@truffle/hdwallet-provider");

Web3.providers.HttpProvider.prototype.sendAsync = Web3.providers.HttpProvider.prototype.send

const provider = new Web3.providers.HttpProvider("https://proxy.devnet.neonlabs.org/solana");

const privateKey = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"; // Specify your private key here

module.exports = {
networks: {
neonlabs: {
provider: () => {
return new HDWalletProvider(
privateKey,
provider,
);
},
from: "xxxxxxxxxxxxxxxxxxxxxxxxxxxx", // Specify public key corresponding to private key defined above
network_id: "*",
gas: 3000000000,
gasPrice: 443065000000,
}
}
};

Note: If both mnemonic and private keys are provided, the mnemonic is used.

Use next command to deploy contracts using neonlabs network:

truffle migrate --network neonlabs

Example Project

You can obtain example Truffle project from here: https://github.com/neonlabsorg/examples/tree/main/simple-erc20-truffle