-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathfungibles.go
128 lines (110 loc) · 3.72 KB
/
fungibles.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package hiveenginego
import (
"encoding/json"
)
/*
type tokenArray struct {
tokArray []HiveEngineFungibleToken
}
*/
type HiveEngineFungibleToken struct {
FungibleId int `json:"_id"`
Issuer string `json:"issuer"`
Symbol string `json:"symbol"`
Name string `json:"name"`
MetaData json.RawMessage `json:"metadata"`
DelegationEnabled bool `json:"delegationEnabled"`
Precision int `json:"precision"`
StakingEnabled bool `json:"stakingEnabled"`
UndelegationCooldown int `json:"undelegationCooldown"`
UnstakingCooldown int `json:"unstakingCooldown"`
}
type FungibleBalance struct {
Id int `json:"_id"`
Account string `json:"account"`
Symbol string `json:"symbol"`
Balance string `json:"balance"`
Stake string `json:"stake"`
PendingUnstake string `json:"pendingUnstake"`
DelegationsIn string `json:"delegationsIn"`
DelegationsOut string `json:"delegationsOut"`
PendingUndelegations string `json:"pendingUndelegations"`
}
type FungibleTokenTransfer struct {
Symbol string `json:"symbol"`
To string `json:"to"`
Quantity string `json:"quantity"`
Memo string `json:"memo"`
}
func CreateFungibleTokenTransfer(symbol string, to string, quantity string, memo string) ContractTx {
tokenTransfer := FungibleTokenTransfer{
Symbol: symbol,
To: to,
Quantity: quantity,
Memo: memo,
}
contractTrx := ContractTx{
ContractName: "tokens",
ContractAction: "transfer",
ContractPayload: tokenTransfer,
}
return contractTrx
}
func (h HiveEngineRpcNode) getFungibleTokenCount() (int, error) {
if len(h.Endpoints.Contracts) == 0 {
h.Endpoints.Contracts = "/contracts"
}
endpoint := h.Endpoints.Contracts
qParamsQ := ContractQueryParamsQuery{}
qParamsIndex := []ContractQueryParamsIndex{}
qParamsIndex = append(qParamsIndex, ContractQueryParamsIndex{Index: "_id", Descending: true})
qParams := ContractQueryParams{Contract: "tokens", Table: "tokens", Query: qParamsQ, Limit: 1, Offset: 0, Index: qParamsIndex}
query := herpcQuery{method: "find", params: qParams}
res, err := h.rpcExec(endpoint, query)
if err != nil {
return 0, err
}
token := []HiveEngineFungibleToken{}
if err := json.Unmarshal(res, &token); err != nil {
return 0, err
}
countSymbolNFT := token[0].FungibleId
return countSymbolNFT, nil
}
func (h HiveEngineRpcNode) GetAllFungibleTokens() ([]HiveEngineFungibleToken, error) {
totalTokens, err := h.getFungibleTokenCount()
if err != nil {
return nil, err
}
offsetsNeeded := totalTokens / 1000
qParamsIndex := []ContractQueryParamsIndex{}
qParamsQ := ContractQueryParamsQuery{}
var queries []herpcQuery
for i := 0; i <= offsetsNeeded; i++ {
offset := i * 1000
qParams := ContractQueryParams{Contract: "tokens", Table: "tokens", Query: qParamsQ, Limit: 1000, Offset: offset, Index: qParamsIndex}
query := herpcQuery{method: "find", params: qParams}
queries = append(queries, query)
}
tokens := []HiveEngineFungibleToken{}
if len(queries) > 0 {
if len(h.Endpoints.Contracts) == 0 {
h.Endpoints.Contracts = "/contracts"
}
endpoint := h.Endpoints.Contracts
ress, err := h.rpcExecBatch(endpoint, queries)
if err != nil {
return nil, err
}
var batchResult []HiveEngineFungibleToken
for _, res := range ress {
thisresult := []HiveEngineFungibleToken{}
if err := json.Unmarshal(res, &thisresult); err != nil { // Parse []byte to the go struct pointer
return nil, err
}
batchResult = append(tokens, thisresult...)
}
tokens = append(tokens, batchResult...)
}
return tokens, nil
}