Skip to content

Commit

Permalink
refactor: avoid converting origin coin to ERC20 during crosschain ref…
Browse files Browse the repository at this point in the history
…und (#762)

Co-authored-by: nulnut <[email protected]>
  • Loading branch information
zakir-code and nulnut authored Oct 22, 2024
1 parent 378231d commit 880148d
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 7 deletions.
19 changes: 18 additions & 1 deletion x/crosschain/keeper/bridge_call_out.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"strconv"
"time"

"cosmossdk.io/collections"
"cosmossdk.io/errors"
sdkmath "cosmossdk.io/math"
storetypes "cosmossdk.io/store/types"
"github.com/cosmos/cosmos-sdk/telemetry"
Expand All @@ -19,6 +21,7 @@ import (
"github.com/hashicorp/go-metrics"

fxtelemetry "github.com/functionx/fx-core/v8/telemetry"
fxtypes "github.com/functionx/fx-core/v8/types"
"github.com/functionx/fx-core/v8/x/crosschain/types"
)

Expand Down Expand Up @@ -138,8 +141,22 @@ func (k Keeper) RefundOutgoingBridgeCall(ctx sdk.Context, data *types.OutgoingBr
sdk.NewAttribute(types.AttributeKeyRefund, refund.String()),
))

originAmount, err := k.erc20Keeper.GetCache(ctx, types.NewOriginTokenKey(k.moduleName, data.Nonce))
if err != nil && !errors.IsOf(err, collections.ErrNotFound) {
return err
}
if errors.IsOf(err, collections.ErrNotFound) {
originAmount = sdkmath.ZeroInt()
}

originCoin := sdk.NewCoin(fxtypes.DefaultDenom, originAmount)
if !baseCoins.IsAllGTE(sdk.NewCoins(originCoin)) {
return types.ErrInvalid.Wrapf("bridge call coin less than origin amount")
}
baseCoins = baseCoins.Sub(originCoin)

for _, coin := range baseCoins {
_, err := k.erc20Keeper.BaseCoinToEvm(ctx, common.BytesToAddress(refund.Bytes()), coin)
_, err = k.erc20Keeper.BaseCoinToEvm(ctx, common.BytesToAddress(refund.Bytes()), coin)
if err != nil {
return err
}
Expand Down
9 changes: 3 additions & 6 deletions x/crosschain/keeper/many_to_one.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,14 +148,11 @@ func (k Keeper) IBCCoinRefund(ctx sdk.Context, holder sdk.AccAddress, ibcCoin sd
if err != nil {
return err
}
if !found {
return nil
if found {
return k.erc20Keeper.DeleteCache(ctx, ibcTransferKey)
}
_, err = k.erc20Keeper.BaseCoinToEvm(ctx, common.BytesToAddress(holder.Bytes()), sdk.NewCoin(baseDenom, ibcCoin.Amount))
if err != nil {
return err
}
return k.erc20Keeper.DeleteCache(ctx, ibcTransferKey)
return err
}

func (k Keeper) AfterIBCAckSuccess(ctx sdk.Context, ibcChannel string, ibcSequence uint64) error {
Expand Down
15 changes: 15 additions & 0 deletions x/crosschain/mock/expected_keepers_mocks.go

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

1 change: 1 addition & 0 deletions x/crosschain/types/expected_keepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ type Erc20Keeper interface {
HasCache(ctx context.Context, key string) (bool, error)
SetCache(ctx context.Context, key string, amount sdkmath.Int) error
DeleteCache(ctx context.Context, key string) error
GetCache(ctx context.Context, key string) (sdkmath.Int, error)

HasToken(ctx context.Context, token string) (bool, error)
GetBaseDenom(ctx context.Context, token string) (string, error)
Expand Down
4 changes: 4 additions & 0 deletions x/erc20/keeper/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,7 @@ func (k Keeper) SetCache(ctx context.Context, key string, amount sdkmath.Int) er
func (k Keeper) DeleteCache(ctx context.Context, key string) error {
return k.Cache.Remove(ctx, key)
}

func (k Keeper) GetCache(ctx context.Context, key string) (sdkmath.Int, error) {
return k.Cache.Get(ctx, key)
}
2 changes: 2 additions & 0 deletions x/ibc/middleware/keeper/relay_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/ethereum/go-ethereum/common"

"github.com/functionx/fx-core/v8/testutil/helpers"
crosschaintypes "github.com/functionx/fx-core/v8/x/crosschain/types"
)

func (suite *KeeperTestSuite) TestOnRecvPacket() {
Expand Down Expand Up @@ -80,6 +81,7 @@ func (suite *KeeperTestSuite) TestOnAcknowledgementPacket() {

escrowAddress := transfertypes.GetEscrowAddress(packet.SourcePort, packet.SourceChannel)
suite.mintCoin(escrowAddress, coin)
suite.Require().NoError(suite.App.Erc20Keeper.SetCache(suite.Ctx, crosschaintypes.NewIBCTransferKey(packet.SourceChannel, 1), coin.Amount))
},
true,
"",
Expand Down

0 comments on commit 880148d

Please sign in to comment.