diff --git a/.env-mock b/.env-mock index 338e6ca..01c8fca 100644 --- a/.env-mock +++ b/.env-mock @@ -9,7 +9,7 @@ DEXORDER_DEPLOYMENT_42161=latest DEXORDER_RPC_URL_42161=http://localhost:8545 # Mockchain -DEXORDER_DEPLOYMENT_1338=latest -DEXORDER_RPC_URL_1338=http://localhost:8545 +DEXORDER_DEPLOYMENT_31337=latest +DEXORDER_RPC_URL_31337=http://localhost:8545 # dev account #2 -DEXORDER_ACCOUNTS_1338=0x5de4111afa1a4b94908f83103eb1f1706367c2e68ca870fc3fb9a804cdab365a +DEXORDER_ACCOUNTS_31337=0x5de4111afa1a4b94908f83103eb1f1706367c2e68ca870fc3fb9a804cdab365a diff --git a/abi.js b/abi.js index b91a9ad..423c0cc 100644 --- a/abi.js +++ b/abi.js @@ -1,5 +1,5 @@ -import {readFile} from './misc.js' import {ethers} from "ethers"; +import {readFile} from "./misc.js"; const ABI_BASE_PATH = '../contract/out' diff --git a/cache.js b/cache.js index 06ae25a..c7019b5 100644 --- a/cache.js +++ b/cache.js @@ -48,7 +48,7 @@ export class CacheObject { } const blockCaches = { - '1338': new CacheObject('1338|latest_block'), + '31337': new CacheObject('31337|latest_block'), '42161': new CacheObject('42161|latest_block'), } diff --git a/chain.js b/chain.js new file mode 100644 index 0000000..dcd1bbf --- /dev/null +++ b/chain.js @@ -0,0 +1,69 @@ +import fs from "fs"; +import {ethers, keccak256} from "ethers"; +import {getProvider} from "./blockchain.js"; +import {getAbi} from "./abi.js"; + +const vaultCode = JSON.parse(fs.readFileSync('../contract/out/Vault.sol/Vault.json').toString()) +export const VAULT_INIT_CODE_HASH = keccak256(vaultCode.bytecode.object) +console.log('VAULT_INIT_CODE_HASH', VAULT_INIT_CODE_HASH) + +export const chainInfo = {} + +const _chains = [ + {id:42161, name:'Arbitrum'}, + {id:31337, name:'Mock'}, +] + +function _setChainInfo(c, k, v) { + c.update(v) + chainInfo[c][k] = v +} + +for (const chain of _chains) { + const path = `../contract/broadcast/Deploy.sol/${chain.id}/run-${process.env['DEXORDER_DEPLOYMENT_' + chain.id]}.json`; + let deployment + try { + deployment = JSON.parse(fs.readFileSync(path, 'utf8')) //null synchronous is ok we only do this once on init + } + catch { + console.log(`warning: could not read deployment files for ${chain.id}`) + continue + } + for (const tx of deployment.transactions) { + if (tx.contractName === 'Factory') + chain.factory = tx.contractAddress + else if (tx.contractName === 'QueryHelper') + chain.helper = tx.contractAddress + else if (tx.contractName === 'MockEnv') { + // set up mock coins, etc + if( chain.id === 31337 ) { + console.log('Detected MockEnv at', tx.contractAddress) + const mock = new ethers.Contract(tx.contractAddress, await getAbi('MockEnv'), getProvider(chain.id)) + const coinAddr = await mock.COIN() + const usdAddr = await mock.USD() + chain.tokens = [ + { + name: 'Mockcoin', + symbol: 'MOCK', + decimals: 18, + icon: null, + address: coinAddr, + }, + { + name: 'Universally Stable Denomination', + symbol: 'USD', + decimals: 6, + icon: null, + address: usdAddr, + }, + ] + } + } + } + if (chain.factory === undefined) + throw Error(`No Factory deployment found for chainId ${chain.id} ${path}`) + if (chain.helper === undefined) + throw Error(`No QueryHelper deployment found for chainId ${chain.id} ${path}`) + console.log('Factory', chain.factory) + chainInfo[chain.id] = chain +} diff --git a/main.js b/main.js index c1d8ea4..3ab9860 100644 --- a/main.js +++ b/main.js @@ -3,7 +3,7 @@ import 'dotenv/config' import {lookupToken} from "./token.js"; import {httpServer, io} from "./io.js"; import {ensureVault, loginAddress} from "./vault.js"; -import {chainInfo, VAULT_INIT_CODE_HASH} from "./misc.js"; +import {chainInfo, VAULT_INIT_CODE_HASH} from "./chain.js"; // setup socket.io diff --git a/misc.js b/misc.js index 7986bfd..f3f4cae 100644 --- a/misc.js +++ b/misc.js @@ -1,48 +1,4 @@ -import fs from "fs"; import util from "util"; -import {keccak256} from "ethers"; +import fs from "fs"; export const readFile = (fileName) => util.promisify(fs.readFile)(fileName, 'utf8'); - -const vaultCode = JSON.parse(fs.readFileSync('../contract/out/Vault.sol/Vault.json').toString()) -export const VAULT_INIT_CODE_HASH = keccak256(vaultCode.bytecode.object) -console.log('VAULT_INIT_CODE_HASH', VAULT_INIT_CODE_HASH) - -export const chainInfo = {} - -const _chains = [ - {id:42161, name:'Arbitrum'}, - {id:1338, name:'Mock'}, -] - -function _setChainInfo(c, k, v) { - c.update(v) - chainInfo[c][k] = v -} - -for (const chain of _chains) { - const path = `../contract/broadcast/Deploy.sol/${chain.id}/run-${process.env['DEXORDER_DEPLOYMENT_' + chain.id]}.json`; - let deployment - try { - deployment = JSON.parse(fs.readFileSync(path, 'utf8')) //null synchronous is ok we only do this once on init - } - catch { - console.log(`warning: could not read deployment files for ${chain.id}`) - continue - } - for (const tx of deployment.transactions) { - if (tx.contractName === 'Factory') - chain.factory = tx.contractAddress - else if (tx.contractName === 'QueryHelper') - chain.helper = tx.contractAddress - else if (tx.contractName === 'MockEnv') { - // todo set up mock coins, etc - } - } - if (chain.factory === undefined) - throw Error(`No Factory deployment found for chainId ${chain.id} ${path}`) - if (chain.helper === undefined) - throw Error(`No QueryHelper deployment found for chainId ${chain.id} ${path}`) - console.log('Factory', chain.factory) - chainInfo[chain.id] = chain -} diff --git a/token.js b/token.js index 4b96267..374fe28 100644 --- a/token.js +++ b/token.js @@ -9,7 +9,7 @@ import {getProvider} from "./blockchain.js"; const std_arbitrum_tokens = {} const tokens = { 42161: std_arbitrum_tokens, - 1338: std_arbitrum_tokens, + 31337: std_arbitrum_tokens, } export async function lookupToken(chainId, address) { diff --git a/vault.js b/vault.js index 3b5205b..e629bc6 100644 --- a/vault.js +++ b/vault.js @@ -2,7 +2,7 @@ import {ethers} from "ethers"; import {getAbi} from "./abi.js"; import {getProvider, getSigner} from "./blockchain.js"; import {vaults} from './cache.js'; -import {chainInfo, VAULT_INIT_CODE_HASH} from "./misc.js"; +import {chainInfo, VAULT_INIT_CODE_HASH} from "./chain.js"; // Vault // address owner @@ -50,6 +50,8 @@ export async function loginAddress(socket, chainId, address) { export async function ensureVault(socket, chainId, owner, num) { + if( !(chainId in chainInfo) ) + return console.log('ensureVault', chainId, owner, num) const address = vaultAddress(chainId, owner, num) if (!await vaults.contains(chainId,address)) {