forked from stellar/go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchange_trust_details.go
85 lines (70 loc) · 2.32 KB
/
change_trust_details.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
package ingest
import (
"fmt"
"github.com/stellar/go/xdr"
)
type ChangeTrustDetail struct {
AssetCode string `json:"asset_code"`
AssetIssuer string `json:"asset_issuer"`
AssetType string `json:"asset_type"`
LiquidityPoolID string `json:"liquidity_pool_id"`
Limit int64 `json:"limit"`
Trustee string `json:"trustee"`
Trustor string `json:"trustor"`
TrustorMuxed string `json:"trustor_muxed"`
TrustorMuxedID uint64 `json:"trustor_muxed_id"`
}
func (o *LedgerOperation) ChangeTrustDetails() (ChangeTrustDetail, error) {
op, ok := o.Operation.Body.GetChangeTrustOp()
if !ok {
return ChangeTrustDetail{}, fmt.Errorf("could not access GetChangeTrust info for this operation (index %d)", o.OperationIndex)
}
var err error
changeTrustDetail := ChangeTrustDetail{
Trustor: o.SourceAccount(),
Limit: int64(op.Limit),
}
if op.Line.Type == xdr.AssetTypeAssetTypePoolShare {
changeTrustDetail.AssetType, changeTrustDetail.LiquidityPoolID, err = getLiquidityPoolAssetDetails(*op.Line.LiquidityPool)
if err != nil {
return ChangeTrustDetail{}, err
}
} else {
var assetCode, assetIssuer, assetType string
err = op.Line.ToAsset().Extract(&assetType, &assetCode, &assetIssuer)
if err != nil {
return ChangeTrustDetail{}, err
}
changeTrustDetail.AssetCode = assetCode
changeTrustDetail.AssetIssuer = assetIssuer
changeTrustDetail.AssetType = assetType
changeTrustDetail.Trustee = assetIssuer
}
var trustorMuxed string
var trustorMuxedID uint64
trustorMuxed, trustorMuxedID, err = getMuxedAccountDetails(o.sourceAccountXDR())
if err != nil {
return ChangeTrustDetail{}, err
}
changeTrustDetail.TrustorMuxed = trustorMuxed
changeTrustDetail.TrustorMuxedID = trustorMuxedID
return changeTrustDetail, nil
}
func getLiquidityPoolAssetDetails(lpp xdr.LiquidityPoolParameters) (string, string, error) {
if lpp.Type != xdr.LiquidityPoolTypeLiquidityPoolConstantProduct {
return "", "", fmt.Errorf("unknown liquidity pool type %d", lpp.Type)
}
cp := lpp.ConstantProduct
var err error
var poolID xdr.PoolId
var poolIDString string
poolID, err = xdr.NewPoolId(cp.AssetA, cp.AssetB, cp.Fee)
if err != nil {
return "", "", err
}
poolIDString, err = PoolIDToString(poolID)
if err != nil {
return "", "", err
}
return "liquidity_pool_shares", poolIDString, nil
}