Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Full Typescript Support #12

Draft
wants to merge 11 commits into
base: main
Choose a base branch
from
25 changes: 16 additions & 9 deletions src/index.js → src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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);
}
Expand All @@ -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 = {};
}
Expand Down Expand Up @@ -291,7 +298,7 @@ export class FIAT {
}

// collateralTypesFilter: [{ vault: Address, tokenId: Number }]
async fetchCollateralTypes(collateralTypesFilter) {
async fetchCollateralTypes(collateralTypesFilter: CollateralTypesFilter) : Promise<CollateralTypes> {
const graphData = await this.query(
queryCollateralTypes,
(collateralTypesFilter && collateralTypesFilter.length !== 0)
Expand Down Expand Up @@ -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<CollateralTypes> {
const collateralTypes = (collateralTypesFilter && collateralTypesFilter.length)
? collateralTypesFilter
: Object.keys(this.metadata).reduce((collateralTypes_, vault) => (
Expand Down Expand Up @@ -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<UserData[]> {
const graphData = await Promise.all([
this.query(queryUser, { id: userOrProxyOwner.toLowerCase() }),
this.query(queryUserProxies, { where: { owner: userOrProxyOwner.toLowerCase() } })
Expand All @@ -452,7 +459,7 @@ export class FIAT {
collateral: ethers.BigNumber.from(position.collateral),
normalDebt: ethers.BigNumber.from(position.normalDebt)
}))
}));
} as UserData ));
}

// collateral in WAD
Expand Down
117 changes: 117 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
import { BigNumber } from "ethers";

export type CollateralTypesFilter = Array<{
vault: string;
tokenId: number;
}>;

export type CollateralTypes = Array<CollateralType>;

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;
}
1 change: 1 addition & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: [] },
Expand Down
5 changes: 4 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
"declarationDir": "lib",
"allowJs": true,
"pretty": true,
"target": "es5"
"target": "es2015",
"resolveJsonModule": true,
"moduleResolution": "node",
"esModuleInterop": true
},
"include": ["./src/**/*"],
"exclude": [
Expand Down