Truffle

Compatibility with Truffle

In Klaytn, a smart contract written in Solidity can be compiled and deployed via Truffle. At the moment, Klaytn supports up to Truffle v5.0.26, the latest version at the time of writing. Please find details about Truffle on the websites below.
You can install Truffle as the following:
1
$ sudo npm install -g truffle
Copied!
If you have a local EN running, you can deploy contracts directly with truffle framework. For more details, refer to this link.
If you want to deploy with a remote EN node, you should use truffle-hdwallet-provider-klaytn.

Configuring truffle-hdwallet-provider-klaytn

truffle-hdwallet-provider-klaytn is a JavaScript HD wallet provider forked from truffle-hdwallet-provider.
Install as the following:
1
$ nvm use 10
2
$ yarn install [email protected]
Copied!
1
$ nvm use 12 # for node v12 and higher
2
$ yarn install [email protected]
Copied!
Set truffle-config.js as below.

Using a mnemonic

1
const HDWalletProvider = require("truffle-hdwallet-provider-klaytn");
2
3
const mnemonic = "mountains supernatural bird ...";
4
5
module.exports = {
6
networks: {
7
development: {
8
host: "localhost",
9
port: 8551,
10
network_id: "*", // Match any network id
11
},
12
klaytn: {
13
provider: () => {
14
const mnemonic = JSON.parse(
15
fs.readFileSync(path.resolve(__dirname) + "/mnemonics.js")
16
);
17
18
return new HDWalletProvider(
19
mnemonic,
20
"http://localhost:8551",
21
0,
22
mnemonic.length
23
);
24
},
25
network_id: "1001", //Klaytn baobab testnet's network id
26
gas: "8500000",
27
gasPrice: null,
28
},
29
kasBaobab: {
30
provider: () => {
31
const option = {
32
headers: [
33
{
34
name: "Authorization",
35
value:
36
"Basic " +
37
Buffer.from(accessKeyId + ":" + secretAccessKey).toString(
38
"base64"
39
),
40
},
41
{ name: "x-chain-id", value: "1001" },
42
],
43
keepAlive: false,
44
};
45
return new HDWalletProvider(
46
mnemonic,
47
new Caver.providers.HttpProvider(
48
"https://node-api.klaytnapi.com/v1/klaytn",
49
option
50
)
51
);
52
},
53
network_id: "1001", //Klaytn baobab testnet's network id
54
gas: "8500000",
55
gasPrice: "25000000000",
56
},
57
kasCypress: {
58
provider: () => {
59
const option = {
60
headers: [
61
{
62
name: "Authorization",
63
value:
64
"Basic " +
65
Buffer.from(accessKeyId + ":" + secretAccessKey).toString(
66
"base64"
67
),
68
},
69
{ name: "x-chain-id", value: "8217" },
70
],
71
keepAlive: false,
72
};
73
return new HDWalletProvider(
74
cypressMnemonic,
75
new Caver.providers.HttpProvider(
76
"https://node-api.klaytnapi.com/v1/klaytn",
77
option
78
)
79
);
80
},
81
network_id: "8217", //Klaytn baobab testnet's network id
82
gas: "8500000",
83
gasPrice: "25000000000",
84
},
85
baobab: {
86
provider: () => {
87
return new HDWalletProvider(mnemonic, "http://your.baobab.en:8551");
88
},
89
network_id: "1001", //Klaytn baobab testnet's network id
90
gas: "8500000",
91
gasPrice: null,
92
},
93
cypress: {
94
provider: () => {
95
return new HDWalletProvider(mnemonic, "http://your.cypress.en:8551");
96
},
97
network_id: "8217", //Klaytn mainnet's network id
98
gas: "8500000",
99
gasPrice: null,
100
},
101
},
102
};
Copied!

Using a private key

1
const HDWalletProvider = require("truffle-hdwallet-provider-klaytn");
2
3
const privateKey = "0x123 ...";
4
5
module.exports = {
6
networks: {
7
development: {
8
host: "localhost",
9
port: 8551,
10
network_id: "*", // Match any network id
11
},
12
klaytn: {
13
provider: () => {
14
const pks = JSON.parse(
15
fs.readFileSync(path.resolve(__dirname) + "/privateKeys.js")
16
);
17
18
return new HDWalletProvider(
19
pks,
20
"http://localhost:8551",
21
0,
22
pks.length
23
);
24
},
25
network_id: "1001", //Klaytn baobab testnet's network id
26
gas: "8500000",
27
gasPrice: null,
28
},
29
kasBaobab: {
30
provider: () => {
31
const option = {
32
headers: [
33
{
34
name: "Authorization",
35
value:
36
"Basic " +
37
Buffer.from(accessKeyId + ":" + secretAccessKey).toString(
38
"base64"
39
),
40
},
41
{ name: "x-chain-id", value: "1001" },
42
],
43
keepAlive: false,
44
};
45
return new HDWalletProvider(
46
privateKey,
47
new Caver.providers.HttpProvider(
48
"https://node-api.klaytnapi.com/v1/klaytn",
49
option
50
)
51
);
52
},
53
network_id: "1001", //Klaytn baobab testnet's network id
54
gas: "8500000",
55
gasPrice: "25000000000",
56
},
57
kasCypress: {
58
provider: () => {
59
const option = {
60
headers: [
61
{
62
name: "Authorization",
63
value:
64
"Basic " +
65
Buffer.from(accessKeyId + ":" + secretAccessKey).toString(
66
"base64"
67
),
68
},
69
{ name: "x-chain-id", value: "8217" },
70
],
71
keepAlive: false,
72
};
73
return new HDWalletProvider(
74
cypressPrivateKey,
75
new Caver.providers.HttpProvider(
76
"https://node-api.klaytnapi.com/v1/klaytn",
77
option
78
)
79
);
80
},
81
network_id: "8217", //Klaytn baobab testnet's network id
82
gas: "8500000",
83
gasPrice: "25000000000",
84
},
85
baobab: {
86
provider: () => {
87
return new HDWalletProvider(privateKey, "http://your.baobab.en:8551");
88
},
89
network_id: "1001", //Klaytn baobab testnet's network id
90
gas: "8500000",
91
gasPrice: null,
92
},
93
cypress: {
94
provider: () => {
95
return new HDWalletProvider(privateKey, "http://your.cypress.en:8551");
96
},
97
network_id: "8217", //Klaytn mainnet's network id
98
gas: "8500000",
99
gasPrice: null,
100
},
101
},
102
};
Copied!
WARNING: Be very careful not to expose your mneomonic or private key.
Deploying on Klaytn :
1
$ truffle deploy --network baobab # testnet
2
$ truffle deploy --network cypress # mainnet
Copied!
Making transaction on Klaytn : (using an example from Truffle Docs quick start - Creating a project)
1
$ truffle console --network baobab
2
truffle(baobab)> Migrations.deployed().then(function(instance) {return instance.setCompleted(3)}) // making transaction
3
{
4
tx: '0x734676311194c1ab8e004e2990e414b7b47a9d0a8506682707f5db03fa6dcee0',
5
receipt: {
6
blockHash: '0xdf9d77ef893a70b3a3f073525cdf5b2ee36620a3ac81815437788e4cf121678d',
7
blockNumber: 65284860,
8
contractAddress: null,
9
from: '0x50c82047a414d2aad88ae67a5f02c311d2d86e69',
10
gas: '0x500000',
11
gasPrice: '0x5d21dba00',
12
gasUsed: 27001,
13
input: '0xfdacd5760000000000000000000000000000000000000000000000000000000000000003',
14
logs: [],
15
logsBloom: '0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000',
16
nonce: '0x1047',
17
senderTxHash: '0x734676311194c1ab8e004e2990e414b7b47a9d0a8506682707f5db03fa6dcee0',
18
signatures: [ [Object] ],
19
status: true,
20
to: '0x69527b5f0078ae1757b631af155fa9be21ef6a85',
21
transactionHash: '0x734676311194c1ab8e004e2990e414b7b47a9d0a8506682707f5db03fa6dcee0',
22
transactionIndex: 0,
23
type: 'TxTypeLegacyTransaction',
24
typeInt: 0,
25
value: '0x0',
26
cumulativeGasUsed: undefined,
27
rawLogs: []
28
},
29
logs: []
30
}
31
32
truffle(baobab)> Migrations.deployed().then(function(instance) {return instance.last_completed_migration.call()}) // read public variable
33
BN { negative: 0, words: [ 3, <1 empty item> ], length: 1, red: null }
Copied!
Last modified 2mo ago