Skip to content

Commit

Permalink
Add field use_relayer
Browse files Browse the repository at this point in the history
  • Loading branch information
gokuseii committed Apr 19, 2023
1 parent 723e593 commit fafe2be
Show file tree
Hide file tree
Showing 25 changed files with 64 additions and 10 deletions.
5 changes: 5 additions & 0 deletions docs/spec/components/schemas/AddMatch.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ allOf:
- token_to_sell
- src_chain_id
- origin_chain_id
- use_relayer
properties:
token_to_sell:
type: string
Expand All @@ -29,3 +30,7 @@ allOf:
format: int64
description: Source blockchain of the order to match
example: 5
use_relayer:
type: bool
description: Relayer execute match order
example: false
5 changes: 5 additions & 0 deletions docs/spec/components/schemas/AddOrder.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ allOf:
- token_to_buy
- src_chain_id
- dest_chain_id
- use_relayer
properties:
token_to_sell:
type: string
Expand All @@ -35,3 +36,7 @@ allOf:
format: int64
description: Source blockchain of the match order which is intended to execute the order
example: 5
use_relayer:
type: bool
description: Relayer execute order
example: false
5 changes: 5 additions & 0 deletions docs/spec/components/schemas/MatchAttributes.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ required:
- token_to_sell
- amount_to_sell
- state
- use_relayer
properties:
match_id:
type: integer
Expand All @@ -31,3 +32,7 @@ properties:
format: uint8
description: Match order state
enum: [1, 2, 3, 4]
use_relayer:
type: bool
description: Relayer execute match order
example: false
5 changes: 5 additions & 0 deletions docs/spec/components/schemas/OrderAttributes.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ required:
- amount_to_sell
- amount_to_buy
- state
- use_relayer
properties:
order_id:
type: integer
Expand Down Expand Up @@ -41,3 +42,7 @@ properties:
pattern: '^0x[0-9A-Fa-f]{40}$'
description: Swapica contract address on the destination network
example: "0xe6e5a1E8236394cCC8cFaD6E44d1705B09233BA7"
use_relayer:
type: bool
description: Relayer execute order
example: false
2 changes: 2 additions & 0 deletions internal/assets/migrations/001_initial.sql
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ CREATE TABLE orders
buy_amount numeric(78) NOT NULL,
dest_chain bigint NOT NULL,
state smallint NOT NULL,
use_relayer boolean NOT NULL,
executed_by_match bigint,
match_id bigint,
match_swapica varchar(42),
Expand All @@ -44,6 +45,7 @@ CREATE TABLE match_orders
sell_token bigint NOT NULL,
sell_amount numeric(78) NOT NULL,
state smallint NOT NULL,
use_relayer boolean NOT NULL,

UNIQUE (match_id, src_chain),
FOREIGN KEY (sell_token) REFERENCES tokens (id),
Expand Down
2 changes: 2 additions & 0 deletions internal/data/match.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ type MatchOrders interface {
FilterByState(*uint8) MatchOrders
FilterExpired(*bool) MatchOrders
FilterClaimable(creator string, srcChain *int64) MatchOrders
FilterByUseRelayer(*bool) MatchOrders
}

// Match Fields ID and OriginOrder are database-generated properties, any other come from the
Expand All @@ -36,4 +37,5 @@ type Match struct {
SellToken int64 `structs:"sell_token" db:"sell_token"`
SellAmount string `structs:"sell_amount" db:"sell_amount"`
State uint8 `structs:"state" db:"state"`
UseRelayer bool `structs:"use_relayer" db:"use_relayer"`
}
2 changes: 2 additions & 0 deletions internal/data/order.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ type Orders interface {
FilterByTokenToSell(*string) Orders
FilterByDestChain(*int64) Orders
FilterByState(*uint8) Orders
FilterByUseRelayer(*bool) Orders
}

// Order Fields ID and ExecutedByMatch are database-generated properties, any other come from the
Expand All @@ -40,6 +41,7 @@ type Order struct {
BuyAmount string `structs:"buy_amount" db:"buy_amount"`
DestChain int64 `structs:"dest_chain" db:"dest_chain"`
State uint8 `structs:"state" db:"state"`
UseRelayer bool `structs:"use_relayer" db:"use_relayer"`

// ExecutedByMatch foreign key for match_orders(ID)
ExecutedByMatch sql.NullInt64 `structs:"executed_by_match,omitempty,omitnested" db:"executed_by_match"`
Expand Down
4 changes: 4 additions & 0 deletions internal/data/postgres/match.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,10 @@ func (q *matches) FilterClaimable(creator string, chain *int64) data.MatchOrders
return q
}

func (q *matches) FilterByUseRelayer(useRelayer *bool) data.MatchOrders {
return q.filterByCol("use_relayer", useRelayer)
}

func (q *matches) filterByCol(column string, value interface{}) *matches {
if isNilInterface(value) {
return q
Expand Down
4 changes: 4 additions & 0 deletions internal/data/postgres/order.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,10 @@ func (q *orders) FilterByState(state *uint8) data.Orders {
return q.filterByCol("state", state)
}

func (q *orders) FilterByUseRelayer(useRelayer *bool) data.Orders {
return q.filterByCol("use_relayer", useRelayer)
}

func (q *orders) filterByCol(column string, value interface{}) *orders {
if isNilInterface(value) {
return q
Expand Down
2 changes: 2 additions & 0 deletions internal/data/postgres/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ func isNilInterface(v interface{}) bool {
return v == nil
case *uint8:
return v == nil
case *bool:
return v == nil
}
return v == nil
// return v == nil || (reflect.ValueOf(v).Kind() == reflect.Ptr && reflect.ValueOf(v).IsNil()) // 7 times slower
Expand Down
3 changes: 2 additions & 1 deletion internal/service/handlers/list_matches.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ func ListMatches(w http.ResponseWriter, r *http.Request) {
FilterBySrcChain(req.FilterSrcChain).
FilterByCreator(req.FilterCreator).
FilterByState(req.FilterState).
FilterExpired(req.FilterExpired)
FilterExpired(req.FilterExpired).
FilterByUseRelayer(req.FilterUseRelayer)

matches, err := q.Page(&req.OffsetPageParams).Select()
if err != nil {
Expand Down
3 changes: 2 additions & 1 deletion internal/service/handlers/list_orders.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ func ListOrders(w http.ResponseWriter, r *http.Request) {
FilterByTokenToBuy(req.FilterBuyToken).
FilterByTokenToSell(req.FilterSellToken).
FilterByCreator(req.FilterCreator).
FilterByState(req.FilterState)
FilterByState(req.FilterState).
FilterByUseRelayer(req.FilterUseRelayer)

orders, err := q.Page(&req.OffsetPageParams).Select()
if err != nil {
Expand Down
2 changes: 2 additions & 0 deletions internal/service/requests/add_match.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ func (r *AddMatch) validate() error {
"data/attributes/src_chain_id": val.Validate(a.SrcChainId, val.Required, val.Min(1)),
"data/attributes/origin_chain_id": val.Validate(a.OriginChainId, val.Required, val.Min(1)),
"data/attributes/origin_order_id": val.Validate(a.OriginOrderId, val.Required, val.Min(1)),
"data/attributes/use_relayer": val.Validate(a.UseRelayer, val.NotNil),
}.Filter()
}

Expand All @@ -47,5 +48,6 @@ func (r *AddMatch) DBModel(originOrder, sellToken int64) data.Match {
SellToken: sellToken,
SellAmount: r.Data.Attributes.AmountToSell,
State: r.Data.Attributes.State,
UseRelayer: r.Data.Attributes.UseRelayer,
}
}
2 changes: 2 additions & 0 deletions internal/service/requests/add_order.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ func (r *AddOrder) validate() error {
"data/attributes/dest_chain_id": val.Validate(a.DestChainId, val.Required, val.Min(1)),
"data/attributes/match_id": val.Validate(a.MatchId, val.Nil),
"data/attributes/match_swapica": val.Validate(a.MatchSwapica, val.Nil),
"data/attributes/use_relayer": val.Validate(a.UseRelayer, val.NotNil),
}.Filter()
}

Expand All @@ -50,6 +51,7 @@ func (r *AddOrder) DBModel(sellToken, buyToken int64) data.Order {
BuyAmount: r.Data.Attributes.AmountToBuy,
DestChain: r.Data.Attributes.DestChainId,
State: r.Data.Attributes.State,
UseRelayer: r.Data.Attributes.UseRelayer,
// ExecutedByMatch, MatchID, MatchSwapica must not appear on the order creation, according to the core contract
}
}
1 change: 1 addition & 0 deletions internal/service/requests/list_matches.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ type ListMatches struct {
FilterState *uint8 `filter:"state"`
FilterCreator *string `filter:"creator"`
FilterExpired *bool `filter:"expired"`
FilterUseRelayer *bool `filter:"use_relayer"`
IncludeSrcChain bool `include:"src_chain"`
IncludeOriginChain bool `include:"origin_chain"`
IncludeOriginOrder bool `include:"origin_order"`
Expand Down
1 change: 1 addition & 0 deletions internal/service/requests/list_orders.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ type ListOrders struct {
FilterSellToken *string `filter:"token_to_sell"`
FilterDestChain *int64 `filter:"destination_chain"`
FilterState *uint8 `filter:"state"`
FilterUseRelayer *bool `filter:"use_relayer"`
IncludeSrcChain bool `include:"src_chain"`
IncludeDestChain bool `include:"destination_chain"`
IncludeBuyToken bool `include:"token_to_buy"`
Expand Down
1 change: 1 addition & 0 deletions internal/service/responses/matches.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ func ToMatchResource(m data.Match, srcChain, originChain resources.Key) resource
MatchId: m.MatchID,
OriginOrderId: m.OrderID,
State: m.State,
UseRelayer: m.UseRelayer,
},
Relationships: resources.MatchRelationships{
OriginChain: resources.Relation{Data: &originChain},
Expand Down
1 change: 1 addition & 0 deletions internal/service/responses/orders.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ func ToOrderResource(o data.Order, srcChain, destChain resources.Key) resources.
MatchSwapica: matchSwapica,
OrderId: o.OrderID,
State: o.State,
UseRelayer: o.UseRelayer,
},
Relationships: resources.OrderRelationships{
DestinationChain: resources.Relation{Data: &destChain},
Expand Down
4 changes: 2 additions & 2 deletions resources/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"gitlab.com/distributed_lab/logan/v3/errors"
)

// driverValue - converts interface into db supported type
//driverValue - converts interface into db supported type
func driverValue(data interface{}) (driver.Value, error) {
data, err := json.Marshal(data)
if err != nil {
Expand All @@ -22,7 +22,7 @@ func driverValue(data interface{}) (driver.Value, error) {
return data, nil
}

// driveScan - converts jsonb into type struct
//driveScan - converts jsonb into type struct
func driveScan(src, dest interface{}) error {
data, err := convertJSONB(src)
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions resources/included.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func (c *Included) add(include Resource) {
c.includes[include.GetKey()] = json.RawMessage(data)
}

// MarshalJSON - marshals include collection as array of json objects
//MarshalJSON - marshals include collection as array of json objects
func (c Included) MarshalJSON() ([]byte, error) {
uniqueEntries := make([]json.RawMessage, 0, len(c.includes))
for _, value := range c.includes {
Expand All @@ -56,7 +56,7 @@ func (c Included) MarshalJSON() ([]byte, error) {
return json.Marshal(uniqueEntries)
}

// UmarshalJSON - unmarshal array of json objects into include collection
//UmarshalJSON - unmarshal array of json objects into include collection
func (c *Included) UnmarshalJSON(data []byte) error {
var keys []Key
err := json.Unmarshal(data, &keys)
Expand Down
2 changes: 2 additions & 0 deletions resources/model_add_match_attributes.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ type AddMatchAttributes struct {
OriginOrderId int64 `json:"origin_order_id"`
// Match order state
State uint8 `json:"state"`
// Relayer execute match order
UseRelayer bool `json:"use_relayer"`
// Source blockchain of the order to match
OriginChainId int64 `json:"origin_chain_id"`
// Source blockchain where the match order appeared
Expand Down
2 changes: 2 additions & 0 deletions resources/model_add_order_attributes.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ type AddOrderAttributes struct {
OrderId int64 `json:"order_id"`
// Order state
State uint8 `json:"state"`
// Relayer execute order
UseRelayer bool `json:"use_relayer"`
// Source blockchain of the match order which is intended to execute the order
DestChainId int64 `json:"dest_chain_id"`
// Source blockchain where the order appeared
Expand Down
8 changes: 4 additions & 4 deletions resources/model_details.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (

type Details json.RawMessage

// UnmarshalJSON - casts data to Details
//UnmarshalJSON - casts data to Details
func (d *Details) UnmarshalJSON(data []byte) error {
if d == nil {
return errors.New("regources.Details: UnmarshalJSON on nil pointer")
Expand All @@ -22,7 +22,7 @@ func (d *Details) UnmarshalJSON(data []byte) error {
return nil
}

// MarshalJSON - casts Details to []byte
//MarshalJSON - casts Details to []byte
func (d Details) MarshalJSON() ([]byte, error) {
if d == nil {
return []byte("null"), nil
Expand All @@ -34,7 +34,7 @@ func (d Details) String() string {
return string(d)
}

// Value - implements db driver method for auto marshal
//Value - implements db driver method for auto marshal
func (r Details) Value() (driver.Value, error) {
result, err := json.Marshal(r)
if err != nil {
Expand All @@ -44,7 +44,7 @@ func (r Details) Value() (driver.Value, error) {
return result, nil
}

// Scan - implements db driver method for auto unmarshal
//Scan - implements db driver method for auto unmarshal
func (r *Details) Scan(src interface{}) error {
var data []byte
switch rawData := src.(type) {
Expand Down
2 changes: 2 additions & 0 deletions resources/model_match_attributes.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,6 @@ type MatchAttributes struct {
OriginOrderId int64 `json:"origin_order_id"`
// Match order state
State uint8 `json:"state"`
// Relayer execute match order
UseRelayer bool `json:"use_relayer"`
}
2 changes: 2 additions & 0 deletions resources/model_order_attributes.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,6 @@ type OrderAttributes struct {
OrderId int64 `json:"order_id"`
// Order state
State uint8 `json:"state"`
// Relayer execute order
UseRelayer bool `json:"use_relayer"`
}

0 comments on commit fafe2be

Please sign in to comment.