Truffle
Using Truffle for DApp development on Theta is pretty much the same as on Ethereum. This page will take you through the basics of testing a smart contract and deploying it to the Theta Mainnet through truffle. In particular, we will use the following simple "coin-like" smart contract project for the demo:
https://github.com/truffle-box/metacoin-box
Install Truffle
If you haven't done so, please follow the instructions below to install the Truffle suite. NodeJS v8.9.4 or later is required. For readers that are not yet familiar with Truffle, please click here to learn more.
npm install -g truffle
Setup the MetaCoin Demo Project
Next, clone the MetaCoin demo project, and install the dependencies:
git clone https://github.com/truffle-box/metacoin-box
cd metacoin-box
npm install @truffle/hdwallet-provider
Use your favorite editor to edit the truffle-config.js
file. Replace its content with the following code snippet, which configures the Theta local privatenet and the Mainnet.
const HDWalletProvider = require('@truffle/hdwallet-provider');
module.exports = {
mocha: {
enableTimeouts: false,
before_timeout: 480000
},
networks: {
theta_privatenet: {
provider: () => {
// private key for test wallet #1: 0x19E7E376E7C213B7E7e7e46cc70A5dD086DAff2A
var privateKeyTest1 = '1111111111111111111111111111111111111111111111111111111111111111';
// private key for test wallet #2: 0x1563915e194D8CfBA1943570603F7606A3115508
var privateKeyTest2 = '2222222222222222222222222222222222222222222222222222222222222222';
return new HDWalletProvider({
privateKeys: [privateKeyTest1, privateKeyTest2],
providerOrUrl: 'http://localhost:18888/rpc',
});
},
network_id: 366,
gasPrice: 4000000000000,
},
theta_testnet: {
provider: () => {
// Replace the private key below with the private key of the deployer wallet.
// Make sure the deployer wallet has a sufficient amount of TFuel, e.g. 100 TFuel
var deployerPrivateKey = '12345';
return new HDWalletProvider({
privateKeys: [deployerPrivateKey],
providerOrUrl: 'https://eth-rpc-api-testnet.thetatoken.org/rpc',
});
},
network_id: 365,
gasPrice: 4000000000000,
},
theta_mainnet: {
provider: () => {
// Replace the private key below with the private key of the deployer wallet.
// Make sure the deployer wallet has a sufficient amount of TFuel, e.g. 100 TFuel
var deployerPrivateKey = '12345';
return new HDWalletProvider({
privateKeys: [deployerPrivateKey],
providerOrUrl: 'https://eth-rpc-api.thetatoken.org/rpc',
});
},
network_id: 361,
gasPrice: 4000000000000,
}
}
};
Run the MetaCoin Unit Test Suite against the Theta Local Privatenet
First, we need to setup the Theta local privatenet with the Theta/Ethereum RPC Adaptor following this guide.
Recall that on the Theta blockchain, TFuel is the gas token, similar to Ether on the Ethereum blockchain. Thus, to run the unit test cases, we first need to send some privatenet TFuel to the test wallets from the privatenet faucet:
export SEQ=`thetacli query account --address=0x2E833968E5bB786Ae419c4d13189fB081Cc43bab | grep sequence | grep -o '[[:digit:]]\+'`
thetacli tx send --chain="privatenet" --from=0x2E833968E5bB786Ae419c4d13189fB081Cc43bab --to=0x19E7E376E7C213B7E7e7e46cc70A5dD086DAff2A --tfuel=1000 --password=qwertyuiop --seq=$(($SEQ+1))
Now we can proceed to deploy the smart contracts to the local privatenet:
truffle deploy --network theta_privatenet --reset
Next we can run the unit test cases (sometimes the test could take 2-3 minutes to complete):
truffle test ./test/TestMetaCoin.sol --network theta_privatenet
truffle test ./test/metacoin.js --network theta_privatenet
Deploy the MetaCoin Contract to the Theta Mainnet
First, modify the following line in the truffle-config.js
file to replace the private key with the actual private key of the deployer wallet (should delete the key after use, do NOT commit the private key to GitHub):
var deployerPrivateKey = '12345';
Also, make sure the deployer wallet has a sufficient amount of TFuel on the Theta Mainnet (e.g. 100 TFuel). Next, execute the command below to deploy the smart contracts to the Theta Mainnet. In rare cases, the command might exit with an error. If that happens, simply re-execute the deploy command.
truffle deploy --network theta_mainnet --reset
Updated about 3 years ago