-
Notifications
You must be signed in to change notification settings - Fork 3
/
nft.go
247 lines (210 loc) · 7.17 KB
/
nft.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
package hiveenginego
import (
"encoding/json"
"math"
"strconv"
"strings"
)
type EngineNft struct {
Id int `json:"_id"`
Account string `json:"account"`
OwnedBy string `json:"ownedBy"`
LockedTokens json.RawMessage `json:"lockedTokens"`
Properties json.RawMessage `json:"properties"`
PreviousAccount string `json:"previousAccount"`
PreviousOwnedBy string `json:"previousOwnedBy"`
}
type NftMarketOffer struct {
Id int `json:"_id"`
Account string `json:"account"`
OwnedBy string `json:"ownedBy"`
NftId string `json:"nftId"`
Grouping json.RawMessage `json:"grouping"`
Timestamp int `json:"timestamp"`
Price string `json:"price"`
PriceDec json.RawMessage `json:"priceDec"`
PriceSymbol string `json:"priceSymbol"`
Fee int `json:"fee"`
}
type NftTransferPayload struct {
Nfts []NftsForNftTransfer `json:"nfts"`
To string `json:"to"`
Memo string `json:"memo,omitempty"`
}
type NftsForNftTransfer struct {
Symbol string `json:"symbol"`
Ids []string `json:"ids"`
}
func CreateNftTransfer(symbol string, nftIds []int, to string, memo string) []ContractTx {
var nftIdsStr []string
for _, nftId := range nftIds {
nftIdsStr = append(nftIdsStr, strconv.Itoa(nftId))
}
var preppedTx []ContractTx
if len(nftIdsStr) > 50 {
for i := 0; i <= len(nftIdsStr); {
var l int
if i+50 > len(nftIdsStr) {
l = len(nftIdsStr)
} else {
l = i + 50
}
strid := nftIdsStr[i:l]
nftTransfer := NftsForNftTransfer{Symbol: symbol, Ids: strid}
payload := NftTransferPayload{Nfts: []NftsForNftTransfer{nftTransfer}, To: to, Memo: memo}
contrx := ContractTx{ContractName: "nft", ContractAction: "transfer", ContractPayload: payload}
preppedTx = append(preppedTx, contrx)
i += 50
}
} else {
nftTransfer := NftsForNftTransfer{Symbol: symbol, Ids: nftIdsStr}
payload := NftTransferPayload{Nfts: []NftsForNftTransfer{nftTransfer}, To: to, Memo: memo}
contrx := ContractTx{ContractName: "nft", ContractAction: "transfer", ContractPayload: payload}
preppedTx = append(preppedTx, contrx)
}
return preppedTx
}
func (h HiveEngineRpcNode) getSymbolNFTCount(nftSymbol string) (int, error) {
nftSymbolUpper := strings.ToUpper(nftSymbol)
if len(h.Endpoints.Contracts) == 0 {
h.Endpoints.Contracts = "/contracts"
}
endpoint := h.Endpoints.Contracts
qParamsIndex := []ContractQueryParamsIndex{{Index: "_id", Descending: true}}
qParams := ContractQueryParams{Contract: "nft", Table: nftSymbolUpper + "instances", Query: struct{}{}, Limit: 1, Offset: 0, Index: qParamsIndex}
query := herpcQuery{method: "find", params: qParams}
res, err := h.rpcExec(endpoint, query)
if err != nil {
return 0, err
}
nft := []EngineNft{}
if err := json.Unmarshal(res, &nft); err != nil {
return 0, nil
}
countSymbolNFT := nft[0].Id
return countSymbolNFT, nil
}
func (h HiveEngineRpcNode) GetSymbolAllNft(nftSymbol string) ([]EngineNft, error) {
if len(h.Endpoints.Contracts) == 0 {
h.Endpoints.Contracts = "/contracts"
}
if h.RpcNode.MaxConn == 0 {
h.RpcNode.MaxConn = 1
}
if h.RpcNode.MaxBatch == 0 {
h.RpcNode.MaxBatch = 2
}
nftSymbolUpper := strings.ToUpper(nftSymbol)
collectionSize, err := h.getSymbolNFTCount(nftSymbolUpper)
if err != nil {
return nil, err
}
offsetsNeeded := collectionSize / 1000
qParamsIndex := []ContractQueryParamsIndex{}
var queries []herpcQuery
for i := 0; i <= offsetsNeeded; i++ {
offset := i * 1000
queryFilter := QueryIDRange{QueryIntRange{offset, offset + 999}}
qParams := ContractQueryParams{Contract: "nft", Table: nftSymbolUpper + "instances", Query: queryFilter, Limit: 1000, Offset: 0, Index: qParamsIndex}
query := herpcQuery{method: "find", params: qParams}
queries = append(queries, query)
}
endpoint := h.Endpoints.Contracts
ress, err := h.rpcExecBatch(endpoint, queries)
if err != nil {
return nil, err
}
var nfts []EngineNft
for _, res := range ress {
thisresult := []EngineNft{}
if err := json.Unmarshal(res, &thisresult); err != nil { // Parse []byte to the go struct pointer
return nil, err
}
nfts = append(nfts, thisresult...)
}
return nfts, nil
}
func (h HiveEngineRpcNode) GetSymbolAllNftFast(nftSymbol string) ([][]byte, error) {
if len(h.Endpoints.Contracts) == 0 {
h.Endpoints.Contracts = "/contracts"
}
if h.RpcNode.MaxConn == 0 {
h.RpcNode.MaxConn = 1
}
if h.RpcNode.MaxBatch == 0 {
h.RpcNode.MaxBatch = 2
}
nftSymbolUpper := strings.ToUpper(nftSymbol)
collectionSize, err := h.getSymbolNFTCount(nftSymbolUpper)
if err != nil {
return nil, err
}
offsetsNeeded := collectionSize / 1000
qParamsIndex := []ContractQueryParamsIndex{}
var queries []herpcQuery
for i := 0; i <= offsetsNeeded; i++ {
offset := i * 1000
queryFilter := QueryIDRange{QueryIntRange{offset, offset + 999}}
qParams := ContractQueryParams{Contract: "nft", Table: nftSymbolUpper + "instances", Query: queryFilter, Limit: 1000, Offset: 0, Index: qParamsIndex}
query := herpcQuery{method: "find", params: qParams}
queries = append(queries, query)
}
endpoint := h.Endpoints.Contracts
ress, err := h.rpcExecBatchFast(endpoint, queries)
if err != nil {
return nil, err
}
return ress, nil
}
func (h HiveEngineRpcNode) GetSymbolAllNftMarket(nftSymbol string, chunkSize int) ([]NftMarketOffer, error) {
var allNftMarketOffers []NftMarketOffer
var offset int
chunkSize = int(math.Ceil(float64(chunkSize)/1000) * 1000)
marketNftFetched := chunkSize + 1000
for marketNftFetched == chunkSize+1000 {
nftOffersThisRound, err := h.getSymbolNftMarketN(nftSymbol, chunkSize, offset)
if err != nil {
return nil, err
}
allNftMarketOffers = append(allNftMarketOffers, nftOffersThisRound...)
marketNftFetched = len(nftOffersThisRound)
offset = offset + chunkSize + 1000
}
return allNftMarketOffers, nil
}
//chunkSize is the number to be grabbed and tested to see if another round is needed
func (h HiveEngineRpcNode) getSymbolNftMarketN(nftSymbol string, chunkSize int, startingOffset int) ([]NftMarketOffer, error) {
if len(h.Endpoints.Contracts) == 0 {
h.Endpoints.Contracts = "/contracts"
}
if h.RpcNode.MaxConn == 0 {
h.RpcNode.MaxConn = 1
}
if h.RpcNode.MaxBatch == 0 {
h.RpcNode.MaxBatch = 2
}
nftSymbolUpper := strings.ToUpper(nftSymbol)
offsetsNeeded := chunkSize / 1000
qParamsIndex := []ContractQueryParamsIndex{}
var queries []herpcQuery
for i := 0; i <= offsetsNeeded; i++ {
offset := (i * 1000) + startingOffset
qParams := ContractQueryParams{Contract: "nftmarket", Table: nftSymbolUpper + "sellBook", Query: struct{}{}, Limit: 1000, Offset: offset, Index: qParamsIndex}
query := herpcQuery{method: "find", params: qParams}
queries = append(queries, query)
}
endpoint := h.Endpoints.Contracts
ress, err := h.rpcExecBatch(endpoint, queries)
if err != nil {
return nil, err
}
var nftOffers []NftMarketOffer
for _, res := range ress {
thisresult := []NftMarketOffer{}
if err := json.Unmarshal(res, &thisresult); err != nil { // Parse []byte to the go struct pointer
return nil, err
}
nftOffers = append(nftOffers, thisresult...)
}
return nftOffers, nil
}