-
Notifications
You must be signed in to change notification settings - Fork 5
/
core_helper.go
90 lines (77 loc) · 2.52 KB
/
core_helper.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
package keeper
import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/duality-labs/duality/x/dex/types"
)
///////////////////////////////////////////////////////////////////////////////
// STATE CALCULATIONS //
///////////////////////////////////////////////////////////////////////////////
func (k Keeper) GetCurrPrice(ctx sdk.Context, tradePairID *types.TradePairID) (sdk.Dec, bool) {
liq := k.GetCurrLiq(ctx, tradePairID)
if liq != nil {
return liq.Price(), true
}
return sdk.ZeroDec(), false
}
// Returns a takerToMaker tick index
func (k Keeper) GetCurrTickIndexTakerToMaker(
ctx sdk.Context,
tradePairID *types.TradePairID,
) (int64, bool) {
liq := k.GetCurrLiq(ctx, tradePairID)
if liq != nil {
return liq.TickIndex(), true
}
return 0, false
}
// Returns a takerToMaker tick index
func (k Keeper) GetCurrTickIndexTakerToMakerNormalized(
ctx sdk.Context,
tradePairID *types.TradePairID,
) (int64, bool) {
tickIndexTakerToMaker, found := k.GetCurrTickIndexTakerToMaker(ctx, tradePairID)
if found {
tickIndexTakerToMakerNormalized := tradePairID.TickIndexNormalized(tickIndexTakerToMaker)
return tickIndexTakerToMakerNormalized, true
}
return 0, false
}
func (k Keeper) GetCurrLiq(ctx sdk.Context, tradePairID *types.TradePairID) *types.TickLiquidity {
ti := k.NewTickIterator(ctx, tradePairID)
defer ti.Close()
for ; ti.Valid(); ti.Next() {
tick := ti.Value()
if tick.HasToken() {
return &tick
}
}
return nil
}
///////////////////////////////////////////////////////////////////////////////
// TOKENIZER UTILS //
///////////////////////////////////////////////////////////////////////////////
func (k Keeper) MintShares(ctx sdk.Context, addr sdk.AccAddress, shareCoin sdk.Coin) error {
// mint share tokens
sharesCoins := sdk.Coins{shareCoin}
if err := k.bankKeeper.MintCoins(ctx, types.ModuleName, sharesCoins); err != nil {
return err
}
// transfer them to addr
err := k.bankKeeper.SendCoinsFromModuleToAccount(ctx, types.ModuleName, addr, sharesCoins)
return err
}
func (k Keeper) BurnShares(
ctx sdk.Context,
addr sdk.AccAddress,
amount sdk.Int,
sharesID string,
) error {
sharesCoins := sdk.Coins{sdk.NewCoin(sharesID, amount)}
// transfer tokens to module
if err := k.bankKeeper.SendCoinsFromAccountToModule(ctx, addr, types.ModuleName, sharesCoins); err != nil {
return err
}
// burn tokens
err := k.bankKeeper.BurnCoins(ctx, types.ModuleName, sharesCoins)
return err
}