Skip to content

Commit 34f2467

Browse files
authored
fix velar exchange scraper trades direction (#1159)
1 parent b3e2378 commit 34f2467

File tree

1 file changed

+29
-9
lines changed

1 file changed

+29
-9
lines changed

pkg/dia/scraper/exchange-scrapers/VelarScraper.go

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ func (s *VelarScraper) mainLoop() {
108108
case <-s.ticker.C:
109109
err := s.Update()
110110
if err != nil {
111-
s.logger.Error(err)
111+
s.logger.WithError(err).Error("failed to run Update")
112112
}
113113
case <-s.shutdown:
114114
s.logger.Println("shutting down")
@@ -121,7 +121,6 @@ func (s *VelarScraper) mainLoop() {
121121
func (s *VelarScraper) Update() error {
122122
txs, err := s.api.GetAllBlockTransactions(s.currentHeight)
123123
if err != nil {
124-
s.logger.WithError(err).Error("failed to GetBlockTransactions")
125124
return err
126125
}
127126

@@ -130,10 +129,14 @@ func (s *VelarScraper) Update() error {
130129
}
131130
s.currentHeight += 1
132131

133-
swapTxs := s.filterSwapTransactions(txs)
132+
swapTxs, err := s.fetchSwapTransactions(txs)
133+
if err != nil {
134+
return err
135+
}
134136
if len(swapTxs) == 0 {
135137
return nil
136138
}
139+
137140
pools, err := s.getPools()
138141
if err != nil {
139142
s.logger.WithError(err).Error("failed to GetAllPoolsExchange")
@@ -154,6 +157,17 @@ func (s *VelarScraper) Update() error {
154157
continue
155158
}
156159

160+
if swapInfo.TokenIn == "" || swapInfo.TokenOut == "" {
161+
for _, arg := range tx.ContractCall.FunctionArgs {
162+
switch arg.Name {
163+
case "token-in":
164+
swapInfo.TokenIn = arg.Repr[1:]
165+
case "token-out":
166+
swapInfo.TokenOut = arg.Repr[1:]
167+
}
168+
}
169+
}
170+
157171
diaTrade := s.handleTrade(&pool, swapInfo, tx)
158172
s.logger.
159173
WithField("parentAddress", pool.Address).
@@ -171,7 +185,7 @@ func (s *VelarScraper) getPools() ([]dia.Pool, error) {
171185
return s.db.GetAllPoolsExchange(s.exchangeName, 0)
172186
}
173187

174-
func (s *VelarScraper) filterSwapTransactions(txs []stackshelper.Transaction) []stackshelper.Transaction {
188+
func (s *VelarScraper) fetchSwapTransactions(txs []stackshelper.Transaction) ([]stackshelper.Transaction, error) {
175189
swapTxs := make([]stackshelper.Transaction, 0)
176190

177191
for _, tx := range txs {
@@ -180,11 +194,20 @@ func (s *VelarScraper) filterSwapTransactions(txs []stackshelper.Transaction) []
180194
strings.HasPrefix(tx.ContractCall.FunctionName, "swap")
181195

182196
if isSwapTx && tx.TxStatus == "success" {
183-
swapTxs = append(swapTxs, tx)
197+
// This is a temporary workaround introduced due to a bug in hiro stacks API.
198+
// Results returned from /blocks/{block_height}/transactions route have empty
199+
// `name` field in `contract_call.function_args` list.
200+
// TODO: remove this as soon as the issue is fixed.
201+
normalizedTx, err := s.api.GetTransactionAt(tx.TxID)
202+
if err != nil {
203+
return nil, err
204+
}
205+
206+
swapTxs = append(swapTxs, normalizedTx)
184207
}
185208
}
186209

187-
return swapTxs
210+
return swapTxs, nil
188211
}
189212

190213
func (s *VelarScraper) handleTrade(pool *dia.Pool, swapInfo velarhelper.SwapInfo, tx stackshelper.Transaction) *dia.Trade {
@@ -199,7 +222,6 @@ func (s *VelarScraper) handleTrade(pool *dia.Pool, swapInfo velarhelper.SwapInfo
199222
var trade dia.Trade
200223

201224
if swapInfo.TokenIn == token0.Address {
202-
log.Info("here")
203225
trade.Pair = fmt.Sprintf("%s-%s", token1.Symbol, token0.Symbol)
204226
trade.Symbol = token1.Symbol
205227
trade.BaseToken = token0
@@ -209,9 +231,7 @@ func (s *VelarScraper) handleTrade(pool *dia.Pool, swapInfo velarhelper.SwapInfo
209231
amount1Out, _ := utils.StringToFloat64(amountOut, int64(token1.Decimals))
210232
volume = amount1Out
211233
price = amount0In / amount1Out
212-
213234
} else {
214-
log.Info("there")
215235
trade.Pair = fmt.Sprintf("%s-%s", token0.Symbol, token1.Symbol)
216236
trade.Symbol = token0.Symbol
217237
trade.BaseToken = token1

0 commit comments

Comments
 (0)