ABI's from //contract/out URLs; arbsep; placement fee considers upcoming fee changes; vault detection bugfixes; order placement bugfixes; BETA

This commit is contained in:
Tim
2024-07-03 16:18:29 -04:00
parent 104b798d4f
commit d38baccd49
18 changed files with 200 additions and 170 deletions

View File

@@ -1,3 +1,5 @@
import * as fs from "node:fs";
export function mixin(child, ...parents) {
// child is modified directly, assigning fields from parents that are missing in child. parents fields are
// assigned by parents order, highest priority first
@@ -41,3 +43,99 @@ export function decodeIEE754(value) {
view.setUint32(0, value, false)
return view.getFloat32(0, false)
}
//
// AsyncCache
//
export class AsyncCache {
// fetch(key) returns a value
constructor(fetch) {
this.cache = {}
this.fetchLocks = {}
this.fetch = fetch
}
async get(key) {
if (this.cache[key]) {
return this.cache[key]
}
if (this.fetchLocks[key]) {
return await this.fetchLocks[key]
}
const fetchPromise = this.fetch(key)
this.fetchLocks[key] = fetchPromise
const result = await fetchPromise
this.cache[key] = result
delete this.fetchLocks[key]
return result
}
}
export class AsyncAbiCache extends AsyncCache {
constructor(fetch) {
super(async (key)=>{
const result = await fetch(key)
return result.abi
});
}
}
export class AsyncURLCache extends AsyncAbiCache {
constructor(urlForKey) {
super(async (key) => {
const URL = this.urlForKey(key)
const response = await fetch(URL)
if (!response.ok)
throw new Error(`Could not fetch ${URL} (status ${response.status})`)
return await response.json()
})
this.urlForKey = urlForKey
}
}
export class AsyncFileCache extends AsyncAbiCache {
constructor(pathForKey) {
super(async (key) => {
const path = this.pathForKey(key)
const data = fs.readFileSync(path, 'utf8');
return JSON.parse(data);
})
this.pathForKey = pathForKey
}
}
export class AbiURLCache extends AsyncURLCache {
constructor(baseUrl) {
super((name)=>{
return this.baseUrl+abiPath(name)
})
this.baseUrl = baseUrl.endsWith('/') ? baseUrl : baseUrl + '/'
}
}
export class AbiFileCache extends AsyncFileCache {
constructor(basePath) {
super((name)=>{
return this.basePath+abiPath(name)
})
this.basePath = basePath.endsWith('/') ? basePath : basePath + '/'
}
}
const files = {
// If a contract is in a file different than its name, put the exception here
// 'IVaultLogic' : 'IVault', // for example
}
function abiPath(name) {
const file = files[name]
return `${file?file:name}.sol/${name}.json`
}