Skip to content

Truffle

Intro diagram

Introduction

Truffle is a popular development framework for compiling, testing, and deploying Solidity smart contracts. Since Moonbeam is Ethereum compatible, with a few lines of extra configuration, you can use Truffle as you normally would with Ethereum to develop on Moonbeam.

Configure Truffle to Connect to Moonbeam

If you haven't yet, you'll want to globally install Truffle:

npm install -g truffle

In your truffle-config.js file, add network configurations for a Moonbeam development node and the Moonbase Alpha TestNet:

const HDWalletProvider = require('@truffle/hdwallet-provider');
// Moonbeam Private Key - Note: This is for example purposes only. Never store your private keys in a JavaScript file.
const privateKeyMoonbeam = "YOUR-PRIVATE-KEY-HERE";
// Moonriver Private Key - Note: This is for example purposes only. Never store your private keys in a JavaScript file.
const privateKeyMoonriver = "YOUR-PRIVATE-KEY-HERE";
// Moonbase Alpha Private Key
const privateKeyMoonbase = "YOUR-PRIVATE-KEY-HERE";
// Moonbeam Development Node Private Key
const privateKeyDev = '99B3C12287537E38C90A9219D4CB074A89A16E9CDB20BF85728EBD97C343E342';

module.exports = {
   networks: {
      // Moonbeam
      moonbeam: {
        provider: () => {
          return new HDWalletProvider(
            privateKeyMoonbeam,
            'https://rpc.api.moonbeam.network'
          );
        },
        network_id: 1284, // 0x504 in hex,
      }
      // Moonriver
      moonriver: {
        provider: () => {
          return new HDWalletProvider(
            privateKeyMoonriver,
            'https://rpc.moonriver.moonbeam.network'
          );
        },
        network_id: 1285, // 0x505 in hex,
      }
      // Moonbase Alpha TestNet
      moonbase: {
        provider: () => {
          return new HDWalletProvider(
            privateKeyMoonbase,
            'https://rpc.api.moonbase.moonbeam.network'
          );
        },
        network_id: 1287, // 0x507 in hex,
      },
      // Moonbeam Development Node
      dev: {
        provider: () => {
          return new HDWalletProvider(privateKeyDev, 'http://127.0.0.1:9933')
         },
        network_id: 1281, // 0x501 in hex,
      },
   },
};

Tutorial

If you are interested in a more detailed step-by-step guide, go to our specific tutorial about using Truffle with Moonbeam.