mirror of
https://github.com/0glabs/0g-chain.git
synced 2024-11-15 04:25:27 +00:00
2b123bf007
* add eip712 ante * minor cleanup * eip712 integration test with bridge conversion * fix issues * update bridge module * merge bridge module convert logic * update eip712 tests & update deps * remove v17 migrations * remove v17 migrations * fix genesis test * fix erc20 to coin tx * remove eth check * clean up imports * remove * fix evmutil cli * remove bridge comments * address feedback * rename mint method * add transfer checks for locking & unlocking funds * fix gas * increase gas even more * fix amount check Co-authored-by: Ruaridh <rhuairahrighairidh@users.noreply.github.com> Co-authored-by: Ruaridh <rhuairahrighairidh@users.noreply.github.com>
74 lines
2.1 KiB
Go
74 lines
2.1 KiB
Go
package cli
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"strings"
|
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
"github.com/ethereum/go-ethereum/common"
|
|
"github.com/kava-labs/kava/x/evmutil/types"
|
|
)
|
|
|
|
// ParseAddrFromHexOrBech32 parses a string address that can be either a hex or
|
|
//Bech32 string.
|
|
func ParseAddrFromHexOrBech32(addrString string) (common.Address, error) {
|
|
if common.IsHexAddress(addrString) {
|
|
return common.HexToAddress(addrString), nil
|
|
}
|
|
|
|
cfg := sdk.GetConfig()
|
|
|
|
if !strings.HasPrefix(addrString, cfg.GetBech32AccountAddrPrefix()) {
|
|
return common.Address{}, fmt.Errorf("receiver '%s' is not a hex or bech32 address (prefix does not match)", addrString)
|
|
}
|
|
|
|
accAddr, err := sdk.AccAddressFromBech32(addrString)
|
|
if err != nil {
|
|
return common.Address{}, fmt.Errorf("receiver '%s' is not a hex or bech32 address (could not parse as bech32 string)", addrString)
|
|
}
|
|
|
|
return common.BytesToAddress(accAddr), nil
|
|
|
|
}
|
|
|
|
// ParseOrQueryConversionPairAddress returns an EVM address of the provided
|
|
// ERC20 contract address string or denom. If an address string, just returns
|
|
// the parsed address. If a denom, fetches params, searches the enabled
|
|
// conversion pairs, and returns corresponding ERC20 contract address.
|
|
func ParseOrQueryConversionPairAddress(
|
|
queryClient types.QueryClient,
|
|
addrOrDenom string,
|
|
) (common.Address, error) {
|
|
if common.IsHexAddress(addrOrDenom) {
|
|
return common.HexToAddress(addrOrDenom), nil
|
|
}
|
|
|
|
if err := sdk.ValidateDenom(addrOrDenom); err != nil {
|
|
return common.Address{}, fmt.Errorf(
|
|
"Kava ERC20 '%s' is not a valid hex address or denom",
|
|
addrOrDenom,
|
|
)
|
|
}
|
|
|
|
// Valid denom, try looking up as denom to get corresponding Kava ERC20 address
|
|
paramsRes, err := queryClient.Params(
|
|
context.Background(),
|
|
&types.QueryParamsRequest{},
|
|
)
|
|
if err != nil {
|
|
return common.Address{}, err
|
|
}
|
|
|
|
for _, pair := range paramsRes.Params.EnabledConversionPairs {
|
|
if pair.Denom == addrOrDenom {
|
|
return pair.GetAddress().Address, nil
|
|
}
|
|
}
|
|
|
|
return common.Address{}, fmt.Errorf(
|
|
"Kava ERC20 '%s' is not a valid hex address or denom (did not match any denoms in queried enabled conversion pairs)",
|
|
addrOrDenom,
|
|
)
|
|
}
|