From 45d2fbf3c02e477183c4147dd4ca70180cc29fbe Mon Sep 17 00:00:00 2001 From: Mario Karagiorgas Date: Wed, 2 Jun 2021 12:08:45 +0300 Subject: [PATCH] #51 Add unit test --- x/market/keeper/keeper.go | 2 - x/market/keeper/keeper_test.go | 88 +++++++++++++++++++++++++++++++++- 2 files changed, 87 insertions(+), 3 deletions(-) diff --git a/x/market/keeper/keeper.go b/x/market/keeper/keeper.go index 63d37686..0fb77fad 100644 --- a/x/market/keeper/keeper.go +++ b/x/market/keeper/keeper.go @@ -158,8 +158,6 @@ func (k *Keeper) calcOrderGas( var liquidTrxFee sdk.Gas k.paramStore.Get(ctx, types.KeyLiquidTrxFee, &liquidTrxFee) - // TODO is destination sufficient to evaluate the total filling impact - // for limit/market orders? if dstFilled.IsZero() { // 0% fill -> 100% totalRebate return liquidTrxFee diff --git a/x/market/keeper/keeper_test.go b/x/market/keeper/keeper_test.go index e9d7db0d..3edac0ef 100644 --- a/x/market/keeper/keeper_test.go +++ b/x/market/keeper/keeper_test.go @@ -53,7 +53,6 @@ func TestKeeper_calcOrderGas(t *testing.T) { var ( stdTrxFee sdk.Gas = k.GetTrxFee(ctx) liquidTrxFee sdk.Gas = k.GetLiquidTrxFee(ctx) - //liquidIntervalMin int64 = k.GetLiquidityRebateMinutesSpan(ctx) ) tests := []struct { name string @@ -108,6 +107,93 @@ func TestKeeper_calcOrderGas(t *testing.T) { } } +func TestKeeper_calcReplaceOrderGas(t *testing.T) { + ctx, k, _, _ := createTestComponents(t) + + ctx = ctx.WithBlockTime(time.Now()) + + var ( + stdTrxFee sdk.Gas = k.GetTrxFee(ctx) + liquidTrxFee sdk.Gas = k.GetLiquidTrxFee(ctx) + // set ctx to rebate allowance + 1 + liquidIntervalMin = time.Duration(k.GetLiquidityRebateMinutesSpan(ctx)+1) * + time.Minute + ) + + ctx = ctx.WithGasMeter(sdk.NewInfiniteGasMeter()) + + tests := []struct { + name string + ctx sdk.Context + stdTrxFee sdk.Gas + liquidTrxFee sdk.Gas + dstFilled sdk.Int + dstAmount sdk.Int + origOrderCreated time.Time + want sdk.Gas + }{ + { + name: "0% Filled Full Gas", + ctx: ctx.WithBlockTime(ctx.BlockTime().Add(liquidIntervalMin)), + stdTrxFee: stdTrxFee, + dstFilled: sdk.NewInt(0), + dstAmount: sdk.NewInt(0), + origOrderCreated: time.Now(), + want: liquidTrxFee, + }, + { + name: "0% Filled but not meeting qualified minutes, Full Gas", + ctx: ctx, + stdTrxFee: stdTrxFee, + dstFilled: sdk.NewInt(0), + dstAmount: sdk.NewInt(0), + origOrderCreated: time.Now(), + want: stdTrxFee, + }, + { + name: "100% Filled Full Gas", + ctx: ctx.WithBlockTime(ctx.BlockTime().Add(liquidIntervalMin)), + stdTrxFee: stdTrxFee, + dstFilled: sdk.NewInt(1), + dstAmount: sdk.NewInt(1), + origOrderCreated: time.Now(), + want: stdTrxFee, + }, + { + name: "10% Filled Full Gas", + ctx: ctx.WithBlockTime(ctx.BlockTime().Add(liquidIntervalMin)), + stdTrxFee: stdTrxFee, + dstFilled: sdk.NewInt(10), + dstAmount: sdk.NewInt(100), + origOrderCreated: time.Now(), + want: stdTrxFee / 10, + }, + { + name: "90% Filled Full Gas", + ctx: ctx.WithBlockTime(ctx.BlockTime().Add(liquidIntervalMin)), + stdTrxFee: stdTrxFee, + dstFilled: sdk.NewInt(90), + dstAmount: sdk.NewInt(100), + origOrderCreated: time.Now(), + want: sdk.Gas(float64(stdTrxFee) * 0.9), + }, + } + + for _, tt := range tests { + t.Run( + tt.name, func(t *testing.T) { + if got := k.calcReplaceOrderGas( + tt.ctx, tt.dstFilled, tt.dstAmount, tt.origOrderCreated, + ); got != tt.want { + t.Errorf( + "calcReplaceOrderGas() = %v, want %v", got, tt.want, + ) + } + }, + ) + } +} + func TestBasicTrade(t *testing.T) { ctx, k, ak, bk := createTestComponents(t)