-
Notifications
You must be signed in to change notification settings - Fork 607
/
pool.go
133 lines (108 loc) · 3.79 KB
/
pool.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
package model
import (
fmt "fmt"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/gogo/protobuf/proto"
"github.com/osmosis-labs/osmosis/v15/x/cosmwasmpool/cosmwasm"
"github.com/osmosis-labs/osmosis/v15/x/cosmwasmpool/types"
gammtypes "github.com/osmosis-labs/osmosis/v15/x/gamm/types"
poolmanagertypes "github.com/osmosis-labs/osmosis/v15/x/poolmanager/types"
)
type Pool struct {
CosmWasmPool
WasmKeeper types.WasmKeeper
}
var (
_ poolmanagertypes.PoolI = &Pool{}
_ types.CosmWasmExtension = &Pool{}
)
// NewCosmWasmPool creates a new CosmWasm pool with the specified parameters.
func NewCosmWasmPool(poolId uint64, codeId uint64, instantiateMsg []byte) Pool {
pool := Pool{
CosmWasmPool: CosmWasmPool{
PoolAddress: gammtypes.NewPoolAddress(poolId).String(),
ContractAddress: "", // N.B. This is to be set in InitializePool()
PoolId: poolId,
CodeId: codeId,
InstantiateMsg: instantiateMsg,
},
WasmKeeper: nil, // N.B.: this is set in InitializePool().
}
return pool
}
// poolmanager.PoolI interface implementation
// GetAddress returns the address of the concentrated liquidity pool
func (p Pool) GetAddress() sdk.AccAddress {
addr, err := sdk.AccAddressFromBech32(p.PoolAddress)
if err != nil {
panic(fmt.Sprintf("could not bech32 decode address of pool with id: %d", p.GetId()))
}
return addr
}
// GetId returns the id of the concentrated liquidity pool
func (p Pool) GetId() uint64 {
return p.PoolId
}
// String returns the json marshalled string of the pool
func (p Pool) String() string {
return p.CosmWasmPool.String()
}
// GetSwapFee returns the swap fee of the pool.
func (p Pool) GetSwapFee(ctx sdk.Context) sdk.Dec {
request := cosmwasm.GetSwapFee{}
response := cosmwasm.MustQuery[cosmwasm.GetSwapFee, cosmwasm.GetSwapFeeResponse](ctx, p.WasmKeeper, p.ContractAddress, request)
return response.SwapFee
}
// GetExitFee returns the exit fee of the pool
func (p Pool) GetExitFee(ctx sdk.Context) sdk.Dec {
request := cosmwasm.GetExitFee{}
response := cosmwasm.MustQuery[cosmwasm.GetExitFee, cosmwasm.GetExitFeeResponse](ctx, p.WasmKeeper, p.ContractAddress, request)
return response.ExitFee
}
// IsActive returns true if the pool is active
func (p Pool) IsActive(ctx sdk.Context) bool {
return true
}
// SpotPrice returns the spot price of the pool.
func (p Pool) SpotPrice(ctx sdk.Context, baseAssetDenom string, quoteAssetDenom string) (sdk.Dec, error) {
request := cosmwasm.SpotPrice{}
response, err := cosmwasm.Query[cosmwasm.SpotPrice, cosmwasm.SpotPriceResponse](ctx, p.WasmKeeper, p.ContractAddress, request)
if err != nil {
return sdk.Dec{}, err
}
return sdk.MustNewDecFromStr(response.SpotPrice), nil
}
// GetTotalShares returns the total shares of the pool.
func (p Pool) GetTotalShares() sdk.Int {
panic("TODO: remove from PoolI")
}
// GetType returns the type of the pool.
func (p Pool) GetType() poolmanagertypes.PoolType {
return poolmanagertypes.CosmWasm
}
// GetTotalPoolLiquidity returns the total pool liquidity
func (p Pool) GetTotalPoolLiquidity(ctx sdk.Context) sdk.Coins {
request := cosmwasm.GetTotalPoolLiquidity{}
response := cosmwasm.MustQuery[cosmwasm.GetTotalPoolLiquidity, cosmwasm.GetTotalPoolLiquidityResponse](ctx, p.WasmKeeper, p.ContractAddress, request)
return response.TotalPoolLiquidity
}
// types.CosmWasmExtension interface implementation
func (p Pool) GetCodeId() uint64 {
return p.CodeId
}
func (p Pool) GetInstantiateMsg() []byte {
return p.InstantiateMsg
}
func (p Pool) GetContractAddress() string {
return p.ContractAddress
}
func (p Pool) SetContractAddress(contractAddress string) {
p.ContractAddress = contractAddress
}
func (p Pool) GetStoreModel() proto.Message {
return &p.CosmWasmPool
}
// Set the wasm keeper.
func (p *Pool) SetWasmKeeper(wasmKeeper types.WasmKeeper) {
p.WasmKeeper = wasmKeeper
}