/
pool.go
164 lines (137 loc) · 4.91 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package types
import (
errorsmod "cosmossdk.io/errors"
"cosmossdk.io/math"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
ammtypes "github.com/elys-network/elys/x/amm/types"
)
func NewPool(poolId uint64) Pool {
return Pool{
AmmPoolId: poolId,
Health: sdk.NewDec(100),
Enabled: true,
Closed: false,
BorrowInterestRate: sdk.NewDecFromIntWithPrec(sdk.NewInt(1), 1),
PoolAssetsLong: []PoolAsset{},
PoolAssetsShort: []PoolAsset{},
LastHeightBorrowInterestRateComputed: 0,
FundingRate: sdk.ZeroDec(),
}
}
// Get relevant pool asset array based on position direction
func (p *Pool) GetPoolAssets(position Position) *[]PoolAsset {
if position == Position_LONG {
return &p.PoolAssetsLong
} else {
return &p.PoolAssetsShort
}
}
// Get relevant pool asset based on position direction and asset denom
func (p *Pool) GetPoolAsset(position Position, assetDenom string) *PoolAsset {
poolAssets := p.GetPoolAssets(position)
for i, asset := range *poolAssets {
if asset.AssetDenom == assetDenom {
return &(*poolAssets)[i]
}
}
return nil
}
// Update the asset balance
func (p *Pool) UpdateBalance(ctx sdk.Context, assetDenom string, amount math.Int, isIncrease bool, position Position) error {
poolAsset := p.GetPoolAsset(position, assetDenom)
if poolAsset == nil {
return errorsmod.Wrap(sdkerrors.ErrInvalidCoins, "invalid asset denom")
}
if isIncrease {
poolAsset.AssetBalance = poolAsset.AssetBalance.Add(amount)
} else {
poolAsset.AssetBalance = poolAsset.AssetBalance.Sub(amount)
}
return nil
}
// Update the asset liabilities
func (p *Pool) UpdateLiabilities(ctx sdk.Context, assetDenom string, amount math.Int, isIncrease bool, position Position) error {
poolAsset := p.GetPoolAsset(position, assetDenom)
if poolAsset == nil {
return errorsmod.Wrap(sdkerrors.ErrInvalidCoins, "invalid asset denom")
}
if isIncrease {
poolAsset.Liabilities = poolAsset.Liabilities.Add(amount)
} else {
poolAsset.Liabilities = poolAsset.Liabilities.Sub(amount)
}
return nil
}
// Update the asset take profit liabilities
func (p *Pool) UpdateTakeProfitLiabilities(ctx sdk.Context, assetDenom string, amount math.Int, isIncrease bool, position Position) error {
poolAsset := p.GetPoolAsset(position, assetDenom)
if poolAsset == nil {
return errorsmod.Wrap(sdkerrors.ErrInvalidCoins, "invalid asset denom")
}
if isIncrease {
poolAsset.TakeProfitLiabilities = poolAsset.TakeProfitLiabilities.Add(amount)
} else {
poolAsset.TakeProfitLiabilities = poolAsset.TakeProfitLiabilities.Sub(amount)
}
return nil
}
// Update the asset take profit custody
func (p *Pool) UpdateTakeProfitCustody(ctx sdk.Context, assetDenom string, amount math.Int, isIncrease bool, position Position) error {
poolAsset := p.GetPoolAsset(position, assetDenom)
if poolAsset == nil {
return errorsmod.Wrap(sdkerrors.ErrInvalidCoins, "invalid asset denom")
}
if isIncrease {
poolAsset.TakeProfitCustody = poolAsset.TakeProfitCustody.Add(amount)
} else {
poolAsset.TakeProfitCustody = poolAsset.TakeProfitCustody.Sub(amount)
}
return nil
}
// Update the asset custody
func (p *Pool) UpdateCustody(ctx sdk.Context, assetDenom string, amount math.Int, isIncrease bool, position Position) error {
poolAsset := p.GetPoolAsset(position, assetDenom)
if poolAsset == nil {
return errorsmod.Wrap(sdkerrors.ErrInvalidCoins, "invalid asset denom")
}
if isIncrease {
poolAsset.Custody = poolAsset.Custody.Add(amount)
} else {
poolAsset.Custody = poolAsset.Custody.Sub(amount)
}
return nil
}
// Update the unsettled liabilities balance
func (p *Pool) UpdateBlockBorrowInterest(ctx sdk.Context, assetDenom string, amount math.Int, isIncrease bool, position Position) error {
poolAsset := p.GetPoolAsset(position, assetDenom)
if poolAsset == nil {
return errorsmod.Wrap(sdkerrors.ErrInvalidCoins, "invalid asset denom")
}
if isIncrease {
poolAsset.BlockBorrowInterest = poolAsset.BlockBorrowInterest.Add(amount)
} else {
poolAsset.BlockBorrowInterest = poolAsset.BlockBorrowInterest.Sub(amount)
}
return nil
}
// Initialite pool asset according to its corresponding amm pool assets.
func (p *Pool) InitiatePool(ctx sdk.Context, ammPool *ammtypes.Pool) error {
if ammPool == nil {
return errorsmod.Wrap(sdkerrors.ErrInvalidType, "invalid amm pool")
}
// Set pool Id
p.AmmPoolId = ammPool.PoolId
for _, asset := range ammPool.PoolAssets {
poolAsset := PoolAsset{
Liabilities: sdk.ZeroInt(),
Custody: sdk.ZeroInt(),
AssetBalance: sdk.ZeroInt(),
BlockBorrowInterest: sdk.ZeroInt(),
AssetDenom: asset.Token.Denom,
}
p.PoolAssetsLong = append(p.PoolAssetsLong, poolAsset)
p.PoolAssetsShort = append(p.PoolAssetsShort, poolAsset)
}
return nil
}