Files
server/abi.js

64 lines
1.9 KiB
JavaScript

import {ethers} from "ethers";
import {readFile} from "./misc.js";
const ABI_BASE_PATH = '../contract/out'
export const erc20Abi = [
'function name() view returns (string)',
'function symbol() view returns (string)',
'function decimals() view returns (uint8)',
'function totalSupply() view returns (uint256)',
'function balanceOf(address) view returns (uint256)',
'function transfer(address,uint256) returns (bool)',
'function transferFrom(address,address,uint256) returns (bool)',
'function approve(address,uint256) returns (bool success)',
'function allowance(address,address) view returns (uint256)',
'event Transfer(address indexed,address indexed,uint256)',
'event Approval(address indexed,address indexed,uint256)',
]
export const mockErc20Abi = [
...erc20Abi,
'function mint(address,uint256)',
]
const factoryAbi = [
'function deployVault(address owner, uint8 num) returns (address vault)',
'event VaultCreated( address deployer, address owner )',
]
export const abi = {
'ERC20': erc20Abi,
'Factory': factoryAbi,
}
export async function getAbi(className) {
let found = abi[className]
if (found === undefined) {
console.log('warning: loading ABI from filesystem for '+className)
let data = await readFile(ABI_BASE_PATH + `/${className}.sol/${className}.json`)
// Tricky code to handle proxy Vault where interface file is needed instead of class file
if (className == 'Vault') {
try {
data = await readFile(ABI_BASE_PATH + `/I${className}.sol/I${className}.json`)
}
catch (e) {
if (e.code !== 'ENOENT') throw e;
}
}
found = JSON.parse(data.toString())['abi']
abi[className] = found
}
return found
}
export async function getInterface(className) {
return new ethers.Interface(await getAbi(className))
}