-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
bubble up provider and api to config
- Loading branch information
1 parent
4240868
commit bb03e72
Showing
4 changed files
with
93 additions
and
105 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 |
---|---|---|
@@ -1,95 +1,103 @@ | ||
import { ADDRESSES } from '@acala-network/asset-router/dist/consts'; | ||
import { CHAIN_ID_ACALA, CHAIN_ID_KARURA, ChainId } from '@certusone/wormhole-sdk'; | ||
import { AcalaJsonRpcProvider } from '@acala-network/eth-providers'; | ||
import { ApiPromise } from '@polkadot/api'; | ||
import { CHAIN_ID_ACALA, CHAIN_ID_KARURA } from '@certusone/wormhole-sdk'; | ||
import { Wallet } from 'ethers'; | ||
import dotenv from 'dotenv'; | ||
|
||
import { ROUTER_CHAIN_ID } from './utils'; | ||
import { ROUTER_CHAIN_ID, getApi } from './utils'; | ||
|
||
dotenv.config({ path: '.env' }); | ||
|
||
export type RelayerEnvironment = { | ||
supportedChains: ChainConfig[]; | ||
}; | ||
|
||
export type ChainConfig = { | ||
chainId: ROUTER_CHAIN_ID; | ||
ethRpc: string; | ||
nodeUrl: string; | ||
walletPrivateKey: string; | ||
provider: AcalaJsonRpcProvider; | ||
wallet: Wallet; | ||
api: ApiPromise; | ||
relayerSubstrateAddr: string; | ||
tokenBridgeAddr: string; | ||
feeAddr: string; | ||
factoryAddr: string; | ||
isTestnet: boolean; | ||
}; | ||
|
||
const isTestnet = Boolean(Number(process.env.TESTNET_MODE ?? 1)); | ||
|
||
export function prepareEnvironment(): RelayerEnvironment { | ||
const supportedChains: ChainConfig[] = []; | ||
supportedChains.push(configKarura()); | ||
supportedChains.push(configAcala()); | ||
|
||
return { supportedChains }; | ||
} | ||
|
||
function configKarura(): ChainConfig { | ||
const requiredEnvVars = [ | ||
'KARURA_ETH_RPC', | ||
'KARURA_PRIVATE_KEY', | ||
'KARURA_NODE_URL', | ||
]; | ||
export type RelayerEnvironment = ChainConfig[]; | ||
|
||
for (const envVar of requiredEnvVars) { | ||
const ensureEnvVars = (envVars: string[]) => { | ||
for (const envVar of envVars) { | ||
if (!process.env[envVar]) { | ||
console.error(`Missing environment variable ${envVar}`); | ||
process.exit(1); | ||
} | ||
} | ||
}; | ||
|
||
const isTestnet = Boolean(Number(process.env.TESTNET_MODE ?? 1)); | ||
|
||
export const prepareEnvironment = (): Promise<RelayerEnvironment> => Promise.all([ | ||
configKarura(), | ||
configAcala(), | ||
]); | ||
|
||
const configKarura = async (): Promise<ChainConfig> => { | ||
ensureEnvVars([ | ||
'KARURA_ETH_RPC', | ||
'KARURA_PRIVATE_KEY', | ||
'KARURA_NODE_URL', | ||
]); | ||
|
||
const addresses = isTestnet | ||
? ADDRESSES.KARURA_TESTNET | ||
: ADDRESSES.KARURA; | ||
|
||
const provider = new AcalaJsonRpcProvider(process.env.KARURA_ETH_RPC!); | ||
const wallet = new Wallet(process.env.KARURA_PRIVATE_KEY!, provider); | ||
const { substrateAddr, api } = await getApi( | ||
process.env.KARURA_PRIVATE_KEY!, | ||
process.env.KARURA_NODE_URL!, | ||
); | ||
|
||
return { | ||
chainId: CHAIN_ID_KARURA, | ||
ethRpc: process.env.KARURA_ETH_RPC!, | ||
nodeUrl: process.env.KARURA_NODE_URL!, | ||
walletPrivateKey: process.env.KARURA_PRIVATE_KEY!, | ||
provider, | ||
wallet, | ||
api, | ||
relayerSubstrateAddr: substrateAddr, | ||
isTestnet, | ||
...addresses, | ||
}; | ||
} | ||
}; | ||
|
||
function configAcala(): ChainConfig { | ||
const requiredEnvVars = [ | ||
const configAcala = async (): Promise<ChainConfig> => { | ||
ensureEnvVars([ | ||
'ACALA_ETH_RPC', | ||
'ACALA_PRIVATE_KEY', | ||
'ACALA_NODE_URL', | ||
]; | ||
|
||
for (const envVar of requiredEnvVars) { | ||
if (!process.env[envVar]) { | ||
console.error(`Missing environment variable ${envVar}`); | ||
process.exit(1); | ||
} | ||
} | ||
]); | ||
|
||
const addresses = isTestnet | ||
? ADDRESSES.ACALA_TESTNET | ||
: ADDRESSES.ACALA; | ||
|
||
const provider = new AcalaJsonRpcProvider(process.env.ACALA_ETH_RPC!); | ||
const wallet = new Wallet(process.env.ACALA_PRIVATE_KEY!, provider); | ||
const { substrateAddr, api } = await getApi( | ||
process.env.ACALA_PRIVATE_KEY!, | ||
process.env.ACALA_NODE_URL!, | ||
); | ||
|
||
return { | ||
chainId: CHAIN_ID_ACALA, | ||
ethRpc: process.env.ACALA_ETH_RPC!, | ||
nodeUrl: process.env.ACALA_NODE_URL!, | ||
walletPrivateKey: process.env.ACALA_PRIVATE_KEY!, | ||
provider, | ||
wallet, | ||
api, | ||
relayerSubstrateAddr: substrateAddr, | ||
isTestnet, | ||
...addresses, | ||
}; | ||
} | ||
|
||
const env: RelayerEnvironment = prepareEnvironment(); | ||
}; | ||
|
||
// TODO: maybe export signer and api directly from here? | ||
export const getChainConfig = (chainId: ChainId): ChainConfig | undefined => ( | ||
env.supportedChains.find((x) => x.chainId === chainId) | ||
const env = prepareEnvironment(); | ||
export const getChainConfig = async (chainId: ROUTER_CHAIN_ID): Promise<ChainConfig> => ( | ||
(await env).find((x) => x.chainId === chainId)! | ||
); |
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