diff --git a/src/index.js b/src/index.ts similarity index 96% rename from src/index.js rename to src/index.ts index 4928f0b..99ae4ef 100644 --- a/src/index.js +++ b/src/index.ts @@ -24,6 +24,7 @@ import Publican from 'changelog/abis/Publican.sol/Publican.json'; import VaultEPTActions from 'changelog/abis/VaultEPTActions.sol/VaultEPTActions.json'; import VaultFCActions from 'changelog/abis/VaultFCActions.sol/VaultFCActions.json'; import VaultFYActions from 'changelog/abis/VaultFYActions.sol/VaultFYActions.json'; +import { CollateralTypes, CollateralType, CollateralTypesFilter, UserData } from './types'; import VaultSPTActions from 'changelog/abis/VaultSPTActions.sol/VaultSPTActions.json'; import { @@ -89,6 +90,13 @@ export * from './queries'; // all number values are generally expected as ethers.BigNumber unless they come from the subgraph directly export class FIAT { + signer; + provider; + ethcallProvider; + gasMultiplier; + subgraphUrl; + addresses; + metadata; constructor(signer, provider, chainId, opts) { // supported networks: 1 - Mainnet, 5 - Goerli, 1337 - Ganache @@ -133,7 +141,6 @@ export class FIAT { #getContract(artifact, address) { const contract = new ethers.ContractFactory(artifact.abi, artifact.bytecode, this.signer).attach(address); - contract.abi = artifact.abi; return contract; } @@ -178,7 +185,7 @@ export class FIAT { async multicall(calls) { const multicall = calls.map(({ contract, method, args }) => { - return (new EthCallContract(contract.address, contract.abi))[method](...args); + return (new EthCallContract(contract.address, contract.interface.fragments))[method](...args); }); return await this.ethcallProvider.all(multicall); } @@ -193,7 +200,7 @@ export class FIAT { let txOpts = txRequest[txRequest.length - 1]; if (txOpts && Object.getPrototypeOf(txOpts) === Object.prototype) { if (txOpts.from) contract = contract.connect(new ethers.VoidSigner(txOpts.from, this.provider)); - delete txRequest.splice([txRequest.length - 1], 1); + txRequest.splice(-1, 1); } else { txOpts = {}; } @@ -291,7 +298,7 @@ export class FIAT { } // collateralTypesFilter: [{ vault: Address, tokenId: Number }] - async fetchCollateralTypes(collateralTypesFilter) { + async fetchCollateralTypes(collateralTypesFilter: CollateralTypesFilter) : Promise { const graphData = await this.query( queryCollateralTypes, (collateralTypesFilter && collateralTypesFilter.length !== 0) @@ -367,12 +374,12 @@ export class FIAT { discountRate: (collateralType.discountRate) ? ethers.BigNumber.from(collateralType.discountRate.discountRate) : null, } } - }; + } as CollateralType; }); } // collateralTypesFilter: [{ vault: Address, tokenId: Number }] - async fetchCollateralTypesAndPrices(collateralTypesFilter) { + async fetchCollateralTypesAndPrices(collateralTypesFilter: CollateralTypesFilter) : Promise { const collateralTypes = (collateralTypesFilter && collateralTypesFilter.length) ? collateralTypesFilter : Object.keys(this.metadata).reduce((collateralTypes_, vault) => ( @@ -423,12 +430,12 @@ export class FIAT { faceValue: prices.faceValue } } - }; + } as CollateralType; }); } // user: either owner of a Position or owner of a Proxy which is the owner of a Position - async fetchUserData(userOrProxyOwner) { + async fetchUserData(userOrProxyOwner: string) : Promise { const graphData = await Promise.all([ this.query(queryUser, { id: userOrProxyOwner.toLowerCase() }), this.query(queryUserProxies, { where: { owner: userOrProxyOwner.toLowerCase() } }) @@ -452,7 +459,7 @@ export class FIAT { collateral: ethers.BigNumber.from(position.collateral), normalDebt: ethers.BigNumber.from(position.normalDebt) })) - })); + } as UserData )); } // collateral in WAD diff --git a/src/types.ts b/src/types.ts new file mode 100644 index 0000000..4ab4dc3 --- /dev/null +++ b/src/types.ts @@ -0,0 +1,117 @@ +import { BigNumber } from "ethers"; + +export type CollateralTypesFilter = Array<{ + vault: string; + tokenId: number; +}>; + +export type CollateralTypes = Array; + +export interface CollateralType { + properties: Properties; + metadata: Metadata; + settings: Settings; + state: State; +} +export interface Properties { + vault: string; + vaultType: string; + name: string; + protocol: string; + token: string; + tokenId: string; + tokenScale: BigNumber; + tokenSymbol: string; + underlierToken: string; + underlierScale: BigNumber; + underlierSymbol: string; + maturity: BigNumber; + eptData?: EptData; + fcData?: FcData; + fyData?: FyData; + sptData?: SptData; +} + +export interface EptData { + balancerVault: string; + convergentCurvePool: string; + poolId: string; +} + +export interface FcData { + notional: string; + tenor: string; +} + +export interface FyData { + yieldSpacePool: string; +} + +export interface SptData { + adapter: string; + balancerVault: string; + maturity: string; + spacePool: string; + target: string; +} + +export interface Metadata { + protocol: string; + asset: string; + symbol: string; + decimals: number; + name: string; + tokenIds?: (string)[] | null; + icons: Icons; + urls: Urls; +} + +export interface Icons { + protocol: string; + asset: string; + underlier: string; +} + +export interface Urls { + project: string; + asset: string; +} + +export interface Settings { + codex: any; + collybus: any; + limes: any; + collateralAuction: any; +} + +export interface State { + codex: any; + limes: any; + publican: any; + collybus: any; +} + +export interface UserData { + balances?: Balances[]; + credit: BigNumber; + delegated?: any[]; + delegates?: any[]; + isProxy: boolean; + positions?: Positions[] | null; + unbackedDebt: BigNumber; + user: string; +} + +export interface Balances { + collateralType: any; + balance: BigNumber; +} + +export interface Positions { + owner: string; + vault: string; + token: string; + tokenId: BigNumber; + collateral: BigNumber; + normalDebt: BigNumber; +} diff --git a/test/test.js b/test/test.js index 6f6840d..c490cb9 100644 --- a/test/test.js +++ b/test/test.js @@ -142,6 +142,7 @@ describe('FIAT', () => { }); test('multicall', async () => { + console.log('contract codex') const results = await fiat.multicall([ { contract: contracts.codex, method: 'globalDebt', args: [] }, { contract: contracts.codex, method: 'globalUnbackedDebt', args: [] }, diff --git a/tsconfig.json b/tsconfig.json index 3282e21..d993fea 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -5,7 +5,10 @@ "declarationDir": "lib", "allowJs": true, "pretty": true, - "target": "es5" + "target": "es2015", + "resolveJsonModule": true, + "moduleResolution": "node", + "esModuleInterop": true }, "include": ["./src/**/*"], "exclude": [