Skip to content

Commit

Permalink
feat: add Market's FeeCollector
Browse files Browse the repository at this point in the history
  • Loading branch information
kingcre committed Sep 27, 2023
1 parent 05470f1 commit 5bf44ac
Show file tree
Hide file tree
Showing 13 changed files with 356 additions and 208 deletions.
2 changes: 1 addition & 1 deletion client/docs/statik/statik.go

Large diffs are not rendered by default.

10 changes: 10 additions & 0 deletions client/docs/swagger-ui/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11604,6 +11604,8 @@ paths:
type: string
escrow_address:
type: string
fee_collector:
type: string
maker_fee_rate:
type: string
taker_fee_rate:
Expand Down Expand Up @@ -11753,6 +11755,8 @@ paths:
type: string
escrow_address:
type: string
fee_collector:
type: string
maker_fee_rate:
type: string
taker_fee_rate:
Expand Down Expand Up @@ -20178,6 +20182,8 @@ definitions:
type: string
escrow_address:
type: string
fee_collector:
type: string
maker_fee_rate:
type: string
taker_fee_rate:
Expand Down Expand Up @@ -20339,6 +20345,8 @@ definitions:
type: string
escrow_address:
type: string
fee_collector:
type: string
maker_fee_rate:
type: string
taker_fee_rate:
Expand Down Expand Up @@ -20538,6 +20546,8 @@ definitions:
type: string
escrow_address:
type: string
fee_collector:
type: string
maker_fee_rate:
type: string
taker_fee_rate:
Expand Down
15 changes: 8 additions & 7 deletions proto/crescent/exchange/v1beta1/exchange.proto
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,20 @@ message Market {
string base_denom = 2;
string quote_denom = 3;
string escrow_address = 4;
string maker_fee_rate = 5
string fee_collector = 5;
string maker_fee_rate = 6
[(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec", (gogoproto.nullable) = false];
string taker_fee_rate = 6
string taker_fee_rate = 7
[(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec", (gogoproto.nullable) = false];
string order_source_fee_ratio = 7
string order_source_fee_ratio = 8
[(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec", (gogoproto.nullable) = false];
string min_order_quantity = 8
string min_order_quantity = 9
[(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Int", (gogoproto.nullable) = false];
string min_order_quote = 9
string min_order_quote = 10
[(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Int", (gogoproto.nullable) = false];
string max_order_quantity = 10
string max_order_quantity = 11
[(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Int", (gogoproto.nullable) = false];
string max_order_quote = 11
string max_order_quote = 12
[(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Int", (gogoproto.nullable) = false];
}

Expand Down
19 changes: 10 additions & 9 deletions proto/crescent/exchange/v1beta1/query.proto
Original file line number Diff line number Diff line change
Expand Up @@ -104,20 +104,21 @@ message MarketResponse {
string base_denom = 2;
string quote_denom = 3;
string escrow_address = 4;
string maker_fee_rate = 5
string fee_collector = 5;
string maker_fee_rate = 6
[(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec", (gogoproto.nullable) = false];
string taker_fee_rate = 6
string taker_fee_rate = 7
[(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec", (gogoproto.nullable) = false];
string order_source_fee_ratio = 7
string order_source_fee_ratio = 8
[(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec", (gogoproto.nullable) = false];
string min_order_quantity = 8
string min_order_quantity = 9
[(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Int", (gogoproto.nullable) = false];
string min_order_quote = 9
string min_order_quote = 10
[(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Int", (gogoproto.nullable) = false];
string max_order_quantity = 10
string max_order_quantity = 11
[(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Int", (gogoproto.nullable) = false];
string max_order_quote = 11
string max_order_quote = 12
[(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Int", (gogoproto.nullable) = false];
string last_price = 12 [(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec"];
int64 last_matching_height = 13;
string last_price = 13 [(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec"];
int64 last_matching_height = 14;
}
13 changes: 8 additions & 5 deletions x/exchange/keeper/matching.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func (k Keeper) ConstructMemOrderBookSide(
deposit := types.DepositAmount(opts.IsBuy, price, qty)
if escrow != nil {
payDenom, _ := types.PayReceiveDenoms(market.BaseDenom, market.QuoteDenom, opts.IsBuy)
escrow.Lock(ordererAddr, sdk.NewCoin(payDenom, deposit))
escrow.Escrow(ordererAddr, sdk.NewCoin(payDenom, deposit))
}
obs.AddOrder(types.NewOrderSourceMemOrder(
ordererAddr, opts.IsBuy, price, qty, qty, deposit, source))
Expand Down Expand Up @@ -59,8 +59,8 @@ func (k Keeper) executeOrder(
if mr.IsMatched() {
if !simulate {
payDenom, receiveDenom := types.PayReceiveDenoms(market.BaseDenom, market.QuoteDenom, isBuy)
escrow.Lock(ordererAddr, sdk.NewCoin(payDenom, mr.Paid))
escrow.Unlock(ordererAddr, sdk.NewCoin(receiveDenom, mr.Received))
escrow.Escrow(ordererAddr, sdk.NewCoin(payDenom, mr.Paid))
escrow.Release(ordererAddr, sdk.NewCoin(receiveDenom, mr.Received))
if err = k.finalizeMatching(ctx, market, obs.Orders(), escrow); err != nil {
return
}
Expand Down Expand Up @@ -135,17 +135,20 @@ func (k Keeper) finalizeMatching(
k.SetOrder(ctx, order)
}
}
escrow.Unlock(ordererAddr, receivedCoin)
escrow.Release(ordererAddr, receivedCoin)
}
// Should refund deposit
remainingDeposit := memOrder.RemainingDeposit.Sub(res.Paid)
if memOrder.Type == types.OrderSourceMemOrder && remainingDeposit.IsPositive() {
escrow.Unlock(ordererAddr, sdk.NewCoin(payDenom, remainingDeposit))
escrow.Release(ordererAddr, sdk.NewCoin(payDenom, remainingDeposit))
}
}
if err := escrow.Transact(ctx, k.bankKeeper); err != nil {
return err
}
if err := k.CollectFees(ctx, market); err != nil {
return err
}
for _, sourceName := range sourceNames {
results := ordersBySource[sourceName]
if len(results) > 0 {
Expand Down
20 changes: 20 additions & 0 deletions x/exchange/keeper/order.go
Original file line number Diff line number Diff line change
Expand Up @@ -386,3 +386,23 @@ func (k Keeper) CancelExpiredOrders(ctx sdk.Context) (err error) {
})
return err
}

func (k Keeper) CollectFees(ctx sdk.Context, market types.Market) error {
var deposits sdk.Coins
k.IterateOrdersByMarket(ctx, market.Id, func(order types.Order) (stop bool) {
payDenom, _ := types.PayReceiveDenoms(market.BaseDenom, market.QuoteDenom, order.IsBuy)
deposit := sdk.NewCoin(payDenom, order.RemainingDeposit)
deposits = deposits.Add(deposit)
return false
})
escrowAddr := market.MustGetEscrowAddress()
feeCollectorAddr := market.MustGetFeeCollectorAddress()
escrowBalances := k.bankKeeper.SpendableCoins(ctx, escrowAddr)
fees := escrowBalances.Sub(deposits)
if fees.IsAllPositive() {
if err := k.bankKeeper.SendCoins(ctx, escrowAddr, feeCollectorAddr, fees); err != nil {
return err
}
}
return nil
}
6 changes: 4 additions & 2 deletions x/exchange/keeper/order_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,14 +158,16 @@ func (s *KeeperTestSuite) TestOrderMatching() {

s.AssertEqual(utils.ParseCoins("1001497ucre,704000uusd"), s.App.BankKeeper.GetAllBalances(s.Ctx, aliceAddr))
s.AssertEqual(utils.ParseCoins("998500ucre,1149051uusd"), s.App.BankKeeper.GetAllBalances(s.Ctx, bobAddr))
s.AssertEqual(utils.ParseCoins("3ucre,146949uusd"), s.App.BankKeeper.GetAllBalances(s.Ctx, market.MustGetEscrowAddress()))
s.AssertEqual(utils.ParseCoins("146500uusd"), s.App.BankKeeper.GetAllBalances(s.Ctx, market.MustGetEscrowAddress()))
s.AssertEqual(utils.ParseCoins("3ucre,449uusd"), s.App.BankKeeper.GetAllBalances(s.Ctx, market.MustGetFeeCollectorAddress()))

s.PlaceLimitOrder(
market.Id, bobAddr, false, utils.ParseDec("96"), sdk.NewInt(1500), time.Hour)

s.AssertEqual(utils.ParseCoins("1002994ucre,704000uusd"), s.App.BankKeeper.GetAllBalances(s.Ctx, aliceAddr))
s.AssertEqual(utils.ParseCoins("997000ucre,1295111uusd"), s.App.BankKeeper.GetAllBalances(s.Ctx, bobAddr))
s.AssertEqual(utils.ParseCoins("6ucre,889uusd"), s.App.BankKeeper.GetAllBalances(s.Ctx, market.MustGetEscrowAddress()))
s.AssertEqual(utils.ParseCoins(""), s.App.BankKeeper.GetAllBalances(s.Ctx, market.MustGetEscrowAddress()))
s.AssertEqual(utils.ParseCoins("6ucre,889uusd"), s.App.BankKeeper.GetAllBalances(s.Ctx, market.MustGetFeeCollectorAddress()))
}

func (s *KeeperTestSuite) TestMinMaxPrice() {
Expand Down
4 changes: 2 additions & 2 deletions x/exchange/types/escrow.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func NewEscrow(escrowAddr sdk.AccAddress) *Escrow {
}
}

func (e *Escrow) Lock(addr sdk.AccAddress, amt ...sdk.Coin) {
func (e *Escrow) Escrow(addr sdk.AccAddress, amt ...sdk.Coin) {
saddr := addr.String()
before, ok := e.deltas[saddr]
if !ok {
Expand All @@ -27,7 +27,7 @@ func (e *Escrow) Lock(addr sdk.AccAddress, amt ...sdk.Coin) {
e.deltas[saddr], _ = before.SafeSub(amt)
}

func (e *Escrow) Unlock(addr sdk.AccAddress, amt ...sdk.Coin) {
func (e *Escrow) Release(addr sdk.AccAddress, amt ...sdk.Coin) {
saddr := addr.String()
before, ok := e.deltas[saddr]
if !ok {
Expand Down
Loading

0 comments on commit 5bf44ac

Please sign in to comment.