Skip to content

Commit

Permalink
add seller and buyer fee data to EventBuyDirect
Browse files Browse the repository at this point in the history
  • Loading branch information
aaronc committed Feb 28, 2024
1 parent c4a1c81 commit b1a7ad2
Show file tree
Hide file tree
Showing 7 changed files with 746 additions and 77 deletions.
431 changes: 386 additions & 45 deletions api/regen/ecocredit/marketplace/v1/events.pulsar.go

Large diffs are not rendered by default.

16 changes: 16 additions & 0 deletions proto/regen/ecocredit/marketplace/v1/events.proto
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ syntax = "proto3";

package regen.ecocredit.marketplace.v1;

import "cosmos/base/v1beta1/coin.proto";

option go_package = "github.com/regen-network/regen-ledger/x/ecocredit/marketplace/types/v1";

// EventSell is an event emitted when a sell order is created.
Expand All @@ -17,6 +19,20 @@ message EventBuyDirect {
// sell_order_id is the unique identifier of the sell order that credits were
// purchased from.
uint64 sell_order_id = 1;

// seller is the address of the account that sold the credits.
string seller = 2;

// seller_fee_paid is the amount of coins paid by the seller
// to the marketplace as a fee for facilitating the sale.
cosmos.base.v1beta1.Coin seller_fee_paid = 3;

// buyer is the address of the account that purchased the credits.
string buyer = 4;

// buyer_fee_paid is the amount of coins paid by the buyer
// to the marketplace as a fee for facilitating the sale.
cosmos.base.v1beta1.Coin buyer_fee_paid = 5;
}

// EventUpdateSellOrder is an event emitted when a sell order is updated.
Expand Down
52 changes: 49 additions & 3 deletions x/ecocredit/marketplace/keeper/features/msg_buy_direct.feature
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,17 @@ Feature: Msg/BuyDirect
And expect event buy direct with properties
"""
{
"sell_order_id": 1
"sell_order_id": 1,
"seller": "regen1nzh226hxrsvf4k69sa8v0nfuzx5vgwkczk8j68",
"seller_fee_paid":{
"amount": "0",
"denom": "regen"
},
"buyer": "regen1depk54cuajgkzea6zpgkq36tnjwdzv4ak663u6",
"buyer_fee_paid":{
"amount": "0",
"denom": "regen"
}
}
"""

Expand All @@ -393,7 +403,9 @@ Feature: Msg/BuyDirect
Given a credit type

Scenario: fees go to fee pool
Given alice created a sell order with quantity "10" and ask price "10foo"
Given alice's address "regen1nzh226hxrsvf4k69sa8v0nfuzx5vgwkczk8j68"
* bob's address "regen1depk54cuajgkzea6zpgkq36tnjwdzv4ak663u6"
* alice created a sell order with quantity "10" and ask price "10foo"
* bob has bank balance "110foo"
* alice has bank balance "0foo"
* buyer fees are 0.1 and seller fees are 0.05
Expand All @@ -402,9 +414,27 @@ Feature: Msg/BuyDirect
* expect alice bank balance "95foo"
* expect bob bank balance "0foo"
* expect fee pool balance "15foo"
And expect event buy direct with properties
"""
{
"sell_order_id": 1,
"seller": "regen1nzh226hxrsvf4k69sa8v0nfuzx5vgwkczk8j68",
"seller_fee_paid":{
"amount": "5",
"denom": "foo"
},
"buyer": "regen1depk54cuajgkzea6zpgkq36tnjwdzv4ak663u6",
"buyer_fee_paid":{
"amount": "10",
"denom": "foo"
}
}
"""

Scenario: fees get burned
Given alice created a sell order with quantity "10" and ask price "20uregen"
Given alice's address "regen1nzh226hxrsvf4k69sa8v0nfuzx5vgwkczk8j68"
* bob's address "regen1depk54cuajgkzea6zpgkq36tnjwdzv4ak663u6"
* alice created a sell order with quantity "10" and ask price "20uregen"
* bob has bank balance "240uregen"
* alice has bank balance "0regen"
* buyer fees are 0.2 and seller fees are 0.1
Expand All @@ -413,6 +443,22 @@ Feature: Msg/BuyDirect
* expect alice bank balance "180uregen"
* expect bob bank balance "0uregen"
* expect fee pool balance "0uregen"
And expect event buy direct with properties
"""
{
"sell_order_id": 1,
"seller": "regen1nzh226hxrsvf4k69sa8v0nfuzx5vgwkczk8j68",
"seller_fee_paid":{
"amount": "20",
"denom": "uregen"
},
"buyer": "regen1depk54cuajgkzea6zpgkq36tnjwdzv4ak663u6",
"buyer_fee_paid":{
"amount": "40",
"denom": "uregen"
}
}
"""

Rule: max_fee_amount is enforced
Scenario Outline: max_fee_amount is enforced
Expand Down
6 changes: 0 additions & 6 deletions x/ecocredit/marketplace/keeper/msg_buy_direct.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,12 +148,6 @@ func (k Keeper) BuyDirect(ctx context.Context, req *types.MsgBuyDirect) (*types.
}); err != nil {
return nil, err
}

if err = sdkCtx.EventManager().EmitTypedEvent(&types.EventBuyDirect{
SellOrderId: sellOrder.Id,
}); err != nil {
return nil, err
}
}

return &types.MsgBuyDirectResponse{}, nil
Expand Down
28 changes: 25 additions & 3 deletions x/ecocredit/marketplace/keeper/msg_buy_direct_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,15 @@ import (

"github.com/cockroachdb/apd/v3"
"github.com/gogo/protobuf/jsonpb"
gogoproto "github.com/gogo/protobuf/proto"
"github.com/golang/mock/gomock"
"github.com/google/go-cmp/cmp"
"github.com/regen-network/gocuke"
"github.com/stretchr/testify/require"
abci "github.com/tendermint/tendermint/abci/types"
protoreflect "google.golang.org/protobuf/reflect/protoreflect"
"google.golang.org/protobuf/reflect/protoregistry"
"google.golang.org/protobuf/testing/protocmp"

"github.com/cosmos/cosmos-sdk/orm/types/ormerrors"
sdk "github.com/cosmos/cosmos-sdk/types"
Expand Down Expand Up @@ -549,14 +555,30 @@ func (s *buyDirectSuite) ExpectBatchSupply(a gocuke.DocString) {

func (s *buyDirectSuite) ExpectEventBuyDirectWithProperties(a gocuke.DocString) {
var event types.EventBuyDirect
err := json.Unmarshal([]byte(a.Content), &event)
err := jsonpb.UnmarshalString(a.Content, &event)
require.NoError(s.t, err)

sdkEvent, found := testutil.GetEvent(&event, s.sdkCtx.EventManager().Events())
s.expectEvent(&event)
}

func (s *buyDirectSuite) expectEvent(expected gogoproto.Message) {
sdkEvent, found := testutil.GetEvent(expected, s.sdkCtx.EventManager().Events())
require.True(s.t, found)

err = testutil.MatchEvent(&event, sdkEvent)
foundEvt, err := sdk.ParseTypedEvent(abci.Event(sdkEvent))
require.NoError(s.t, err)

msgType, err := protoregistry.GlobalTypes.FindMessageByName(protoreflect.FullName(gogoproto.MessageName(expected)))
require.NoError(s.t, err)
evt := msgType.New().Interface()
evt2 := msgType.New().Interface()

require.NoError(s.t, gogoToProtoReflect(foundEvt, evt))
require.NoError(s.t, gogoToProtoReflect(expected, evt2))

if diff := cmp.Diff(evt, evt2, protocmp.Transform()); diff != "" {
s.t.Fatalf("unexpected event diff: %s", diff)
}
}

func (s *buyDirectSuite) ExpectEventTransferWithProperties(a gocuke.DocString) {
Expand Down
16 changes: 15 additions & 1 deletion x/ecocredit/marketplace/keeper/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
api "github.com/regen-network/regen-ledger/api/v2/regen/ecocredit/marketplace/v1"
"github.com/regen-network/regen-ledger/types/v2/math"
basetypes "github.com/regen-network/regen-ledger/x/ecocredit/v3/base/types/v1"
types "github.com/regen-network/regen-ledger/x/ecocredit/v3/marketplace/types/v1"
"github.com/regen-network/regen-ledger/x/ecocredit/v3/server/utils"
)

Expand Down Expand Up @@ -243,11 +244,24 @@ func (k Keeper) fillOrder(ctx context.Context, params fillOrderParams) error {
return err
}

seller := sdk.AccAddress(params.sellOrder.Seller)

// send seller payment from buyer account to seller account
return k.bankKeeper.SendCoins(sdkCtx, params.buyerAcc, params.sellOrder.Seller, sdk.NewCoins(sdk.NewCoin(
err = k.bankKeeper.SendCoins(sdkCtx, params.buyerAcc, seller, sdk.NewCoins(sdk.NewCoin(
params.bankDenom,
sellerPayment.SdkIntTrim(),
)))
if err != nil {
return err
}

return sdkCtx.EventManager().EmitTypedEvent(&types.EventBuyDirect{
SellOrderId: params.sellOrder.Id,
Buyer: params.buyerAcc.String(),
BuyerFeePaid: &sdk.Coin{Denom: params.bankDenom, Amount: params.buyerFee.SdkIntTrim()},
Seller: seller.String(),
SellerFeePaid: &sdk.Coin{Denom: params.bankDenom, Amount: sellerFee.SdkIntTrim()},
})
}

// getSubTotalCost calculates the total cost of the buy order by multiplying the price per credit specified
Expand Down
Loading

0 comments on commit b1a7ad2

Please sign in to comment.