Golang SDK
This is the guide how to use Golang SDK

Initializing

1
func SetupKardiaClient() (Node, error){
2
url := "https://dev-1.kardiachain.io"
3
lgr, err := zap.NewProduction()
4
if err != nil {
5
return nil, err
6
}
7
node, err := NewNode(url, lgr)
8
if err != nil {
9
return nil, err
10
}
11
return node, nil
12
}
Copied!

API

Info

1
type IInfo interface {
2
Url() string
3
IsAlive() bool
4
NodeInfo(ctx context.Context) (*NodeInfo, error)
5
GetCirculatingSupply(ctx context.Context) (*big.Int, error)
6
KardiaCall(ctx context.Context, args SMCCallArgs) ([]byte, error)
7
}
Copied!

Blocks

1
type IBlock interface {
2
LatestBlockNumber(ctx context.Context) (uint64, error)
3
BlockByHash(ctx context.Context, hash string) (*Block, error)
4
BlockByHeight(ctx context.Context, height uint64) (*Block, error)
5
BlockHeaderByHash(ctx context.Context, hash string) (*Header, error)
6
BlockHeaderByNumber(ctx context.Context, number uint64) (*Header, error)
7
}
Copied!

Addresses

1
type IAddress interface {
2
Balance(ctx context.Context, addressHash string) (string, error)
3
StorageAt(ctx context.Context, addressHash string, key string) ([]byte, error)
4
Code(ctx context.Context, addressHash string) (common.Bytes, error)
5
NonceAt(ctx context.Context, addressHash string) (uint64, error)
6
}
Copied!

Transactions

1
type ITx interface {
2
GetTransaction(ctx context.Context, hash string) (*Transaction, error)
3
GetTransactionReceipt(ctx context.Context, txHash string) (*Receipt, error)
4
SendTransaction(ctx context.Context, tx *types.Transaction) error
5
SendRawTransaction(ctx context.Context, tx *types.Transaction) error
6
}
Copied!

Examples

Note: Examples can be found at ^_test.go

Create Wallet

1
func Test() {
2
address, privKey, err := GenerateWallet()
3
fmt.Println("WalletAddress", address.Hex())
4
fmt.Println("WalletPrivateKey", privateKeyStr)
5
}
Copied!

Send SignedTx

1
func SendSignedTx() {
2
receivedAddress := common.HexToAddress("0x59173FAF22C3fEd212Ec6B5Ea2E50f7644b614f3")
3
privateKey, err := crypto.HexToECDSA("63e16b5334e76d63ee94f35bd2a81c721ebbbb27e81620be6fc1c448c767eed9")
4
if err != nil {
5
return
6
}
7
8
publicKey := privateKey.Public()
9
publicKeyECDSA, ok := publicKey.(*ecdsa.PublicKey)
10
if !ok {
11
return
12
}
13
fromAddress := crypto.PubkeyToAddress(*publicKeyECDSA)
14
15
node, err := setupTestNodeInterface()
16
assert.Nil(t, err)
17
nonce, err := node.NonceAt(context.Background(), fromAddress.String())
18
assert.Nil(t, err)
19
balance, err := node.Balance(context.Background(), fromAddress.String())
20
assert.Nil(t, err)
21
gasLimit := uint64(3100000)
22
gasPrice := big.NewInt(1000000000)
23
// Send 1 KAI to from test account to receivedAddress
24
var Hydro = big.NewInt(1000000000000000000) // 18 decimals
25
oneKai := new(big.Int).Mul(new(big.Int).SetInt64(1), Hydro)
26
27
//nonce uint64, to common.Address, amount *big.Int, gasLimit uint64, gasPrice *big.Int, data []byte
28
tx := types.NewTransaction(nonce, receivedAddress, oneKai, gasLimit, gasPrice, nil)
29
signedTx, err := types.SignTx(types.HomesteadSigner{}, tx, privateKey)
30
assert.Nil(t, err)
31
32
err = node.SendTransaction(context.Background(), signedTx)
33
assert.Nil(t, err)
34
fmt.Printf("tx sent: %s", signedTx.Hash().Hex())
35
}
Copied!

Interact with SMC

Function Definition

1
{
2
"constant": true,
3
"inputs": [
4
{
5
"internalType": "address",
6
"name": "_delAddr",
7
"type": "address"
8
}
9
],
10
"name": "getDelegationRewards",
11
"outputs": [
12
{
13
"internalType": "uint256",
14
"name": "",
15
"type": "uint256"
16
}
17
],
18
"payable": false,
19
"stateMutability": "view",
20
"type": "function"
21
},
Copied!

Create Node Instance

1
n, err := NewNode("https://dev-1.kardiachain.io", zap.L())
2
if err != nil {
3
return
4
}
Copied!

Create Contract Instance

1
// validatorSMC ABI can be found at smc package
2
validatorSmcAbi, err := abi.JSON(strings.NewReader(smc.ValidatorABI))
3
if err != nil {
4
return err
5
}
6
validatorUtil := &Contract{
7
Abi: &validatorSmcAbi,
8
}
9
n.validatorSMC = validatorUtil
Copied!

Build payload

1
payload, err := n.validatorSMC.Abi.Pack("getDelegationRewards", common.HexToAddress(delegatorAddress))
2
if err != nil {
3
n.lgr.Error("Error packing delegation rewards payload: ", zap.Error(err))
4
return nil, err
5
}
Copied!

Send payload

1
res, err := n.KardiaCall(ctx, ConstructCallArgs(validatorSMCAddr, payload))
2
if err != nil {
3
n.lgr.Error("GetDelegationRewards KardiaCall error: ", zap.Error(err))
4
return nil, err
5
}
Copied!

Get response

1
var result struct {
2
Rewards *big.Int
3
}
4
// unpack result
5
err = n.validatorSMC.Abi.UnpackIntoInterface(&result, "getDelegationRewards", res)
6
if err != nil {
7
n.lgr.Error("Error unpacking delegation rewards: ", zap.Error(err))
8
return nil, err
9
}
Copied!

Subscribe NewHeader event

1
func TestSubscription_NewBlockHead(t *testing.T) {
2
lgr, err := zap.NewProduction()
3
assert.Nil(t, err)
4
url := "wss://ws-dev.kardiachain.io/ws"
5
6
node, err := NewNode(url, lgr)
7
assert.Nil(t, err)
8
9
headersCh := make(chan *types.Header)
10
sub, err := node.SubscribeNewHead(context.Background(), headersCh)
11
assert.Nil(t, err, "cannot subscribe")
12
13
for {
14
select {
15
case err := <-sub.Err():
16
log.Fatal(err)
17
case header := <-headersCh:
18
fmt.Println(header.Hash().Hex())
19
}
20
}
21
}
Copied!

Subscribe log filters

1
node, err := NewNode("wss://ws-dev.kardiachain.io", zap.L())
2
if err != nil {
3
return err
4
}
5
smcAddress := "0x0f0524Aa6c70d8B773189C0a6aeF3B01719b0b47"
6
args := kardia.FilterArgs{
7
Address: []string{smcAddress},
8
}
9
logEventCh := make(chan *Log)
10
sub, err := node.KaiSubscribe(context.Background(), logEventCh, "logs", args)
11
if err != nil {
12
return err
13
}
14
15
for {
16
select {
17
case err := <-sub.Err():
18
lgr.Debug("subscribe err", zap.Error(err))
19
case log := <-logEventCh:
20
logger.Debug("Log", zap.Any("detail", log))
21
// Process event
22
}
23
}
Copied!
Note: More filters args can be found at Kardia RPC API
Last modified 5mo ago