-
Notifications
You must be signed in to change notification settings - Fork 717
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
6b810ac
commit 3ce5f16
Showing
4 changed files
with
188 additions
and
7 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
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,93 @@ | ||
package sdk | ||
|
||
import ( | ||
"errors" | ||
"strings" | ||
|
||
"github.com/wormhole-foundation/wormhole/sdk/vaa" | ||
) | ||
|
||
type EvmChainIDs map[vaa.ChainID]int | ||
|
||
var mainnetEvmChainIDs = EvmChainIDs{ | ||
vaa.ChainIDAcala: 787, | ||
vaa.ChainIDArbitrum: 42161, | ||
vaa.ChainIDAurora: 1313161554, | ||
vaa.ChainIDAvalanche: 43114, | ||
vaa.ChainIDBSC: 56, | ||
vaa.ChainIDBase: 8453, | ||
vaa.ChainIDBlast: 81457, | ||
vaa.ChainIDCelo: 42220, | ||
vaa.ChainIDEthereum: 1, | ||
vaa.ChainIDFantom: 250, | ||
vaa.ChainIDGnosis: 100, | ||
vaa.ChainIDKarura: 686, | ||
vaa.ChainIDKlaytn: 8217, | ||
vaa.ChainIDLinea: 0, // TODO: We need this value | ||
vaa.ChainIDMantle: 5000, | ||
vaa.ChainIDMoonbeam: 1284, | ||
vaa.ChainIDOasis: 42262, | ||
vaa.ChainIDOptimism: 10, | ||
vaa.ChainIDPolygon: 137, | ||
vaa.ChainIDRootstock: 30, | ||
vaa.ChainIDScroll: 534352, | ||
vaa.ChainIDSnaxchain: 2192, | ||
vaa.ChainIDXLayer: 196, | ||
} | ||
|
||
var testnetEvmChainIDs = EvmChainIDs{ | ||
vaa.ChainIDAcala: 597, | ||
vaa.ChainIDArbitrum: 421613, | ||
vaa.ChainIDArbitrumSepolia: 421614, | ||
vaa.ChainIDAurora: 1313161555, | ||
vaa.ChainIDAvalanche: 43113, | ||
vaa.ChainIDBSC: 97, | ||
vaa.ChainIDBase: 84531, | ||
vaa.ChainIDBaseSepolia: 84532, | ||
vaa.ChainIDBerachain: 80084, | ||
vaa.ChainIDBlast: 168587773, | ||
vaa.ChainIDCelo: 44787, | ||
vaa.ChainIDEthereum: 17000, // This is actually the value for Holesky, since Goerli obsolete. | ||
vaa.ChainIDFantom: 4002, | ||
vaa.ChainIDGnosis: 77, | ||
vaa.ChainIDHolesky: 17000, | ||
vaa.ChainIDKarura: 596, | ||
vaa.ChainIDKlaytn: 1001, | ||
vaa.ChainIDLinea: 59141, | ||
vaa.ChainIDMantle: 5003, | ||
vaa.ChainIDMoonbeam: 1287, | ||
vaa.ChainIDOasis: 42261, | ||
vaa.ChainIDOptimism: 420, | ||
vaa.ChainIDOptimismSepolia: 11155420, | ||
vaa.ChainIDPolygon: 80001, | ||
vaa.ChainIDPolygonSepolia: 80002, | ||
vaa.ChainIDRootstock: 31, | ||
vaa.ChainIDScroll: 534353, | ||
vaa.ChainIDSeiEVM: 713715, | ||
vaa.ChainIDSepolia: 11155111, | ||
vaa.ChainIDSnaxchain: 13001, | ||
vaa.ChainIDXLayer: 195, | ||
} | ||
|
||
var ErrInvalidEnv = errors.New("invalid environment") | ||
var ErrNotFound = errors.New("not found") | ||
|
||
// 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 EvmChainIDs, 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,41 @@ | ||
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) | ||
} | ||
}) | ||
} | ||
} |