import {ethers} from "ethers"; 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 ) throw Error(`WARNING: No provider found for chainId ${chainId}. Using localhost.`) 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 }