Skip to content

Commit

Permalink
feat(x/ecocredit): Msg Server implementation for buy/sell functionali…
Browse files Browse the repository at this point in the history
…ty (#613)

* first edits for msg server

* begin msg server implementation for buy / sell functionality

* buy order sequence

* add buy/sell events

* update sell order

* buy order processing

* go mod tidy

* cleanup and msgs

* go mod tidy

* validate basic

* allow ask denom

* fix and cleanup

* process buy order

Co-authored-by: Cory <[email protected]>

* verify root address

* Apply suggestions from code review

Co-authored-by: MD Aleem <[email protected]>
Co-authored-by: Tyler <[email protected]>

* improve validate

* Apply suggestions from code review

Co-authored-by: Tyler <[email protected]>

* address review comments

* address review comments

* add filled event

* update sell order

* complete sell order

* Apply suggestions from code review

Co-authored-by: Tyler <[email protected]>

* address review comment

* buy order table

* fix primary key

Co-authored-by: Ryan Christoffersen <[email protected]>
Co-authored-by: MD Aleem <[email protected]>
Co-authored-by: Tyler <[email protected]>
  • Loading branch information
4 people authored Nov 15, 2021
1 parent 8994951 commit 081ae07
Show file tree
Hide file tree
Showing 19 changed files with 10,319 additions and 2,413 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ require (
github.com/tendermint/tm-db v0.6.4
golang.org/x/crypto v0.0.0-20211108221036-ceb1ce70b4fa // indirect
golang.org/x/text v0.3.7 // indirect
google.golang.org/genproto v0.0.0-20211111162719-482062a4217b // indirect
)

require (
Expand Down Expand Up @@ -124,7 +125,6 @@ require (
golang.org/x/net v0.0.0-20210903162142-ad29c8ab022f // indirect
golang.org/x/sys v0.0.0-20210903071746-97244b99971b // indirect
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1 // indirect
google.golang.org/genproto v0.0.0-20211104193956-4c6863e31247 // indirect
google.golang.org/grpc v1.40.0 // indirect
google.golang.org/protobuf v1.27.1 // indirect
gopkg.in/ini.v1 v1.62.0 // indirect
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1543,8 +1543,8 @@ google.golang.org/genproto v0.0.0-20210310155132-4ce2db91004e/go.mod h1:FWY/as6D
google.golang.org/genproto v0.0.0-20210319143718-93e7006c17a6/go.mod h1:FWY/as6DDZQgahTzZj3fqbO1CbirC29ZNUFHwi0/+no=
google.golang.org/genproto v0.0.0-20210402141018-6c239bbf2bb1/go.mod h1:9lPAdzaEmUacj36I+k7YKbEc5CXzPIeORRgDAUOu28A=
google.golang.org/genproto v0.0.0-20210602131652-f16073e35f0c/go.mod h1:UODoCrxHCcBojKKwX1terBiRUaqAsFqJiF615XL43r0=
google.golang.org/genproto v0.0.0-20211104193956-4c6863e31247 h1:ZONpjmFT5e+I/0/xE3XXbG5OIvX2hRYzol04MhKBl2E=
google.golang.org/genproto v0.0.0-20211104193956-4c6863e31247/go.mod h1:5CzLGKJ67TSI2B9POpiiyGha0AjJvZIUgRMt1dSmuhc=
google.golang.org/genproto v0.0.0-20211111162719-482062a4217b h1:qvEQEwKjZRAg6rjY/jqfJ7T8/w/D7jTIFJGcaSka96k=
google.golang.org/genproto v0.0.0-20211111162719-482062a4217b/go.mod h1:5CzLGKJ67TSI2B9POpiiyGha0AjJvZIUgRMt1dSmuhc=
google.golang.org/grpc v1.33.2 h1:EQyQC3sa8M+p6Ulc8yy9SWSS2GVwyRc83gAbG8lrl4o=
google.golang.org/grpc v1.33.2/go.mod h1:JMHMWHQWaTccqQQlmk3MJZS+GWXOdAesneDmEnv2fbc=
google.golang.org/protobuf v0.0.0-20200109180630-ec00e32a8dfd/go.mod h1:DFci5gLYBciE7Vtevhsrf46CRTquxDuWsQurQQe4oz8=
Expand Down
113 changes: 113 additions & 0 deletions proto/regen/ecocredit/v1alpha1/events.proto
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ syntax = "proto3";

package regen.ecocredit.v1alpha1;

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

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

// EventCreateClass is an event emitted when a credit class is created.
Expand Down Expand Up @@ -103,3 +105,114 @@ message EventCancel {
// amount is the decimal number of credits that have been cancelled.
string amount = 3;
}

// EventSell is an event emitted when a sell order is created.
message EventSell {

// order_id is the unique ID of sell order.
uint64 order_id = 1;

// batch_denom is the credit batch being sold.
string batch_denom = 2;

// quantity is the quantity of credits being sold.
string quantity = 3;

// ask_price is the price the seller is asking for each unit of the
// batch_denom. Each credit unit of the batch will be sold for at least the
// ask_price or more.
cosmos.base.v1beta1.Coin ask_price = 4;

// disable_auto_retire disables auto-retirement of credits which allows a
// buyer to disable auto-retirement in their buy order enabling them to
// resell the credits to another buyer.
bool disable_auto_retire = 5;
}

// EventUpdateSellOrder is an event emitted when a sell order is updated.
message EventUpdateSellOrder {

// owner is the owner of the sell orders.
string owner = 1;

// sell_order_id is the ID of an existing sell order.
uint64 sell_order_id = 2;

// batch_denom is the credit batch being sold.
string batch_denom = 3;

// new_quantity is the updated quantity of credits available to sell, if it
// is set to zero then the order is cancelled.
string new_quantity = 4;

// new_ask_price is the new ask price for this sell order
cosmos.base.v1beta1.Coin new_ask_price = 5;

// disable_auto_retire updates the disable_auto_retire field in the sell order.
bool disable_auto_retire = 6;
}

// EventBuyOrderCreated is an event emitted when a buy order is created.
message EventBuyOrderCreated {

// buy_order_id is the unique ID of buy order.
uint64 buy_order_id = 1;

// sell_order_id is the sell order ID against which the buyer is trying to buy.
uint64 sell_order_id = 2;

// quantity is the quantity of credits to buy. If the quantity of credits
// available is less than this amount the order will be partially filled
// unless disable_partial_fill is true.
string quantity = 3;

// bid price is the bid price for this buy order. A credit unit will be
// settled at a purchase price that is no more than the bid price. The
// buy order will fail if the buyer does not have enough funds available
// to complete the purchase.
cosmos.base.v1beta1.Coin bid_price = 4;

// disable_auto_retire allows auto-retirement to be disabled. If it is set to true
// the credits will not auto-retire and can be resold assuming that the
// corresponding sell order has auto-retirement disabled. If the sell order
// hasn't disabled auto-retirement and the buy order tries to disable it,
// that buy order will fail.
bool disable_auto_retire = 5;

// disable_partial_fill disables the default behavior of partially filling
// buy orders if the requested quantity is not available.
bool disable_partial_fill = 6;
}

// EventBuyOrderFilled is an event emitted when a buy order is filled.
message EventBuyOrderFilled {

// buy_order_id is the unique ID of the buy order.
uint64 buy_order_id = 1;

// sell_order_id is the unique ID of the sell order.
uint64 sell_order_id = 2;

// batch_denom is the credit batch ID of the purchased credits.
string batch_denom = 3;

// quantity is the quantity of the purchased credits.
string quantity = 4;

// total_price is the total price for the purchased credits.
cosmos.base.v1beta1.Coin total_price = 5;
}

// EventAllowAskDenom is an event emitted when an ask denom is added.
message EventAllowAskDenom {

// denom is the denom to allow (ex. ibc/GLKHDSG423SGS)
string denom = 1;

// display_denom is the denom to display to the user and is informational
string display_denom = 2;

// exponent is the exponent that relates the denom to the display_denom and is
// informational
uint32 exponent = 3;
}
1 change: 0 additions & 1 deletion proto/regen/ecocredit/v1alpha1/tx.proto
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,6 @@ message MsgBuyResponse {
repeated uint64 buy_order_ids = 1;
}


// MsgAllowAskDenom is the Msg/AllowAskDenom request type.
message MsgAllowAskDenom {
// root_address is the address of the governance account which can authorize ask denoms
Expand Down
92 changes: 91 additions & 1 deletion proto/regen/ecocredit/v1alpha1/types.proto
Original file line number Diff line number Diff line change
Expand Up @@ -117,4 +117,94 @@ message CreditTypeSeq {

// The sequence number of classes of the credit type
uint64 seq_number = 2;
}
}

// SellOrder represents the information for a sell order.
message SellOrder {

// order_id is the unique ID of sell order.
uint64 order_id = 1;

// owner is the address of the owner of the credits being sold.
string owner = 2;

// batch_denom is the credit batch being sold.
string batch_denom = 3;

// quantity is the quantity of credits being sold.
string quantity = 4;

// ask_price is the price the seller is asking for each unit of the
// batch_denom. Each credit unit of the batch will be sold for at least the
// ask_price or more.
cosmos.base.v1beta1.Coin ask_price = 5;

// disable_auto_retire disables auto-retirement of credits which allows a
// buyer to disable auto-retirement in their buy order enabling them to
// resell the credits to another buyer.
bool disable_auto_retire = 6;
}

// BuyOrder represents the information for a buy order.
message BuyOrder {

// Selection defines a buy order selection.
message Selection {

// sum defines the type of selection.
oneof sum {
// sell_order_id is the sell order ID against which the buyer is trying to buy.
// When sell_order_id is set, this is known as a direct buy order because it
// is placed directly against a specific sell order.
uint64 sell_order_id = 1;

// TODO: once we have filters defined for baskets, we can enable filtered
// buy orders which will involve full double-sided batch order matching
//
// filter selects credits to buy based upon the specified filter criteria.
// Filter filter = 2;
}
}

// buy_order_id is the unique ID of buy order.
uint64 buy_order_id = 1;

// selection is the buy order selection.
Selection selection = 2;

// quantity is the quantity of credits to buy. If the quantity of credits
// available is less than this amount the order will be partially filled
// unless disable_partial_fill is true.
string quantity = 3;

// bid price is the bid price for this buy order. A credit unit will be
// settled at a purchase price that is no more than the bid price. The
// buy order will fail if the buyer does not have enough funds available
// to complete the purchase.
cosmos.base.v1beta1.Coin bid_price = 4;

// disable_auto_retire allows auto-retirement to be disabled. If it is set to true
// the credits will not auto-retire and can be resold assuming that the
// corresponding sell order has auto-retirement disabled. If the sell order
// hasn't disabled auto-retirement and the buy order tries to disable it,
// that buy order will fail.
bool disable_auto_retire = 5;

// disable_partial_fill disables the default behavior of partially filling
// buy orders if the requested quantity is not available.
bool disable_partial_fill = 6;
}

// AskDenom represents the information for an ask denom.
message AskDenom {

// denom is the denom to allow (ex. ibc/GLKHDSG423SGS)
string denom = 1;

// display_denom is the denom to display to the user and is informational
string display_denom = 2;

// exponent is the exponent that relates the denom to the display_denom and is
// informational
uint32 exponent = 3;
}
1 change: 1 addition & 0 deletions x/ecocredit/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ var (
ErrParseFailure = sdkerrors.Register(ModuleName, 2, "parse error")
ErrInsufficientFunds = sdkerrors.Register(ModuleName, 3, "insufficient credit balance")
ErrMaxLimit = sdkerrors.Register(ModuleName, 4, "limit exceeded")
ErrInvalidSellOrder = sdkerrors.Register(ModuleName, 5, "invalid sell order")
)
Loading

0 comments on commit 081ae07

Please sign in to comment.