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
npm -vand you should see the version number, like
pet-shop, which includes the basic project structure as well as code for the user interface. Use the
truffle unboxcommand 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.
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!
TomoChain is EVM-compatible, which means that every contract written in Ethereum can be seamlessly ported to TomoChain without effort
1_initial_migration_js. This handles deploying the
Migrations.solcontract to observe subsequent smart contract migrations, and ensures we don't double-migrate unchanged contracts in the future.
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-config.json 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:
truffle.jsfile with this new content:
truffle.jsfile using your own wallet recovery phrase. Copy the 12 words obtained previously and paste it as the value of the
tomotestnetnetwork will be used to deploy our smart contract. We have also added the
tomomainnetnetwork, in case you want to deploy to TomoChain Mainnet. However, if you are familiar with Ganache, you could use the
developmentnetwork 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!
dotenvyou can load an environment variable from a file
.env, — then update your truffle.js to use this secret
30 TOMOand the deployment has costed
5.38 TOMOin gas fees.
Note: The command to deploy to TomoChain mainnet is very similar:
truffle migrate --network
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.jsand 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.
pet-shopTruffle Box was code for the app’s front-end. That code exists within the
/src/js/app.jsin a text editor.
Appobject to manage our application, load in the pet data in
init()and then call the function
markAdopted()function in case any pets are already adopted from a previous visit.
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
petIdwith 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
package.json, if you want to take a look. These tell npm to run our local install of
lite-serverwhen we execute
npm run devfrom the console.