Skip to content
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

Cosmos fee priority using memo #501

Merged
merged 22 commits into from
Apr 29, 2024
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
1dceede
introduce priority fee config for channel and TakenFeeByIbcSequence list
RustNinja Apr 25, 2024
e659cf7
read and write into the SequenceFee by Sequence value.
RustNinja Apr 25, 2024
b99fd26
call SetSequenceFee during ibc tx workflow.
RustNinja Apr 25, 2024
fd48041
call SetSequenceFee during keeper transfer execution flow.
RustNinja Apr 25, 2024
f16a2d1
update the if statement in fee cosmos sequence storage updates
RustNinja Apr 25, 2024
d8a9943
remove circulate dependency.
RustNinja Apr 25, 2024
28723bd
read memo to find a priority fee if exists and add to a min fee
RustNinja Apr 25, 2024
1230be8
read SequenceFee and refund exact amount, then delete SequenceFee value
RustNinja Apr 25, 2024
d10ec6d
DeleteSequenceFee on OnAcknowledgementPacket
RustNinja Apr 25, 2024
58b184c
rename func parameter from validator to coin
RustNinja Apr 26, 2024
58ec4cc
add a upgrade handler for new feature cosmos eth ibc fee priority
RustNinja Apr 26, 2024
14e56e7
remove usage of new coin with empty denom bz it triggers incorrect denom
RustNinja Apr 28, 2024
d57b0c0
Revert "remove usage of new coin with empty denom bz it triggers inco…
RustNinja Apr 28, 2024
1937cbd
use correct denom instead of empty.
RustNinja Apr 28, 2024
f805bfa
Merge pull request #506 from ComposableFi/rustdev/remove-usage-newcoi…
RustNinja Apr 28, 2024
8271fef
extend AddAllowedIbcToken tx to add the priority of gas for ibc bridge
RustNinja Apr 28, 2024
8961903
Merge pull request #507 from ComposableFi/rustdev/extend-add-allowedI…
RustNinja Apr 28, 2024
40d6175
remove unused code
RustNinja Apr 29, 2024
7b08a4c
remove unused code.
RustNinja Apr 29, 2024
f800b2c
rename to UpgradeName to "v6_5_5"
RustNinja Apr 29, 2024
be273fa
fmt fix
RustNinja Apr 29, 2024
0427e0a
Merge pull request #508 from ComposableFi/rustninja/priority-fee-upgr…
RustNinja Apr 29, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 42 additions & 2 deletions custom/ibc-transfer/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package keeper

import (
"context"
"encoding/json"
"fmt"
"time"

Expand All @@ -16,6 +17,7 @@ import (
"github.com/cosmos/ibc-go/v7/modules/core/exported"
custombankkeeper "github.com/notional-labs/composable/v6/custom/bank/keeper"
ibctransfermiddleware "github.com/notional-labs/composable/v6/x/ibctransfermiddleware/keeper"
ibctransfermiddlewaretypes "github.com/notional-labs/composable/v6/x/ibctransfermiddleware/types"
)

type Keeper struct {
Expand Down Expand Up @@ -57,6 +59,7 @@ func NewKeeper(
func (k Keeper) Transfer(goCtx context.Context, msg *types.MsgTransfer) (*types.MsgTransferResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)
params := k.IbcTransfermiddleware.GetParams(ctx)
charge_coin := sdk.NewCoin("", sdk.ZeroInt())
if params.ChannelFees != nil && len(params.ChannelFees) > 0 {
channelFee := findChannelParams(params.ChannelFees, msg.SourceChannel)
if channelFee != nil {
Expand All @@ -79,7 +82,16 @@ func (k Keeper) Transfer(goCtx context.Context, msg *types.MsgTransfer) (*types.
if coin == nil {
return nil, fmt.Errorf("token not allowed to be transferred in this channel")
}

minFee := coin.MinFee.Amount
priority := GetPriority(msg.Memo)
if priority != nil {
p := findPriority(coin.TxPriorityFee, *priority)
if p != nil && coin.MinFee.Denom == p.PriorityFee.Denom {
minFee = minFee.Add(p.PriorityFee.Amount)
}
}

charge := minFee
if charge.GT(msg.Token.Amount) {
charge = msg.Token.Amount
Expand All @@ -103,7 +115,8 @@ func (k Keeper) Transfer(goCtx context.Context, msg *types.MsgTransfer) (*types.
return nil, err
}

send_err := k.bank.SendCoins(ctx, msgSender, feeAddress, sdk.NewCoins(sdk.NewCoin(msg.Token.Denom, charge)))
charge_coin = sdk.NewCoin(msg.Token.Denom, charge)
send_err := k.bank.SendCoins(ctx, msgSender, feeAddress, sdk.NewCoins(charge_coin))
if send_err != nil {
return nil, send_err
}
Expand All @@ -114,5 +127,32 @@ func (k Keeper) Transfer(goCtx context.Context, msg *types.MsgTransfer) (*types.
msg.Token.Amount = newAmount
}
}
return k.Keeper.Transfer(goCtx, msg)
ret, err := k.Keeper.Transfer(goCtx, msg)
if err == nil && ret != nil && !charge_coin.IsZero() {
k.IbcTransfermiddleware.SetSequenceFee(ctx, ret.Sequence, charge_coin)
}
return ret, err
}

func GetPriority(jsonString string) *string {
var data map[string]interface{}
if err := json.Unmarshal([]byte(jsonString), &data); err != nil {
return nil
}

priority, ok := data["priority"].(string)
vmarkushin marked this conversation as resolved.
Show resolved Hide resolved
if !ok {
return nil
}

return &priority
}

func findPriority(priorities []*ibctransfermiddlewaretypes.TxPriorityFee, priority string) *ibctransfermiddlewaretypes.TxPriorityFee {
for _, p := range priorities {
if p.Priority == priority {
return p
}
}
return nil
}
19 changes: 17 additions & 2 deletions custom/ibc-transfer/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ func NewMsgServerImpl(ibcKeeper Keeper, bankKeeper custombankkeeper.Keeper) type
func (k msgServer) Transfer(goCtx context.Context, msg *types.MsgTransfer) (*types.MsgTransferResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)
params := k.Keeper.IbcTransfermiddleware.GetParams(ctx)
charge_coin := sdk.NewCoin("", sdk.ZeroInt())
if params.ChannelFees != nil && len(params.ChannelFees) > 0 {
channelFee := findChannelParams(params.ChannelFees, msg.SourceChannel)
if channelFee != nil {
Expand All @@ -55,7 +56,16 @@ func (k msgServer) Transfer(goCtx context.Context, msg *types.MsgTransfer) (*typ
if coin == nil {
return nil, fmt.Errorf("token not allowed to be transferred in this channel")
}

minFee := coin.MinFee.Amount
priority := GetPriority(msg.Memo)
if priority != nil {
p := findPriority(coin.TxPriorityFee, *priority)
if p != nil && coin.MinFee.Denom == p.PriorityFee.Denom {
minFee = minFee.Add(p.PriorityFee.Amount)
}
}

charge := minFee
if charge.GT(msg.Token.Amount) {
charge = msg.Token.Amount
Expand All @@ -79,7 +89,8 @@ func (k msgServer) Transfer(goCtx context.Context, msg *types.MsgTransfer) (*typ
return nil, err
}

send_err := k.bank.SendCoins(ctx, msgSender, feeAddress, sdk.NewCoins(sdk.NewCoin(msg.Token.Denom, charge)))
charge_coin = sdk.NewCoin(msg.Token.Denom, charge)
send_err := k.bank.SendCoins(ctx, msgSender, feeAddress, sdk.NewCoins(charge_coin))
if send_err != nil {
return nil, send_err
}
Expand All @@ -90,7 +101,11 @@ func (k msgServer) Transfer(goCtx context.Context, msg *types.MsgTransfer) (*typ
msg.Token.Amount = newAmount
}
}
return k.msgServer.Transfer(goCtx, msg)
ret, err := k.msgServer.Transfer(goCtx, msg)
if err == nil && ret != nil && !charge_coin.IsZero() {
k.IbcTransfermiddleware.SetSequenceFee(ctx, ret.Sequence, charge_coin)
}
return ret, err
}

func findChannelParams(channelFees []*ibctransfermiddlewaretypes.ChannelFee, targetChannelID string) *ibctransfermiddlewaretypes.ChannelFee {
Expand Down
3 changes: 3 additions & 0 deletions proto/composable/ibctransfermiddleware/v1beta1/genesis.proto
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@ package composable.ibctransfermiddleware.v1beta1;
import "gogoproto/gogo.proto";
import "composable/ibctransfermiddleware/v1beta1/ibctransfermiddleware.proto";
import "amino/amino.proto";
import "cosmos/base/v1beta1/coin.proto";


option go_package = "x/ibctransfermiddleware/types";

// GenesisState defines the ibctransfermiddleware module's genesis state.
message GenesisState {
Params params = 1 [ (gogoproto.nullable) = false ];

repeated cosmos.base.v1beta1.Coin taken_fee_by_ibc_sequence = 2 [(gogoproto.nullable) = false, (amino.dont_omitempty) = true];
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,11 @@ message ChannelFee{
message CoinItem{
cosmos.base.v1beta1.Coin min_fee = 1 [(gogoproto.nullable) = false, (amino.dont_omitempty) = true];
int64 percentage = 2;
repeated TxPriorityFee tx_priority_fee = 3;
}

message TxPriorityFee{
string priority = 1;
cosmos.base.v1beta1.Coin priority_fee = 2 [(gogoproto.nullable) = false, (amino.dont_omitempty) = true];
}

21 changes: 21 additions & 0 deletions x/ibctransfermiddleware/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,27 @@
return p
}

func (k Keeper) GetSequenceFee(ctx sdk.Context, sequence uint64) (coin sdk.Coin, found bool) {
store := ctx.KVStore(k.storeKey)

value := store.Get(types.GetSequenceKey(sequence))
if value == nil {
return sdk.Coin{}, false
}

fee := types.MustUnmarshalCoin(k.cdc, value)
return fee, true
}

Check failure on line 78 in x/ibctransfermiddleware/keeper/keeper.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gofumpt`-ed with `-extra` (gofumpt)
func (k Keeper) SetSequenceFee(ctx sdk.Context, sequence uint64, coin sdk.Coin) {
store := ctx.KVStore(k.storeKey)
store.Set(types.GetSequenceKey(sequence), types.MustMarshalCoin(k.cdc, &coin))
}

func (k Keeper) DeleteSequenceFee(ctx sdk.Context, sequence uint64) {
store := ctx.KVStore(k.storeKey)
store.Delete(types.GetSequenceKey(sequence))
}

func (k Keeper) GetCoin(ctx sdk.Context, targetChannelID, denom string) *types.CoinItem {
params := k.GetParams(ctx)
channelFee := findChannelParams(params.ChannelFees, targetChannelID)
Expand Down
101 changes: 85 additions & 16 deletions x/ibctransfermiddleware/types/genesis.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading