Skip to content

Commit

Permalink
add expected fee to order fulfillment (#40)
Browse files Browse the repository at this point in the history
* Add expected fee to order fulfillment

* Add expected fee to order fulfillment

* Fix event subscriptions: order-created, order-finalized

* Fix linter issue

* Fix linter issue

* Fix more linter issues

* feat: fee rule functionality (#38)

Fee rule functionality, refactor

* Fix fee parsing
  • Loading branch information
zale144 authored Sep 17, 2024
1 parent 47239dd commit cd13d75
Show file tree
Hide file tree
Showing 8 changed files with 319 additions and 202 deletions.
14 changes: 12 additions & 2 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ type Config struct {
Gas GasConfig `mapstructure:"gas"`
OrderPolling OrderPollingConfig `mapstructure:"order_polling"`

Whale whaleConfig `mapstructure:"whale"`
Bots botConfig `mapstructure:"bots"`
Whale whaleConfig `mapstructure:"whale"`
Bots botConfig `mapstructure:"bots"`
FulfillCriteria fulfillCriteria `mapstructure:"fulfill_criteria"`

LogLevel string `mapstructure:"log_level"`
SlackConfig slackConfig `mapstructure:"slack"`
Expand Down Expand Up @@ -52,6 +53,15 @@ type whaleConfig struct {
AllowedBalanceThresholds map[string]string `mapstructure:"allowed_balance_thresholds"`
}

type fulfillCriteria struct {
MinFeePercentage minFeePercentage `mapstructure:"min_fee_percentage"`
}

type minFeePercentage struct {
Chain map[string]float32 `mapstructure:"chain"`
Asset map[string]float32 `mapstructure:"asset"`
}

type slackConfig struct {
Enabled bool `mapstructure:"enabled"`
BotToken string `mapstructure:"bot_token"`
Expand Down
11 changes: 3 additions & 8 deletions order_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,24 +71,19 @@ func newOrderClient(ctx context.Context, config Config) (*orderClient, error) {
fulfilledOrdersCh := make(chan *orderBatch, newOrderBufferSize) // TODO: make buffer size configurable
bstore := store.NewBotStore(db)

denomsWhitelist := make(map[string]struct{}, len(config.Whale.AllowedBalanceThresholds))
for denom := range config.Whale.AllowedBalanceThresholds {
denomsWhitelist[denom] = struct{}{}
}

ordTracker := newOrderTracker(
fetcherCosmosClient,
bstore,
fulfilledOrdersCh,
subscriberID,
denomsWhitelist,
&config.FulfillCriteria,
logger,
)

eventer := newOrderEventer(
fetcherCosmosClient,
subscriberID,
ordTracker,
ordTracker.canFulfillOrder,
config.Bots.MaxOrdersPerTx,
orderCh,
logger,
Expand Down Expand Up @@ -173,7 +168,7 @@ func newOrderClient(ctx context.Context, config Config) (*orderClient, error) {
if config.OrderPolling.Enabled {
oc.orderPoller = newOrderPoller(
fetcherCosmosClient,
ordTracker,
ordTracker.canFulfillOrder,
config.OrderPolling,
config.Bots.MaxOrdersPerTx,
orderCh,
Expand Down
59 changes: 34 additions & 25 deletions order_eventer.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,27 +15,27 @@ type orderEventer struct {
client cosmosclient.Client
logger *zap.Logger

batchSize int
newOrders chan []*demandOrder
tracker *orderTracker
subscriberID string
batchSize int
newOrders chan []*demandOrder
canFulfillOrder func(*demandOrder) bool
subscriberID string
}

func newOrderEventer(
client cosmosclient.Client,
subscriberID string,
tracker *orderTracker,
canFulfillOrder func(*demandOrder) bool,
batchSize int,
newOrders chan []*demandOrder,
logger *zap.Logger,
) *orderEventer {
return &orderEventer{
client: client,
subscriberID: subscriberID,
batchSize: batchSize,
logger: logger.With(zap.String("module", "order-eventer")),
newOrders: newOrders,
tracker: tracker,
client: client,
subscriberID: subscriberID,
batchSize: batchSize,
logger: logger.With(zap.String("module", "order-eventer")),
newOrders: newOrders,
canFulfillOrder: canFulfillOrder,
}
}

Expand All @@ -52,10 +52,7 @@ func (e *orderEventer) start(ctx context.Context) error {
}

func (e *orderEventer) enqueueEventOrders(res tmtypes.ResultEvent) error {
newOrders, err := e.parseOrdersFromEvents(res)
if err != nil {
return fmt.Errorf("failed to parse orders from events: %w", err)
}
newOrders := e.parseOrdersFromEvents(res)

if e.logger.Level() <= zap.DebugLevel {
ids := make([]string, 0, len(newOrders))
Expand Down Expand Up @@ -87,38 +84,50 @@ func (e *orderEventer) enqueueEventOrders(res tmtypes.ResultEvent) error {

const createdEvent = "dymensionxyz.dymension.eibc.EventDemandOrderCreated"

func (e *orderEventer) parseOrdersFromEvents(res tmtypes.ResultEvent) ([]*demandOrder, error) {
func (e *orderEventer) parseOrdersFromEvents(res tmtypes.ResultEvent) []*demandOrder {
ids := res.Events[createdEvent+".order_id"]

if len(ids) == 0 {
return nil, nil
return nil
}

prices := res.Events[createdEvent+".price"]
fees := res.Events[createdEvent+".fee"]
statuses := res.Events[createdEvent+".packet_status"]
rollapps := res.Events[createdEvent+".rollapp_id"]
newOrders := make([]*demandOrder, 0, len(ids))

for i, id := range ids {
price, err := sdk.ParseCoinNormalized(prices[i])
price, err := sdk.ParseCoinsNormalized(prices[i])
if err != nil {
return nil, fmt.Errorf("failed to parse price: %w", err)
e.logger.Error("failed to parse price", zap.Error(err))
continue
}

if !e.tracker.canFulfillOrder(id, price.Denom) {
fee, err := sdk.ParseCoinsNormalized(fees[i])
if err != nil {
e.logger.Error("failed to parse fee", zap.Error(err))
continue
}

order := &demandOrder{
id: id,
amount: sdk.NewCoins(price),
fee: fees[i],
status: statuses[i],
id: id,
denom: fee.GetDenomByIndex(0),
amount: price,
fee: fee,
status: statuses[i],
rollappId: rollapps[i],
// blockHeight: height,
}

if !e.canFulfillOrder(order) {
continue
}

newOrders = append(newOrders, order)
}

return newOrders, nil
return newOrders
}

func (e *orderEventer) subscribeToPendingDemandOrders(ctx context.Context) error {
Expand Down
14 changes: 2 additions & 12 deletions order_fulfiller.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@ import (
"time"

sdk "github.com/cosmos/cosmos-sdk/types"
"go.uber.org/zap"

"github.com/dymensionxyz/cosmosclient/cosmosclient"
"go.uber.org/zap"

"github.com/dymensionxyz/eibc-client/types"
)
Expand Down Expand Up @@ -185,16 +184,7 @@ func (ol *orderFulfiller) fulfillDemandOrders(demandOrder ...*demandOrder) error
msgs := make([]sdk.Msg, len(demandOrder))

for i, order := range demandOrder {
if order.fee == "" {
continue
}
fee, err := sdk.ParseCoinNormalized(order.fee)
if err != nil {
return fmt.Errorf("failed to parse fee: %w", err)
}
feeAmount := fee.Amount.String()

msgs[i] = types.NewMsgFulfillOrder(ol.accountSvc.account.GetAddress().String(), order.id, feeAmount)
msgs[i] = types.NewMsgFulfillOrder(ol.accountSvc.account.GetAddress().String(), order.id, order.feeStr)
}

_, err := ol.client.BroadcastTx(ol.accountSvc.accountName, msgs...)
Expand Down
Loading

0 comments on commit cd13d75

Please sign in to comment.