From a5ea47ea4ef5e26c7a89e565c5d5a2cb681ed6d7 Mon Sep 17 00:00:00 2001 From: fx0x55 Date: Thu, 22 Dec 2022 19:35:03 +0800 Subject: [PATCH] fix: CompatibilityGetBytes fx ibc transfer * retain ibc fee is ibc router is empty --- types/version.go | 12 ++++++++ x/ibc/applications/transfer/keeper/relay.go | 10 ++++++- x/ibc/applications/transfer/types/packet.go | 5 ++++ .../transfer/types/packet_test.go | 28 +++++++++++++++++++ 4 files changed, 54 insertions(+), 1 deletion(-) diff --git a/types/version.go b/types/version.go index 80621d2..02e85f8 100644 --- a/types/version.go +++ b/types/version.go @@ -1,6 +1,7 @@ package types import ( + "math" "os" "sync" @@ -12,6 +13,8 @@ const ( TestnetChainId = "payalebar" testnetMintDenom = "bsc0x0BEdB58eC8D603E71556ef8aA4014c68DBd57AD7" testnetStakingBondDenom = "ibc/169A52CA4862329131348484982CE75B3D6CC78AFB94C3107026C70CB66E7B2E" + + testnetCompatibilityIBCTransferHeight = math.MaxInt64 ) // mainnet constant @@ -19,6 +22,8 @@ const ( MainnetChainId = "PUNDIX" mainnetMintDenom = "bsc0x29a63F4B209C29B4DC47f06FFA896F32667DAD2C" mainnetStakingBondDenom = "ibc/55367B7B6572631B78A93C66EF9FDFCE87CDE372CC4ED7848DA78C1EB1DCDD78" + + mainnetCompatibilityIBCTransferHeight = math.MaxInt64 ) var ( @@ -62,3 +67,10 @@ func StakingBondDenom() string { } return mainnetStakingBondDenom } + +func CompatibilityIBCTransferHeight() int64 { + if TestnetChainId == ChainId() { + return testnetCompatibilityIBCTransferHeight + } + return mainnetCompatibilityIBCTransferHeight +} diff --git a/x/ibc/applications/transfer/keeper/relay.go b/x/ibc/applications/transfer/keeper/relay.go index 9984e61..deeeba6 100644 --- a/x/ibc/applications/transfer/keeper/relay.go +++ b/x/ibc/applications/transfer/keeper/relay.go @@ -2,6 +2,7 @@ package keeper import ( "fmt" + pxtypes "github.com/pundix/pundix/types" "strings" coretypes "github.com/cosmos/ibc-go/v3/modules/core/types" @@ -163,8 +164,15 @@ func (k Keeper) sendTransfer(ctx sdk.Context, sourcePort, sourceChannel string, } } + // hard fork: compatibility fx ibc transfer fee + var packetDateBytes []byte + if ctx.BlockHeight() >= pxtypes.CompatibilityIBCTransferHeight() { + packetDateBytes = packetData.CompatibilityGetBytes() + } else { + packetDateBytes = packetData.GetBytes() + } packet := channeltypes.NewPacket( - packetData.GetBytes(), + packetDateBytes, sequence, sourcePort, sourceChannel, diff --git a/x/ibc/applications/transfer/types/packet.go b/x/ibc/applications/transfer/types/packet.go index ec38e74..e0d5b80 100644 --- a/x/ibc/applications/transfer/types/packet.go +++ b/x/ibc/applications/transfer/types/packet.go @@ -64,6 +64,11 @@ func (ftpd FungibleTokenPacketData) GetBytes() []byte { return sdk.MustSortJSON(mustProtoMarshalJSON(&ftpd)) } +// CompatibilityGetBytes is a helper for serialising, compatibility fx ibc transfer +func (ftpd FungibleTokenPacketData) CompatibilityGetBytes() []byte { + return sdk.MustSortJSON(mustProtoMarshalJSON(&ftpd)) +} + // GetBytes is a helper for serialising func (ftpd FungibleTokenPacketData) ToIBCPacketData() transfertypes.FungibleTokenPacketData { result := transfertypes.NewFungibleTokenPacketData(ftpd.Denom, ftpd.Amount, ftpd.Sender, ftpd.Receiver) diff --git a/x/ibc/applications/transfer/types/packet_test.go b/x/ibc/applications/transfer/types/packet_test.go index 12ea2e1..cf72666 100644 --- a/x/ibc/applications/transfer/types/packet_test.go +++ b/x/ibc/applications/transfer/types/packet_test.go @@ -75,6 +75,20 @@ func TestUnmarshalJSON(t *testing.T) { Fee: "", }, }, + { + name: "fx transfer packet - no router - Compatibility", + data: types.NewFungibleTokenPacketData("FX", "100", "Add1", "Add2", "", "0").CompatibilityGetBytes(), + pass: true, + expErr: nil, + exp: types.FungibleTokenPacketData{ + Denom: "FX", + Amount: "100", + Sender: "Add1", + Receiver: "Add2", + Router: "", + Fee: "0", + }, + }, { name: "fx transfer packet - router with 0fee", data: types.NewFungibleTokenPacketData("FX", "100", "Add1", "Add2", "router", "0").GetBytes(), @@ -89,6 +103,20 @@ func TestUnmarshalJSON(t *testing.T) { Fee: "0", }, }, + { + name: "fx transfer packet - router with 0fee", + data: types.NewFungibleTokenPacketData("FX", "100", "Add1", "Add2", "router", "0").CompatibilityGetBytes(), + pass: true, + expErr: nil, + exp: types.FungibleTokenPacketData{ + Denom: "FX", + Amount: "100", + Sender: "Add1", + Receiver: "Add2", + Router: "router", + Fee: "0", + }, + }, { name: "fx transfer packet - router with empty fee", data: types.NewFungibleTokenPacketData("FX", "100", "Add1", "Add2", "router", "").GetBytes(),