forked from stellar/go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
trust_line_asset.go
113 lines (94 loc) · 4.07 KB
/
trust_line_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
package txnbuild
import (
"github.com/stellar/go/support/errors"
"github.com/stellar/go/xdr"
)
// TrustLineAsset represents a Stellar trust line asset.
type TrustLineAsset interface {
BasicAsset
GetLiquidityPoolID() (LiquidityPoolId, bool)
ToXDR() (xdr.TrustLineAsset, error)
}
// LiquidityPoolShareTrustLineAsset represents shares in a liquidity pool on the Stellar network.
type LiquidityPoolShareTrustLineAsset struct {
LiquidityPoolID LiquidityPoolId
}
// GetType for LiquidityPoolShareAsset returns the enum type of the asset, based on its code length.
func (lpsa LiquidityPoolShareTrustLineAsset) GetType() (AssetType, error) {
return AssetTypePoolShare, nil
}
// IsNative for LiquidityPoolShareAsset returns false (this is not an XLM asset).
func (lpsa LiquidityPoolShareTrustLineAsset) IsNative() bool { return false }
// GetCode for LiquidityPoolShareAsset returns blank string
func (lpsa LiquidityPoolShareTrustLineAsset) GetCode() string { return "" }
// GetIssuer for LiquidityPoolShareAsset returns blank string
func (lpsa LiquidityPoolShareTrustLineAsset) GetIssuer() string { return "" }
// GetLiquidityPoolID for LiquidityPoolShareTrustLineAsset returns the pool id.
func (lpsa LiquidityPoolShareTrustLineAsset) GetLiquidityPoolID() (LiquidityPoolId, bool) {
return lpsa.LiquidityPoolID, true
}
// ToXDR for LiquidityPoolShareAsset produces a corresponding XDR asset.
func (lpsa LiquidityPoolShareTrustLineAsset) ToXDR() (xdr.TrustLineAsset, error) {
xdrPoolId, err := lpsa.LiquidityPoolID.ToXDR()
if err != nil {
return xdr.TrustLineAsset{}, errors.Wrap(err, "failed to build XDR liquidity pool id")
}
return xdr.TrustLineAsset{LiquidityPoolId: &xdrPoolId}, nil
}
// ToAsset for LiquidityPoolShareTrustLineAsset returns an error.
func (lpsa LiquidityPoolShareTrustLineAsset) ToAsset() (Asset, error) {
return nil, errors.New("Cannot transform LiquidityPoolShare into Asset")
}
// MustToAsset for LiquidityPoolShareTrustLineAsset panics.
func (lpsa LiquidityPoolShareTrustLineAsset) MustToAsset() Asset {
panic("Cannot transform LiquidityPoolShare into Asset")
}
// ToChangeTrustAsset for LiquidityPoolShareTrustLineAsset returns an error.
func (lpsa LiquidityPoolShareTrustLineAsset) ToChangeTrustAsset() (ChangeTrustAsset, error) {
return nil, errors.New("Cannot transform LiquidityPoolShare into ChangeTrustAsset")
}
// MustToChangeTrustAsset for LiquidityPoolShareTrustLineAsset panics.
func (lpsa LiquidityPoolShareTrustLineAsset) MustToChangeTrustAsset() ChangeTrustAsset {
panic("Cannot transform LiquidityPoolShare into ChangeTrustAsset")
}
// ToTrustLineAsset for LiquidityPoolShareTrustLineAsset returns itself unchanged.
func (lpsa LiquidityPoolShareTrustLineAsset) ToTrustLineAsset() (TrustLineAsset, error) {
return lpsa, nil
}
// MustToTrustLineAsset for LiquidityPoolShareTrustLineAsset returns itself unchanged.
func (lpsa LiquidityPoolShareTrustLineAsset) MustToTrustLineAsset() TrustLineAsset {
return lpsa
}
func assetFromTrustLineAssetXDR(xAsset xdr.TrustLineAsset) (TrustLineAsset, error) {
if xAsset.Type == xdr.AssetTypeAssetTypePoolShare {
if xAsset.LiquidityPoolId == nil {
return nil, errors.New("invalid XDR liquidity pool id")
}
poolId, err := liquidityPoolIdFromXDR(*xAsset.LiquidityPoolId)
if err != nil {
return nil, errors.Wrap(err, "invalid XDR liquidity pool id")
}
return LiquidityPoolShareTrustLineAsset{LiquidityPoolID: poolId}, nil
}
a, err := assetFromXDR(xAsset.ToAsset())
if err != nil {
return nil, err
}
return TrustLineAssetWrapper{a}, nil
}
// TrustLineAssetWrapper wraps a native/credit Asset so it generates xdr to be used in a trust line operation.
type TrustLineAssetWrapper struct {
Asset
}
// GetLiquidityPoolID for TrustLineAssetWrapper returns false.
func (tlaw TrustLineAssetWrapper) GetLiquidityPoolID() (LiquidityPoolId, bool) {
return LiquidityPoolId{}, false
}
// ToXDR for TrustLineAssetWrapper generates the xdr.TrustLineAsset.
func (tlaw TrustLineAssetWrapper) ToXDR() (xdr.TrustLineAsset, error) {
x, err := tlaw.Asset.ToXDR()
if err != nil {
return xdr.TrustLineAsset{}, err
}
return x.ToTrustLineAsset(), nil
}