50 lines
1.2 KiB
JavaScript
50 lines
1.2 KiB
JavaScript
import {AsyncAbiCache, abiPath} from "../web/src/common.js"
|
|
import {ethers} from "ethers";
|
|
import fs from "fs";
|
|
|
|
const ABI_BASE_URL = '../contract/out/'
|
|
|
|
|
|
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 AbiFileCache extends AsyncFileCache {
|
|
constructor(basePath) {
|
|
super((name)=>{
|
|
return this.basePath+abiPath(name)
|
|
})
|
|
this.basePath = basePath.endsWith('/') ? basePath : basePath + '/'
|
|
}
|
|
}
|
|
|
|
|
|
const abiCache = new AbiFileCache(ABI_BASE_URL)
|
|
|
|
|
|
export async function getAbi(className) {
|
|
return await abiCache.get(className)
|
|
}
|
|
|
|
|
|
export async function newContract(addr, name, provider) {
|
|
const abi = await getAbi(name);
|
|
return new ethers.Contract(addr, abi, provider)
|
|
}
|
|
|
|
export async function mockERC20Contract(addr, provider) {
|
|
return await newContract(addr, 'MockERC20', provider)
|
|
}
|
|
|
|
export async function erc20Contract(addr, provider) {
|
|
return await newContract(addr, 'IERC20Metadata', provider)
|
|
}
|