-
Notifications
You must be signed in to change notification settings - Fork 129
/
nftexchanges.go
197 lines (178 loc) · 5.62 KB
/
nftexchanges.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
package models
import (
"context"
"database/sql"
"fmt"
"github.com/diadata-org/diadata/pkg/dia"
)
func (rdb *RelDB) SetNFTExchange(exchange dia.NFTExchange) (err error) {
fields := fmt.Sprintf("INSERT INTO %s (name,centralized,contract,blockchain,rest_api,ws_api,watchdog_delay) VALUES ", nftExchangeTable)
values := "($1,$2,NULLIF($3,''),$4,NULLIF($5,''),NULLIF($6,''),$7)"
conflict := " ON CONFLICT (name) DO UPDATE SET contract=NULLIF($3,''),rest_api=$5,ws_api=$6,watchdog_delay=$7"
query := fields + values + conflict
_, err = rdb.postgresClient.Exec(context.Background(), query,
exchange.Name,
exchange.Centralized,
exchange.Contract,
exchange.BlockChain.Name,
exchange.RestAPI,
exchange.WsAPI,
exchange.WatchdogDelay,
)
if err != nil {
return err
}
return nil
}
func (rdb *RelDB) GetNFTExchange(name string) (exchange dia.Exchange, err error) {
query := fmt.Sprintf("SELECT centralized,contract,blockchain,rest_api,ws_api,watchdog_delay FROM %s WHERE name=$1", exchangeTable)
var contract sql.NullString
var blockchainName sql.NullString
var restAPI sql.NullString
var wsAPI sql.NullString
err = rdb.postgresClient.QueryRow(context.Background(), query, name).Scan(
&exchange.Centralized,
&contract,
&blockchainName,
&restAPI,
&wsAPI,
&exchange.WatchdogDelay,
)
if err != nil {
return
}
if contract.Valid {
exchange.Contract = contract.String
}
if blockchainName.Valid {
exchange.BlockChain.Name = blockchainName.String
}
if restAPI.Valid {
exchange.RestAPI = restAPI.String
}
if wsAPI.Valid {
exchange.WsAPI = wsAPI.String
}
exchange.Name = name
return
}
// GetAllNFTExchanges returns all nft exchanges existent in the nftexchange table.
func (rdb *RelDB) GetAllNFTExchanges() (exchanges []dia.NFTExchange, err error) {
query := fmt.Sprintf("SELECT name,contract, centralized,blockchain,rest_api,ws_api,watchdog_delay FROM %s", nftExchangeTable)
rows, err := rdb.postgresClient.Query(context.Background(), query)
if err != nil {
return []dia.NFTExchange{}, err
}
defer rows.Close()
for rows.Next() {
var exchange dia.NFTExchange
var contract sql.NullString
var blockchainName sql.NullString
var restAPI sql.NullString
var wsAPI sql.NullString
err := rows.Scan(
&exchange.Name,
&contract,
&exchange.Centralized,
&blockchainName,
&restAPI,
&wsAPI,
&exchange.WatchdogDelay,
)
if err != nil {
return []dia.NFTExchange{}, err
}
if contract.Valid {
exchange.Contract = contract.String
}
if blockchainName.Valid {
exchange.BlockChain.Name = blockchainName.String
}
if restAPI.Valid {
exchange.RestAPI = restAPI.String
}
if wsAPI.Valid {
exchange.WsAPI = wsAPI.String
}
exchanges = append(exchanges, exchange)
}
return exchanges, nil
}
// Get24HoursNFTExchangeTrades returns the number of trades in last 24 hours
func (rdb *RelDB) Get24HoursNFTExchangeTrades(exchange dia.NFTExchange) (int64, error) {
query := fmt.Sprintf(`
SELECT count(*)
FROM %s
WHERE trade_time>now()- INTERVAL '1 days'
AND trade_time<=now()
AND marketplace='%s'`,
NfttradeCurrTable,
exchange.Name,
)
var numTrades sql.NullInt64
err := rdb.postgresClient.QueryRow(context.Background(), query).Scan(&numTrades)
if numTrades.Valid {
return numTrades.Int64, nil
}
return 0, err
}
// Get24HoursNFTExchangeVolume returns the volume traded in last 24 hours
func (rdb *RelDB) Get24HoursNFTExchangeVolume(exchange dia.NFTExchange) (float64, error) {
var paymentCurrencies []dia.Asset
switch exchange.BlockChain.Name {
case dia.ETHEREUM:
paymentCurrencies = append(paymentCurrencies, dia.Asset{Blockchain: dia.ETHEREUM, Address: "0x0000000000000000000000000000000000000000"})
paymentCurrencies = append(paymentCurrencies, dia.Asset{Blockchain: dia.ETHEREUM, Address: "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2"})
case dia.ASTAR:
paymentCurrencies = append(paymentCurrencies, dia.Asset{Blockchain: dia.ASTAR, Address: "0x0000000000000000000000000000000000000000"})
paymentCurrencies = append(paymentCurrencies, dia.Asset{Blockchain: dia.ASTAR, Address: "0x9dA4A3a345bf6371f8e47c63Cad2293e532022dE"})
case dia.BINANCESMARTCHAIN:
paymentCurrencies = append(paymentCurrencies, dia.Asset{Blockchain: dia.BINANCESMARTCHAIN, Address: "0x0000000000000000000000000000000000000000"})
paymentCurrencies = append(paymentCurrencies, dia.Asset{Blockchain: dia.BINANCESMARTCHAIN, Address: "0xbb4CdB9CBd36B01bD1cBaEBF2De08d9173bc095c"})
}
query := fmt.Sprintf(`
SELECT SUM(price::numeric)
FROM %s nt
INNER JOIN %s a
ON nt.currency_id=a.asset_id
WHERE trade_time>now()- INTERVAL '1 days'
AND trade_time<=now()
AND marketplace='%s' `,
NfttradeCurrTable,
assetTable,
exchange.Name,
)
for i, paymentCurrency := range paymentCurrencies {
if i == 0 {
query += " AND ("
}
query += fmt.Sprintf("(address='%s' and blockchain='%s')", paymentCurrency.Address, paymentCurrency.Blockchain)
if i < len(paymentCurrencies)-1 {
query += " OR "
} else {
query += ")"
}
}
var volume sql.NullFloat64
err := rdb.postgresClient.QueryRow(context.Background(), query).Scan(&volume)
if volume.Valid {
return volume.Float64 / 1e18, nil
}
return 0, err
}
// GetCollectionCountByExchange returns the number of NFT collections traded on exchange
func (rdb *RelDB) GetCollectionCountByExchange(exchange string) (int64, error) {
query := fmt.Sprintf(`
SELECT COUNT (DISTINCT nftclass_id)
FROM %s
WHERE marketplace='%s'`,
NfttradeCurrTable,
exchange,
)
var collections sql.NullInt64
err := rdb.postgresClient.QueryRow(context.Background(), query).Scan(&collections)
if collections.Valid {
return collections.Int64, nil
}
return 0, err
}