KAI Integration for Exchanges
Related Documents:JSON API here ,JSON RPC APIs examples here,JavaScript SDK, Kardia_Golang_SDK.
Public Mainnet RPC endpoint: https://rpc.kardiachain.io

Initialize

Javascript:

KardiaChain JavaScript SDK is published at npm. The installation guide and examples are available there, we need the RPC endpoint of KardiaChain Mainnet to interact with these modules.
1
import KardiaClient from 'kardia-js-sdk';
2
3
const endpoint = "https://rpc.kardiachain.io";
4
const kardiaClient = new KardiaClient({ endpoint });
Copied!

Golang:

Kardia Golang SDK provides the same set of interacting tools in Golang. We also need the RPC endpoint of KardiaChain mainnet to initialize the SDK.
1
func SetupKAIClient(rpcURL string) (*Node, context.Context, error) {
2
ctx, _ := context.WithCancel(context.Background())
3
cfg := zapdriver.NewProductionConfig()
4
logger, err := cfg.Build()
5
if err != nil {
6
return nil, nil, fmt.Errorf("Failed to create logger: %v", err)
7
}
8
node, err := NewNode(rpcURL, lgr)
9
if err != nil {
10
logger.Warn("cannot connect to RPC url", rpcURL)
11
return nil, nil, fmt.Errorf("Failed to dial to RPC URL: %v", err)
12
}
13
return node, ctx, nil
14
}
15
16
node, _, err := SetupKAIClient("https://rpc.kardiachain.io")
Copied!

Blocks

Block Info API:

We can get blocks’ info including header and transactions/receipts using kai_getBlockByNumber and kai_getBlockByHash APIs.

REQUEST PARAMS

kai_getBlockByNumber: QUANTITY|TAG - integer of a block number, or the string "earliest", "latest" or "pending". kai_getBlockByHash: DATA, 32 Bytes - hash of a block.
RESPONSE
kai_getBlockByNumber POST body and response:
1
{
2
"jsonrpc": "2.0",
3
"method": "kai_getBlockByNumber",
4
"params": [
5
1427011
6
],
7
"id": 1
8
}
Copied!
1
{
2
"jsonrpc": "2.0",
3
"id": 1,
4
"result": {
5
"hash": "0x30c1f5453e74cf076615f36b6b61427d8dc6e719ed2dcd50aa4a06a90c74b341",
6
"height": 1427011,
7
"lastBlock": "0x01c0e73f93a697e66ec4bd95bfbe6f0fa16a367eeef2fbe6f51a92c56efdcd14",
8
"commitHash": "0x08ac8406b5d94aac6381036cd2c524e0aa3aeddd025bf2487926fc85a3e8d2d5",
9
"time": "2021-03-23T11:12:16.81455482Z",
10
"numTxs": 1,
11
"gasLimit": 200000000,
12
"gasUsed": 51859,
13
"rewards": "14335139256888646354",
14
"proposerAddress": "0x79B598b9AD8f0C1ed987014FE644Cc73C7b72035",
15
"dataHash": "0x433f81b1480cdc3b19670ea4f5112f768f59e5027c3b52f5e1dbbd1bd5e18d5d",
16
"receiptsRoot": "",
17
"logsBloom": "",
18
"validatorHash": "0x97dda26c0d13ececbd9f8ee53d2597c6047d8b8b4ab53ff131b57d4c25503f38",
19
"nextValidatorHash": "0x97dda26c0d13ececbd9f8ee53d2597c6047d8b8b4ab53ff131b57d4c25503f38",
20
"consensusHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
21
"appHash": "0xf0879b3d44298dbdbf2a3dcfd4bc268e5332fbacbbd7a1e56a847a87033c3191",
22
"evidenceHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
23
"txs": [
24
{
25
"blockHash": "0x30c1f5453e74cf076615f36b6b61427d8dc6e719ed2dcd50aa4a06a90c74b341",
26
"blockNumber": 1427011,
27
"time": "2021-03-23T11:12:16.81455482Z",
28
"from": "0x2D2C6517Dc89E8347eEAEcCe0f3a71B5c6b0a172",
29
"gas": 3000000,
30
"gasPrice": 1000000000,
31
"hash": "0xbe49290b71ad5acbd75a471352df69e39d8c49fc7b209dbbb5cab5681fef3320",
32
"input": "0xf0acd7d5",
33
"nonce": 103,
34
"to": "0xF512B1e8acc96Baf05b470182f1814Bfb14DF563",
35
"transactionIndex": 0,
36
"value": "0",
37
"logsBloom": [...],
38
}
39
],
40
"receipts": [
41
{
42
"transactionHash": "0xbe49290b71ad5acbd75a471352df69e39d8c49fc7b209dbbb5cab5681fef3320",
43
"gasUsed": 51859,
44
"cumulativeGasUsed": 51859,
45
"contractAddress": "0x",
46
"logs": [
47
{
48
"address": "0xF512B1e8acc96Baf05b470182f1814Bfb14DF563",
49
"topics": [
50
"0x15490ebb4bc37fd11febf8b4a71b7bed47ac2d66d82fbdd5c83da3f202726eed"
51
],
52
"data": "0000000000000000000000002d2c6517dc89e8347eeaecce0f3a71b5c6b0a1720000000000000000000000000000000000000000000000000000000000000000",
53
"blockHeight": 1427011,
54
"transactionHash": "0xbe49290b71ad5acbd75a471352df69e39d8c49fc7b209dbbb5cab5681fef3320",
55
"transactionIndex": 0,
56
"blockHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
57
"logIndex": 1,
58
"removed": false
59
}
60
],
61
"root": "0x",
62
"status": 1
63
}
64
]
65
}
66
}
Copied!
Notes: The status of a transaction in the txs array is reflected in the status field of the corresponding response in the receipts array. If the value of the status field is 1, this transaction is successfully applied, otherwise the transaction is failed to execute if the value of this field is 0 or if there is no receipt for this transaction hash.
kai_getBlockByHash parameters and response are similar to the above code.
Notes: The txs array contains transactions of a block, the receipts array contains corresponding receipts of the transactions in block, including status of them.

Javascript SDK

1
const BLOCK_NUMBER = "latest";
2
const block = await kardiaClient.kaiChain.getBlockByBlockNumber(
3
BLOCK_NUMBER
4
);
Copied!
1
const BLOCK_HASH = "0xd3fd9a6e3d7bc200b9fa9aacf768ebf1f30a7a656bba4a2d4af31996762f9308";
2
const block = await kardiaClient.kaiChain.getBlockByHash(
3
BLOCK_HASH
4
);
Copied!

Go KaiClient

1
blockNumber := "latest"
2
block, err := node.BlockByHeight(ctx, blockNumber)
3
if err != nil {
4
return
5
}
Copied!
1
blockHash := "0xd3fd9a6e3d7bc200b9fa9aacf768ebf1f30a7a656bba4a2d4af31996762f9308"
2
b, err := node.BlockByHash(ctx, blockHash)
3
if err != nil {
4
return
5
}
Copied!

Sending transactions

Sending Transaction

Response

tx_sendRawTransaction POST body and response:
1
{
2
"jsonrpc": "2.0",
3
"method": "tx_sendRawTransaction",
4
"params": [
5
"0xf85f800182714894990d94fef322b50c5014d88565851cd5cf0bc45373801ba0c9efea69ea237eefd5c8ee4da8f35faefa56ed8be371204d30049e5c06b27f17a03ebdf6f95633b86ceb064537bc765ff98367638ec71704bf6a8c8920ef14bed6"
6
],
7
"id": 1
8
}
Copied!
1
{
2
"jsonrpc": "2.0",
3
"id": 1,
4
"result": "0xc2f9e58228b1adbd3654c1cffdfc5aed0065b2bfaf1945bee64b55a0f876cdb7"
5
}
Copied!

Javascript SDK

1
const ACCOUNT = {
2
address: '0x9cD2CFa83e6d85625a64fe31779446944c024E36',
3
privateKey:
4
'0xc07bed6441a92d0c8b99da1c4a63a2ff1c6e233729e3bf9428eeee25b8b60996',
5
};
6
7
const nonce = await kardiaClient.account.getNonce(ACCOUNT.address);
8
const txData = {
9
to: ACCOUNT.address,
10
gas: 50000,
11
nonce,
12
gasPrice: 1,
13
value: 22093,
14
data: '',
15
};
16
17
const txHash = await kardiaClient.transaction.sendTransaction(
18
txData,
19
ACCOUNT.privateKey,
20
true
21
);
Copied!

Go KaiClient

1
import "github.com/kardiachain/go-kardia/types"
2
3
tx, err := types.SignTx(types.NewTransaction(
4
nonce,
5
toAddr,
6
amount,
7
DefaultGasLimit,
8
defaultGasPrice,
9
nil,
10
), senderKey)
11
if err != nil {
12
panic(fmt.Sprintf("Fail to sign generated tx: %v", err))
13
}
Copied!

Transactions and Receipts

Transaction and Receipt API:

We can get a transaction and the corresponding receipt using tx_getTransaction and tx_getTransactionReceipt APIs.
REQUEST PARAMS
tx_getTransaction: DATA, 32 Bytes - hash of a transaction.
tx_getTransactionReceipt: DATA, 32 Bytes - hash of a transaction.

RESPONSE

tx_getTransaction POST body and response:
1
{
2
"jsonrpc": "2.0",
3
"method": "tx_getTransaction",
4
"params": [
5
"0x258105a566970c0665408073391ecf6112dd88422464de6c0b9fd5eae26f243d"
6
],
7
"id": 1
8
}
Copied!
1
{
2
"jsonrpc": "2.0",
3
"id": 1,
4
"result": {
5
"blockHash": "0xef0c216eebf803647a057a32dbba19ca8ad4e650225facc689d025f3dcde6590",
6
"blockNumber": 1426129,
7
"time": "2021-03-23T09:52:02.923640411Z",
8
"from": "0x81014E4C752037D6E01E7c1E07342F6645969F9A",
9
"gas": 500000,
10
"gasPrice": 2,
11
"hash": "0x258105a566970c0665408073391ecf6112dd88422464de6c0b9fd5eae26f243d",
12
"input": "0xc7b8981c",
13
"nonce": 148,
14
"to": "0x4dAe614b2eA2FaeeDDE7830A2e7fcEDdAE9f9161",
15
"transactionIndex": 0,
16
"value": "0",
17
"logsBloom": [...]
18
}
19
}
Copied!

RESPONSE

tx_getTransactionReceipt POST body and response:
1
{
2
"jsonrpc": "2.0",
3
"method": "tx_getTransactionReceipt",
4
"params": [
5
"0x258105a566970c0665408073391ecf6112dd88422464de6c0b9fd5eae26f243d"
6
],
7
"id": 1
8
}
Copied!
1
{
2
"jsonrpc": "2.0",
3
"id": 1,
4
"result": {
5
"blockHash": "0xef0c216eebf803647a057a32dbba19ca8ad4e650225facc689d025f3dcde6590",
6
"blockHeight": 1426129,
7
"transactionHash": "0x258105a566970c0665408073391ecf6112dd88422464de6c0b9fd5eae26f243d",
8
"transactionIndex": 0,
9
"from": "0x81014E4C752037D6E01E7c1E07342F6645969F9A",
10
"to": "0x4dAe614b2eA2FaeeDDE7830A2e7fcEDdAE9f9161",
11
"gasUsed": 88667,
12
"cumulativeGasUsed": 88667,
13
"contractAddress": "0x",
14
"logs": [],
15
"logsBloom": [...],
16
"root": "0x",
17
"status": 1
18
}
19
}
Copied!
Notes: The status of a transaction is reflected in the status field of tx_getTransactionReceipt response. If the value of the status field is 1, this transaction is successfully applied, otherwise the transaction is failed to execute if the value of this field is 0.

Javascript SDK

1
const txHash = "0x258105a566970c0665408073391ecf6112dd88422464de6c0b9fd5eae26f243d";
2
const txDetail = await kardiaClient.transaction.getTransaction(txHash);
3
const receipt = await kardiaClient.transaction.getTransactionReceipt(txHash);
Copied!

Go KaiClient

1
hash := "0x258105a566970c0665408073391ecf6112dd88422464de6c0b9fd5eae26f243d"
2
tx, err := node.GetTransaction(ctx, hash)
3
if err != nil {
4
...
5
}
6
receipt, err := node.GetTransactionReceipt(ctx, hash)
7
if err != nil {
8
...
9
}
Copied!

Account Balances

Account Balances

RESPONSE

account_balance POST body and response:
1
{
2
"jsonrpc": "2.0",
3
"method": "account_balance",
4
"params": [
5
"0x14191195F9BB6e54465a341CeC6cce4491599ccC",
6
"latest"
7
],
8
"id": 1
9
}
Copied!
1
{
2
"jsonrpc": "2.0",
3
"id": 1,
4
"result": "2852475404340986866629970957"
5
}
Copied!
Notes: The balance string is balance of address in HYDRO, 1 KAI = 1018 HYDRO.

Javascript SDK

1
const ADDRESS = '0x14191195F9BB6e54465a341CeC6cce4491599ccC';
2
const balanceAtHeight = await kardiaClient.account.getBalance(ADDRESS, {blockHeight: 1});
3
const balanceAtHash = await kardiaClient.account.getBalance(ADDRESS, {blockHash: BLOCK_HASH});
Copied!

Go KaiClient

1
address := "0x14191195F9BB6e54465a341CeC6cce4491599ccC";
2
balance, err := node.Balance(context.Background(), address)
3
if err != nil {
4
...
5
}
Copied!
Last modified 7mo ago