Skip to main content

Deploying Smart Contracts with Truffle

This is a guide to creating and deploying smart contracts with Truffle on the Syscoin Tanenbaum Testnet.

NOTE: Syscoin is designed to be a settlement layer for zkRollups and other L2s/execution layers, as such it has a 2.5 minute block time for the optimal settlement security, it is advised to create dApps using zkRollups rather than using the Syscoin blockchain itself for executing smart contracts. You can find zkRollup resources here.

This guide assumes that you have already:

1. Install Truffle and create a project#

To install Truffle use the following command:

npm install truffle -g

Make a new directory where you wish to create your project then cd into it and run the command below:

truffle init

Then enter this command to install support for HD wallet providers:

npm install @truffle/hdwallet-provider -s

2. Create a new contract#

Create a new file in the contracts directory called HelloNEVM.sol.

Paste the following code into the HelloNEVM.sol file.

pragma solidity ^0.8.10;
contract HelloNEVM {
function sayHello() public pure returns(string memory) {
return('Hello NEVM!');
}
}

3. Configure the network used by Truffle#

In truffle-config.js in the networks:{} section, add the following:

mainnet: {
provider: function() {
return new HDWalletProvider(mnemonic, "https://rpc.syscoin.org")
},
websocket: true, // Enable EventEmitter interface for web3 (default: false)
network_id: 57,
gas: 8000000 // gas should be no higher than 8m
}
tanenbaum: {
provider: function() {
return new HDWalletProvider(mnemonic, "https://rpc.tanenbaum.io/")
},
websocket: true, // Enable EventEmitter interface for web3 (default: false)
network_id: 5700,
gas: 8000000 // gas should be no higher than 8m
}

Then create a new file in the same directory called .secret and store your mnemonic from MetaMask in there and uncomment the line in truffle-config.js where mnemonic is initialized. Don't forget to add an entry in .gitignore for the .secret file.

4. Deploy the contract#

In the migrations folder create a JavaScript file named 2_deploy_contracts.js for deploying the contract.

Paste the following code into the file.

var HelloNEVM = artifacts.require('HelloNEVM');
module.exports = function(deployer) {
deployer.deploy(HelloNEVM, 'Hello NEVM!');
// Additional contracts can be deployed here
};

Once this has been done then you can then deploy the contract (use mainnet if you want to deploy on mainnet rather than the Tanenbaum testnet).

truffle deploy --network tanenbaum

You will then receive something similar to the following output showing that the contracts have been successfully deployed.

Compiling your contracts...
===========================
> Compiling ./contracts/HelloNEVM.sol
> Compiling ./contracts/Migrations.sol
> Compilation warnings encountered:
Warning: SPDX license identifier not provided in source file. Before publishing, consider adding a comment containing "SPDX-License-Identifier: <SPDX-License>" to each source file. Use "SPDX-License-Identifier: UNLICENSED" for non-open-source code. Please see https://spdx.org for more information.
--> project:/contracts/HelloNEVM.sol
> Artifacts written to /home/nevm/Desktop/truffle/build/contracts
> Compiled successfully using:
- solc: 0.8.10+commit.fc410830.Emscripten.clang
Starting migrations...
======================
> Network name: 'tanenbaum'
> Network id: 5700
> Block gas limit: 8000000 (0x7a1200)
1_initial_migration.js
======================
Deploying 'Migrations'
----------------------
> transaction hash: 0xb47d5c5615574d924c993b8950f12790b3bc5c80057a5a277c62970fd12b27b1
> Blocks: 2 Seconds: 57
> contract address: 0xbcB2F470C038bB1C46D37b1d83B583637f4E6Fa7
> block number: 35173
> block timestamp: 1638216695
> account: 0x747825653574060c4AdA31c028cfCb81E1C6C9a4
> balance: 2.494746334985289738
> gas used: 250154 (0x3d12a)
> gas price: 2.500000007 gwei
> value sent: 0 ETH
> total cost: 0.000625385001751078 ETH
> Saving migration to chain.
> Saving artifacts
-------------------------------------
> Total cost: 0.000625385001751078 ETH
2_deploy_contracts.js
=====================
Deploying 'HelloNEVM'
---------------------
> transaction hash: 0xa552bbf6dea81da9f6bb89d4195b238f672cf76064cb3649665fbeb99676aecd
> Blocks: 1 Seconds: 37
> contract address: 0x2ad52f0832Edd1169bB2234A9C931E0C95c892be
> block number: 35178
> block timestamp: 1638216962
> account: 0x747825653574060c4AdA31c028cfCb81E1C6C9a4
> balance: 2.494293884984022878
> gas used: 135067 (0x20f9b)
> gas price: 2.500000007 gwei
> value sent: 0 ETH
> total cost: 0.000337667500945469 ETH
> Saving migration to chain.
> Saving artifacts
-------------------------------------
> Total cost: 0.000337667500945469 ETH
Summary
=======
> Total deployments: 2
> Final cost: 0.000963052502696547 ETH

Congratulations! Your contracts have been deployed and in the Deploying 'HelloNEVM' section you can see the contract's address, which is worth saving if you wish to interact with it at a later date.

5. Interact with the smart contract#

cd to the root of your project directory then use this command to use Truffle on the Tanenbaum network (as specified in the truffle-config.js)

truffle console --network tanenbaum

You can then call the sayHello() function with the following input:

HelloNEVM.deployed().then(function(instance){return instance.sayHello()});