Skip to content

Commit

Permalink
test: protocol fee swap
Browse files Browse the repository at this point in the history
  • Loading branch information
notJoon committed Dec 11, 2024
1 parent 0e443b1 commit b88079b
Showing 1 changed file with 80 additions and 0 deletions.
80 changes: 80 additions & 0 deletions router/protocol_fee_swap_test.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package router

import (
"testing"

pusers "gno.land/p/demo/users"

u256 "gno.land/p/gnoswap/uint256"
)

func TestHandleSwapFee(t *testing.T) {
token0 := "token0"

mockToken := &struct {
GRC20Interface
}{
GRC20Interface: MockGRC20{
TransferFn: func(to pusers.AddressOrName, amount uint64) {},
TransferFromFn: func(from, to pusers.AddressOrName, amount uint64) {},
BalanceOfFn: func(owner pusers.AddressOrName) uint64 { return 1000000 },
ApproveFn: func(spender pusers.AddressOrName, amount uint64) {},
},
}

registerGRC20ForTest(t, token0, mockToken)
defer unregisterGRC20ForTest(t, token0)

tests := []struct {
name string
amount *u256.Uint
swapFeeValue uint64
isDry bool
expectedAmount *u256.Uint
}{
{
name: "zero swap fee",
amount: u256.NewUint(1000),
swapFeeValue: 0,
isDry: false,
expectedAmount: u256.NewUint(1000),
},
{
name: "normal swap fee calculation (0.15%)",
amount: u256.NewUint(10000),
swapFeeValue: 15,
isDry: false,
expectedAmount: u256.NewUint(9985), // 10000 - (10000 * 0.15%)
},
{
name: "Dry Run test",
amount: u256.NewUint(10000),
swapFeeValue: 15,
isDry: true,
expectedAmount: u256.NewUint(9985),
},
{
name: "large amount swap fee calculation",
amount: u256.NewUint(1000000),
swapFeeValue: 15,
isDry: false,
expectedAmount: u256.NewUint(998500), // 1000000 - (1000000 * 0.15%)
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
originalSwapFee := swapFee
swapFee = tt.swapFeeValue
defer func() {
swapFee = originalSwapFee
}()

result := handleSwapFee(token0, tt.amount, tt.isDry)

if !result.Eq(tt.expectedAmount) {
t.Errorf("handleSwapFee() = %v, want %v", result, tt.expectedAmount)
}
})
}
}

0 comments on commit b88079b

Please sign in to comment.