forked from stellar/go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
liquidity_pool_parameters.go
58 lines (50 loc) · 1.71 KB
/
liquidity_pool_parameters.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
//lint:file-ignore U1001 Ignore all unused code, staticcheck doesn't understand testify/suite
package txnbuild
import (
"fmt"
"github.com/stellar/go/support/errors"
"github.com/stellar/go/xdr"
)
const LiquidityPoolFeeV18 = xdr.LiquidityPoolFeeV18
// LiquidityPoolParameters represents the Stellar liquidity pool parameters
type LiquidityPoolParameters struct {
AssetA Asset
AssetB Asset
Fee int32
}
func (lpi LiquidityPoolParameters) ToXDR() (xdr.LiquidityPoolParameters, error) {
xdrAssetA, err := lpi.AssetA.ToXDR()
if err != nil {
return xdr.LiquidityPoolParameters{}, errors.Wrap(err, "failed to build XDR AssetA ID")
}
xdrAssetB, err := lpi.AssetB.ToXDR()
if err != nil {
return xdr.LiquidityPoolParameters{}, errors.Wrap(err, "failed to build XDR AssetB ID")
}
return xdr.LiquidityPoolParameters{
Type: xdr.LiquidityPoolTypeLiquidityPoolConstantProduct,
ConstantProduct: &xdr.LiquidityPoolConstantProductParameters{
AssetA: xdrAssetA,
AssetB: xdrAssetB,
Fee: xdr.Int32(lpi.Fee),
},
}, nil
}
func liquidityPoolParametersFromXDR(params xdr.LiquidityPoolParameters) (LiquidityPoolParameters, error) {
if params.Type != xdr.LiquidityPoolTypeLiquidityPoolConstantProduct {
return LiquidityPoolParameters{}, fmt.Errorf("failed to parse XDR type")
}
assetA, err := assetFromXDR(params.ConstantProduct.AssetA)
if err != nil {
return LiquidityPoolParameters{}, errors.Wrap(err, "failed to parse XDR AssetA")
}
assetB, err := assetFromXDR(params.ConstantProduct.AssetB)
if err != nil {
return LiquidityPoolParameters{}, errors.Wrap(err, "failed to parse XDR AssetB")
}
return LiquidityPoolParameters{
AssetA: assetA,
AssetB: assetB,
Fee: int32(params.ConstantProduct.Fee),
}, nil
}