Files
server/blockchain.js
2023-09-01 18:58:04 -04:00

54 lines
1.6 KiB
JavaScript

import {ethers} from "ethers";
export const chains = {}
const _chainInfo = [
{id:42161, name:'Arbitrum'},
]
for( const chain of _chainInfo )
chains[chain.id] = chain
const providers = {} // indexed by chain id
export function getProvider(chainId) {
let result = providers[chainId]
if( result === undefined ) {
let rpc_url = process.env['DEXORDER_RPC_URL_'+chainId]
if( rpc_url === undefined ) {
console.error(`WARNING: No provider found for chainId ${chainId}. Using localhost.`)
rpc_url = 'http://localhost:8545'
}
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
// todo multiple signers per chain, checked out of a pool
export function getSigner(chainId) {
let signer = signers[chainId]
if (signer === undefined) {
const private_keys = process.env['DEXORDER_ACCOUNTS_' + chainId]
if( !private_keys ) {
console.log(`DEXORDER_ACCOUNTS_${chainId} not defined`)
return null // todo fatal
}
// for (const match of private_keys.matchAll(/([^,]+),?/g))
// signer.push(new ethers.Wallet(match[1]))
signer = new ethers.Wallet(private_keys, getProvider(chainId))
signers[chainId] = signer
}
return signer
// const result = signer[signerIndexes[chainId]]
// signerIndexes[chainId]++
// if( signerIndexes[chainId] >= signer.length )
// signerIndexes[chainId] = 0
// return result
}