Skip to content

Commit

Permalink
fix(eibc): fix demand order fulfill authorized fee check (#1522)
Browse files Browse the repository at this point in the history
  • Loading branch information
zale144 authored Nov 20, 2024
1 parent c6826d7 commit f9d95b0
Show file tree
Hide file tree
Showing 12 changed files with 349 additions and 135 deletions.
4 changes: 4 additions & 0 deletions proto/dymensionxyz/dymension/eibc/events.proto
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ message EventDemandOrderCreated {
string packet_type = 9;
// proof_height is the height of the block when order was created.
uint64 proof_height = 10;
// amount is the amount of the IBC transfer.
string amount = 11;
}

// EventDemandOrderPacketStatusUpdate is emitted when the status of the related packet is updated.
Expand All @@ -57,6 +59,8 @@ message EventDemandOrderFeeUpdated {
string rollapp_id = 5;
// proof_height is the height of the block when order was created.
uint64 proof_height = 6;
// amount is the amount of the IBC transfer.
string amount = 7;
}

// EventDemandOrderFulfilled is emitted when the demand order is fulfilled.
Expand Down
15 changes: 10 additions & 5 deletions proto/dymensionxyz/dymension/eibc/tx.proto
Original file line number Diff line number Diff line change
Expand Up @@ -41,22 +41,27 @@ message MsgFulfillOrderAuthorized {
(gogoproto.nullable) = false,
(gogoproto.castrepeated) = "github.com/cosmos/cosmos-sdk/types.Coins"
];
// amount is the amount of the IBC transfer
cosmos.base.v1beta1.IntProto amount = 4 [
(gogoproto.nullable) = false,
(gogoproto.castrepeated) = "github.com/cosmos/cosmos-sdk/types.IntProto"
];
// lp_address is the bech32-encoded address of the account which the authorization was granted from.
// This account will receive the price amount at the finalization phase.
string lp_address = 4;
string lp_address = 5;
// operator_fee_address is an optional bech32-encoded address of an account that would collect the operator_fee_part
// if it's empty, the operator_fee_part will go to the operator_address
string operator_fee_address = 5;
string operator_fee_address = 6;
// expected_fee is the nominal fee set in the order.
string expected_fee = 6;
string expected_fee = 7;
// operator_fee_share is the share of the fee earnings that goes to the operator
// it will be deduced from the fee of the demand order and paid out immediately
cosmos.base.v1beta1.DecProto operator_fee_share = 7 [
cosmos.base.v1beta1.DecProto operator_fee_share = 8 [
(gogoproto.nullable) = false,
(gogoproto.castrepeated) = "github.com/cosmos/cosmos-sdk/types.DecProto"
];
// settlement_validated signals if the block behind the demand order needs to be "settlement validated" or not
bool settlement_validated = 8;
bool settlement_validated = 9;
}

message MsgFulfillOrderAuthorizedResponse {}
Expand Down
17 changes: 16 additions & 1 deletion x/eibc/client/cli/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ const (
FlagOperatorFeeAddress = "operator-fee-address"
FlagRollappId = "rollapp-id"
FlagPrice = "price"
FlagAmount = "amount"
)

func NewFulfillOrderAuthorizedTxCmd() *cobra.Command {
Expand Down Expand Up @@ -109,6 +110,18 @@ func NewFulfillOrderAuthorizedTxCmd() *cobra.Command {
return fmt.Errorf("invalid price: %w", err)
}

amountStr, err := cmd.Flags().GetString(FlagAmount)
if err != nil {
return fmt.Errorf("amount is required")
}

amountInt, ok := sdk.NewIntFromString(amountStr)
if !ok {
return fmt.Errorf("invalid amount")
}

amount := sdk.IntProto{Int: amountInt}

oepratorFeeShareStr, err := cmd.Flags().GetString(FlagOperatorFeeShare)
if err != nil {
return fmt.Errorf("fulfiller fee part is required")
Expand All @@ -131,6 +144,7 @@ func NewFulfillOrderAuthorizedTxCmd() *cobra.Command {
operatorFeeAddress,
fee,
price,
amount,
operatorFeeShare,
settlementValidated,
)
Expand All @@ -145,7 +159,8 @@ func NewFulfillOrderAuthorizedTxCmd() *cobra.Command {
flags.AddTxFlagsToCmd(cmd)
cmd.Flags().Bool(FlagSettlementValidated, false, "Settlement validated flag")
cmd.Flags().String(FlagRollappId, "", "Rollapp ID")
cmd.Flags().String(FlagPrice, "", "Maximum price")
cmd.Flags().String(FlagPrice, "", "Price")
cmd.Flags().String(FlagAmount, "", "Amount")
cmd.Flags().String(FlagOperatorFeeShare, "", "Operator fee share")
cmd.Flags().String(FlagOperatorFeeAddress, "", "Operator fee address")
return cmd
Expand Down
2 changes: 1 addition & 1 deletion x/eibc/keeper/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func (k Keeper) EIBCDemandOrderHandler(ctx sdk.Context, rollappPacket commontype
return fmt.Errorf("set eibc demand order: %w", err)
}

if err = uevent.EmitTypedEvent(ctx, eibcDemandOrder.GetCreatedEvent(rollappPacket.ProofHeight)); err != nil {
if err = uevent.EmitTypedEvent(ctx, eibcDemandOrder.GetCreatedEvent(rollappPacket.ProofHeight, data.Amount)); err != nil {
return fmt.Errorf("emit event: %w", err)
}

Expand Down
4 changes: 2 additions & 2 deletions x/eibc/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ func (m msgServer) FulfillOrderAuthorized(goCtx context.Context, msg *types.MsgF
}

fee := sdk.NewDecFromInt(demandOrder.GetFeeAmount())
operatorFee := fee.Mul(msg.OperatorFeeShare.Dec).TruncateInt()
operatorFee := fee.MulTruncate(msg.OperatorFeeShare.Dec).TruncateInt()

if operatorFee.IsPositive() {
// Send the fee part to the fulfiller/operator
Expand Down Expand Up @@ -237,7 +237,7 @@ func (m msgServer) UpdateDemandOrder(goCtx context.Context, msg *types.MsgUpdate
return nil, err
}

if err = uevent.EmitTypedEvent(ctx, demandOrder.GetUpdatedEvent(raPacket.ProofHeight)); err != nil {
if err = uevent.EmitTypedEvent(ctx, demandOrder.GetUpdatedEvent(raPacket.ProofHeight, data.Amount)); err != nil {
return nil, fmt.Errorf("emit event: %w", err)
}

Expand Down
10 changes: 10 additions & 0 deletions x/eibc/keeper/msg_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,7 @@ func (suite *KeeperTestSuite) TestMsgFulfillOrderAuthorized() {
msg: &types.MsgFulfillOrderAuthorized{
RollappId: rollappPacket.RollappId,
Price: sdk.NewCoins(sdk.NewInt64Coin("adym", 90)),
Amount: sdk.IntProto{Int: sdk.NewInt(100)},
ExpectedFee: "10",
OperatorFeeShare: sdk.DecProto{Dec: sdk.NewDecWithPrec(2, 1)}, // 0.2
OperatorFeeAddress: sample.AccAddress(),
Expand All @@ -237,6 +238,7 @@ func (suite *KeeperTestSuite) TestMsgFulfillOrderAuthorized() {
msg: &types.MsgFulfillOrderAuthorized{
RollappId: rollappPacket.RollappId,
Price: sdk.NewCoins(sdk.NewInt64Coin("adym", 90)),
Amount: sdk.IntProto{Int: sdk.NewInt(100)},
ExpectedFee: "10",
OperatorFeeShare: sdk.DecProto{Dec: sdk.NewDecWithPrec(2, 1)}, // 0.2
OperatorFeeAddress: sample.AccAddress(),
Expand Down Expand Up @@ -272,6 +274,7 @@ func (suite *KeeperTestSuite) TestMsgFulfillOrderAuthorized() {
msg: &types.MsgFulfillOrderAuthorized{
RollappId: "rollapp2", // Mismatched Rollapp ID
Price: sdk.NewCoins(sdk.NewInt64Coin("adym", 100)),
Amount: sdk.IntProto{Int: sdk.NewInt(110)},
ExpectedFee: "10",
OperatorFeeShare: sdk.DecProto{Dec: sdk.NewDecWithPrec(2, 1)}, // 0.2
OperatorFeeAddress: sample.AccAddress(),
Expand All @@ -292,6 +295,7 @@ func (suite *KeeperTestSuite) TestMsgFulfillOrderAuthorized() {
msg: &types.MsgFulfillOrderAuthorized{
RollappId: rollappPacket.RollappId,
Price: sdk.NewCoins(sdk.NewInt64Coin("adym", 110)), // Mismatched Price
Amount: sdk.IntProto{Int: sdk.NewInt(120)},
ExpectedFee: "10",
OperatorFeeShare: sdk.DecProto{Dec: sdk.NewDecWithPrec(2, 1)}, // 0.2
OperatorFeeAddress: sample.AccAddress(),
Expand All @@ -312,6 +316,7 @@ func (suite *KeeperTestSuite) TestMsgFulfillOrderAuthorized() {
msg: &types.MsgFulfillOrderAuthorized{
RollappId: rollappPacket.RollappId,
Price: sdk.NewCoins(sdk.NewInt64Coin("adym", 100)),
Amount: sdk.IntProto{Int: sdk.NewInt(115)},
ExpectedFee: "15", // Mismatched Expected Fee
OperatorFeeShare: sdk.DecProto{Dec: sdk.NewDecWithPrec(2, 1)}, // 0.2
OperatorFeeAddress: sample.AccAddress(),
Expand All @@ -332,6 +337,7 @@ func (suite *KeeperTestSuite) TestMsgFulfillOrderAuthorized() {
msg: &types.MsgFulfillOrderAuthorized{
RollappId: rollappPacket.RollappId,
Price: sdk.NewCoins(sdk.NewInt64Coin("adym", 100)),
Amount: sdk.IntProto{Int: sdk.NewInt(110)},
ExpectedFee: "10",
OperatorFeeShare: sdk.DecProto{Dec: sdk.NewDecWithPrec(2, 1)}, // 0.2
OperatorFeeAddress: sample.AccAddress(),
Expand All @@ -351,6 +357,7 @@ func (suite *KeeperTestSuite) TestMsgFulfillOrderAuthorized() {
msg: &types.MsgFulfillOrderAuthorized{
RollappId: rollappPacket.RollappId,
Price: sdk.NewCoins(sdk.NewInt64Coin("adym", 100)),
Amount: sdk.IntProto{Int: sdk.NewInt(110)},
ExpectedFee: "10",
OperatorFeeShare: sdk.DecProto{Dec: sdk.NewDecWithPrec(2, 1)}, // 0.2
OperatorFeeAddress: sample.AccAddress(), // Non-existent operator account
Expand All @@ -371,6 +378,7 @@ func (suite *KeeperTestSuite) TestMsgFulfillOrderAuthorized() {
msg: &types.MsgFulfillOrderAuthorized{
RollappId: rollappPacket.RollappId,
Price: sdk.NewCoins(sdk.NewInt64Coin("adym", 100)),
Amount: sdk.IntProto{Int: sdk.NewInt(110)},
ExpectedFee: "10",
OperatorFeeShare: sdk.DecProto{Dec: sdk.NewDecWithPrec(2, 1)}, // 0.2
OperatorFeeAddress: sample.AccAddress(),
Expand All @@ -391,6 +399,7 @@ func (suite *KeeperTestSuite) TestMsgFulfillOrderAuthorized() {
msg: &types.MsgFulfillOrderAuthorized{
RollappId: rollappPacket.RollappId,
Price: sdk.NewCoins(sdk.NewInt64Coin("adym", 100)),
Amount: sdk.IntProto{Int: sdk.NewInt(110)},
ExpectedFee: "10",
OperatorFeeShare: sdk.DecProto{Dec: sdk.NewDecWithPrec(2, 1)}, // 0.2
OperatorFeeAddress: sample.AccAddress(),
Expand Down Expand Up @@ -425,6 +434,7 @@ func (suite *KeeperTestSuite) TestMsgFulfillOrderAuthorized() {
msg: &types.MsgFulfillOrderAuthorized{
RollappId: rollappPacket.RollappId,
Price: sdk.NewCoins(sdk.NewInt64Coin("adym", 90)),
Amount: sdk.IntProto{Int: sdk.NewInt(100)},
ExpectedFee: "10",
OperatorFeeShare: sdk.DecProto{Dec: sdk.NewDecWithPrec(2, 1)}, // 0.2
OperatorFeeAddress: sample.AccAddress(),
Expand Down
6 changes: 4 additions & 2 deletions x/eibc/types/demand_order.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func (m *DemandOrder) Validate() error {
return nil
}

func (m *DemandOrder) GetCreatedEvent(proofHeight uint64) *EventDemandOrderCreated {
func (m *DemandOrder) GetCreatedEvent(proofHeight uint64, amount string) *EventDemandOrderCreated {
packetKey := base64.StdEncoding.EncodeToString([]byte(m.TrackingPacketKey))
return &EventDemandOrderCreated{
OrderId: m.Id,
Expand All @@ -88,6 +88,7 @@ func (m *DemandOrder) GetCreatedEvent(proofHeight uint64) *EventDemandOrderCreat
Recipient: m.Recipient,
PacketType: m.Type.String(),
ProofHeight: proofHeight,
Amount: amount,
}
}

Expand Down Expand Up @@ -117,14 +118,15 @@ func (m *DemandOrder) GetFulfilledAuthorizedEvent(creationHeight uint64, lpAddre
}
}

func (m *DemandOrder) GetUpdatedEvent(proofHeight uint64) *EventDemandOrderFeeUpdated {
func (m *DemandOrder) GetUpdatedEvent(proofHeight uint64, amount string) *EventDemandOrderFeeUpdated {
return &EventDemandOrderFeeUpdated{
OrderId: m.Id,
NewFee: m.Fee.String(),
Price: m.Price.String(),
PacketStatus: m.TrackingPacketStatus.String(),
RollappId: m.RollappId,
ProofHeight: proofHeight,
Amount: amount,
}
}

Expand Down
Loading

0 comments on commit f9d95b0

Please sign in to comment.