/
repository_contracts.go
245 lines (210 loc) · 6.88 KB
/
repository_contracts.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
// Copyright 2020 The klaytn Authors
// This file is part of the klaytn library.
//
// The klaytn library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The klaytn library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the klaytn library. If not, see <http://www.gnu.org/licenses/>.
package kas
import (
"fmt"
"strings"
"time"
"github.com/klaytn/klaytn/blockchain"
"github.com/klaytn/klaytn/blockchain/types"
"github.com/klaytn/klaytn/common"
)
// filterKIPContracts filters the deployed contracts to KIP7, KIP17 and others.
func filterKIPContracts(api BlockchainAPI, event blockchain.ChainEvent) ([]*FT, []*NFT, []*Contract, error) {
var (
kip7s []*FT
kip17s []*NFT
others []*Contract
)
caller := newContractCaller(api)
for _, receipt := range event.Receipts {
if receipt.Status != types.ReceiptStatusSuccessful || receipt.ContractAddress == (common.Address{}) {
continue
}
contract := receipt.ContractAddress
isKIP13, err := caller.isKIP13(contract, nil)
if err != nil {
logger.Error("Failed to call isKIP13", "err", err, "contract", contract.String())
return nil, nil, nil, err
} else if !isKIP13 {
others = append(others, &Contract{Address: contract.Bytes()})
continue
}
if isKIP7, err := caller.isKIP7(contract, nil); err != nil {
logger.Error("Failed to call isKIP7", "err", err, "contract", contract.String())
return nil, nil, nil, err
} else if isKIP7 {
kip7s = append(kip7s, &FT{Address: contract.Bytes()})
continue
}
if isKIP17, err := caller.isKIP17(contract, nil); err != nil {
logger.Error("Failed to call isKIP17", "err", err, "contract", contract.String())
return nil, nil, nil, err
} else if isKIP17 {
kip17s = append(kip17s, &NFT{Address: contract.Bytes()})
continue
}
others = append(others, &Contract{Address: contract.Bytes()})
}
return kip7s, kip17s, others, nil
}
// InsertContracts inserts deployed contracts in the given chain event into KAS database.
func (r *repository) InsertContracts(event blockchain.ChainEvent) error {
kip7s, kip17s, others, err := filterKIPContracts(r.blockchainApi, event)
if err != nil {
logger.Error("Failed to filter KIP contracts", "err", err, "blockNumber", event.Block.NumberU64())
return err
}
if err := r.insertFTs(kip7s); err != nil {
logger.Error("Failed to insert KIP7 contracts", "err", err, "blockNumber", event.Block.NumberU64(), "numKIP7s", len(kip7s))
return err
}
if err := r.insertNFTs(kip17s); err != nil {
logger.Error("Failed to insert KIP17 contracts", "err", err, "blockNumber", event.Block.NumberU64(), "numKIP17s", len(kip17s))
return err
}
if err := r.insertContracts(others); err != nil {
logger.Error("Failed to insert other contracts", "err", err, "blockNumber", event.Block.NumberU64(), "numContracts", len(others))
return err
}
return nil
}
// insertContracts inserts the contracts which are divided into chunkUnit because of max number of placeholders.
func (r *repository) insertContracts(contracts []*Contract) error {
chunkUnit := maxPlaceholders / placeholdersPerContractItem
var chunks []*Contract
for contracts != nil {
if placeholdersPerContractItem*len(contracts) > maxPlaceholders {
chunks = contracts[:chunkUnit]
contracts = contracts[chunkUnit:]
} else {
chunks = contracts
contracts = nil
}
if err := r.bulkInsertContracts(chunks); err != nil {
return err
}
}
return nil
}
// bulkInsertTransactions inserts the given contracts in multiple rows at once.
func (r *repository) bulkInsertContracts(contracts []*Contract) error {
if len(contracts) == 0 {
return nil
}
var valueStrings []string
var valueArgs []interface{}
for _, ft := range contracts {
valueStrings = append(valueStrings, "(?)")
valueArgs = append(valueArgs, ft.Address)
}
rawQuery := `
INSERT INTO contract(address)
VALUES %s
ON DUPLICATE KEY
UPDATE address=address`
query := fmt.Sprintf(rawQuery, strings.Join(valueStrings, ","))
if _, err := r.db.DB().Exec(query, valueArgs...); err != nil {
return err
}
return nil
}
// insertFTs inserts the FT contracts which are divided into chunkUnit because of max number of placeholders.
func (r *repository) insertFTs(fts []*FT) error {
chunkUnit := maxPlaceholders / placeholdersPerFTItem
var chunks []*FT
for fts != nil {
if placeholdersPerFTItem*len(fts) > maxPlaceholders {
chunks = fts[:chunkUnit]
fts = fts[chunkUnit:]
} else {
chunks = fts
fts = nil
}
if err := r.bulkInsertFTs(chunks); err != nil {
return err
}
}
return nil
}
// bulkInsertFTs inserts the given FT contracts in multiple rows at once.
func (r *repository) bulkInsertFTs(fts []*FT) error {
if len(fts) == 0 {
return nil
}
var valueStrings []string
var valueArgs []interface{}
now := time.Now()
for _, ft := range fts {
valueStrings = append(valueStrings, "(?, ?, ?)")
valueArgs = append(valueArgs, ft.Address)
valueArgs = append(valueArgs, &now)
valueArgs = append(valueArgs, &now)
}
rawQuery := `
INSERT INTO kct_ft_metadata(address, createdAt, updatedAt)
VALUES %s
ON DUPLICATE KEY
UPDATE address=address`
query := fmt.Sprintf(rawQuery, strings.Join(valueStrings, ","))
if _, err := r.db.DB().Exec(query, valueArgs...); err != nil {
return err
}
return nil
}
// insertNFTs inserts the NFT contracts which are divided into chunkUnit because of max number of placeholders.
func (r *repository) insertNFTs(nfts []*NFT) error {
chunkUnit := maxPlaceholders / placeholdersPerNFTItem
var chunks []*NFT
for nfts != nil {
if placeholdersPerNFTItem*len(nfts) > maxPlaceholders {
chunks = nfts[:chunkUnit]
nfts = nfts[chunkUnit:]
} else {
chunks = nfts
nfts = nil
}
if err := r.bulkInsertNFTs(chunks); err != nil {
return err
}
}
return nil
}
// bulkInsertNFTs inserts the given NFT contracts in multiple rows at once.
func (r *repository) bulkInsertNFTs(nfts []*NFT) error {
if len(nfts) == 0 {
return nil
}
var valueStrings []string
var valueArgs []interface{}
now := time.Now()
for _, nft := range nfts {
valueStrings = append(valueStrings, "(?, ?, ?)")
valueArgs = append(valueArgs, nft.Address)
valueArgs = append(valueArgs, &now)
valueArgs = append(valueArgs, &now)
}
rawQuery := `
INSERT INTO kct_nft_metadata(address, createdAt, updatedAt)
VALUES %s
ON DUPLICATE KEY
UPDATE address=address`
query := fmt.Sprintf(rawQuery, strings.Join(valueStrings, ","))
if _, err := r.db.DB().Exec(query, valueArgs...); err != nil {
return err
}
return nil
}