Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(x/ecocredit): buyer cannot be the same as seller #1218

Merged
merged 4 commits into from
Jun 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 19 additions & 7 deletions x/ecocredit/client/testsuite/suite.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,9 +239,9 @@ func (s *IntegrationTestSuite) setupGenesis() {
}

func (s *IntegrationTestSuite) setupTestAccounts() {
// create validator account
// create secondary account
info, _, err := s.val.ClientCtx.Keyring.NewMnemonic(
"validator",
"addr2",
keyring.English,
sdk.FullFundraiserPath,
keyring.DefaultBIP39Passphrase,
Expand All @@ -255,9 +255,9 @@ func (s *IntegrationTestSuite) setupTestAccounts() {
// set secondary account
s.addr2 = sdk.AccAddress(info.GetPubKey().Address())

// fund the secondary account
s.fundAccount(s.val.ClientCtx, s.val.Address, s.addr2, sdk.Coins{
sdk.NewInt64Coin(s.cfg.BondDenom, 20000000000000000),
// fund secondary account
s.fundAccount(s.val.ClientCtx, s.addr1, s.addr2, sdk.Coins{
sdk.NewInt64Coin(s.cfg.BondDenom, 100000000),
})
}

Expand All @@ -270,14 +270,20 @@ func (s *IntegrationTestSuite) commonTxFlags() []string {
}

func (s *IntegrationTestSuite) fundAccount(clientCtx client.Context, from, to sdk.AccAddress, coins sdk.Coins) {
_, err := banktestutil.MsgSendExec(
require := s.Require()

out, err := banktestutil.MsgSendExec(
clientCtx,
from,
to,
coins,
s.commonTxFlags()...,
)
s.Require().NoError(err)
require.NoError(err)

var res sdk.TxResponse
require.NoError(clientCtx.Codec.UnmarshalJSON(out.Bytes(), &res))
require.Zero(res.Code, res.RawLog)
}

func (s *IntegrationTestSuite) createClass(clientCtx client.Context, msg *core.MsgCreateClass) (classId string) {
Expand All @@ -297,6 +303,8 @@ func (s *IntegrationTestSuite) createClass(clientCtx client.Context, msg *core.M

var res sdk.TxResponse
require.NoError(clientCtx.Codec.UnmarshalJSON(out.Bytes(), &res))
require.Zero(res.Code, res.RawLog)

for _, e := range res.Logs[0].Events {
if e.Type == proto.MessageName(&core.EventCreateClass{}) {
for _, attr := range e.Attributes {
Expand Down Expand Up @@ -329,6 +337,8 @@ func (s *IntegrationTestSuite) createProject(clientCtx client.Context, msg *core

var res sdk.TxResponse
require.NoError(clientCtx.Codec.UnmarshalJSON(out.Bytes(), &res))
require.Zero(res.Code, res.RawLog)

for _, e := range res.Logs[0].Events {
if e.Type == proto.MessageName(&core.EventCreateProject{}) {
for _, attr := range e.Attributes {
Expand Down Expand Up @@ -363,6 +373,8 @@ func (s *IntegrationTestSuite) createBatch(clientCtx client.Context, msg *core.M

var res sdk.TxResponse
require.NoError(clientCtx.Codec.UnmarshalJSON(out.Bytes(), &res))
require.Zero(res.Code, res.RawLog)

for _, e := range res.Logs[0].Events {
if e.Type == proto.MessageName(&core.EventCreateBatch{}) {
for _, attr := range e.Attributes {
Expand Down
8 changes: 4 additions & 4 deletions x/ecocredit/client/testsuite/tx_marketplace.go
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ func (s *IntegrationTestSuite) TestTxUpdateSellOrders() {
func (s *IntegrationTestSuite) TestTxBuyDirectCmd() {
require := s.Require()

buyer := s.addr1.String()
buyer := s.addr2.String()

sellOrderId := fmt.Sprint(s.sellOrderId)
bidPrice := sdk.NewInt64Coin(s.allowedDenoms[0], 10).String()
Expand Down Expand Up @@ -326,7 +326,7 @@ func (s *IntegrationTestSuite) TestTxBuyDirectCmd() {
"10",
bidPrice,
"true",
fmt.Sprintf("--%s=%s", flags.FlagFrom, s.val.Moniker),
fmt.Sprintf("--%s=%s", flags.FlagFrom, "addr2"),
},
},
{
Expand Down Expand Up @@ -364,7 +364,7 @@ func (s *IntegrationTestSuite) TestTxBuyDirectCmd() {
func (s *IntegrationTestSuite) TestTxBuyDirectBatchCmd() {
require := s.Require()

buyer := s.addr1.String()
buyer := s.addr2.String()

bidPrice := sdk.NewInt64Coin(s.allowedDenoms[0], 10)

Expand Down Expand Up @@ -451,7 +451,7 @@ func (s *IntegrationTestSuite) TestTxBuyDirectBatchCmd() {
name: "valid from key-name",
args: []string{
validJson,
fmt.Sprintf("--%s=%s", flags.FlagFrom, s.val.Moniker),
fmt.Sprintf("--%s=%s", flags.FlagFrom, "addr2"),
},
},
{
Expand Down
16 changes: 16 additions & 0 deletions x/ecocredit/server/marketplace/features/msg_buy_direct.feature
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ Feature: Msg/BuyDirect

Credits can be bought directly:
- when the sell order exists
- when the buyer is not the seller
- when the bid denom matches the sell denom
- when the buyer has a bank balance greater than or equal to the total cost
- when the buyer provides a bid price greater than or equal to the ask price
Expand Down Expand Up @@ -30,6 +31,21 @@ Feature: Msg/BuyDirect
When bob attempts to buy credits with sell order id "1"
Then expect the error "orders[0]: sell order with id 1: not found"

Rule: The buyer must not be the seller

Background:
Given a credit type

Scenario: the buyer is not the seller
Given alice created a sell order with id "1"
When bob attempts to buy credits with sell order id "1"
Then expect no error

Scenario: the buyer is the seller
Given alice created a sell order with id "1"
When alice attempts to buy credits with sell order id "1"
Then expect the error "orders[0]: buyer account cannot be the same as seller account: unauthorized"

Rule: The bid denom must match the sell denom

Background:
Expand Down
7 changes: 7 additions & 0 deletions x/ecocredit/server/marketplace/msg_buy_direct.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@ func (k Keeper) BuyDirect(ctx context.Context, req *marketplace.MsgBuyDirect) (*
return nil, fmt.Errorf("%s: sell order with id %d: %w", orderIndex, order.SellOrderId, err)
}

// check if buyer account is equal to seller account
if buyerAcc.Equals(sdk.AccAddress(sellOrder.Seller)) {
return nil, sdkerrors.ErrUnauthorized.Wrapf(
"%s: buyer account cannot be the same as seller account", orderIndex,
)
}

// check if disable auto-retire is required
if order.DisableAutoRetire && !sellOrder.DisableAutoRetire {
return nil, sdkerrors.ErrInvalidRequest.Wrapf(
Expand Down
18 changes: 18 additions & 0 deletions x/ecocredit/server/marketplace/msg_buy_direct_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,24 @@ func (s *buyDirectSuite) AliceCreatedTwoSellOrdersEachWithQuantityAndAskAmount(a
s.createSellOrders(2)
}

func (s *buyDirectSuite) AliceAttemptsToBuyCreditsWithSellOrderId(a string) {
id, err := strconv.ParseUint(a, 10, 32)
require.NoError(s.t, err)

s.singleBuyOrderExpectCalls()

s.res, s.err = s.k.BuyDirect(s.ctx, &marketplace.MsgBuyDirect{
Buyer: s.alice.String(),
Orders: []*marketplace.MsgBuyDirect_Order{
{
SellOrderId: id,
Quantity: s.quantity,
BidPrice: &s.bidPrice,
},
},
})
}

func (s *buyDirectSuite) BobAttemptsToBuyCreditsWithSellOrderId(a string) {
id, err := strconv.ParseUint(a, 10, 32)
require.NoError(s.t, err)
Expand Down