Truffle is a good framework for deploying and managing your smart contracts, typically on the Ethereum network. In Klaytn you can use Truffle framework as well.
case 1. Running a local node
If you are running your own local EN(endpoint node), you can configure Truffle as below and use it right away.
// truffle-config.js
module.exports = {
networks: {
klaytn: {
host: '127.0.0.1',
port: 8551,
from: '0x75a59b94889a05c03c66c3c84e9d2f8308ca4abd', // enter your account address
network_id: '1001', // Baobab network id
gas: 20000000, // transaction gas limit
gasPrice: 25000000000, // gasPrice of Baobab is 25 Gpeb
},
},
compilers: {
solc: {
version: "0.5.6" // Specify compiler's version to 0.5.6
}
}
};
case 2. Using a public node
Install truffle-hdwallet-provider-klaytn
If are using a public or remote node, you need to install truffle-hdwallet-provider-klaytn
(link). You can install via npm as the following.
$ npm install truffle-hdwallet-provider-klaytn
Configure Truffle
Depending on if you want to use a mnemonic or a plain private key, configure Truffle as one of the following.
Using a mnemonic
const HDWalletProvider = require("truffle-hdwallet-provider-klaytn");
const mnemonic = "mountains supernatural bird ...";
module.exports = {
networks: {
development: {
host: "localhost",
port: 8545,
network_id: "*" // Match any network id
},
testnet: {
provider: () => new HDWalletProvider(mnemonic, "https://api.baobab.klaytn.net:8651"),
network_id: '1001', //Klaytn baobab testnet's network id
gas: '8500000',
gasPrice: null
},
mainnet: {
provider: () => new HDWalletProvider(mnemonic, "https://api.cypress.klaytn.net:8651"),
network_id: '8217', //Klaytn mainnet's network id
gas: '8500000',
gasPrice: null
}
}
};
Using a private key
const HDWalletProvider = require("truffle-hdwallet-provider-klaytn");
const privateKey = "0x123 ...";
module.exports = {
networks: {
development: {
host: "localhost",
port: 8545,
network_id: "*" // Match any network id
},
testnet: {
provider: () => new HDWalletProvider(privateKey, "https://api.baobab.klaytn.net:8651"),
network_id: '1001', //Klaytn baobab testnet's network id
gas: '8500000',
gasPrice: null
},
mainnet: {
provider: () => new HDWalletProvider(privateKey, "https://api.cypress.klaytn.net:8651"),
network_id: '8217', //Klaytn mainnet's network id
gas: '8500000',
gasPrice: null
}
}
};
Warning: In the above examples, keys are hardcoded in your configuration file. In production, you do not want to do this for the sake of security. Make sure you find a way to keep your keys safe.
Deploy contracts
Deploying on Klaytn testnet
$ truffle deploy --network testnet
Deploying on Klaytn mainnet
$ truffle deploy --network mainnet