Deploy contracts using Truffle
This article takes Pet-Shop as an example to introduce how to deploy smart contract into smartBCH chain using truffle.
Please note in the following example we assume you are using a local single-node testnet. Instead, if you want to use a remote node to provide JSON-RPC, just replace "localhost" with the node's IP address.

Start smartBCH single testing node

Please flow this doc to start smartBCH single testing node. When the testing node is started with default options, it will serve JSON-RPC on localhost:8485. You can use the following cmd to see if node works well:
1
$ curl -X POST --data '{"jsonrpc":"2.0","method":"net_version","params":[],"id":67}' \
2
-H "Content-Type: application/json" http://localhost:8545
3
​
4
{"jsonrpc":"2.0","id":67,"result":"1337"}
Copied!
You can also use this public test node (deployed by smartBCH community) if you do not want to deploy your local node:
1
$ curl -X POST --data '{"jsonrpc":"2.0","method":"net_version","params":[],"id":67}' \
2
-H "Content-Type: application/json" https://smartbch.greyh.at
3
​
4
{"jsonrpc":"2.0","id":67,"result":"1337"}
Copied!

Install Truffle

You need to install Node.js first, here are detailed information about how to install it on various platforms. Then, run the following cmd to install truffle:
1
$ npm install -g truffle
Copied!
And run the following cmd the see if truffle was installed successfully:
1
$ truffle version
2
​
3
Truffle v5.1.63 (core: 5.1.63)
4
Solidity v0.5.16 (solc-js)
5
Node v15.10.0
6
Web3.js v1.2.9
Copied!

Clone Pet-Shop

Using git clone cmd to clone pet-shop source code into you local directory:
1
$ cd somedir
2
$ git clone https://github.com/trufflesuite/pet-shop-tutorial.git
3
$ cd pet-shop-tutorial
Copied!
Modify truffle-config.js, change development network port to match your local node (e.g. 8545):
1
module.exports = {
2
// See <http://truffleframework.com/docs/advanced/configuration>
3
// for more about customizing your Truffle configuration!
4
networks: {
5
development: {
6
host: "127.0.0.1",
7
port: 8545,
8
network_id: "*" // Match any network id
9
}
10
}
11
};
Copied!
You can modify truffle-config.js further and add a network config like this if your want to use greyh's test node too:
1
const Web3 = require('web3');
2
​
3
module.exports = {
4
// See <http://truffleframework.com/docs/advanced/configuration>
5
// for more about customizing your Truffle configuration!
6
networks: {
7
development: {
8
host: "127.0.0.1",
9
port: 8545,
10
network_id: "*" // Match any network id
11
},
12
greyh: {
13
provider: () => new Web3.providers.HttpProvider('https://smartbch.greyh.at'),
14
network_id: "*"
15
},
16
}
17
};
Copied!
Do not forget to install web3 through npm install cmd if you use public test node:
1
$ npm install web3
Copied!

Deploy to smartBCH

In directory pet-shop-tutorial, using truffle migrate cmd to deploy Pet-Shop contract into smartBCH local testing node:
1
$ truffle migrate --network development
2
# truffle migrate --network greyh # deploy to greyh's testing node
Copied!
The output looks like this:
1
Compiling your contracts...
2
===========================
3
> Compiling ./contracts/Adoption.sol
4
> Artifacts written to /Users/zxh/bitmain/github/truffle_suite/pet-shop-tutorial/build/contracts
5
> Compiled successfully using:
6
- solc: 0.5.16+commit.9c3226ce.Emscripten.clang
7
​
8
​
9
​
10
Starting migrations...
11
======================
12
> Network name: 'development'
13
> Network id: 1337
14
> Block gas limit: 200000000 (0xbebc200)
15
​
16
​
17
1_initial_migration.js
18
======================
19
​
20
Replacing 'Migrations'
21
----------------------
22
> transaction hash: 0xdd1548be1a2448b471390a309ca50a3168a8c690438329d71acd07ccfeab4736
23
> Blocks: 0 Seconds: 0
24
> contract address: 0xC7BBd3373c6D9f582102c332bE91e8dCDd087e35
25
> block number: 156
26
> block timestamp: 1618372330
27
> account: 0x09F236e4067f5FcA5872d0c09f92Ce653377aE41
28
> balance: 9.99549526
29
> gas used: 225237 (0x36fd5)
30
> gas price: 20 gwei
31
> value sent: 0 ETH
32
> total cost: 0.00450474 ETH
33
​
34
​
35
> Saving migration to chain.
36
> Saving artifacts
37
-------------------------------------
38
> Total cost: 0.00450474 ETH
39
​
40
​
41
2_deploy_contracts.js
42
=====================
43
​
44
Replacing 'Adoption'
45
--------------------
46
> transaction hash: 0x5143aaff4315c7fca0e762422dcb7daedcfbbdea047668922948ede2a982e97f
47
> Blocks: 0 Seconds: 0
48
> contract address: 0x531f499C35945C83C87B5f33b56a5aFFa9CF0d05
49
> block number: 160
50
> block timestamp: 1618372334
51
> account: 0x09F236e4067f5FcA5872d0c09f92Ce653377aE41
52
> balance: 9.99057146
53
> gas used: 203827 (0x31c33)
54
> gas price: 20 gwei
55
> value sent: 0 ETH
56
> total cost: 0.00407654 ETH
57
​
58
​
59
> Saving migration to chain.
60
> Saving artifacts
61
-------------------------------------
62
> Total cost: 0.00407654 ETH
63
​
64
​
65
Summary
66
=======
67
> Total deployments: 2
68
> Final cost: 0.00858128 ETH
Copied!
Last modified 5mo ago