-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathtrade.go
392 lines (334 loc) · 13.8 KB
/
trade.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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
package transform
import (
"fmt"
"math"
"time"
"github.com/guregu/null"
"github.com/pkg/errors"
"github.com/stellar/go/exp/orderbook"
"github.com/stellar/go/ingest"
"github.com/stellar/go/support/log"
"github.com/stellar/go/xdr"
"github.com/stellar/stellar-etl/internal/toid"
"github.com/stellar/stellar-etl/internal/utils"
)
// TransformTrade converts a relevant operation from the history archive ingestion system into a form suitable for BigQuery
func TransformTrade(operationIndex int32, operationID int64, transaction ingest.LedgerTransaction, ledgerCloseTime time.Time) ([]TradeOutput, error) {
operationResults, ok := transaction.Result.OperationResults()
if !ok {
return []TradeOutput{}, fmt.Errorf("could not get any results from this transaction")
}
if !transaction.Result.Successful() {
return []TradeOutput{}, fmt.Errorf("transaction failed; no trades")
}
operation := transaction.Envelope.Operations()[operationIndex]
// operation id is +1 incremented to stay in sync with ingest package
outputOperationID := operationID + 1
claimedOffers, BuyingOffer, sellerIsExact, err := extractClaimedOffers(operationResults, operationIndex, operation.Body.Type)
if err != nil {
return []TradeOutput{}, err
}
transformedTrades := []TradeOutput{}
for claimOrder, claimOffer := range claimedOffers {
outputOrder := int32(claimOrder)
outputLedgerClosedAt := ledgerCloseTime
var outputSellingAssetType, outputSellingAssetCode, outputSellingAssetIssuer string
err = claimOffer.AssetSold().Extract(&outputSellingAssetType, &outputSellingAssetCode, &outputSellingAssetIssuer)
if err != nil {
return []TradeOutput{}, err
}
outputSellingAssetID := FarmHashAsset(outputSellingAssetCode, outputSellingAssetIssuer, outputSellingAssetType)
outputSellingAmount := claimOffer.AmountSold()
if outputSellingAmount < 0 {
return []TradeOutput{}, fmt.Errorf("amount sold is negative (%d) for operation at index %d", outputSellingAmount, operationIndex)
}
var outputBuyingAssetType, outputBuyingAssetCode, outputBuyingAssetIssuer string
err = claimOffer.AssetBought().Extract(&outputBuyingAssetType, &outputBuyingAssetCode, &outputBuyingAssetIssuer)
if err != nil {
return []TradeOutput{}, err
}
outputBuyingAssetID := FarmHashAsset(outputBuyingAssetCode, outputBuyingAssetIssuer, outputBuyingAssetType)
outputBuyingAmount := int64(claimOffer.AmountBought())
if outputBuyingAmount < 0 {
return []TradeOutput{}, fmt.Errorf("amount bought is negative (%d) for operation at index %d", outputBuyingAmount, operationIndex)
}
if outputSellingAmount == 0 && outputBuyingAmount == 0 {
log.Debugf("Both Selling and Buying amount are 0 for operation at index %d", operationIndex)
continue
}
// Final price should be buy / sell
outputPriceN, outputPriceD, err := findTradeSellPrice(transaction, operationIndex, claimOffer)
if err != nil {
return []TradeOutput{}, err
}
var outputSellingAccountAddress string
var liquidityPoolID null.String
var outputPoolFee, roundingSlippageBips null.Int
var outputSellingOfferID, outputBuyingOfferID null.Int
var tradeType int32
if claimOffer.Type == xdr.ClaimAtomTypeClaimAtomTypeLiquidityPool {
id := claimOffer.MustLiquidityPool().LiquidityPoolId
liquidityPoolID = null.StringFrom(PoolIDToString(id))
tradeType = int32(2)
var fee uint32
if fee, err = findPoolFee(transaction, operationIndex, id); err != nil {
return []TradeOutput{}, fmt.Errorf("cannot parse fee for liquidity pool %v", liquidityPoolID)
}
outputPoolFee = null.IntFrom(int64(fee))
change, err := liquidityPoolChange(transaction, operationIndex, claimOffer)
if err != nil {
return nil, err
}
if change != nil {
roundingSlippageBips, err = roundingSlippage(transaction, operationIndex, claimOffer, change)
if err != nil {
return nil, err
}
}
} else {
outputSellingOfferID = null.IntFrom(int64(claimOffer.OfferId()))
outputSellingAccountAddress = claimOffer.SellerId().Address()
tradeType = int32(1)
}
if BuyingOffer != nil {
outputBuyingOfferID = null.IntFrom(int64(BuyingOffer.OfferId))
} else {
outputBuyingOfferID = null.IntFrom(toid.EncodeOfferId(uint64(operationID)+1, toid.TOIDType))
}
var outputBuyingAccountAddress string
if buyer := operation.SourceAccount; buyer != nil {
accid := buyer.ToAccountId()
outputBuyingAccountAddress = accid.Address()
} else {
sa := transaction.Envelope.SourceAccount().ToAccountId()
outputBuyingAccountAddress = sa.Address()
}
trade := TradeOutput{
Order: outputOrder,
LedgerClosedAt: outputLedgerClosedAt,
SellingAccountAddress: outputSellingAccountAddress,
SellingAssetType: outputSellingAssetType,
SellingAssetCode: outputSellingAssetCode,
SellingAssetIssuer: outputSellingAssetIssuer,
SellingAssetID: outputSellingAssetID,
SellingAmount: utils.ConvertStroopValueToReal(outputSellingAmount),
BuyingAccountAddress: outputBuyingAccountAddress,
BuyingAssetType: outputBuyingAssetType,
BuyingAssetCode: outputBuyingAssetCode,
BuyingAssetIssuer: outputBuyingAssetIssuer,
BuyingAssetID: outputBuyingAssetID,
BuyingAmount: utils.ConvertStroopValueToReal(xdr.Int64(outputBuyingAmount)),
PriceN: outputPriceN,
PriceD: outputPriceD,
SellingOfferID: outputSellingOfferID,
BuyingOfferID: outputBuyingOfferID,
SellingLiquidityPoolID: liquidityPoolID,
LiquidityPoolFee: outputPoolFee,
HistoryOperationID: outputOperationID,
TradeType: tradeType,
RoundingSlippage: roundingSlippageBips,
SellerIsExact: sellerIsExact,
}
transformedTrades = append(transformedTrades, trade)
}
return transformedTrades, nil
}
func extractClaimedOffers(operationResults []xdr.OperationResult, operationIndex int32, operationType xdr.OperationType) (claimedOffers []xdr.ClaimAtom, BuyingOffer *xdr.OfferEntry, sellerIsExact null.Bool, err error) {
if operationIndex >= int32(len(operationResults)) {
err = fmt.Errorf("operation index of %d is out of bounds in result slice (len = %d)", operationIndex, len(operationResults))
return
}
if operationResults[operationIndex].Tr == nil {
err = fmt.Errorf("could not get result Tr for operation at index %d", operationIndex)
return
}
operationTr, ok := operationResults[operationIndex].GetTr()
if !ok {
err = fmt.Errorf("could not get result Tr for operation at index %d", operationIndex)
return
}
switch operationType {
case xdr.OperationTypeManageBuyOffer:
var buyOfferResult xdr.ManageBuyOfferResult
if buyOfferResult, ok = operationTr.GetManageBuyOfferResult(); !ok {
err = fmt.Errorf("could not get ManageBuyOfferResult for operation at index %d", operationIndex)
return
}
if success, ok := buyOfferResult.GetSuccess(); ok {
claimedOffers = success.OffersClaimed
BuyingOffer = success.Offer.Offer
return
}
err = fmt.Errorf("could not get ManageOfferSuccess for operation at index %d", operationIndex)
case xdr.OperationTypeManageSellOffer:
var sellOfferResult xdr.ManageSellOfferResult
if sellOfferResult, ok = operationTr.GetManageSellOfferResult(); !ok {
err = fmt.Errorf("could not get ManageSellOfferResult for operation at index %d", operationIndex)
return
}
if success, ok := sellOfferResult.GetSuccess(); ok {
claimedOffers = success.OffersClaimed
BuyingOffer = success.Offer.Offer
return
}
err = fmt.Errorf("could not get ManageOfferSuccess for operation at index %d", operationIndex)
case xdr.OperationTypeCreatePassiveSellOffer:
// KNOWN ISSUE: stellar-core creates results for CreatePassiveOffer operations
// with the wrong result arm set.
if operationTr.Type == xdr.OperationTypeManageSellOffer {
passiveSellResult := operationTr.MustManageSellOfferResult().MustSuccess()
claimedOffers = passiveSellResult.OffersClaimed
BuyingOffer = passiveSellResult.Offer.Offer
return
} else {
passiveSellResult := operationTr.MustCreatePassiveSellOfferResult().MustSuccess()
claimedOffers = passiveSellResult.OffersClaimed
BuyingOffer = passiveSellResult.Offer.Offer
return
}
case xdr.OperationTypePathPaymentStrictSend:
var pathSendResult xdr.PathPaymentStrictSendResult
sellerIsExact = null.BoolFrom(false)
if pathSendResult, ok = operationTr.GetPathPaymentStrictSendResult(); !ok {
err = fmt.Errorf("could not get PathPaymentStrictSendResult for operation at index %d", operationIndex)
return
}
success, ok := pathSendResult.GetSuccess()
if ok {
claimedOffers = success.Offers
return
}
err = fmt.Errorf("could not get PathPaymentStrictSendSuccess for operation at index %d", operationIndex)
case xdr.OperationTypePathPaymentStrictReceive:
var pathReceiveResult xdr.PathPaymentStrictReceiveResult
sellerIsExact = null.BoolFrom(true)
if pathReceiveResult, ok = operationTr.GetPathPaymentStrictReceiveResult(); !ok {
err = fmt.Errorf("could not get PathPaymentStrictReceiveResult for operation at index %d", operationIndex)
return
}
if success, ok := pathReceiveResult.GetSuccess(); ok {
claimedOffers = success.Offers
return
}
err = fmt.Errorf("could not get GetPathPaymentStrictReceiveSuccess for operation at index %d", operationIndex)
default:
err = fmt.Errorf("operation of type %s at index %d does not result in trades", operationType, operationIndex)
return
}
return
}
func findTradeSellPrice(t ingest.LedgerTransaction, operationIndex int32, trade xdr.ClaimAtom) (n, d int64, err error) {
if trade.Type == xdr.ClaimAtomTypeClaimAtomTypeLiquidityPool {
return int64(trade.AmountBought()), int64(trade.AmountSold()), nil
}
key := xdr.LedgerKey{}
if err := key.SetOffer(trade.SellerId(), uint64(trade.OfferId())); err != nil {
return 0, 0, errors.Wrap(err, "Could not create offer ledger key")
}
change, err := findLatestOperationChange(t, operationIndex, key)
if err != nil {
return 0, 0, errors.Wrap(err, "could not find change for trade offer")
}
return int64(change.Pre.Data.MustOffer().Price.N), int64(change.Pre.Data.MustOffer().Price.D), nil
}
func findLatestOperationChange(t ingest.LedgerTransaction, operationIndex int32, key xdr.LedgerKey) (ingest.Change, error) {
changes, err := t.GetOperationChanges(uint32(operationIndex))
if err != nil {
return ingest.Change{}, errors.Wrap(err, "could not determine changes for operation")
}
var change ingest.Change
// traverse through the slice in reverse order
for i := len(changes) - 1; i >= 0; i-- {
change = changes[i]
if change.Pre != nil {
preKey, err := change.Pre.LedgerKey()
if err != nil {
return ingest.Change{}, errors.Wrap(err, "could not determine ledger key for change")
}
if key.Equals(preKey) {
return change, nil
}
}
}
return ingest.Change{}, errors.Errorf("could not find operation for key %v", key)
}
func findPoolFee(t ingest.LedgerTransaction, operationIndex int32, poolID xdr.PoolId) (fee uint32, err error) {
key := xdr.LedgerKey{}
if err := key.SetLiquidityPool(poolID); err != nil {
return 0, errors.Wrap(err, "Could not create liquidity pool ledger key")
}
change, err := findLatestOperationChange(t, operationIndex, key)
if err != nil {
return 0, errors.Wrap(err, "could not find change for liquidity pool")
}
return uint32(change.Pre.Data.MustLiquidityPool().Body.MustConstantProduct().Params.Fee), nil
}
func liquidityPoolChange(t ingest.LedgerTransaction, operationIndex int32, trade xdr.ClaimAtom) (*ingest.Change, error) {
if trade.Type != xdr.ClaimAtomTypeClaimAtomTypeLiquidityPool {
return nil, nil
}
poolID := trade.LiquidityPool.LiquidityPoolId
key := xdr.LedgerKey{}
if err := key.SetLiquidityPool(poolID); err != nil {
return nil, errors.Wrap(err, "Could not create liquidity pool ledger key")
}
change, err := findLatestOperationChange(t, operationIndex, key)
if err != nil {
return nil, errors.Wrap(err, "Could not find change for liquidity pool")
}
return &change, nil
}
func liquidityPoolReserves(trade xdr.ClaimAtom, change *ingest.Change) (int64, int64) {
pre := change.Pre.Data.MustLiquidityPool().Body.ConstantProduct
a := int64(pre.ReserveA)
b := int64(pre.ReserveB)
if !trade.AssetSold().Equals(pre.Params.AssetA) {
a, b = b, a
}
return a, b
}
func roundingSlippage(t ingest.LedgerTransaction, operationIndex int32, trade xdr.ClaimAtom, change *ingest.Change) (null.Int, error) {
disbursedReserves, depositedReserves := liquidityPoolReserves(trade, change)
pre := change.Pre.Data.MustLiquidityPool().Body.ConstantProduct
op, found := t.GetOperation(uint32(operationIndex))
if !found {
return null.Int{}, errors.New("Could not find operation")
}
amountDeposited := trade.AmountBought()
amountDisbursed := trade.AmountSold()
switch op.Body.Type {
case xdr.OperationTypePathPaymentStrictReceive:
// User specified the disbursed amount
_, roundingSlippageBips, ok := orderbook.CalculatePoolPayout(
xdr.Int64(depositedReserves),
xdr.Int64(disbursedReserves),
amountDisbursed,
pre.Params.Fee,
true,
)
if !ok {
// This is a temporary workaround and will be addressed when
// https://github.com/stellar/go/issues/4203 is closed
roundingSlippageBips = xdr.Int64(math.MaxInt64)
}
return null.IntFrom(int64(roundingSlippageBips)), nil
case xdr.OperationTypePathPaymentStrictSend:
// User specified the deposited amount
_, roundingSlippageBips, ok := orderbook.CalculatePoolPayout(
xdr.Int64(depositedReserves),
xdr.Int64(disbursedReserves),
amountDeposited,
pre.Params.Fee,
true,
)
if !ok {
// Temporary workaround for https://github.com/stellar/go/issues/4203
// Given strict receives that would overflow here, minimum slippage
// so they get excluded.
roundingSlippageBips = xdr.Int64(math.MinInt64)
}
return null.IntFrom(int64(roundingSlippageBips)), nil
default:
return null.Int{}, fmt.Errorf("unexpected trade operation type: %v", op.Body.Type)
}
}