Execute Smart Contract

This page introduces how to send the transaction that executes a smart contract to Klaytn with Wallet API.

Before Getting Started

  • The x-chain-id value for calling the API is 8217 (Cypress) or 1001 (Baobab).

  • Essential parameters for calling APIs are described in individual examples.

Values that a user needs to enter for calling APIs will be represented with one pair of braces ({}). A user must enter the following values.

Item

Description

Note

chain-id

8217 or 1001

Cypress(Klaytn mainnet) or Baobab(Klaytn testnet

access-key-id

Auth ID

accessKeyId obtained from KAS Console - Security - Credential

secret-access-key

Auth Password

secretAccessKey obtained from KAS Console - Security - Credential

krn

(optional) ID of Account Pool

Unnecessary when using Default Account Pool

A KAS API Authentication Key (API Auth Key) provides access to all KAS services and all the rights to a Klaytn account which was created by calling Wallet API via this API Auth Key. The rights here include accessing and transferring all the assets (KLAY, etc.) of or sending a transaction from a Klaytn account. If you shared your API Auth Key with any unauthorized personnel, your Klaytn account could be compromised and might cause unwanted transaction execution.

DO NOT share your API Auth Key (Secret AccessKey or Authorization) with any unauthorized personnel. DO PUT efforts necessary to keep your API Auth Key safe for the security of your KAS/Klaytn account.

Execute Smart Contract Functions: Direct Payment of Transaction Fee

A user may execute or call a smart contract function using KAS. The smart contract function execution records data in a smart contract or changes data stored in a smart contract. The smart contract function call only reads data stored in a smart contract. For more details on the smart contract function execution and call, refer to the followings. A smart contract must have been deployed to Klaytn Node before you execute a smart contract function. For more details on deploying a smart contract, refer to the followings.

To execute a smart contract function, the bytecode of the execution function is required. The bytecode of the function can be obtained by compiling the contract code using the Solidity Compiler when deploying the smart contract. To executes a smart contract to KAS, the user must send the transaction using KAS to Klaytn Node and pay the transaction fee for this. To call the API, he/she must first create an Account Pool and account, and then select an account to use. In this example, the executing account must directly pay the transaction fee for executing the smart contract.

For details about KAS SDK (caver-js/caver-java extensions) installation and execution, please visit KAS SDK. For more details on creating an Account Pool, account, and selecting an account, refer to Getting Started.

API Request

Call the smart contract execution API. You may use the REST API or KAS SDKs (caver-js, caver-java extensions) for this.

curl --location --request POST "https://wallet-api.klaytnapi.com/v2/tx/contract/execute" \
-u {access-key-id}:{secret-access-key} \
--header "x-chain-id: {chain-id}" \
--header "Content-Type: application/json" \
--data-raw "{
  "from": "0x5bb85d4032354E88020595AFAFC081C24098202e",
  "value": "0x0",
  "to": "0x71ca8095264fdf58f1a2007e0f91140d67292736",
  "input": "0xd14e62b80000000000000000000000000000000000000000000000000000000000000005",
  "nonce": 0,
  "gasLimit": 1000000,
  "submit": true
}"
  • input: The bytecode of smart contract to be executed. It can be obtained by compiling this smart contract that is written with Solidity using the Solidity compiler

  • submit: If it is false, a transaction is not sent and the RLP of signed transaction and the transaction information are returned.

API Response

Here is the response of the smart contract execution API.

{
  "from": "0xa4068f0d4e4ffad5945824cddd748ba2dc450330",
  "gasLimit": 1000000,
  "gasPrice": "0x5d21dba00",
  "input": "0xd14e62b80000000000000000000000000000000000000000000000000000000000000005",
  "nonce": 3,
  "rlp": "0x30f8a4038505d21dba00830f4240940a0ca70bd0074160874dc9337ca7ff8049b2e63e8094a4068f0d4e4ffad5945824cddd748ba2dc450330a4d14e62b80000000000000000000000000000000000000000000000000000000000000005f847f8458207f5a002b41abdf77b3d943c3c9000be45144ccc8cd25a75e523705aca3621ffdd4dd8a01bbf357d02e5f4bbc015cf35107e150d50094ffcb8fccbb7db981c739149eadb",
  "signatures": [
    {
      "R": "0x2b41abdf77b3d943c3c9000be45144ccc8cd25a75e523705aca3621ffdd4dd8",
      "S": "0x1bbf357d02e5f4bbc015cf35107e150d50094ffcb8fccbb7db981c739149eadb",
      "V": "0x7f5"
    }
  ],
  "status": "Submitted",
  "to": "0x0a0ca70bd0074160874dc9337ca7ff8049b2e63e",
  "transactionHash": "0x039e174f22e402caae753396d58e68e47062d8d32be1ae988bb1ddea7134afc0",
  "typeInt": 48,
  "value": "0x0"
}

For details about this API, please visit here. For inquires about this document or KAS, please visit KAS Developer forum.

Execute Smart Contract Functions: Transaction Fee Delegation by User

A user may execute or call a smart contract function using KAS. The smart contract function execution records data in a smart contract or changes data stored in a smart contract. The smart contract function call only reads data stored in a smart contract. For more details on the smart contract function execution and call, refer to the followings. A smart contract must have been deployed to Klaytn Node before you execute a smart contract function. For more details on deploying a smart contract, refer to the followings.

To execute a smart contract function, the bytecode of the execution function is required. The bytecode of the function can be obtained by compiling the contract code using the Solidity Compiler when deploying the smart contract. To executes a smart contract to KAS, the user must send the transaction using KAS to Klaytn Node and pay the transaction fee for this. To call this API, you first need to create the Klaytn account who sends this transaction and the Klaytn account who pays the transaction fee (fee-payer) in Account Pool and Fee Payer Account Pool, each. In this example, the fee payer account pays the transaction transmission fee, not the account who sends this transaction. For details about fee-delegation methods that KAS provides for you, please visit here.

For details about KAS SDK (caver-js/caver-java extensions) installation and execution, please visit KAS SDK. For more details on creating an Account Pool, account, and selecting an account, refer to Getting Started. For more details on creating an fee payer Account Pool, fee payer account, and selecting an fee payer account, refer to Getting Started.

API Request

Call the smart contract execution with fee-delegation API. You may use the REST API or KAS SDKs (caver-js, caver-java extensions) for this.

curl --location --request POST "https://wallet-api.klaytnapi.com/v2/tx/fd-user/contract/execute" \
-u {access-key-id}:{secret-access-key} \
--header "x-chain-id: {chain-id}" \
--header "Content-Type: application/json" \
--data-raw "{
  "from": "0x5bb85d4032354E88020595AFAFC081C24098202e",
  "value": "0x0",
  "to": "0x71ca8095264fdf58f1a2007e0f91140d67292736",
  "input": "0xd14e62b80000000000000000000000000000000000000000000000000000000000000005",
  "nonce": 0,
  "gasLimit": 1000000,
  "submit": true,
  "feePayer": "0x85B98485444c89880cD9C48807CEF727C296F2da",
  "feeRatio": 10
}"
  • input: The bytecode of smart contract to be executed. It can be obtained by compiling this smart contract that is written with Solidity using the Solidity compiler

  • submit: If it is false, a transaction is not sent and the RLP of signed transaction and the transaction information are returned.

  • fee_payer: If a user uses a separate transaction fee-payer account, he/she provides the account address to this parameter. Do not use the "feePayer" parameter to make KAS pay for the transaction fee first.

  • feeRatio: The ratio (1%~99%) of the transaction fee that a feePayer pays. The remaining fees will be directly paid by the account (from) that sends transactions to Klaytn.

    • This parameter is available for fee delegation through either a user fee-payer account or and KAS (KAS Global fee payer)

API Response

Here is a response of the smart contract execution with fee-delegation API.

{
  "feePayer": "0xe8ab1729ab614551021cf5cc22c0e037f5a82930",
  "from": "0x9c56b45b7443bc73f47234199982481c64807f78",
  "gasLimit": 1000000,
  "gasPrice": "0x5d21dba00",
  "input": "0xd14e62b80000000000000000000000000000000000000000000000000000000000000005",
  "nonce": 2,
  "rlp": "0x31f90102028505d21dba00830f424094182efef646797698059fb0e6cbb4823687d869d880949c56b45b7443bc73f47234199982481c64807f78a4d14e62b80000000000000000000000000000000000000000000000000000000000000005f847f8458207f5a03a5ff2dfb032a774d84af240a2aeacf4ac4bcf428de4f1f5adc6f4c003a96794a053e39aa65cd1006136be9ade19413e5bfd53c5575b40d5f32aa7e19037003df794e8ab1729ab614551021cf5cc22c0e037f5a82930f847f8458207f5a029cb2ac282e5880ef0ed8bf1bfca08f51ae151757ff50cdf08eccb23600c4ee4a048aedf8ef1415a3a7d32358f0d45d7223fd37532eafb8c4937828b2a284b80e3",
  "signatures": [
    {
      "R": "0x3a5ff2dfb032a774d84af240a2aeacf4ac4bcf428de4f1f5adc6f4c003a96794",
      "S": "0x53e39aa65cd1006136be9ade19413e5bfd53c5575b40d5f32aa7e19037003df7",
      "V": "0x7f5"
    }
  ],
  "status": "Submitted",
  "to": "0x182efef646797698059fb0e6cbb4823687d869d8",
  "transactionHash": "0x4fcb4c06f81676d6b811f5b867f3ab5d166014e6c1dd982fec037eafdf89c88e",
  "typeInt": 49,
  "value": "0x0"
}

“typeInt” variable is a value for identifying the transaction type. For details about the types of transactions, please visit here.

For details about this API, please visit here. For inquires about this document or KAS, please visit KAS Developer forum.

Execute Smart Contract Functions: Transaction Fee Delegation by KAS

A user may execute or call a smart contract function using KAS. The smart contract function execution records data in a smart contract or changes data stored in a smart contract. The smart contract function call only reads data stored in a smart contract. For more details on the smart contract function execution and call, refer to the followings. A smart contract must have been deployed to Klaytn Node before you execute a smart contract function. For more details on deploying a smart contract, refer to the followings.

To execute a smart contract function, the bytecode of the execution function is required. The bytecode of the function can be obtained by compiling the contract code using the Solidity Compiler when deploying the smart contract. To executes a smart contract to KAS, the user must send the transaction using KAS to Klaytn Node and pay the transaction fee for this. To call this API, you first need to create an Account Pool and a Klaytn account who sends this transaction in that Account Pool. In this example, the KAS global fee payer account pays the transaction transmission fee, not the account who sends this transaction. This fee paid by KAS global fee payer instead of the Klaytn account who sent a transaction will later be charged to your KAS account. For details about fee-delegation methods that KAS provides for you, please visit here.

For details about KAS SDK (caver-js/caver-java extensions) installation and execution, please visit KAS SDK. For more details on creating an Account Pool, account, and selecting an account, refer to Getting Started.

API Request

Call the smart contract execution with KAS GlobalFeePayer fee-delegation API. You may use the REST API or KAS SDKs (caver-js, caver-java extensions) for this.

curl --location --request POST "https://wallet-api.klaytnapi.com/v2/tx/fd/contract/execute" \
-u {access-key-id}:{secret-access-key} \
--header "x-chain-id: {chain-id}" \
--header "Content-Type: application/json" \
--data-raw "{
  "from": "0x5bb85d4032354E88020595AFAFC081C24098202e",
  "value": "0x0",
  "to": "0x71ca8095264fdf58f1a2007e0f91140d67292736",
  "input": "0xd14e62b80000000000000000000000000000000000000000000000000000000000000005",
  "nonce": 0,
  "gasLimit": 1000000,
  "submit": true,
  "feePayer": "0x85B98485444c89880cD9C48807CEF727C296F2da",
  "feeRatio": 10
}"
  • input: The bytecode of smart contract to be executed. It can be obtained by compiling this smart contract that is written with Solidity using the Solidity compiler

  • submit: If it is false, a transaction is not sent and the RLP of signed transaction and the transaction information are returned.

  • feeRatio: The ratio (1%~99%) of the transaction fee that a feePayer pays. The remaining fees will be directly paid by the account (from) that sends transactions to Klaytn.

    • This parameter is available for fee delegation through either a user fee-payer account or and KAS (KAS Global fee payer)

API Response

Here is a response of the smart contract execution with KAS GlobalFeePayer fee-delegation API.

{
  "feePayer": "0x85b98485444c89880cd9c48807cef727c296f2da",
  "feeRatio": 10,
  "from": "0x9c56b45b7443bc73f47234199982481c64807f78",
  "gasLimit": 1000000,
  "gasPrice": "0x5d21dba00",
  "input": "0xd14e62b80000000000000000000000000000000000000000000000000000000000000005",
  "nonce": 2,
  "rlp": "0x31f90102028505d21dba00830f424094182efef646797698059fb0e6cbb4823687d869d880949c56b45b7443bc73f47234199982481c64807f78a4d14e62b80000000000000000000000000000000000000000000000000000000000000005f847f8458207f5a03a5ff2dfb032a774d84af240a2aeacf4ac4bcf428de4f1f5adc6f4c003a96794a053e39aa65cd1006136be9ade19413e5bfd53c5575b40d5f32aa7e19037003df794e8ab1729ab614551021cf5cc22c0e037f5a82930f847f8458207f5a029cb2ac282e5880ef0ed8bf1bfca08f51ae151757ff50cdf08eccb23600c4ee4a048aedf8ef1415a3a7d32358f0d45d7223fd37532eafb8c4937828b2a284b80e3",
  "signatures": [
    {
      "R": "0x3a5ff2dfb032a774d84af240a2aeacf4ac4bcf428de4f1f5adc6f4c003a96794",
      "S": "0x53e39aa65cd1006136be9ade19413e5bfd53c5575b40d5f32aa7e19037003df7",
      "V": "0x7f5"
    }
  ],
  "status": "Submitted",
  "to": "0x182efef646797698059fb0e6cbb4823687d869d8",
  "transactionHash": "0x4fcb4c06f81676d6b811f5b867f3ab5d166014e6c1dd982fec037eafdf89c88e",
  "typeInt": 49,
  "value": "0x0"
}

“typeInt” variable is a value for identifying the transaction type. For details about the types of transactions, please visit here.

For details about this API, please visit here. For inquires about this document or KAS, please visit KAS Developer forum.

Last updated