LogoLogo
Getting StartedSmart ContractNodeBApp Developers
  • Welcome
  • Klaytn Overview
    • Why Klaytn
    • Klaytn Design
      • Consensus Mechanism
      • Accounts
      • Transactions
        • Basic
        • Fee Delegation
        • Partial Fee Delegation
      • Computation
        • Klaytn Smart Contract
        • Execution Model
        • Computation Cost
        • Klaytn Virtual Machine
      • Storage
      • Transaction Fees
      • Klaytn native coin - KLAY
      • Token Economy
      • Governance
      • Multi-Channel
      • KNI
    • Scaling Solutions
  • Getting Started
    • Quick Start
      • Launch an Endpoint Node
      • Top up your Account
      • Install Development Tools
      • Deploy a Smart Contract
      • Check the Deployment
    • Account Management
      • Creating Accounts
      • Managing Accounts
    • Development Environment
    • Getting KLAY
  • Smart Contract
    • Solidity - Smart Contract Language
    • Precompiled Contracts
    • IDE and Tools
      • Klaytn IDE
      • Truffle
    • Sample Contracts
      • KlaytnGreeter
      • ERC-20
        • 1. Writing ERC-20 Smart Contract
        • 2. Deploying Smart Contract
        • 3. Interacting with ERC-20 token from Klaytn Wallet
      • ERC-721
        • 1. Writing ERC-721 Smart Contract
        • 2. Deploying Smart Contract
    • Testing Guide
    • Deployment Guide
    • Klaytn Compatible Tokens
    • Porting Ethereum Contract
  • Installation & Operation
    • Endpoint Node
      • System Requirements
      • Installation Guide
        • Download
        • Installation Guide
        • Configuration
        • Startup the EN
        • Testing the Installation
      • Operation Guide
        • Starting/Stopping EN
        • Configuration
        • Errors & Troubleshooting
      • ken CLI commands
      • JSON-RPC APIs
    • Core Cell
      • System Requirements
      • Network Configuration
      • Installation Guide
        • Download
        • Before You Install
        • Consensus Node Setup
          • Installation Guide
          • Configuration
          • Startup the CN
        • Proxy Node Setup
          • Installation Guide
          • Configuration
          • Startup the PN
        • Testing the Core Cell
      • Operation Guide
        • Starting/Stopping Core Cell
        • Configuration
        • Errors & Troubleshooting
      • Monitoring Setup
      • H/A Setup
    • Service Chain
      • Getting Started
        • Setting up a 4-node Service Chain
        • Connecting to Baobab
        • Cross-Chain Value Transfer
      • Reference Manuals
        • System Requirements
        • Download
        • SCN User Guide
          • Installation
          • Configuration
          • Starting/Stopping SCN
          • Checking Node Status
          • kscn commands
          • homi commands
        • SPN/SEN User Guide
          • Installation
          • Configuration
          • Starting/Stopping Node
          • Checking Node Status
        • Bridge Configuration
        • Anchoring
        • KAS Anchoring
        • Value Transfer
        • Configuration Files
        • Log Files
        • Genesis JSON
      • How-To Guides
    • Errors & Troubleshooting
    • Download
      • v1.7.0
      • v1.6.4
      • v1.6.3
      • v1.6.2
      • v1.6.1
      • v1.6.0
      • v1.5.3
      • v1.5.2
      • v1.5.1
      • v1.5.0
      • v1.4.2
      • v1.4.1
      • v1.4.0
      • v1.3.0
      • v1.2.0
      • v1.1.1
      • v1.0.0
      • v0.9.6
      • v0.8.2
  • BApp Developers
    • JSON-RPC APIs
      • API references
        • klay
          • Account
          • Block
          • Transaction
            • Working with Klaytn Transaction Types
          • Configuration
          • Filter
          • Miscellaneous
        • net
        • debug
          • Logging
          • Profiling
          • Runtime Tracing
          • Runtime Debugging
          • VM Tracing
          • VM Standard Tracing
          • Blockchain Inspection
        • admin
        • personal
        • txpool
        • governance
      • Service Chain API references
        • mainbridge
        • subbridge
      • Transaction Error Codes
    • Klaytn SDKs
      • caver-js
        • Getting Started
        • API references
          • caver.account
          • caver.wallet
            • caver.wallet.keyring
          • caver.transaction
            • Basic
            • Fee Delegation
            • Partial Fee Delegation
          • caver.rpc
            • caver.rpc.klay
            • caver.rpc.net
            • caver.rpc.governance
          • caver.contract
          • caver.abi
          • caver.kct
            • caver.kct.kip7
            • caver.kct.kip17
            • caver.kct.kip37
          • caver.validator
          • caver.utils
          • caver.ipfs
        • caver-js ~v1.4.1
          • Getting Started (~v1.4.1)
          • API references
            • caver.klay
              • Account
              • Block
              • Transaction
                • Legacy
                • Value Transfer
                • Value Transfer Memo
                • Account Update
                • Smart Contract Deploy
                • Smart Contract Execution
                • Cancel
              • Configuration
              • Filter
              • Miscellaneous
            • caver.klay.net
            • caver.klay.accounts
            • caver.klay.Contract
            • caver.klay.KIP7
            • caver.klay.KIP17
            • caver.klay.abi
            • caver.utils (~v1.4.1)
          • Porting from web3.js
      • caver-java
        • Getting Started
        • API references
        • caver-java ~v1.4.0
          • Getting Started (~v1.4.0)
          • Porting from web3j
    • Tutorials
      • Fee Delegation Example
      • BApp on Baobab (Video Lecture)
        • 1. Introduction
        • 2. Weakness of Blockchain Platform
        • 3. Understanding Klaytn
        • 4. Developing an Addition Game with Klaytn Tools
        • 5. Front-end for Klaytn Addition Game Development
        • 6. Summary
      • Count BApp
        • 1. Environment Setup
        • 2. Clone Count BApp
        • 3. Directory Structure
        • 4. Write Smart Contract
        • 5. Frontend Code Overview
          • 5-1. Blocknumber Component
          • 5-2. Auth Component
          • 5-3. Count Component
        • 6. Deploy Contract
        • 7. Run App
      • Klaystagram
        • 1. Environment Setup
        • 2. Clone Klaystagram BApp
        • 3. Directory Structure
        • 4. Write Klaystagram Smart Contract
        • 5. Deploy Contract
        • 6. Frontend Code Overview
        • 7. FeedPage
          • 7-1. Connect Contract to Frontend
          • 7-2. UploadPhoto Component
          • 7-3. Feed Component
          • 7-4. TransferOwnership Component
        • 8. Run App
      • Migrating Ethereum App to Klaytn
      • Connecting MetaMask
      • Connecting Remix
    • Developer Tools
      • Kaikas
      • Klaytn Wallet
      • Klaytnscope
  • ---
    • Vision
      • Klaytn BApp (Blockchain App)
      • Value Proposition
      • Open Source
    • FAQ
    • Glossary
    • Terms of Use
    • Languages
Powered by GitBook
On this page
  • Compatibility with Truffle
  • Configuring truffle-hdwallet-provider-klaytn
  • Using a mnemonic
  • Using a private key

Was this helpful?

  1. Smart Contract
  2. IDE and Tools

Truffle

PreviousKlaytn IDENextSample Contracts

Last updated 3 years ago

Was this helpful?

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:

$ sudo npm install -g truffle

If you have a local EN running, you can deploy contracts directly with truffle framework. For more details, refer to this .

If you want to deploy with a remote EN node, you should use .

Configuring truffle-hdwallet-provider-klaytn

truffle-hdwallet-provider-klaytn is a JavaScript HD wallet provider forked from truffle-hdwallet-provider.

Install as the following:

$ nvm use 10
$ yarn install truffle-hdwallet-provider-klaytn@1.0.18
$ nvm use 12 # for node v12 and higher
$ yarn install truffle-hdwallet-provider-klaytn@1.4.1

Set truffle-config.js as below.

Using a mnemonic

const HDWalletProvider = require("truffle-hdwallet-provider-klaytn");

const mnemonic = "mountains supernatural bird ...";

module.exports = {
  networks: {
    development: {
      host: "localhost",
      port: 8551,
      network_id: "*", // Match any network id
    },
    klaytn: {
      provider: () => {
        const mnemonic = JSON.parse(
          fs.readFileSync(path.resolve(__dirname) + "/mnemonics.js")
        );

        return new HDWalletProvider(
          mnemonic,
          "http://localhost:8551",
          0,
          mnemonic.length
        );
      },
      network_id: "1001", //Klaytn baobab testnet's network id
      gas: "8500000",
      gasPrice: null,
    },
    kasBaobab: {
      provider: () => {
        const option = {
          headers: [
            {
              name: "Authorization",
              value:
                "Basic " +
                Buffer.from(accessKeyId + ":" + secretAccessKey).toString(
                  "base64"
                ),
            },
            { name: "x-chain-id", value: "1001" },
          ],
          keepAlive: false,
        };
        return new HDWalletProvider(
          mnemonic,
          new Caver.providers.HttpProvider(
            "https://node-api.klaytnapi.com/v1/klaytn",
            option
          )
        );
      },
      network_id: "1001", //Klaytn baobab testnet's network id
      gas: "8500000",
      gasPrice: "25000000000",
    },
    kasCypress: {
      provider: () => {
        const option = {
          headers: [
            {
              name: "Authorization",
              value:
                "Basic " +
                Buffer.from(accessKeyId + ":" + secretAccessKey).toString(
                  "base64"
                ),
            },
            { name: "x-chain-id", value: "8217" },
          ],
          keepAlive: false,
        };
        return new HDWalletProvider(
          cypressMnemonic,
          new Caver.providers.HttpProvider(
            "https://node-api.klaytnapi.com/v1/klaytn",
            option
          )
        );
      },
      network_id: "8217", //Klaytn baobab testnet's network id
      gas: "8500000",
      gasPrice: "25000000000",
    },
    baobab: {
      provider: () => {
        return new HDWalletProvider(mnemonic, "http://your.baobab.en:8551");
      },
      network_id: "1001", //Klaytn baobab testnet's network id
      gas: "8500000",
      gasPrice: null,
    },
    cypress: {
      provider: () => {
        return new HDWalletProvider(mnemonic, "http://your.cypress.en:8551");
      },
      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: 8551,
      network_id: "*", // Match any network id
    },
    klaytn: {
      provider: () => {
        const pks = JSON.parse(
          fs.readFileSync(path.resolve(__dirname) + "/privateKeys.js")
        );

        return new HDWalletProvider(
          pks,
          "http://localhost:8551",
          0,
          pks.length
        );
      },
      network_id: "1001", //Klaytn baobab testnet's network id
      gas: "8500000",
      gasPrice: null,
    },
    kasBaobab: {
      provider: () => {
        const option = {
          headers: [
            {
              name: "Authorization",
              value:
                "Basic " +
                Buffer.from(accessKeyId + ":" + secretAccessKey).toString(
                  "base64"
                ),
            },
            { name: "x-chain-id", value: "1001" },
          ],
          keepAlive: false,
        };
        return new HDWalletProvider(
          privateKey,
          new Caver.providers.HttpProvider(
            "https://node-api.klaytnapi.com/v1/klaytn",
            option
          )
        );
      },
      network_id: "1001", //Klaytn baobab testnet's network id
      gas: "8500000",
      gasPrice: "25000000000",
    },
    kasCypress: {
      provider: () => {
        const option = {
          headers: [
            {
              name: "Authorization",
              value:
                "Basic " +
                Buffer.from(accessKeyId + ":" + secretAccessKey).toString(
                  "base64"
                ),
            },
            { name: "x-chain-id", value: "8217" },
          ],
          keepAlive: false,
        };
        return new HDWalletProvider(
          cypressPrivateKey,
          new Caver.providers.HttpProvider(
            "https://node-api.klaytnapi.com/v1/klaytn",
            option
          )
        );
      },
      network_id: "8217", //Klaytn baobab testnet's network id
      gas: "8500000",
      gasPrice: "25000000000",
    },
    baobab: {
      provider: () => {
        return new HDWalletProvider(privateKey, "http://your.baobab.en:8551");
      },
      network_id: "1001", //Klaytn baobab testnet's network id
      gas: "8500000",
      gasPrice: null,
    },
    cypress: {
      provider: () => {
        return new HDWalletProvider(privateKey, "http://your.cypress.en:8551");
      },
      network_id: "8217", //Klaytn mainnet's network id
      gas: "8500000",
      gasPrice: null,
    },
  },
};

WARNING: Be very careful not to expose your mneomonic or private key.

Deploying on Klaytn :

$ truffle deploy --network baobab  # testnet
$ truffle deploy --network cypress # mainnet
$ truffle console --network baobab
truffle(baobab)> Migrations.deployed().then(function(instance) {return instance.setCompleted(3)}) // making transaction
{
  tx: '0x734676311194c1ab8e004e2990e414b7b47a9d0a8506682707f5db03fa6dcee0',
  receipt: {
    blockHash: '0xdf9d77ef893a70b3a3f073525cdf5b2ee36620a3ac81815437788e4cf121678d',
    blockNumber: 65284860,
    contractAddress: null,
    from: '0x50c82047a414d2aad88ae67a5f02c311d2d86e69',
    gas: '0x500000',
    gasPrice: '0x5d21dba00',
    gasUsed: 27001,
    input: '0xfdacd5760000000000000000000000000000000000000000000000000000000000000003',
    logs: [],
    logsBloom: '0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000',
    nonce: '0x1047',
    senderTxHash: '0x734676311194c1ab8e004e2990e414b7b47a9d0a8506682707f5db03fa6dcee0',
    signatures: [ [Object] ],
    status: true,
    to: '0x69527b5f0078ae1757b631af155fa9be21ef6a85',
    transactionHash: '0x734676311194c1ab8e004e2990e414b7b47a9d0a8506682707f5db03fa6dcee0',
    transactionIndex: 0,
    type: 'TxTypeLegacyTransaction',
    typeInt: 0,
    value: '0x0',
    cumulativeGasUsed: undefined,
    rawLogs: []
  },
  logs: []
}

truffle(baobab)> Migrations.deployed().then(function(instance) {return instance.last_completed_migration.call()}) // read public variable
BN { negative: 0, words: [ 3, <1 empty item> ], length: 1, red: null }

Making transaction on Klaytn : (using an example from )

Truffle Docs quick start - Creating a project
Truffle overview
Truffle repository
truffle-hdwallet-provider-klaytn
link