Sample Contracts
This chapter describes how to write and deploy smart contracts using truffle.
Creating Smart Contracts
This chapter describes how to create and distribute smart contracts using Truffle.
How to use Truffle
1. Installation
Install truffle using NPM, the Node Package Manager. Please refer to the following link for how to install npm: https://www.npmjs.com/
2. Initialization
When truffle init is performed, a truffle project is created. The Truffle project consists of build/contracts, contracts, migrations, tests, and truffle-config.js.
build/contracts: The result of compiling the contract written in solidity (abi, bytecode) is saved
contract: smart contract solidity files
migrations: deploy code(js format)
test: test code(js format)
truffle-config.js: Configuration such as solidity compiler version setting, network setting, etc
Performing truffle init, the example solidity code called Migrations.sol is created as follows in the contract folder. To deploy the written smart contract, the user must first compile the written smart contract.
3. Settings
You must modify the truffle-config.js file for compilation and deployment.
3.1 Account Settings
Add HDWalletProvider and privKeys in the Truflle-config.js file for settings for the account you want to use for deployment. Private keys can be imported after creating an account through the Web Wallet (https://wallet.test.wemix.com).
3.2 Network Settings
To use the WEMIX3.0 Testnet, add the network settings as follows:
3.3 Compiler Settings
Set the last Solidity compiler version you want to use. The current version is 0.8.x, and the example uses 0.8.11. If it is installed directly on the operating system using the compiler, you can comment on the docker setting just like the initial setting. To install using docker, set "docker: true" as shown below and docker should be installed. If you use the optimizer, you can un-annotate and set the appropriate settings.
4. Write a Solidity code
Here is a simple smart contract example that increments or decrements the count variable and retrieves its value. Save it as Counter.sol.
5. compile a Solidity code
When truffle compile <file name> is performed in the truffle project folder, it compiles the target solidity file. The compiled solidity contracts are stored in the build/contracts folder as <contract name>.json format.
6. Deploy a smart contract
To distribute the compiled contract file to the blockchain, write the script and save it as 2_deploy_contracts.js:
Deploy a contract individually by loading them as artifacts.require (<artifacts file name stored in build/contracts>) and performing developer.deploy(contract_artifacts, {constructor arguments}).
The deployment script can be executed with the truffle migrate <file name>. If not used, it is performed in numerical order used in the script name.
The results of the deployment on WEMIX3.0 Testnet are as follows: For deployment, the account that you want to use for deployment must have WEMIX. You can receive the required WEMIX from WEMIX-Faucet at https://wallet.test.wemix.com.
7. Test a smart contract
The deployed contract can be tested through the javascript file as follows.
contract : test target contact
describe : test case set
it : Individual test case
When the test code is completed, save the test file as <testName.test.js> format in the test folder, and perform the truffle test <testName.test.js> to perform the test.