/
handler.go
170 lines (148 loc) · 6.2 KB
/
handler.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
165
166
167
168
169
170
package bonds
import (
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/furya-official/fury-blockchain/x/bonds/keeper"
"github.com/furya-official/fury-blockchain/x/bonds/types"
abci "github.com/tendermint/tendermint/abci/types"
)
func NewHandler(k keeper.Keeper) sdk.Handler {
msgServer := keeper.NewMsgServerImpl(k)
return func(ctx sdk.Context, msg sdk.Msg) (*sdk.Result, error) {
ctx = ctx.WithEventManager(sdk.NewEventManager())
switch msg := msg.(type) {
case *types.MsgCreateBond:
res, err := msgServer.CreateBond(sdk.WrapSDKContext(ctx), msg)
return sdk.WrapServiceResult(ctx, res, err)
case *types.MsgEditBond:
res, err := msgServer.EditBond(sdk.WrapSDKContext(ctx), msg)
return sdk.WrapServiceResult(ctx, res, err)
case *types.MsgSetNextAlpha:
res, err := msgServer.SetNextAlpha(sdk.WrapSDKContext(ctx), msg)
return sdk.WrapServiceResult(ctx, res, err)
case *types.MsgUpdateBondState:
res, err := msgServer.UpdateBondState(sdk.WrapSDKContext(ctx), msg)
return sdk.WrapServiceResult(ctx, res, err)
case *types.MsgBuy:
res, err := msgServer.Buy(sdk.WrapSDKContext(ctx), msg)
return sdk.WrapServiceResult(ctx, res, err)
case *types.MsgSell:
res, err := msgServer.Sell(sdk.WrapSDKContext(ctx), msg)
return sdk.WrapServiceResult(ctx, res, err)
case *types.MsgSwap:
res, err := msgServer.Swap(sdk.WrapSDKContext(ctx), msg)
return sdk.WrapServiceResult(ctx, res, err)
case *types.MsgMakeOutcomePayment:
res, err := msgServer.MakeOutcomePayment(sdk.WrapSDKContext(ctx), msg)
return sdk.WrapServiceResult(ctx, res, err)
case *types.MsgWithdrawShare:
res, err := msgServer.WithdrawShare(sdk.WrapSDKContext(ctx), msg)
return sdk.WrapServiceResult(ctx, res, err)
case *types.MsgWithdrawReserve:
res, err := msgServer.WithdrawReserve(sdk.WrapSDKContext(ctx), msg)
return sdk.WrapServiceResult(ctx, res, err)
default:
err := sdkerrors.Wrap(sdkerrors.ErrUnknownRequest, "unrecognized bonds Msg")
return nil, err
}
}
}
func EndBlocker(ctx sdk.Context, keeper keeper.Keeper) []abci.ValidatorUpdate {
iterator := keeper.GetBondIterator(ctx)
for ; iterator.Valid(); iterator.Next() {
bond := keeper.MustGetBondByKey(ctx, iterator.Key())
batch := keeper.MustGetBatch(ctx, bond.BondDid)
// Subtract one block
batch.BlocksRemaining = batch.BlocksRemaining.SubUint64(1)
keeper.SetBatch(ctx, bond.BondDid, batch)
// If blocks remaining > 0 do not perform orders
if !batch.BlocksRemaining.IsZero() {
continue
}
// Store current reserve to check if this has changed later on
reserveBeforeOrderProcessing := bond.CurrentReserve
// Perform orders
keeper.PerformOrders(ctx, bond.BondDid)
// Get bond again just in case current supply was updated
// Get batch again just in case orders were cancelled
bond = keeper.MustGetBond(ctx, bond.BondDid)
batch = keeper.MustGetBatch(ctx, bond.BondDid)
// For augmented, if hatch phase and newSupply >= S0, go to open phase
if bond.FunctionType == types.AugmentedFunction &&
bond.State == types.HatchState.String() {
args := bond.FunctionParameters.AsMap()
if bond.CurrentSupply.Amount.ToDec().GTE(args["S0"]) {
keeper.SetBondState(ctx, bond.BondDid, types.OpenState.String())
bond = keeper.MustGetBond(ctx, bond.BondDid) // get updated bond
}
}
// For augmented, if hatch phase and newSupply >= S0, go to open phase
if bond.FunctionType == types.BondingFunction &&
bond.State == types.HatchState.String() {
args := bond.FunctionParameters.AsMap()
if bond.CurrentSupply.Amount.ToDec().GTE(args["Hatch_Supply"]) {
keeper.SetBondState(ctx, bond.BondDid, types.OpenState.String())
bond = keeper.MustGetBond(ctx, bond.BondDid) // get updated bond
}
}
// Update alpha value if in open state and next alpha is not null
if bond.State == types.OpenState.String() && batch.HasNextAlpha() {
if bond.FunctionType == types.BondingFunction {
keeper.HandleBondingFunctionAlphaUpdate(ctx, bond.BondDid)
} else {
keeper.UpdateAlpha(ctx, bond.BondDid)
}
}
// Save current batch as last batch and reset current batch
keeper.SetLastBatch(ctx, bond.BondDid, batch)
keeper.SetBatch(ctx, bond.BondDid, types.NewBatch(bond.BondDid, bond.Token, bond.BatchBlocks))
// If reserve has not changed, no need to recalculate I0; rest of function can be skipped
if bond.CurrentReserve.IsEqual(reserveBeforeOrderProcessing) && !batch.HasNextAlpha() {
continue
}
// Recalculate and re-set I0 if alpha bond
if bond.AlphaBond && bond.FunctionType == types.AugmentedFunction {
bond = keeper.MustGetBond(ctx, bond.BondDid)
bondFunctions := bond.FunctionParameters.AsMap()
// I0 := bondFunctions["I0"]
currentSystemAlpha := bondFunctions["systemAlpha"]
S := bond.CurrentSupply.Amount
//fmt.Println("S: ", S)
R := bond.CurrentReserve[0].Amount
C := bond.OutcomePayment
// Kappa := bondFunctions["kappa"]
// Recalculate kappa and V0 using new alpha
// Set new function parameters
if bond.State == types.OpenState.String() {
//fmt.Println("Updating I0 -------------------")
//fmt.Println("Current Supply: ", S)
//fmt.Println("Current Reserve: ", R)
//fmt.Println("Current I0: ", I0)
//fmt.Println("Current Kappa: ", Kappa)
//fmt.Println("Current SystemAlpha: ", currentSystemAlpha)
newI0 := types.InvariantI(C, currentSystemAlpha, R)
//fmt.Println("New I0: ", newI0)
newKappa := types.Kappa(newI0, C, currentSystemAlpha)
//fmt.Println("newKappa: ", newKappa)
newV0, err := types.Invariant(R.ToDec(), S.ToDec(), newKappa)
if err != nil {
ctx.EventManager().EmitEvent(sdk.NewEvent(
types.EventTypeEditAlphaFailed,
sdk.NewAttribute(types.AttributeKeyBondDid, bond.BondDid),
sdk.NewAttribute(types.AttributeKeyToken, bond.Token),
sdk.NewAttribute(types.AttributeKeyCancelReason, err.Error()),
))
//fmt.Println(err)
continue
}
//fmt.Println("new V0: ", newV0)
bond.FunctionParameters.ReplaceParam("V0", newV0)
bond.FunctionParameters.ReplaceParam("kappa", newKappa)
bond.FunctionParameters.ReplaceParam("I0", newI0)
}
}
// Save bond
keeper.SetBond(ctx, bond.BondDid, bond)
}
return []abci.ValidatorUpdate{}
}