import {buildMetadataMap} from "@/common.js";

function _json(name) {
    return async function(response) {
        try {
            return await response.json()
        }
        catch (e) {
            console.error(`could not read ${name}`)
            return null
        }
    }
}

const versionPromise = fetch('/contract/version.json').then(_json('version.json'))
const metadataPromise = fetch('/metadata.json').then(_json('metadata.json'))

export const version = await versionPromise
// console.log('version', version)
export const metadata = await metadataPromise
// console.log('metadata', metadata)

export function dexorderAddress(chainId) { return version['chainInfo'][chainId]['dexorder'] }
export function factoryAddress(chainId) { return version['chainInfo'][chainId]['factory'] }
export function helperAddress(chainId) { return version['chainInfo'][chainId]['helper'] }
export function vaultInitCodeHash(chainId) { return version['chainInfo'][chainId]['vaultInitCodeHash'] }

// maps [chainId][addr] to pool or token metadata
export const metadataMap = buildMetadataMap(metadata)
