reworked to optionally use Hardhat in mock; chain id 31337; refactored TransactionJob management; execute() mostly commented out for minimalism

This commit is contained in:
Tim Olson
2023-10-26 16:56:08 -04:00
parent bd127b00ce
commit e05f0cc8dc
8 changed files with 80 additions and 53 deletions

View File

@@ -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

2
abi.js
View File

@@ -1,5 +1,5 @@
import {readFile} from './misc.js'
import {ethers} from "ethers";
import {readFile} from "./misc.js";
const ABI_BASE_PATH = '../contract/out'

View File

@@ -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'),
}

69
chain.js Normal file
View File

@@ -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
}

View File

@@ -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

46
misc.js
View File

@@ -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
}

View File

@@ -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) {

View File

@@ -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)) {