Skip to content

Commit

Permalink
refactor(bridge): automate the addition of bridge tokens (#881)
Browse files Browse the repository at this point in the history
  • Loading branch information
zakir-code authored Jan 8, 2025
1 parent ff94709 commit 9020cc4
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 36 deletions.
28 changes: 8 additions & 20 deletions x/crosschain/keeper/bridge_token.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package keeper

import (
"context"
"strings"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/ethereum/go-ethereum/common"
Expand All @@ -14,28 +15,15 @@ import (
func (k Keeper) AddBridgeTokenExecuted(ctx sdk.Context, claim *types.MsgBridgeTokenClaim) error {
k.Logger(ctx).Info("add bridge token claim", "symbol", claim.Symbol, "token", claim.TokenContract)

// Check if it already exists
bridgeDenom := types.NewBridgeDenom(k.moduleName, claim.TokenContract)
has, err := k.erc20Keeper.HasToken(ctx, bridgeDenom)
if err != nil {
return err
}
if has {
return types.ErrInvalid.Wrapf("bridge token is exist %s", bridgeDenom)
if claim.Symbol == fxtypes.DefaultDenom {
if uint64(fxtypes.DenomUnit) != claim.Decimals {
return types.ErrInvalid.Wrapf("%s denom decimals not match %d, expect %d",
fxtypes.DefaultDenom, claim.Decimals, fxtypes.DenomUnit)
}
return k.erc20Keeper.AddBridgeToken(ctx, claim.Symbol, k.moduleName, claim.TokenContract, true)
}

if claim.Symbol == fxtypes.DefaultDenom && uint64(fxtypes.DenomUnit) != claim.Decimals {
return types.ErrInvalid.Wrapf("%s denom decimals not match %d, expect %d",
fxtypes.DefaultDenom, claim.Decimals, fxtypes.DenomUnit)
}
bridgeToken, err := k.erc20Keeper.GetBridgeToken(ctx, k.moduleName, claim.Symbol)
if err != nil {
return err
}
if bridgeToken.Contract != claim.TokenContract {
return types.ErrInvalid.Wrapf("bridge token contract not match %s, expect %s", bridgeToken.Contract, claim.TokenContract)
}
return nil
return k.erc20Keeper.AddBridgeToken(ctx, strings.ToLower(claim.Symbol), k.moduleName, claim.TokenContract, false)
}

func (k Keeper) BridgeCoinSupply(ctx context.Context, token, target string) (sdk.Coin, error) {
Expand Down
29 changes: 14 additions & 15 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.

2 changes: 1 addition & 1 deletion x/crosschain/types/expected_keepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,13 @@ type Erc20Keeper interface {
GetCache(ctx context.Context, key string) (sdkmath.Int, error)
ReSetCache(ctx context.Context, oldKey, newKey string) error

HasToken(ctx context.Context, token string) (bool, error)
GetBaseDenom(ctx context.Context, token string) (string, error)

GetERC20Token(ctx context.Context, baseDenom string) (erc20types.ERC20Token, error)

GetBridgeToken(ctx context.Context, chainName, baseDenom string) (erc20types.BridgeToken, error)
GetBridgeTokens(ctx context.Context, chainName string) ([]erc20types.BridgeToken, error)
AddBridgeToken(ctx context.Context, baseDenom, chainName, contract string, isNative bool) error

GetIBCToken(ctx context.Context, channel, baseDenom string) (erc20types.IBCToken, error)
}
Expand Down

0 comments on commit 9020cc4

Please sign in to comment.