Every Dapp running on Ethereum can be easily ported to TomoChain
Note: Because deploying a smart contract on mainnet is much similar to testnet, the differences are just the configuration information, this document will explicitly mention the differences where possible
node -v
. This should print a version number, like v10.15.0
.npm -v
and you should see the version number, like 6.4.1
.truffle version
.pet-shop
, which includes the basic project structure as well as code for the user interface. Use the truffle unbox
command to unpack this Truffle Box:Note: This tutorial is focused on the whole process to build a Dapp on TomoChain, so we will not enter into all the details.
TomoChain TestNet
or TomoChain
[mainnet].mnemonic
) is:Important! Always keep your private key and recovery phrase secret!
30 TOMO
. Now your wallet has enough balance to do everything in this tutorial so… let’s go ahead!Adoption.sol
in the contracts/
directory.Note: Code from Truffle’s Pet-Shop tutorial — if you want to look deeper into the Solidity code, they slowly go through the Truffle link explaining the details.
TomoChain is EVM-compatible, which means that every contract written in Ethereum can be seamlessly ported to TomoChain without effort
migrations/
directory and you will see one JavaScript file: 1_initial_migration_js
. This handles deploying the Migrations.sol
contract to observe subsequent smart contract migrations, and ensures we don't double-migrate unchanged contracts in the future.2_deploy_contracts.js
in the migrations/
directory.2_deploy_contracts.js
file:HDWalletProvider
, a separate npm package to find and sign transactions for addresses derived from a 12-word mnemonic
— in a certain blockchain. (Read more about HDWalletProvider.)truffle.js
file (truffle-config.js
on Windows). You can edit here the migration settings: networks, chain IDs, gas... The current file has only a single network defined, you can define multiple. We will add three networks to migrate our Dapp: development
, tomotestnet
and tomomainnet
.RPC endpoint
, the Chain id
and the HD derivation path
.truffle.js
file with this new content:truffle.js
file using your own wallet recovery phrase. Copy the 12 words obtained previously and paste it as the value of the mnemonic
variable.tomotestnet
network will be used to deploy our smart contract. We have also added the tomomainnet
network, in case you want to deploy to TomoChain Mainnet. However, if you are familiar with Ganache, you could use the development
network to do the local test as well if you want to. Ganache is a locally running personal blockchain for Ethereum development you can use to deploy contracts, develop applications, and run tests.Warning: In production, we highly recommend storing the mnemonic in another secret file (loaded from environment variables or a secure secret management system), to reduce the risk of the mnemonic becoming known. If someone knows your mnemonic, they have all of your addresses and private keys!
dotenv
you can load an environment variable from a file .env
, — then update your truffle.js to use this secret mnemonic
.truffle compile
.30 TOMO
and the deployment has costed 5.38 TOMO
in gas fees.Note: The command to deploy to TomoChain mainnet is very similar:truffle migrate --network
tomomainnet
smart contract creation cost is under allowance
. Why? Increasing transaction fees for smart contract creation is one of the ways TomoChain defends against spamming attacks. Solution: edit truffle.js
and add more gas/gas Price to deploy.insufficient funds for gas * price + value
. Why? You don’t have enough tokens in your wallet for gas fees. Solution: you need more funds in your wallet to deploy, go to faucet and get more tokens.test/
directory and execute with truffle test
. Find more details on Truffle’s Pet Shop tutorial.pet-shop
Truffle Box was code for the app’s front-end. That code exists within the src/
directory./src/js/app.js
in a text editor.App
object to manage our application, load in the pet data in init()
and then call the function initWeb3()
. The web3 JavaScript library interacts with the Ethereum blockchain. It can retrieve user accounts, send transactions, interact with smart contracts, and more.initContract()
Retrieves the artifact file for our smart contract. Artifacts are information about our contract such as its deployed address and Application Binary Interface (ABI). The ABI is a JavaScript object defining how to interact with the contract including its variables, functions and their parameters. We then call the app's markAdopted()
function in case any pets are already adopted from a previous visit.markAdopted()
After calling getAdopters()
, we then loop through all of them, checking to see if an address is stored for each pet. Ethereum initializes the array with 16 empty addresses. This is why we check for an empty address string rather than null or other false value. Once a petId
with a corresponding address is found, we disable its Adopt button and change the button text to "Success", so the user gets some feedback.handleAdopt()
We get the deployed contract and store the instance in adoptionInstance
. We're going to send a transaction instead of a call by executing the adopt()
function with both the pet's ID and an object containing the account address. Then, we proceed to call our markAdopted()
function to sync the UI with our newly stored data.lite-server
. This shipped with the pet-shop
Truffle box.bs-config.json
and package.json
, if you want to take a look. These tell npm to run our local install of lite-server
when we execute npm run dev
from the console.