forked from stellar/go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathasset.go
79 lines (65 loc) · 2.54 KB
/
asset.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
package asset
import (
"fmt"
"time"
utils "github.com/stellar/go/ingest/processors/processor_utils"
"github.com/stellar/go/toid"
"github.com/stellar/go/xdr"
)
// AssetOutput is a representation of an asset that aligns with the BigQuery table history_assets
type AssetOutput struct {
AssetCode string `json:"asset_code"`
AssetIssuer string `json:"asset_issuer"`
AssetType string `json:"asset_type"`
AssetID int64 `json:"asset_id"`
ClosedAt time.Time `json:"closed_at"`
LedgerSequence uint32 `json:"ledger_sequence"`
}
// TransformAsset converts an asset from a payment operation into a form suitable for BigQuery
func TransformAsset(operation xdr.Operation, operationIndex int32, transactionIndex int32, ledgerSeq int32, lcm xdr.LedgerCloseMeta) (AssetOutput, error) {
operationID := toid.New(ledgerSeq, int32(transactionIndex), operationIndex).ToInt64()
opType := operation.Body.Type
if opType != xdr.OperationTypePayment && opType != xdr.OperationTypeManageSellOffer {
return AssetOutput{}, fmt.Errorf("operation of type %d cannot issue an asset (id %d)", opType, operationID)
}
asset := xdr.Asset{}
switch opType {
case xdr.OperationTypeManageSellOffer:
opSellOf, ok := operation.Body.GetManageSellOfferOp()
if !ok {
return AssetOutput{}, fmt.Errorf("operation of type ManageSellOfferOp cannot issue an asset (id %d)", operationID)
}
asset = opSellOf.Selling
case xdr.OperationTypePayment:
opPayment, ok := operation.Body.GetPaymentOp()
if !ok {
return AssetOutput{}, fmt.Errorf("could not access Payment info for this operation (id %d)", operationID)
}
asset = opPayment.Asset
}
outputAsset, err := TransformSingleAsset(asset)
if err != nil {
return AssetOutput{}, fmt.Errorf("%s (id %d)", err.Error(), operationID)
}
outputCloseTime, err := utils.GetCloseTime(lcm)
if err != nil {
return AssetOutput{}, err
}
outputAsset.ClosedAt = outputCloseTime
outputAsset.LedgerSequence = utils.GetLedgerSequence(lcm)
return outputAsset, nil
}
func TransformSingleAsset(asset xdr.Asset) (AssetOutput, error) {
var outputAssetType, outputAssetCode, outputAssetIssuer string
err := asset.Extract(&outputAssetType, &outputAssetCode, &outputAssetIssuer)
if err != nil {
return AssetOutput{}, fmt.Errorf("could not extract asset from this operation")
}
farmAssetID := utils.FarmHashAsset(outputAssetCode, outputAssetIssuer, outputAssetType)
return AssetOutput{
AssetCode: outputAssetCode,
AssetIssuer: outputAssetIssuer,
AssetType: outputAssetType,
AssetID: farmAssetID,
}, nil
}