/
get-token-account-balance.go
64 lines (49 loc) · 1.47 KB
/
get-token-account-balance.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
// Copyright 2022 Fluidity Money. All rights reserved. Use of this
// source code is governed by a GPL-style license that can be found in the
// LICENSE.md file.
package rpc
import (
"encoding/json"
"fmt"
"github.com/fluidity-money/fluidity-app/common/solana"
)
type GetTokenAccountBalanceResult struct {
RpcContext
Value *struct {
// Raw balance of tokens as a string, ignoring decimals.
Amount string `json:"amount"`
// Number of decimals configured for token's mint.
Decimals uint8 `json:"decimals"`
// TODO: <number> == int64 ???
// DEPRECATED: Token amount as a float, accounting for decimals.
UiAmount *float64 `json:"uiAmount"`
// Token amount as a string, accounting for decimals.
UiAmountString string `json:"uiAmountString"`
} `json:"value"`
}
func (s *Provider) GetTokenAccountBalance(pda solana.PublicKey, commitment string) (*GetTokenAccountBalanceResult, error) {
params := []interface{}{
pda.String(),
}
if commitment != "" {
params = append(params, map[string]string{
"commitment": commitment,
})
}
res, err := s.RawInvoke("getTokenAccountBalance", params)
if err != nil {
return nil, fmt.Errorf(
"failed to getTokenAccountBalance: %v",
err,
)
}
var tokenBalanceResult GetTokenAccountBalanceResult
if err := json.Unmarshal(res, &tokenBalanceResult); err != nil {
return nil, fmt.Errorf(
"failed to decode getTokenAccountBalance, message %#v: %v",
string(res),
err,
)
}
return &tokenBalanceResult, nil
}