Skip to content

Commit

Permalink
[2137]: Add some exchange test stuff involving scopes.
Browse files Browse the repository at this point in the history
  • Loading branch information
SpicyLemon committed Oct 1, 2024
1 parent 3dd85af commit 5f0b18b
Show file tree
Hide file tree
Showing 5 changed files with 176 additions and 74 deletions.
34 changes: 34 additions & 0 deletions x/exchange/keeper/commitments_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/provenance-io/provenance/x/exchange"
"github.com/provenance-io/provenance/x/exchange/keeper"
markertypes "github.com/provenance-io/provenance/x/marker/types"
metadatatypes "github.com/provenance-io/provenance/x/metadata/types"
)

func (s *TestSuite) TestKeeper_GetCommitmentAmount() {
Expand Down Expand Up @@ -1811,15 +1812,19 @@ func (s *TestSuite) TestKeeper_SettleCommitments() {
navSource := func(marketID uint32) string {
return fmt.Sprintf("x/exchange market %d", marketID)
}
scopeID1 := s.scopeID("1_scope")
scopeID2 := s.scopeID("2_scope")

tests := []struct {
name string
setup func()
mdKeeper *MockMetadataKeeper
markerKeeper *MockMarkerKeeper
holdKeeper *MockHoldKeeper
bankKeeper *MockBankKeeper
req *exchange.MsgMarketCommitmentSettleRequest
expEvents sdk.Events
expMDCalls MetadataCalls
expMarkerCalls MarkerCalls
expHoldCalls HoldCalls
expBankCalls BankCalls
Expand Down Expand Up @@ -1926,13 +1931,37 @@ func (s *TestSuite) TestKeeper_SettleCommitments() {
{Assets: s.coin("11apple"), Price: s.coin("700nhash")},
{Assets: s.coin("12banana"), Price: s.coin("62cherry")},
{Assets: s.coin("13banana"), Price: s.coin("1500nhash")},
{Assets: scopeID1.Coin(), Price: s.coin("71cherry")},
{Assets: scopeID1.Coin(), Price: s.coin("400nhash")},
{Assets: scopeID2.Coin(), Price: s.coin("5cherry")},
{Assets: scopeID2.Coin(), Price: s.coin("6600nhash")},
},
EventTag: "testtag3",
},
expEvents: sdk.Events{
s.untypeEvent(exchange.NewEventCommitmentReleased(s.addr3.String(), 4, s.coins("10apple,10banana"), "testtag3")),
s.untypeEvent(exchange.NewEventFundsCommitted(s.addr5.String(), 4, s.coins("10apple,10banana"), "testtag3")),
},
expMDCalls: MetadataCalls{
AddSetNetAssetValues: []*MDAddSetNetAssetValuesArgs{
{
ScopeID: scopeID1,
NAVs: []metadatatypes.NetAssetValue{
{Price: s.coin("71cherry")},
{Price: s.coin("400nhash")},
},
Source: navSource(4),
},
{
ScopeID: scopeID2,
NAVs: []metadatatypes.NetAssetValue{
{Price: s.coin("5cherry")},
{Price: s.coin("6600nhash")},
},
Source: navSource(4),
},
},
},
expMarkerCalls: MarkerCalls{
GetMarker: []sdk.AccAddress{appleMarker.GetAddress(), bananaMarker.GetAddress()},
AddSetNetAssetValues: []*AddSetNetAssetValuesArgs{
Expand Down Expand Up @@ -2147,6 +2176,9 @@ func (s *TestSuite) TestKeeper_SettleCommitments() {
tc.setup()
}

if tc.mdKeeper == nil {
tc.mdKeeper = NewMockMetadataKeeper()
}
if tc.markerKeeper == nil {
tc.markerKeeper = NewMockMarkerKeeper()
}
Expand All @@ -2169,6 +2201,7 @@ func (s *TestSuite) TestKeeper_SettleCommitments() {
}

kpr := s.k.
WithMetadataKeeper(tc.mdKeeper).
WithMarkerKeeper(tc.markerKeeper).
WithBankKeeper(tc.bankKeeper).
WithHoldKeeper(tc.holdKeeper)
Expand All @@ -2183,6 +2216,7 @@ func (s *TestSuite) TestKeeper_SettleCommitments() {

actEvents := em.Events()
s.assertEqualEvents(tc.expEvents, actEvents, "events emitted during SettleCommitments")
s.assertMetadataKeeperCalls(tc.mdKeeper, tc.expMDCalls, "SettleCommitments")
s.assertMarkerKeeperCalls(tc.markerKeeper, tc.expMarkerCalls, "SettleCommitments")
s.assertBankKeeperCalls(tc.bankKeeper, tc.expBankCalls, "SettleCommitments")
s.assertHoldKeeperCalls(tc.holdKeeper, tc.expHoldCalls, "SettleCommitments")
Expand Down
77 changes: 62 additions & 15 deletions x/exchange/keeper/fulfillment_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package keeper_test

import (
"github.com/google/uuid"

sdk "github.com/cosmos/cosmos-sdk/types"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
"github.com/cosmos/gogoproto/proto"
Expand Down Expand Up @@ -535,7 +533,7 @@ func (s *TestSuite) TestKeeper_FillBids() {
expEvents: []*exchange.EventOrderFilled{
{OrderId: 13, Assets: "12apple", Price: "60plum", MarketId: 6},
},
adlEvents: sdk.Events{s.navSetEvent("12apple", "60plum", 6)},
adlEvents: sdk.Events{s.markerNavSetEvent("12apple", "60plum", 6)},
expHoldCalls: HoldCalls{ReleaseHold: []*ReleaseHoldArgs{{addr: s.addr2, funds: s.coins("60plum")}}},
expBankCalls: BankCalls{
BlockedAddr: []sdk.AccAddress{s.addr2, s.addr5},
Expand Down Expand Up @@ -566,7 +564,7 @@ func (s *TestSuite) TestKeeper_FillBids() {
expEvents: []*exchange.EventOrderFilled{
{OrderId: 13, Assets: "12apple", Price: "60plum", MarketId: 6},
},
adlEvents: sdk.Events{s.navSetEvent("12apple", "60plum", 6)},
adlEvents: sdk.Events{s.markerNavSetEvent("12apple", "60plum", 6)},
expHoldCalls: HoldCalls{ReleaseHold: []*ReleaseHoldArgs{{addr: s.addr2, funds: s.coins("60plum")}}},
expBankCalls: BankCalls{
BlockedAddr: []sdk.AccAddress{s.addr2, s.addr5},
Expand Down Expand Up @@ -598,7 +596,7 @@ func (s *TestSuite) TestKeeper_FillBids() {
expEvents: []*exchange.EventOrderFilled{
{OrderId: 13, Assets: "184467440737095516150apple", Price: "60plum", MarketId: 6},
},
adlEvents: sdk.Events{s.navSetEvent("184467440737095516150apple", "60plum", 6)},
adlEvents: sdk.Events{s.markerNavSetEvent("184467440737095516150apple", "60plum", 6)},
expHoldCalls: HoldCalls{ReleaseHold: []*ReleaseHoldArgs{{addr: s.addr2, funds: s.coins("60plum")}}},
expBankCalls: BankCalls{
BlockedAddr: []sdk.AccAddress{s.addr2, s.addr5},
Expand Down Expand Up @@ -1398,7 +1396,7 @@ func (s *TestSuite) TestKeeper_FillAsks() {
expEvents: []*exchange.EventOrderFilled{
{OrderId: 13, Assets: "12apple", Price: "60plum", MarketId: 6},
},
adlEvents: sdk.Events{s.navSetEvent("12apple", "60plum", 6)},
adlEvents: sdk.Events{s.markerNavSetEvent("12apple", "60plum", 6)},
expHoldCalls: HoldCalls{ReleaseHold: []*ReleaseHoldArgs{{addr: s.addr2, funds: s.coins("12apple")}}},
expBankCalls: BankCalls{
BlockedAddr: []sdk.AccAddress{s.addr5, s.addr2},
Expand Down Expand Up @@ -1429,7 +1427,7 @@ func (s *TestSuite) TestKeeper_FillAsks() {
expEvents: []*exchange.EventOrderFilled{
{OrderId: 13, Assets: "12apple", Price: "60plum", MarketId: 6},
},
adlEvents: sdk.Events{s.navSetEvent("12apple", "60plum", 6)},
adlEvents: sdk.Events{s.markerNavSetEvent("12apple", "60plum", 6)},
expHoldCalls: HoldCalls{ReleaseHold: []*ReleaseHoldArgs{{addr: s.addr2, funds: s.coins("12apple")}}},
expBankCalls: BankCalls{
BlockedAddr: []sdk.AccAddress{s.addr5, s.addr2},
Expand Down Expand Up @@ -1460,7 +1458,7 @@ func (s *TestSuite) TestKeeper_FillAsks() {
expEvents: []*exchange.EventOrderFilled{
{OrderId: 13, Assets: "184467440737095516150apple", Price: "60plum", MarketId: 6},
},
adlEvents: sdk.Events{s.navSetEvent("184467440737095516150apple", "60plum", 6)},
adlEvents: sdk.Events{s.markerNavSetEvent("184467440737095516150apple", "60plum", 6)},
expHoldCalls: HoldCalls{ReleaseHold: []*ReleaseHoldArgs{{addr: s.addr2, funds: s.coins("184467440737095516150apple")}}},
expBankCalls: BankCalls{
BlockedAddr: []sdk.AccAddress{s.addr5, s.addr2},
Expand Down Expand Up @@ -1733,12 +1731,14 @@ func (s *TestSuite) TestKeeper_FillAsks() {

func (s *TestSuite) TestKeeper_SettleOrders() {
appleMarker := s.markerAccount("1000000000apple")
scopeID1 := s.scopeID("1_scopeID1")

tests := []struct {
name string
bankKeeper *MockBankKeeper
holdKeeper *MockHoldKeeper
markerKeeper *MockMarkerKeeper
mdKeeper *MockMetadataKeeper
setup func()
marketID uint32
askOrderIDs []uint64
Expand All @@ -1751,6 +1751,7 @@ func (s *TestSuite) TestKeeper_SettleOrders() {
expHoldCalls HoldCalls
expBankCalls BankCalls
expMarkerCalls MarkerCalls
expMDCalls MetadataCalls
expLog []string
}{
// Tests on error conditions.
Expand Down Expand Up @@ -2036,6 +2037,49 @@ func (s *TestSuite) TestKeeper_SettleOrders() {
},
},
},
{
name: "one ask one bid: scope",
setup: func() {
s.requireCreateMarket(exchange.Market{MarketId: 1})
store := s.getStore()
s.requireSetOrderInStore(store, exchange.NewOrder(1).WithAsk(&exchange.AskOrder{
Assets: scopeID1.Coin(), Price: s.coin("5peach"), MarketId: 1, Seller: s.addr3.String(),
}))
s.requireSetOrderInStore(store, exchange.NewOrder(5).WithBid(&exchange.BidOrder{
Assets: scopeID1.Coin(), Price: s.coin("5peach"), MarketId: 1, Buyer: s.addr4.String(),
}))
},
marketID: 1,
askOrderIDs: []uint64{1},
bidOrderIDs: []uint64{5},
expectPartial: false,
expEvents: []proto.Message{
&exchange.EventOrderFilled{OrderId: 1, Assets: scopeID1.Coin().String(), Price: "5peach", MarketId: 1},
&exchange.EventOrderFilled{OrderId: 5, Assets: scopeID1.Coin().String(), Price: "5peach", MarketId: 1},
},
expHoldCalls: HoldCalls{
ReleaseHold: []*ReleaseHoldArgs{
{addr: s.addr3, funds: scopeID1.Coins()},
{addr: s.addr4, funds: s.coins("5peach")},
},
},
expBankCalls: BankCalls{
BlockedAddr: []sdk.AccAddress{s.addr4, s.addr3},
SendCoins: []*SendCoinsArgs{
{ctxHasQuarantineBypass: true, fromAddr: s.addr3, toAddr: s.addr4, amt: scopeID1.Coins()},
{ctxHasQuarantineBypass: true, fromAddr: s.addr4, toAddr: s.addr3, amt: s.coins("5peach")},
},
},
expMDCalls: MetadataCalls{
AddSetNetAssetValues: []*MDAddSetNetAssetValuesArgs{
{
ScopeID: scopeID1,
NAVs: []metadatatypes.NetAssetValue{{Price: s.coin("5peach")}},
Source: "x/exchange market 1",
},
},
},
},
{
name: "one ask one bid: both full, no fees, error getting marker",
markerKeeper: NewMockMarkerKeeper().WithGetMarkerErr(appleMarker.GetAddress(), "sample apple error"),
Expand All @@ -2057,7 +2101,7 @@ func (s *TestSuite) TestKeeper_SettleOrders() {
&exchange.EventOrderFilled{OrderId: 1, Assets: "1apple", Price: "5peach", MarketId: 1},
&exchange.EventOrderFilled{OrderId: 5, Assets: "1apple", Price: "5peach", MarketId: 1},
},
adlEvents: sdk.Events{s.navSetEvent("1apple", "5peach", 1)},
adlEvents: sdk.Events{s.markerNavSetEvent("1apple", "5peach", 1)},
expHoldCalls: HoldCalls{
ReleaseHold: []*ReleaseHoldArgs{
{addr: s.addr3, funds: s.coins("1apple")},
Expand Down Expand Up @@ -2096,7 +2140,7 @@ func (s *TestSuite) TestKeeper_SettleOrders() {
&exchange.EventOrderFilled{OrderId: 1, Assets: "1apple", Price: "5peach", MarketId: 1},
&exchange.EventOrderFilled{OrderId: 5, Assets: "1apple", Price: "5peach", MarketId: 1},
},
adlEvents: sdk.Events{s.navSetEvent("1apple", "5peach", 1)},
adlEvents: sdk.Events{s.markerNavSetEvent("1apple", "5peach", 1)},
expHoldCalls: HoldCalls{
ReleaseHold: []*ReleaseHoldArgs{
{addr: s.addr3, funds: s.coins("1apple")},
Expand Down Expand Up @@ -2142,7 +2186,7 @@ func (s *TestSuite) TestKeeper_SettleOrders() {
{addr: s.addr4, funds: s.coins("5peach")},
},
},
adlEvents: sdk.Events{s.navSetEvent("184467440737095516150apple", "5peach", 1)},
adlEvents: sdk.Events{s.markerNavSetEvent("184467440737095516150apple", "5peach", 1)},
expBankCalls: BankCalls{
BlockedAddr: []sdk.AccAddress{s.addr4, s.addr3},
SendCoins: []*SendCoinsArgs{
Expand Down Expand Up @@ -2495,6 +2539,9 @@ func (s *TestSuite) TestKeeper_SettleOrders() {
if tc.markerKeeper == nil {
tc.markerKeeper = NewMockMarkerKeeper()
}
if tc.mdKeeper == nil {
tc.mdKeeper = NewMockMetadataKeeper()
}

expEvents := untypeEvents(s, tc.expEvents)
if len(tc.adlEvents) > 0 {
Expand Down Expand Up @@ -2524,7 +2571,8 @@ func (s *TestSuite) TestKeeper_SettleOrders() {
kpr := s.k.WithAccountKeeper(s.accKeeper).
WithBankKeeper(tc.bankKeeper).
WithHoldKeeper(tc.holdKeeper).
WithMarkerKeeper(tc.markerKeeper)
WithMarkerKeeper(tc.markerKeeper).
WithMetadataKeeper(tc.mdKeeper)
s.logBuffer.Reset()
var err error
testFunc := func() {
Expand All @@ -2537,6 +2585,7 @@ func (s *TestSuite) TestKeeper_SettleOrders() {
s.assertHoldKeeperCalls(tc.holdKeeper, tc.expHoldCalls, "SettleOrders")
s.assertBankKeeperCalls(tc.bankKeeper, tc.expBankCalls, "SettleOrders")
s.assertMarkerKeeperCalls(tc.markerKeeper, tc.expMarkerCalls, "SettleOrders")
s.assertMetadataKeeperCalls(tc.mdKeeper, tc.expMDCalls, "SettleOrders")

outputLog := s.getLogOutput("SettleOrders")
actLog := s.splitOutputLog(outputLog)
Expand Down Expand Up @@ -2570,9 +2619,7 @@ func (s *TestSuite) TestKeeper_SettleOrders() {
}

func (s *TestSuite) TestKeeper_GetNav() {
scopeUUID, err := uuid.FromBytes([]byte("scope_uuid______"))
s.Require().NoError(err, "uuid.FromBytes([]byte(\"scope_uuid______\"))")
scopeDenom := metadatatypes.ScopeMetadataAddress(scopeUUID).Denom()
scopeDenom := s.scopeID("scope_uuid").Denom()
tests := []struct {
name string
metadataKeeper *MockMetadataKeeper
Expand Down
Loading

0 comments on commit 5f0b18b

Please sign in to comment.