-
Notifications
You must be signed in to change notification settings - Fork 501
/
change_trust_asset.go
134 lines (113 loc) · 5.25 KB
/
change_trust_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
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
package txnbuild
import (
"github.com/stellar/go/support/errors"
"github.com/stellar/go/xdr"
)
// ChangeTrustAsset represents a Stellar change trust asset.
type ChangeTrustAsset interface {
BasicAsset
GetLiquidityPoolID() (LiquidityPoolId, bool)
GetLiquidityPoolParameters() (LiquidityPoolParameters, bool)
ToXDR() (xdr.ChangeTrustAsset, error)
ToChangeTrustAsset() (ChangeTrustAsset, error)
ToTrustLineAsset() (TrustLineAsset, error)
}
// LiquidityPoolShareChangeTrustAsset represents non-XLM assets on the Stellar network.
type LiquidityPoolShareChangeTrustAsset struct {
LiquidityPoolParameters LiquidityPoolParameters
}
// GetType for LiquidityPoolShareChangeTrustAsset returns the enum type of the asset, based on its code length.
func (lpsa LiquidityPoolShareChangeTrustAsset) GetType() (AssetType, error) {
return AssetTypePoolShare, nil
}
// IsNative for LiquidityPoolShareChangeTrustAsset returns false (this is not an XLM asset).
func (lpsa LiquidityPoolShareChangeTrustAsset) IsNative() bool { return false }
// GetCode for LiquidityPoolShareChangeTrustAsset returns blank string
func (lpsa LiquidityPoolShareChangeTrustAsset) GetCode() string { return "" }
// GetIssuer for LiquidityPoolShareChangeTrustAsset returns blank string
func (lpsa LiquidityPoolShareChangeTrustAsset) GetIssuer() string { return "" }
// GetLiquidityPoolID for LiquidityPoolShareChangeTrustAsset returns the pool id computed from the parameters.
func (lpsa LiquidityPoolShareChangeTrustAsset) GetLiquidityPoolID() (LiquidityPoolId, bool) {
poolId, err := NewLiquidityPoolId(lpsa.LiquidityPoolParameters.AssetA, lpsa.LiquidityPoolParameters.AssetB)
return poolId, err == nil
}
// GetLiquidityPoolParameters for LiquidityPoolShareChangeTrustAsset returns the pool parameters.
func (lpsa LiquidityPoolShareChangeTrustAsset) GetLiquidityPoolParameters() (LiquidityPoolParameters, bool) {
return lpsa.LiquidityPoolParameters, true
}
// ToXDR for LiquidityPoolShareChangeTrustAsset produces a corresponding XDR change trust asset.
func (lpsa LiquidityPoolShareChangeTrustAsset) ToXDR() (xdr.ChangeTrustAsset, error) {
xdrPoolParams, err := lpsa.LiquidityPoolParameters.ToXDR()
if err != nil {
return xdr.ChangeTrustAsset{}, errors.Wrap(err, "failed to build XDR liquidity pool parameters")
}
return xdr.ChangeTrustAsset{Type: xdr.AssetTypeAssetTypePoolShare, LiquidityPool: &xdrPoolParams}, nil
}
// ToAsset for LiquidityPoolShareChangeTrustAsset returns an error.
func (lpsa LiquidityPoolShareChangeTrustAsset) ToAsset() (Asset, error) {
return nil, errors.New("Cannot transform LiquidityPoolShare into Asset")
}
// MustToAsset for LiquidityPoolShareChangeTrustAsset panics.
func (lpsa LiquidityPoolShareChangeTrustAsset) MustToAsset() Asset {
panic("Cannot transform LiquidityPoolShare into Asset")
}
// ToChangeTrustAsset for LiquidityPoolShareChangeTrustAsset returns itself unchanged.
func (lpsa LiquidityPoolShareChangeTrustAsset) ToChangeTrustAsset() (ChangeTrustAsset, error) {
return lpsa, nil
}
// MustToChangeTrustAsset for LiquidityPoolShareChangeTrustAsset returns itself unchanged.
func (lpsa LiquidityPoolShareChangeTrustAsset) MustToChangeTrustAsset() ChangeTrustAsset {
return lpsa
}
// ToTrustLineAsset for LiquidityPoolShareChangeTrustAsset hashes the pool parameters to get the pool id, and converts this to a TrustLineAsset.
func (lpsa LiquidityPoolShareChangeTrustAsset) ToTrustLineAsset() (TrustLineAsset, error) {
poolId, err := NewLiquidityPoolId(lpsa.LiquidityPoolParameters.AssetA, lpsa.LiquidityPoolParameters.AssetB)
if err != nil {
return nil, err
}
return LiquidityPoolShareTrustLineAsset{
LiquidityPoolID: poolId,
}, nil
}
// MustToTrustLineAsset for LiquidityPoolShareChangeTrustAsset hashes the pool parameters to get the pool id, and converts this to a TrustLineAsset. It panics on failure.
func (lpsa LiquidityPoolShareChangeTrustAsset) MustToTrustLineAsset() TrustLineAsset {
tla, err := lpsa.ToTrustLineAsset()
if err != nil {
panic(err)
}
return tla
}
func assetFromChangeTrustAssetXDR(xAsset xdr.ChangeTrustAsset) (ChangeTrustAsset, error) {
if xAsset.Type == xdr.AssetTypeAssetTypePoolShare {
params, err := liquidityPoolParametersFromXDR(*xAsset.LiquidityPool)
if err != nil {
return nil, errors.Wrap(err, "invalid XDR liquidity pool parameters")
}
return LiquidityPoolShareChangeTrustAsset{LiquidityPoolParameters: params}, nil
}
a, err := assetFromXDR(xAsset.ToAsset())
if err != nil {
return nil, err
}
return ChangeTrustAssetWrapper{a}, nil
}
// ChangeTrustAssetWrapper wraps a native/credit Asset so it generates xdr to be used in a change trust operation.
type ChangeTrustAssetWrapper struct {
Asset
}
// GetLiquidityPoolID for ChangeTrustAssetWrapper returns false.
func (ctaw ChangeTrustAssetWrapper) GetLiquidityPoolID() (LiquidityPoolId, bool) {
return LiquidityPoolId{}, false
}
// GetLiquidityPoolParameters for ChangeTrustAssetWrapper returns false.
func (ctaw ChangeTrustAssetWrapper) GetLiquidityPoolParameters() (LiquidityPoolParameters, bool) {
return LiquidityPoolParameters{}, false
}
// ToXDR for ChangeTrustAssetWrapper generates the xdr.TrustLineAsset.
func (ctaw ChangeTrustAssetWrapper) ToXDR() (xdr.ChangeTrustAsset, error) {
x, err := ctaw.Asset.ToXDR()
if err != nil {
return xdr.ChangeTrustAsset{}, err
}
return x.ToChangeTrustAsset(), nil
}