-
Notifications
You must be signed in to change notification settings - Fork 722
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c888252
commit 4f0037f
Showing
3 changed files
with
168 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package sdk | ||
|
||
import ( | ||
"errors" | ||
"strings" | ||
|
||
"github.com/wormhole-foundation/wormhole/sdk/vaa" | ||
) | ||
|
||
var ErrInvalidEnv = errors.New("invalid environment") | ||
var ErrNotFound = errors.New("not found") | ||
|
||
// IsEvmChainID if the specified chain is defined as an EVM chain ID in the specified environment. | ||
func IsEvmChainID(env string, chainID vaa.ChainID) (bool, error) { | ||
var m *map[vaa.ChainID]int | ||
if env == "prod" || env == "mainnet" { | ||
m = &MainnetEvmChainIDs | ||
} else if env == "test" || env == "testnet" { | ||
m = &TestnetEvmChainIDs | ||
} else { | ||
return false, ErrInvalidEnv | ||
} | ||
_, exists := (*m)[chainID] | ||
return exists, nil | ||
} | ||
|
||
// GetEvmChainID returns the expected EVM chain ID associated with the given Wormhole chain ID and environment passed it. | ||
func GetEvmChainID(env string, chainID vaa.ChainID) (int, error) { | ||
env = strings.ToLower(env) | ||
if env == "prod" || env == "mainnet" { | ||
return getEvmChainID(MainnetEvmChainIDs, chainID) | ||
} | ||
if env == "test" || env == "testnet" { | ||
return getEvmChainID(TestnetEvmChainIDs, chainID) | ||
} | ||
return 0, ErrInvalidEnv | ||
} | ||
|
||
func getEvmChainID(evmChains map[vaa.ChainID]int, chainID vaa.ChainID) (int, error) { | ||
id, exists := evmChains[chainID] | ||
if !exists { | ||
return 0, ErrNotFound | ||
} | ||
return id, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package sdk | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
"github.com/wormhole-foundation/wormhole/sdk/vaa" | ||
) | ||
|
||
func TestGetEvmChainID(t *testing.T) { | ||
type test struct { | ||
env string | ||
input vaa.ChainID | ||
output int | ||
err error | ||
} | ||
|
||
// Note: Don't intend to list every chain here, just enough to verify `GetEvmChainID`. | ||
tests := []test{ | ||
{env: "mainnet", input: vaa.ChainIDUnset, output: 0, err: ErrNotFound}, | ||
{env: "mainnet", input: vaa.ChainIDSepolia, output: 0, err: ErrNotFound}, | ||
{env: "mainnet", input: vaa.ChainIDEthereum, output: 1}, | ||
{env: "mainnet", input: vaa.ChainIDArbitrum, output: 42161}, | ||
{env: "testnet", input: vaa.ChainIDSepolia, output: 11155111}, | ||
{env: "testnet", input: vaa.ChainIDEthereum, output: 17000}, | ||
{env: "junk", input: vaa.ChainIDEthereum, output: 17000, err: ErrInvalidEnv}, | ||
} | ||
|
||
for _, tc := range tests { | ||
t.Run(tc.env+"-"+tc.input.String(), func(t *testing.T) { | ||
evmChainID, err := GetEvmChainID(tc.env, tc.input) | ||
if tc.err != nil { | ||
assert.ErrorIs(t, tc.err, err) | ||
} else { | ||
require.NoError(t, err) | ||
assert.Equal(t, tc.output, evmChainID) | ||
} | ||
}) | ||
} | ||
} | ||
func TestIsEvmChainID(t *testing.T) { | ||
type test struct { | ||
env string | ||
input vaa.ChainID | ||
output bool | ||
err error | ||
} | ||
|
||
// Note: Don't intend to list every chain here, just enough to verify `GetEvmChainID`. | ||
tests := []test{ | ||
{env: "mainnet", input: vaa.ChainIDUnset, output: false}, | ||
{env: "mainnet", input: vaa.ChainIDSepolia, output: false}, | ||
{env: "mainnet", input: vaa.ChainIDEthereum, output: true}, | ||
{env: "mainnet", input: vaa.ChainIDArbitrum, output: true}, | ||
{env: "mainnet", input: vaa.ChainIDSolana, output: false}, | ||
{env: "testnet", input: vaa.ChainIDSepolia, output: true}, | ||
{env: "testnet", input: vaa.ChainIDEthereum, output: true}, | ||
{env: "testnet", input: vaa.ChainIDTerra, output: false}, | ||
{env: "junk", input: vaa.ChainIDEthereum, output: true, err: ErrInvalidEnv}, | ||
} | ||
|
||
for _, tc := range tests { | ||
t.Run(tc.env+"-"+tc.input.String(), func(t *testing.T) { | ||
result, err := IsEvmChainID(tc.env, tc.input) | ||
if tc.err != nil { | ||
assert.ErrorIs(t, tc.err, err) | ||
} else { | ||
require.NoError(t, err) | ||
assert.Equal(t, tc.output, result) | ||
} | ||
}) | ||
} | ||
} |