@@ -188,9 +188,6 @@ func (k Keeper) ExecuteDeposit(ctx sdk.Context, msg types.DepositMsgState, batch
188188
189189 params := k .GetParams (ctx )
190190
191- var inputs []banktypes.Input
192- var outputs []banktypes.Output
193-
194191 reserveCoins := k .GetReserveCoins (ctx , pool )
195192
196193 // reinitialize pool if the pool is depleted
@@ -240,77 +237,59 @@ func (k Keeper) ExecuteDeposit(ctx sdk.Context, msg types.DepositMsgState, batch
240237 return nil
241238 }
242239
243- // only two coins are acceptable
244- if reserveCoins .Len () != msg .Msg .DepositCoins .Len () {
245- return types .ErrNumOfReserveCoin
246- }
247-
248240 reserveCoins .Sort ()
249241
250- // Decimal Error, divide the Int coin amount by the Decimal Rate and erase the decimal point to deposit a lower value
251- lastReserveCoinA := reserveCoins [0 ].Amount
252- lastReserveCoinB := reserveCoins [1 ].Amount
253- lastReserveRatio := lastReserveCoinA .ToDec ().QuoTruncate (lastReserveCoinB .ToDec ())
242+ lastReserveCoinA := reserveCoins [0 ]
243+ lastReserveCoinB := reserveCoins [1 ]
254244
255245 depositCoinA := depositCoins [0 ]
256246 depositCoinB := depositCoins [1 ]
257- depositCoinAmountA := depositCoinA .Amount
258- depositCoinAmountB := depositCoinB .Amount
259- depositableCoinAmountA := depositCoinB .Amount .ToDec ().MulTruncate (lastReserveRatio ).TruncateInt ()
260-
261- refundedCoins := sdk .NewCoins ()
262- refundedCoinA := sdk .ZeroInt ()
263- refundedCoinB := sdk .ZeroInt ()
264-
265- var acceptedCoins sdk.Coins
266- // handle when depositing coin A amount is less than, greater than or equal to depositable amount
267- if depositCoinA .Amount .LT (depositableCoinAmountA ) {
268- depositCoinAmountB = depositCoinA .Amount .ToDec ().QuoTruncate (lastReserveRatio ).TruncateInt ()
269- acceptedCoins = sdk .NewCoins (depositCoinA , sdk .NewCoin (depositCoinB .Denom , depositCoinAmountB ))
270-
271- inputs = append (inputs , banktypes .NewInput (batchEscrowAcc , acceptedCoins ))
272- outputs = append (outputs , banktypes .NewOutput (reserveAcc , acceptedCoins ))
273-
274- refundedCoinB = depositCoinB .Amount .Sub (depositCoinAmountB )
275-
276- if refundedCoinB .IsPositive () {
277- refundedCoins = sdk .NewCoins (sdk .NewCoin (depositCoinB .Denom , refundedCoinB ))
278- inputs = append (inputs , banktypes .NewInput (batchEscrowAcc , refundedCoins ))
279- outputs = append (outputs , banktypes .NewOutput (depositor , refundedCoins ))
280- }
281- } else if depositCoinA .Amount .GT (depositableCoinAmountA ) {
282- depositCoinAmountA = depositCoinB .Amount .ToDec ().MulTruncate (lastReserveRatio ).TruncateInt ()
283- acceptedCoins = sdk .NewCoins (depositCoinB , sdk .NewCoin (depositCoinA .Denom , depositCoinAmountA ))
284247
285- inputs = append (inputs , banktypes .NewInput (batchEscrowAcc , acceptedCoins ))
286- outputs = append (outputs , banktypes .NewOutput (reserveAcc , acceptedCoins ))
287-
288- refundedCoinA = depositCoinA .Amount .Sub (depositCoinAmountA )
289-
290- if refundedCoinA .IsPositive () {
291- refundedCoins = sdk .NewCoins (sdk .NewCoin (depositCoinA .Denom , refundedCoinA ))
292- inputs = append (inputs , banktypes .NewInput (batchEscrowAcc , refundedCoins ))
293- outputs = append (outputs , banktypes .NewOutput (depositor , refundedCoins ))
294- }
295- } else {
296- acceptedCoins = sdk .NewCoins (depositCoinA , depositCoinB )
297- inputs = append (inputs , banktypes .NewInput (batchEscrowAcc , acceptedCoins ))
298- outputs = append (outputs , banktypes .NewOutput (reserveAcc , acceptedCoins ))
248+ poolCoinTotalSupply := k .GetPoolCoinTotalSupply (ctx , pool ).ToDec ()
249+ if err := types .CheckOverflowWithDec (poolCoinTotalSupply , depositCoinA .Amount .ToDec ()); err != nil {
250+ return err
299251 }
252+ if err := types .CheckOverflowWithDec (poolCoinTotalSupply , depositCoinB .Amount .ToDec ()); err != nil {
253+ return err
254+ }
255+ poolCoinMintAmt := sdk .MinDec (
256+ poolCoinTotalSupply .MulTruncate (depositCoinA .Amount .ToDec ()).QuoTruncate (lastReserveCoinA .Amount .ToDec ()),
257+ poolCoinTotalSupply .MulTruncate (depositCoinB .Amount .ToDec ()).QuoTruncate (lastReserveCoinB .Amount .ToDec ()),
258+ )
259+ mintRate := poolCoinMintAmt .TruncateDec ().QuoTruncate (poolCoinTotalSupply )
260+ acceptedCoins := sdk .NewCoins (
261+ sdk .NewCoin (depositCoins [0 ].Denom , lastReserveCoinA .Amount .ToDec ().Mul (mintRate ).TruncateInt ()),
262+ sdk .NewCoin (depositCoins [1 ].Denom , lastReserveCoinB .Amount .ToDec ().Mul (mintRate ).TruncateInt ()),
263+ )
264+ refundedCoins := depositCoins .Sub (acceptedCoins )
265+ refundedCoinA := sdk .NewCoin (depositCoinA .Denom , refundedCoins .AmountOf (depositCoinA .Denom ))
266+ refundedCoinB := sdk .NewCoin (depositCoinB .Denom , refundedCoins .AmountOf (depositCoinB .Denom ))
300267
301- // calculate pool token mint amount
302- poolCoinTotalSupply := k .GetPoolCoinTotalSupply (ctx , pool )
303- poolCoinAmt := sdk .MinInt (
304- poolCoinTotalSupply .ToDec ().MulTruncate (depositCoinAmountA .ToDec ()).QuoTruncate (reserveCoins [0 ].Amount .ToDec ()).TruncateInt (),
305- poolCoinTotalSupply .ToDec ().MulTruncate (depositCoinAmountB .ToDec ()).QuoTruncate (reserveCoins [1 ].Amount .ToDec ()).TruncateInt ())
306- mintPoolCoin := sdk .NewCoin (pool .PoolCoinDenom , poolCoinAmt )
268+ mintPoolCoin := sdk .NewCoin (pool .PoolCoinDenom , poolCoinMintAmt .TruncateInt ())
307269 mintPoolCoins := sdk .NewCoins (mintPoolCoin )
308270
309- // mint pool token to the depositor
271+ if mintPoolCoins .IsZero () || acceptedCoins .IsZero () {
272+ return fmt .Errorf ("pool coin truncated, no accepted coin, refund" )
273+ }
274+
310275 if err := k .bankKeeper .MintCoins (ctx , types .ModuleName , mintPoolCoins ); err != nil {
311276 return err
312277 }
313278
279+ var inputs []banktypes.Input
280+ var outputs []banktypes.Output
281+
282+ if ! refundedCoins .IsZero () {
283+ // refund truncated deposit coins
284+ inputs = append (inputs , banktypes .NewInput (batchEscrowAcc , refundedCoins ))
285+ outputs = append (outputs , banktypes .NewOutput (depositor , refundedCoins ))
286+ }
287+
288+ // send accepted deposit coins
289+ inputs = append (inputs , banktypes .NewInput (batchEscrowAcc , acceptedCoins ))
290+ outputs = append (outputs , banktypes .NewOutput (reserveAcc , acceptedCoins ))
291+
292+ // send minted pool coins
314293 inputs = append (inputs , banktypes .NewInput (batchEscrowAcc , mintPoolCoins ))
315294 outputs = append (outputs , banktypes .NewOutput (depositor , mintPoolCoins ))
316295
@@ -328,10 +307,10 @@ func (k Keeper) ExecuteDeposit(ctx sdk.Context, msg types.DepositMsgState, batch
328307 afterReserveCoinA := afterReserveCoins [0 ].Amount
329308 afterReserveCoinB := afterReserveCoins [1 ].Amount
330309
331- MintingPoolCoinsInvariant (poolCoinTotalSupply , mintPoolCoin .Amount , depositCoinA .Amount , depositCoinB .Amount ,
332- lastReserveCoinA , lastReserveCoinB , refundedCoinA , refundedCoinB )
333- DepositInvariant (lastReserveCoinA , lastReserveCoinB , depositCoinA .Amount , depositCoinB .Amount ,
334- afterReserveCoinA , afterReserveCoinB , refundedCoinA , refundedCoinB )
310+ MintingPoolCoinsInvariant (poolCoinTotalSupply . TruncateInt () , mintPoolCoin .Amount , depositCoinA .Amount , depositCoinB .Amount ,
311+ lastReserveCoinA . Amount , lastReserveCoinB . Amount , refundedCoinA . Amount , refundedCoinB . Amount )
312+ DepositInvariant (lastReserveCoinA . Amount , lastReserveCoinB . Amount , depositCoinA .Amount , depositCoinB .Amount ,
313+ afterReserveCoinA , afterReserveCoinB , refundedCoinA . Amount , refundedCoinB . Amount )
335314 }
336315
337316 ctx .EventManager ().EmitEvent (
@@ -350,7 +329,7 @@ func (k Keeper) ExecuteDeposit(ctx sdk.Context, msg types.DepositMsgState, batch
350329 )
351330
352331 reserveCoins = k .GetReserveCoins (ctx , pool )
353- lastReserveRatio = sdk .NewDecFromInt (reserveCoins [0 ].Amount ).Quo (sdk .NewDecFromInt (reserveCoins [1 ].Amount ))
332+ lastReserveRatio : = sdk .NewDecFromInt (reserveCoins [0 ].Amount ).Quo (sdk .NewDecFromInt (reserveCoins [1 ].Amount ))
354333
355334 logger := k .Logger (ctx )
356335 logger .Debug (
@@ -405,6 +384,12 @@ func (k Keeper) ExecuteWithdrawal(ctx sdk.Context, msg types.WithdrawMsgState, b
405384 } else {
406385 // Calculate withdraw amount of respective reserve coin considering fees and pool coin's totally supply
407386 for _ , reserveCoin := range reserveCoins {
387+ if err := types .CheckOverflow (reserveCoin .Amount , msg .Msg .PoolCoin .Amount ); err != nil {
388+ return err
389+ }
390+ if err := types .CheckOverflow (reserveCoin .Amount .Mul (msg .Msg .PoolCoin .Amount ).ToDec ().TruncateInt (), poolCoinTotalSupply ); err != nil {
391+ return err
392+ }
408393 // WithdrawAmount = ReserveAmount * PoolCoinAmount * WithdrawFeeProportion / TotalSupply
409394 withdrawAmtWithFee := reserveCoin .Amount .Mul (msg .Msg .PoolCoin .Amount ).ToDec ().TruncateInt ().Quo (poolCoinTotalSupply )
410395 withdrawAmt := reserveCoin .Amount .Mul (msg .Msg .PoolCoin .Amount ).ToDec ().MulTruncate (withdrawProportion ).TruncateInt ().Quo (poolCoinTotalSupply )
@@ -797,21 +782,12 @@ func (k Keeper) ValidateMsgWithdrawWithinBatch(ctx sdk.Context, msg types.MsgWit
797782}
798783
799784// ValidateMsgSwapWithinBatch validates MsgSwapWithinBatch.
800- func (k Keeper ) ValidateMsgSwapWithinBatch (ctx sdk.Context , msg types.MsgSwapWithinBatch ) error {
801- pool , found := k .GetPool (ctx , msg .PoolId )
802- if ! found {
803- return types .ErrPoolNotExists
804- }
805-
785+ func (k Keeper ) ValidateMsgSwapWithinBatch (ctx sdk.Context , msg types.MsgSwapWithinBatch , pool types.Pool ) error {
806786 denomA , denomB := types .AlphabeticalDenomPair (msg .OfferCoin .Denom , msg .DemandCoinDenom )
807787 if denomA != pool .ReserveCoinDenoms [0 ] || denomB != pool .ReserveCoinDenoms [1 ] {
808788 return types .ErrNotMatchedReserveCoin
809789 }
810790
811- if k .IsDepletedPool (ctx , pool ) {
812- return types .ErrDepletedPool
813- }
814-
815791 params := k .GetParams (ctx )
816792
817793 // can not exceed max order ratio of reserve coins that can be ordered at a order
@@ -827,6 +803,10 @@ func (k Keeper) ValidateMsgSwapWithinBatch(ctx sdk.Context, msg types.MsgSwapWit
827803 return types .ErrBadOfferCoinFee
828804 }
829805
806+ if err := types .CheckOverflowWithDec (msg .OfferCoin .Amount .ToDec (), msg .OrderPrice ); err != nil {
807+ return err
808+ }
809+
830810 if ! msg .OfferCoinFee .Equal (types .GetOfferCoinFee (msg .OfferCoin , params .SwapFeeRate )) {
831811 return types .ErrBadOfferCoinFee
832812 }
0 commit comments