Skip to content

Commit

Permalink
#51 Add unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
blewater committed Jun 2, 2021
1 parent c8baf6a commit 45d2fbf
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 3 deletions.
2 changes: 0 additions & 2 deletions x/market/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
88 changes: 87 additions & 1 deletion x/market/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)

Expand Down

0 comments on commit 45d2fbf

Please sign in to comment.