Skip to content

Commit

Permalink
fix: error duplicate asset registration
Browse files Browse the repository at this point in the history
  • Loading branch information
MaxMustermann2 committed Oct 11, 2024
1 parent 3baafe9 commit f9338bf
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 16 deletions.
13 changes: 2 additions & 11 deletions precompiles/assets/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,18 +189,9 @@ func (p Precompile) UpdateToken(
return nil, err
}

// check that the asset being updated actually exists
_, assetID := assetstypes.GetStakerIDAndAssetIDFromStr(uint64(clientChainID), "", hexAssetAddr)
assetInfo, err := p.assetsKeeper.GetStakingAssetInfo(ctx, assetID)
if err != nil {
// fails if asset does not exist with ErrNoClientChainAssetKey
return nil, err
}

// finally, execute the update
assetInfo.AssetBasicInfo.MetaInfo = metadata

if err := p.assetsKeeper.SetStakingAssetInfo(ctx, assetInfo); err != nil {
// this verifies the existence of the asset and returns an error if it doesn't exist
if err := p.assetsKeeper.UpdateStakingAssetMetaInfo(ctx, assetID, metadata); err != nil {
return nil, err
}

Expand Down
32 changes: 27 additions & 5 deletions x/assets/keeper/client_chain_asset.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,9 @@ func (k Keeper) UpdateStakingAssetTotalAmount(ctx sdk.Context, assetID string, c
}

// SetStakingAssetInfo todo: Temporarily use clientChainAssetAddr+'_'+LayerZeroChainID as the key.
// It provides a function to register the client chain assets supported by exoCore.It's called by genesis configuration now,however it will be called by the governance in the future
// The caller is responsible for ensuring that no such asset already exists (if a new asset is being created)
// It provides a function to register the client chain assets supported by Exocore.
// New assets may be registered via ExocoreGateway, which uses precompiles to call this function.
// If an asset with the provided assetID already exists, it will return an error.
func (k Keeper) SetStakingAssetInfo(ctx sdk.Context, info *assetstype.StakingAssetInfo) (err error) {
if info.AssetBasicInfo.Decimals > assetstype.MaxDecimal {
return errorsmod.Wrapf(assetstype.ErrInvalidInputParameter, "the decimal is greater than the MaxDecimal,decimal:%v,MaxDecimal:%v", info.AssetBasicInfo.Decimals, assetstype.MaxDecimal)
Expand All @@ -45,19 +46,39 @@ func (k Keeper) SetStakingAssetInfo(ctx sdk.Context, info *assetstype.StakingAss
return errorsmod.Wrapf(assetstype.ErrInvalidInputParameter, "the total staking amount is negative, StakingTotalAmount:%v", info.StakingTotalAmount)
}
store := prefix.NewStore(ctx.KVStore(k.storeKey), assetstype.KeyPrefixReStakingAssetInfo)
// key := common.HexToAddress(incentive.Contract)
bz := k.cdc.MustMarshal(info)

_, assetID := assetstype.GetStakerIDAndAssetIDFromStr(info.AssetBasicInfo.LayerZeroChainID, "", info.AssetBasicInfo.Address)
if k.IsStakingAsset(ctx, assetID) {
return assetstype.ErrInvalidInputParameter.Wrapf(
"the asset has already been registered,assetID:%v,LayerZeroChainID:%v,ClientChainAssetAddr:%v",
assetID, info.AssetBasicInfo.LayerZeroChainID, info.AssetBasicInfo.Address,
)
}
bz := k.cdc.MustMarshal(info)
store.Set([]byte(assetID), bz)
return nil
}

// IsStakingAsset checks if the assetID is a staking asset.
func (k Keeper) IsStakingAsset(ctx sdk.Context, assetID string) bool {
store := prefix.NewStore(ctx.KVStore(k.storeKey), assetstype.KeyPrefixReStakingAssetInfo)
return store.Has([]byte(assetID))
}

// UpdateStakingAssetMetaInfo updates the meta information stored against a provided assetID.
// If the assetID does not exist, it returns an error.
func (k Keeper) UpdateStakingAssetMetaInfo(ctx sdk.Context, assetID string, metainfo string) error {
info, err := k.GetStakingAssetInfo(ctx, assetID)
if err != nil {
return err
}
store := prefix.NewStore(ctx.KVStore(k.storeKey), assetstype.KeyPrefixReStakingAssetInfo)
info.AssetBasicInfo.MetaInfo = metainfo
bz := k.cdc.MustMarshal(info)
store.Set([]byte(assetID), bz)
return nil
}

// GetStakingAssetInfo returns the asset information stored against a provided assetID.
func (k Keeper) GetStakingAssetInfo(ctx sdk.Context, assetID string) (info *assetstype.StakingAssetInfo, err error) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), assetstype.KeyPrefixReStakingAssetInfo)
value := store.Get([]byte(assetID))
Expand All @@ -70,6 +91,7 @@ func (k Keeper) GetStakingAssetInfo(ctx sdk.Context, assetID string) (info *asse
return &ret, nil
}

// GetAssetsDecimal returns the decimal of all the provided assets.
func (k Keeper) GetAssetsDecimal(ctx sdk.Context, assets map[string]interface{}) (decimals map[string]uint32, err error) {
if assets == nil {
return nil, errorsmod.Wrap(assetstype.ErrInputPointerIsNil, "assets is nil")
Expand Down

0 comments on commit f9338bf

Please sign in to comment.