-
Notifications
You must be signed in to change notification settings - Fork 629
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
Fee Middleware: Escrow logic #465
Changes from all commits
435efc7
8f2c51a
a410af6
0e6823f
f41e520
e4bd49a
ca7f4e2
6a5add7
535e2c6
5bd0e32
31fcee3
487f78d
f25932f
38566bc
dd0044b
5b4a395
c9af582
d325146
ea9b30b
ac7584f
c44a81c
a8c3cff
4f490ea
f1fde1d
eeb4376
4612bf2
3494a0f
f4c37e6
0b733ab
2be4ab5
cb2cbc3
66454c6
06d4c25
f336ca7
8e2eb8f
52fa63e
0587a3c
63e7a1a
a40159f
f379aaf
2f649a8
b100ee0
b19ebd2
a51e2ff
bbfd8d7
6640660
4610c99
bec52c0
2c9185c
f4de334
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,106 @@ | ||||||
package keeper | ||||||
|
||||||
import ( | ||||||
"fmt" | ||||||
|
||||||
sdk "github.com/cosmos/cosmos-sdk/types" | ||||||
|
||||||
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" | ||||||
|
||||||
"github.com/cosmos/ibc-go/modules/apps/29-fee/types" | ||||||
channeltypes "github.com/cosmos/ibc-go/modules/core/04-channel/types" | ||||||
) | ||||||
|
||||||
// EscrowPacketFee sends the packet fee to the 29-fee module account to hold in escrow | ||||||
func (k Keeper) EscrowPacketFee(ctx sdk.Context, refundAcc sdk.AccAddress, identifiedFee *types.IdentifiedPacketFee) error { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does identifiedFee need to be a pointer? |
||||||
// check if the refund account exists | ||||||
hasRefundAcc := k.authKeeper.GetAccount(ctx, refundAcc) | ||||||
if hasRefundAcc == nil { | ||||||
return sdkerrors.Wrap(types.ErrRefundAccNotFound, fmt.Sprintf("Account with address: %s not found", refundAcc)) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
} | ||||||
|
||||||
coins := identifiedFee.Fee.ReceiveFee | ||||||
coins = coins.Add(identifiedFee.Fee.AckFee...) | ||||||
coins = coins.Add(identifiedFee.Fee.TimeoutFee...) | ||||||
|
||||||
if err := k.bankKeeper.SendCoinsFromAccountToModule( | ||||||
ctx, refundAcc, types.ModuleName, coins, | ||||||
); err != nil { | ||||||
return err | ||||||
} | ||||||
|
||||||
// Store fee in state for reference later | ||||||
// feeInEscrow/<port-id>/<channel-id>/packet/<sequence-id>/ -> Fee (timeout, ack, onrecv) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This can be removed imo. If we change the storage path, we will probably forget to remove this. We can indicate the storage path in the |
||||||
k.SetFeeInEscrow(ctx, identifiedFee) | ||||||
return nil | ||||||
} | ||||||
|
||||||
// DistributeFee pays the acknowledgement fee & receive fee for a given packetId while refunding the timeout fee to the refund account associated with the Fee | ||||||
func (k Keeper) DistributeFee(ctx sdk.Context, refundAcc, forwardRelayer, reverseRelayer sdk.AccAddress, packetID *channeltypes.PacketId) error { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ditto on PacketId being a pointer? |
||||||
// check if there is a Fee in escrow for the given packetId | ||||||
feeInEscrow, found := k.GetFeeInEscrow(ctx, packetID) | ||||||
if !found { | ||||||
return sdkerrors.Wrapf(types.ErrFeeNotFound, "with channelID %s, sequence %d", packetID.ChannelId, packetID.Sequence) | ||||||
} | ||||||
|
||||||
// get module accAddr | ||||||
feeModuleAccAddr := k.authKeeper.GetModuleAddress(types.ModuleName) | ||||||
|
||||||
// send receive fee to forward relayer | ||||||
err := k.bankKeeper.SendCoins(ctx, feeModuleAccAddr, forwardRelayer, feeInEscrow.Fee.ReceiveFee) | ||||||
if err != nil { | ||||||
return sdkerrors.Wrap(err, "failed to send fee to forward relayer") | ||||||
} | ||||||
|
||||||
// send ack fee to reverse relayer | ||||||
err = k.bankKeeper.SendCoins(ctx, feeModuleAccAddr, reverseRelayer, feeInEscrow.Fee.AckFee) | ||||||
if err != nil { | ||||||
return sdkerrors.Wrap(err, "error sending fee to reverse relayer") | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
} | ||||||
|
||||||
// refund timeout fee to refundAddr | ||||||
err = k.bankKeeper.SendCoins(ctx, feeModuleAccAddr, refundAcc, feeInEscrow.Fee.TimeoutFee) | ||||||
if err != nil { | ||||||
return sdkerrors.Wrap(err, "error refunding timeout fee") | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
} | ||||||
|
||||||
// removes the fee from the store as fee is now paid | ||||||
k.DeleteFeeInEscrow(ctx, packetID) | ||||||
|
||||||
return nil | ||||||
} | ||||||
|
||||||
// DistributeFeeTimeout pays the timeout fee for a given packetId while refunding the acknowledgement fee & receive fee to the refund account associated with the Fee | ||||||
func (k Keeper) DistributeFeeTimeout(ctx sdk.Context, refundAcc, timeoutRelayer sdk.AccAddress, packetID *channeltypes.PacketId) error { | ||||||
// check if there is a Fee in escrow for the given packetId | ||||||
feeInEscrow, found := k.GetFeeInEscrow(ctx, packetID) | ||||||
if !found { | ||||||
return sdkerrors.Wrap(types.ErrFeeNotFound, refundAcc.String()) | ||||||
} | ||||||
|
||||||
// get module accAddr | ||||||
feeModuleAccAddr := k.authKeeper.GetModuleAddress(types.ModuleName) | ||||||
|
||||||
// refund the receive fee | ||||||
err := k.bankKeeper.SendCoins(ctx, feeModuleAccAddr, refundAcc, feeInEscrow.Fee.ReceiveFee) | ||||||
if err != nil { | ||||||
return sdkerrors.Wrap(err, "error refunding receive fee") | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
} | ||||||
|
||||||
// refund the ack fee | ||||||
err = k.bankKeeper.SendCoins(ctx, feeModuleAccAddr, refundAcc, feeInEscrow.Fee.AckFee) | ||||||
if err != nil { | ||||||
return sdkerrors.Wrap(err, "error refunding ack fee") | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
} | ||||||
|
||||||
// pay the timeout fee to the reverse relayer | ||||||
err = k.bankKeeper.SendCoins(ctx, feeModuleAccAddr, timeoutRelayer, feeInEscrow.Fee.TimeoutFee) | ||||||
if err != nil { | ||||||
return sdkerrors.Wrap(err, "error sending fee to timeout relayer") | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
} | ||||||
|
||||||
// removes the fee from the store as fee is now paid | ||||||
k.DeleteFeeInEscrow(ctx, packetID) | ||||||
|
||||||
return nil | ||||||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.