Skip to content

Commit

Permalink
dex test
Browse files Browse the repository at this point in the history
  • Loading branch information
codchen committed Jun 12, 2023
1 parent 286eff4 commit 0f9f7aa
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 4 deletions.
41 changes: 37 additions & 4 deletions tests/dex_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,56 @@ func TestOrders(t *testing.T) {
admin := app.NewSignableAccount("admin")
app.FundAccount(admin, 100000000)
alice := app.NewSignableAccount("alice")
app.FundAccount(alice, 1000000)
app.FundAccount(alice, 10000000)
bob := app.NewSignableAccount("bob")
app.FundAccount(bob, 10000000)
charlie := app.NewSignableAccount("charlie")
app.FundAccount(charlie, 10000000)
contract := app.NewContract(admin, "./mars.wasm")

market := msgs.NewMarket(contract.String(), "SEI", "ATOM")
newContract := app.NewContract(admin, "./mars.wasm")

registerTx := app.Sign(admin, market.Register(admin, []string{}, 20000000), 1000000)
block1 := []signing.Tx{registerTx}
require.Equal(t, []uint32{0}, app.RunBlock(block1)) // 1st block to register contract/pair

aliceLimitOrder := market.LongLimitOrder(alice, "10.5", "5")
msgs := []sdk.Msg{aliceLimitOrder}
tx := app.Sign(alice, msgs, 10000)
m := []sdk.Msg{aliceLimitOrder}
tx := app.Sign(alice, m, 10000)

block2 := []signing.Tx{tx}
blockRunner := func() []uint32 { return app.RunBlock(block2) } // 2nd block to place order
blockRunner := func() []uint32 { return app.RunBlock(block2) } // 2nd block to place the first limit order
blockRunner = verify.DexOrders(t, app, blockRunner, block2)
blockRunner = verify.Balance(t, app, blockRunner, block2)

require.Equal(t, []uint32{0}, blockRunner())

newMarket := msgs.NewMarket(newContract.String(), "USDC", "BTC")
bobMarketOrder := market.ShortMarketOrder(bob, "10", "2")
charlieLimitOrder := market.ShortLimitOrder(charlie, "11", "3")
block3 := []signing.Tx{
app.Sign(admin, newMarket.Register(admin, []string{}, 20000000), 1000000),
app.Sign(bob, []sdk.Msg{bobMarketOrder}, 10000),
app.Sign(charlie, []sdk.Msg{charlieLimitOrder}, 10000),
}
blockRunner = func() []uint32 { return app.RunBlock(block3) } // 2nd block to place more orders
blockRunner = verify.DexOrders(t, app, blockRunner, block3)
blockRunner = verify.Balance(t, app, blockRunner, block3)

require.Equal(t, []uint32{0, 0, 0}, blockRunner())

aliceLimitOrder = newMarket.ShortLimitOrder(alice, "100", "50")
bobLimitOrder := market.LongLimitOrder(bob, "11", "1")
charlieMarketOrder := market.LongMarketOrder(charlie, "12", "4")
block4 := []signing.Tx{
app.Sign(alice, []sdk.Msg{aliceLimitOrder}, 10000),
app.Sign(bob, []sdk.Msg{bobLimitOrder}, 10000),
app.Sign(charlie, []sdk.Msg{charlieMarketOrder}, 10000),
}
blockRunner = func() []uint32 { return app.RunBlock(block4) } // 2nd block to place more orders
blockRunner = verify.DexOrders(t, app, blockRunner, block4)
blockRunner = verify.Balance(t, app, blockRunner, block4)

require.Equal(t, []uint32{0, 0, 0}, blockRunner())
}
3 changes: 3 additions & 0 deletions testutil/processblock/verify/bank.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ func Balance(t *testing.T, app *processblock.App, f BlockRunnable, txs []signing
case *dextypes.MsgPlaceOrders:
updateMultipleExpectedBalanceChange(expectedChanges, m.Creator, m.Funds, false)
updateMultipleExpectedBalanceChange(expectedChanges, m.ContractAddr, m.Funds, true)
case *dextypes.MsgRegisterContract:
funds := sdk.NewCoins(sdk.NewCoin("usei", sdk.NewInt(int64(m.Contract.RentBalance))))
updateMultipleExpectedBalanceChange(expectedChanges, m.Creator, funds, false)
default:
// TODO: add coverage for other balance-affecting messages to enable testing for those message types
continue
Expand Down
1 change: 1 addition & 0 deletions testutil/processblock/verify/dex.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ func DexOrders(t *testing.T, app *processblock.App, f BlockRunnable, txs []signi
o.Id = app.DexKeeper.GetNextOrderID(app.Ctx(), m.ContractAddr) + uint64(len(orders))
orderPlacementsByMarket[id] = append(orders, o)
} else {
o.Id = app.DexKeeper.GetNextOrderID(app.Ctx(), m.ContractAddr)
orderPlacementsByMarket[id] = []*dextypes.Order{o}
}
}
Expand Down

0 comments on commit 0f9f7aa

Please sign in to comment.