Introducing the Moonbeam Truffle Box

Alberto Viera
Moonbeam Network
Published in
7 min readNov 11, 2020

A Boilerplate Setup to Get Started Deploying Smart Contracts on Moonbeam Quickly

One of the most important things about emerging blockchain is not only the technology itself, but also the tools that are available and developed around it.

For example, in the Ethereum ecosystem, developers have access to a wide range of tools such as MetaMask, Remix and Truffle, that provides an easier on-ramp when building on top of Ethereum. This could be one of the reasons why its developer community is almost four times bigger than any other crypto ecosystem, according to Electric Capital in this report.

The goal of Moonbeam is to create an Ethereum compatible environment on Polkadot, where all of these familiar tools can be used. And one of these commonly used tools is Truffle. Truffle is a development environment which also offers a testing framework (with Mocha and Chai) and asset pipeline for Ethereum, and now Polkadot as well thanks to Moonbeam. You can read more about all the tools that are available with Truffle in their GitHub repository.

As part of an ongoing effort to help developers that want to start working on Moonbeam, we have launched the Moonbeam Truffle box. With this Truffle Box, developers will find a boilerplate Truffle setup to get started deploying smart contracts on Moonbeam quickly. The box is pre-configured with two networks: dev (for a standalone node) and moonbase (Moonbeam TestNet). Included as well, as an example, is an ERC-20 token contract, and a simple test script. If you are experienced with Truffle, this setup will feel familiar

With the Moonbeam Truffle box, we have also incorporated the Moonbeam Truffle plugin, that introduces some commands to run a standalone node in your local environment as a Docker image. This removes the process of setting up a local node which can take up to 40 minutes when building its binary, and is a quick and easy solution to get started developing in your local environment.

Getting Started

To get started with the Moonbeam Truffle box, if you have Truffle installed globally, you can execute:

mkdir moonbeam-truffle-box && cd moonbeam-truffle-boxtruffle unbox PureStake/moonbeam-truffle-box

To install Truffle globally, you can run the following command:

npm install -g truffle

Nevertheless, the box has also Truffle as a dependency in case you do not want to have it installed globally. In such a case, you can directly clone the following repository:

git clone https://github.com/PureStake/moonbeam-truffle-boxcd moonbeam-truffle-box

With the files in your local system, the next step is to install all dependencies by running:

npm install

And that is it, all the prerequisites that you need to start using the Moonbeam Truffle box.

Basic Functionalities

As stated before, at the time of writing the Moonbeam Truffle box comes with two networks pre-configured in the truffle-config.js file. The private key of the genesis account for the standalone node is included as well, the address associated with this key holds all the tokens in this development environment. For deployments in the Moonbase Alpha TestNet, you need to provide the private key of an address that holds funds. To do so, you can create an account in MetaMask, fund it using the TestNet faucet, and export its private key.

const PrivateKeyProvider = require('./private-provider');
// Standalone Development Node Private Key
const privateKeyDev =
'99B3C12287537E38C90A9219D4CB074A89A16E9CDB20BF85728EBD97C343E342';
// Moonbase Alpha Private Key --> Change this to your own funded PK
const privateKeyMoonbase =
'';
module.exports = {
networks: {
dev: {
provider: () => {
return new PrivateKeyProvider(privateKeyDev, 'http://localhost:9933/', 43)
},
network_id: 43,
},
moonbase: {
provider: () => {
return new PrivateKeyProvider(privateKeyMoonbase, 'https://rpc.testnet.moonbeam.network', 43)
},
network_id: 43,
},
},
plugins: ['moonbeam-truffle-plugin']
};

If you are familiar with Truffle, you might have noticed that we are using a custom provider programmed by ourselves, instead of the most common ones such as hdwallet-provider. This custom provider still uses standard libraries such as the web3-provider-engine and ethereumjs-wallet. The reason behind this is because our custom chain ID was not being included by the library used to sign the transactions. Therefore, the signature is invalid because the chain ID in the transaction blob is missing, and the transaction is rejected.

As with using Truffle in any Ethernet network, you can run the normal commands to compile, test and deploy smart contracts in Moonbeam. For example, using the included ERC20 token contract, you can try the following commands:

# compiles the contract
./node_modules/.bin/truffle compile
# run the tests included in the test folder
./node_modules/.bin/truffle test #run the tests included
# deploys to the specified network
./node_modules/.bin/truffle migrate --network network_name

If you have Truffle installed globally, you can remove ./node_modules/.bin/ from the commands. Depending on the network you want to deploy the contracts too, you need to substitute network_name for either dev (to target the standalone node) or moonbase (to target the TestNet).

The Moonbeam Truffle Plugin

Currently, to set up a standalone Moonbeam node, you can follow this tutorial. The process takes around 40 minutes in total, and you need to install Substrate and all its dependencies. The Moonbeam Truffle plugin provides a way to get started with a standalone node much quicker, and the only requirement is to have docker installed (at the time of writing the docker version used was 19.03.6). To download the docker image, run the following line:

./node_modules/.bin/truffle run moonbeam install

Then, you have available a set of commands to control the node included in the Docker image:

./node_modules/.bin/truffle run moonbeam start./node_modules/.bin/truffle run moonbeam status./node_modules/.bin/truffle run moonbeam pause./node_modules/.bin/truffle run moonbeam unpause./node_modules/.bin/truffle run moonbeam stop./node_modules/.bin/truffle run moonbeam remove

Each of the commands shown before does the following action:

  • Start: starts a Moonbeam standalone node, this provides two RPC endpoints: — HTTP: http://127.0.0.1:9933 — WS: ws://127.0.0.1:9944
  • Status: tells the user if there is a Moonbeam standalone node running
  • Pause: pauses the standalone node if it’s running
  • Unpause: unpauses the standalone node if it’s paused
  • Stop: stops the standalone node if it’s running, this also removes the Docker container
  • Remove: deletes the purestake/moonbase Docker image

You can see the output of these commands in the following image:

If you are familiar with Docker, you can skip the plugin commands and interact with the Docker image directly.

Testing the Moonbeam Truffle Box

The box has the minimum requirements to help you get started. Lets first compile the contracts by running:

./node_modules/.bin/truffle compile

Remember that if you have Truffle installed globally, you can skip the ./node_modules/.bin/ part in the commands.

With the contract compiled, we can run the basic test included in the box (note that for these tests Ganache is used, and not the Moonbeam standalone node):

./node_modules/.bin/truffle test

After running the plugin install command, which downloads the Moonbeam standalone node Docker image, let’s start the local node and deploy the token contract to our local environment:

./node_modules/.bin/truffle run moonbeam start./node_modules/.bin/truffle migrate --network dev

And lastly, we can deploy our token contract to Moonbase Alpha, but first, make sure you set a private key with funds in the truffle-config.js file. Once the private key is set, we can execute the migrate command pointing to the TestNet (we need to pass in the — reset flag as both the development and Moonbase Alpha networks have the same chain Id).

./node_modules/.bin/truffle migrate --network moonbase --reset

And that is it, you’ve used the Moonbeam Truffle box to deploy a simple ERC20 token contract in both your standalone Moonbeam node and Moonbase Alpha.

Contact Us

If you have any feedback regarding the Moonbeam Truffle Box or any other Moonbeam-related topic, feel free to visit our website or reach out through our official development Discord server.

--

--