Caduceus testnet deployment DApp tutorial -Truffle development contract

Caduceus
5 min readDec 21, 2021

--

Caduceus Testnet Deployment DApp Tutorial -Truffle Development Contract
Caduceus testnet deployment DApp tutorial -Truffle development contract

Install

npm install -g truffle

Create project

The truffle init command will help us create an empty project
Number 1 in Figure 2–1 represents the command to create a project
Number 2 in Figure 2–1 indicates that the project is successfully created
Number 3 in Figure 2–1 indicates the created directory

D:\contract\HelloDapp>truffle initStarting init...
================
> Copying project files to D:\contract\HelloDappInit successful, sweet!
D:\contract\HelloDapp>

picture2–1

Writing project

We recommend using Goland or idea series as the editor to open the HelloDapp directory. Then download the Intellij-Solidity plug-in to support the Solidity language
The specific steps are: File->Settings->Plugins->Browse Respositories->Intellij-Solidity->Install

  1. Create a new Hello.sol file in the contracts directory
    Number 2 in Figure 3–1 represents the contract code
pragma solidity ^0.4.23;contract Hello {    function print() public pure returns(string) {
return "Hello World";
}
function add(uint n1, uint n2) public pure returns(uint) {
return n1 + n2;
}
}

picture3–1

  1. Create a new TestHello.sol file in the test directory
    Note that the method name must start with test. Otherwise, the unit test will not execute the function.
    The number 2 in Figure 3-2 represents the contract test code
pragma solidity ^0.4.23;import "../contracts/Hello.sol";
import "truffle/Assert.sol";
contract TestHello { function testAdd() public {
Hello hello = new Hello();
uint result = hello.add(1, 20);
Assert.equal(result, 21, "TestAdd");
}
function testPrint() public {
Hello hello = new Hello();
string memory result = hello.print();
Assert.equal(result, "Hello World", "TestPrint");
}
}

picture3–2

  1. Create a new 2_deploy_hello.js file in the migrations directory
    The number 2 in Figure 3–3 represents the contract deployment code
var Hello = artifacts.require("./Hello.sol");module.exports = function(deployer) {
deployer.deploy(Hello);
};

picture3–3

  1. Specify the compiler version
    The name of the file to be modified for number 1 in Figure 3–4
    The content of the file that needs to be modified in number 2 in Figure 3–4, the version here needs to be the same as the version specified by the contract pragma solidity ^0.4.23.

picture3–4

Test

Execute truffle test on the command line

D:\contract\HelloDapp>truffle testCompiling your contracts...
===========================
√ Fetching solc version list from solc-bin. Attempt #1
√ Downloading compiler. Attempt #1.
> Compiling .\contracts\Hello.sol
> Compiling .\contracts\Migrations.sol
> Compiling .\contracts\Hello.sol
> Compiling .\test\TestHello.sol
> Artifacts written to C:\Users\ADMINI~1\AppData\Local\Temp\test--9472-EZIEuorVMRGs
> Compiled successfully using:
- solc: 0.4.23+commit.124ca40d.Emscripten.clang
TestHello
√ testAdd (169ms)
√ testPrint (198ms)
2 passing (11s)

Compile

The truffle compile command will generate a .json file corresponding to the contract in the build directory of the project, which describes the contract’s specific information. If there is a syntax error, an error message will be displayed
As shown in Figure 5–4, number 1 represents the created build directory
As shown in Figure 5–4, number 2 represents the metadata after the contract is compiled

D:\contract\HelloDapp>truffle compileCompiling your contracts...
===========================
> Compiling .\contracts\Hello.sol
> Compiling .\contracts\Migrations.sol
> Artifacts written to D:\contract\HelloDapp\build\contracts
> Compiled successfully using:
- solc: 0.4.23+commit.124ca40d.Emscripten.clang

picture5–4

Deployment contract

If we want to deploy a contract, we need to connect to a blockchain. Truffle has a built-in private chain for testing. It can only be used on the local computer and cannot interact with other main chains

We can use the truffle develop command to create a private chain and interact with it

D:\contract\HelloDapp>truffle compileCompiling your contracts...
===========================
> Compiling .\contracts\Hello.sol
> Compiling .\contracts\Migrations.sol
> Artifacts written to D:\contract\HelloDapp\build\contracts
> Compiled successfully using:
- solc: 0.4.23+commit.124ca40d.Emscripten.clang
D:\contract\HelloDapp>truffle develop
Truffle Develop started at http://127.0.0.1:9545/
Accounts:
(0) 0x7e7e39fb3671c22e21b4cd72ec820b525865737b
(1) 0x0d0b48d4591b6bf80b4a3a574dfba7656d32ca82
(2) 0xf95cc94fcb2fb94855a0c3a98b6ed9b845204914
(3) 0x944d9685f700583fbda1dc0ceafdcc4a3b2d6819
(4) 0x9609f03c031f2af16cd031bb5c69612525695ccc
(5) 0x5a1233a67ca680f7836c82a3be9e76624f38988b
(6) 0x2d7f17e3313f5de720bccfc03980098b755b47bb
(7) 0xe364ad5615916cebe685d5f008fff9b73433548f
(8) 0x957cfc2643c410858d1d4c913f8dc929a01139a9
(9) 0xf8955f2865df9a7b4fa45eaef43553f094c0a70f
Private Keys:
(0) 70b84ec21f9ab1bb90cd6ae774f2a7eae495690cb5fce87fc4ec80669e687929
(1) 2c8b045f7120c954890dce188d933d028b28cc943b6a6267af6a32f1a724eee7
(2) 6686b21ba090272b344001f96dbe27af833c2d5ae12edaccd94aee0a84a7b968
(3) ea67bc90105603631c18d181a7506dd867bd99be580d19609ca04a3d9112ff04
(4) de106fc0c85985be1965f0964ce4141fe1e76ff3f3bff050ed14b3ee0a5cb3d6
(5) d1fd2b7e7e58b350d36f80c58428250162810e46f5049f722b296a1667cda0e9
(6) 1bd4b6ed55aa0a9fe71ccde96e6cf171bbb982b517fb3782c703feb789fce8dd
(7) 9ace1e6169d12b79f6a0658693b035b142d95319b6a04505dadd5be7e3d143e3
(8) 0f9b16aac7b22c80053ed7bab175b1a93a6c5ee16fb5bbf227f4e95ded683b09
(9) 6ea4858fbe1626792e2377f6027ea33bf0075899f475e293f623bdcb6aba4684
Mnemonic: repair episode imitate must favorite penalty draw consider trial tissue boy coffee⚠️ Important ⚠️ : This mnemonic was created for you by Truffle. It is not secure.
Ensure you do not use it on production blockchains, or else you risk losing funds.

By default, a test chain was created using port 9545, and 10 accounts were created for the convenience of testing. Then enter the truffle interactive mode.

Then use the migrate command. Deploy the contract to connect

truffle(develop)> migrateCompiling your contracts...
===========================
> Everything is up to date, there is nothing to compile.
Starting migrations...
======================
> Network name: 'develop'
> Network id: 5777
> Block gas limit: 6721975 (0x6691b7)
1_initial_migration.js
======================
Replacing 'Migrations'
----------------------
> transaction hash: 0x1c4903f1f9b22ceb2eaaf28dbb58dc8c600867a940817a88e8d535c55b90f821
> Blocks: 0 Seconds: 0
> contract address: 0x7b8bE8Cbe806db9b9d836E053c5274178FbFb65d
> block number: 1
> block timestamp: 1636093831
> account: 0x7e7e39Fb3671c22e21B4CD72eC820B525865737B
> balance: 99.99592882
> gas used: 203559 (0x31b27)
> gas price: 20 gwei
> value sent: 0 ETH
> total cost: 0.00407118 ETH
> Saving migration to chain.
> Saving artifacts
-------------------------------------
> Total cost: 0.00407118 ETH
2_deploy_hello.js
=================
Replacing 'Hello'
-----------------
> transaction hash: 0xadb75be42da553548d0b6b86dd361066068e7081b48cf4517376b84e80bb20ae
> Blocks: 0 Seconds: 0
> contract address: 0xF237cDdD2571aD18be13F578C7410E2d8559B52B
> block number: 3
> block timestamp: 1636093832
> account: 0x7e7e39Fb3671c22e21B4CD72eC820B525865737B
> balance: 99.9922213
> gas used: 143047 (0x22ec7)
> gas price: 20 gwei
> value sent: 0 ETH
> total cost: 0.00286094 ETH
> Saving migration to chain.
> Saving artifacts
-------------------------------------
> Total cost: 0.00286094 ETH
Summary
=======
> Total deployments: 2
> Final cost: 0.00693212 ETH

This is successfully deployed to the chain, and the transaction id and contract address are displayed. The contract address under 2_deploy_hello.js: 0xF237cDdD2571aD18be13F578C7410E2d8559B52B represents the contract address.

Contract interaction

truffle(develop)> Hello.address
'0xF237cDdD2571aD18be13F578C7410E2d8559B52B'
truffle(develop)> Hello.deployed().then(inst => {HelloInst = inst});
undefined
truffle(develop)> HelloInst.print();
'Hello World'
truffle(develop)> HelloInst.add(10, 200);
BN {
negative: 0,
words: [ 210, <1 empty item> ],
length: 1,
red: null
}
truffle(develop)> HelloInst.add(1, 30).then(val => val.toNumber());
31
truffle(develop)> HelloInst.address
'0xF237cDdD2571aD18be13F578C7410E2d8559B52B'
truffle(develop)>

For more detailed usage reference: https://learnblockchain.cn/docs/truffle/index.html

For more information: caduceus.foundation / info@caduceus.foundation

--

--

Caduceus
Caduceus

Written by Caduceus

Caduceus Metaverse Protocol — Providing an open blockchain platform for Metaverse development. Join the community — https://linktr.ee/caduceus_cad

No responses yet