43 lines
1.3 KiB
JavaScript
43 lines
1.3 KiB
JavaScript
import {ethers} from "ethers";
|
|
|
|
const providers = {} // indexed by chain id
|
|
|
|
|
|
export function getProvider(chainId) {
|
|
let result = providers[chainId]
|
|
if( result === undefined ) {
|
|
const rpc_url = process.env['DEXORDER_RPC_URL_'+chainId]
|
|
if( rpc_url === undefined ) {
|
|
console.error('No provider found for chainId',chainId)
|
|
return null
|
|
}
|
|
result = rpc_url.startsWith('ws') ?
|
|
new ethers.WebSocketProvider(rpc_url, chainId) :
|
|
new ethers.JsonRpcProvider(rpc_url, chainId)
|
|
providers[chainId] = result
|
|
}
|
|
return result
|
|
}
|
|
|
|
const signers = {} // indexed by chain id, value is an array to be used in round-robin fashion
|
|
const signerIndexes = {}
|
|
|
|
|
|
export function signer(chainId) {
|
|
let chainSigners = signers[chainId]
|
|
if (chainSigners === undefined) {
|
|
chainSigners = []
|
|
const private_keys = process.env['DEXORDER_ACCOUNTS_' + chainId]
|
|
for (const match of private_keys.matchAll(/([^,]+),?/g))
|
|
chainSigners.push(new ethers.Wallet(match[1]))
|
|
signers[chainId] = chainSigners
|
|
signerIndexes[chainId] = 0
|
|
}
|
|
const result = chainSigners[signerIndexes[chainId]]
|
|
signerIndexes[chainId]++
|
|
if( signerIndexes[chainId] >= chainSigners.length )
|
|
signerIndexes[chainId] = 0
|
|
return result
|
|
}
|
|
|