2022-08-23 17:04:40 +00:00
|
|
|
package keeper
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
|
2023-04-05 23:21:59 +00:00
|
|
|
errorsmod "cosmossdk.io/errors"
|
2022-08-23 17:04:40 +00:00
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
|
|
|
2024-08-03 09:51:56 +00:00
|
|
|
"github.com/0glabs/0g-chain/x/evmutil/types"
|
2022-08-23 17:04:40 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// GetParams returns the total set of evm parameters.
|
|
|
|
func (k Keeper) GetParams(ctx sdk.Context) (params types.Params) {
|
2023-05-19 23:01:46 +00:00
|
|
|
k.paramSubspace.GetParamSetIfExists(ctx, ¶ms)
|
2022-08-23 17:04:40 +00:00
|
|
|
return params
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetParams sets the evm parameters to the param space.
|
|
|
|
func (k Keeper) SetParams(ctx sdk.Context, params types.Params) {
|
|
|
|
k.paramSubspace.SetParamSet(ctx, ¶ms)
|
|
|
|
}
|
|
|
|
|
2023-05-24 23:23:33 +00:00
|
|
|
// GetAllowedTokenMetadata gets the token metadata for the given cosmosDenom if it is allowed.
|
|
|
|
// Returns the metadata if allowed, and a bool indicating if the denom was in the allow list or not.
|
|
|
|
func (k Keeper) GetAllowedTokenMetadata(ctx sdk.Context, cosmosDenom string) (types.AllowedCosmosCoinERC20Token, bool) {
|
|
|
|
params := k.GetParams(ctx)
|
|
|
|
for _, token := range params.AllowedCosmosDenoms {
|
|
|
|
if token.CosmosDenom == cosmosDenom {
|
|
|
|
return token, true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return types.AllowedCosmosCoinERC20Token{}, false
|
|
|
|
}
|
|
|
|
|
2022-08-23 17:04:40 +00:00
|
|
|
// GetEnabledConversionPairFromERC20Address returns an ConversionPair from the internal contract address.
|
|
|
|
func (k Keeper) GetEnabledConversionPairFromERC20Address(
|
|
|
|
ctx sdk.Context,
|
|
|
|
address types.InternalEVMAddress,
|
|
|
|
) (types.ConversionPair, error) {
|
|
|
|
params := k.GetParams(ctx)
|
|
|
|
for _, pair := range params.EnabledConversionPairs {
|
2024-08-03 11:04:48 +00:00
|
|
|
if bytes.Equal(pair.ZgChainERC20Address, address.Bytes()) {
|
2022-08-23 17:04:40 +00:00
|
|
|
return pair, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-30 19:00:01 +00:00
|
|
|
return types.ConversionPair{}, errorsmod.Wrap(types.ErrEVMConversionNotEnabled, address.String())
|
2022-08-23 17:04:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetEnabledConversionPairFromDenom returns an ConversionPair from the sdk.Coin denom.
|
|
|
|
func (k Keeper) GetEnabledConversionPairFromDenom(
|
|
|
|
ctx sdk.Context,
|
|
|
|
denom string,
|
|
|
|
) (types.ConversionPair, error) {
|
|
|
|
params := k.GetParams(ctx)
|
|
|
|
for _, pair := range params.EnabledConversionPairs {
|
|
|
|
if pair.Denom == denom {
|
|
|
|
return pair, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-30 19:00:01 +00:00
|
|
|
return types.ConversionPair{}, errorsmod.Wrap(types.ErrEVMConversionNotEnabled, denom)
|
2022-08-23 17:04:40 +00:00
|
|
|
}
|