Skip to content

Commit

Permalink
fix: export some missing types + more data queries (#64)
Browse files Browse the repository at this point in the history
* export missing data types
* add snxHolders({addresses}) option
* add accountsFlaggedForLiquidation data query
* add synthHolders data query
* fix SnxHolderParams#addresses type
* fix SnxHoldersParams.addresses option
* more missing exports
  • Loading branch information
kelonye authored Aug 26, 2021
1 parent 446729b commit 610a424
Show file tree
Hide file tree
Showing 28 changed files with 1,657 additions and 12,420 deletions.
13,757 changes: 1,386 additions & 12,371 deletions package-lock.json

Large diffs are not rendered by default.

5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,5 +56,8 @@
"chokidar": "^3.5.2",
"fsevents": "^2.3.2",
"watchpack-chokidar2": "^2.0.1"
},
"dependencies": {
"bootstrap": "^5.1.0"
}
}
}
2 changes: 1 addition & 1 deletion packages/contracts-interface/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions packages/data/__mocks__/accountsFlaggedForLiquidation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
export const accountsFlaggedForLiquidationMock = {
response: {
account: '0x3239f49d5298613eb4dbf99b37ce795f3159dcd3',
collateral: '21719769773274085136404',
collateralRatio: '502838404148398413',
deadline: '1604580918',
id: '1604580918-0x3239f49d5298613eb4dbf99b37ce795f3159dcd3',
liquidatableNonEscrowSNX: '1738257073391178186056',
},
formatted: {
account: '0x3239f49d5298613eb4dbf99b37ce795f3159dcd3',
collateral: 21719.769773274085,
collateralRatio: 0.5028384041483984,
deadline: 1604580918000,
liquidatableNonEscrowSNX: 1738.2570733911782,
},
};
4 changes: 4 additions & 0 deletions packages/data/__mocks__/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,7 @@ export * from './binaryOptionsMarkets';
export * from './binaryOptionsTransactions';
export * from './exchangeTotals';
export * from './dailyTotalActiveStakers';
export * from './accountsFlaggedForLiquidation';
export * from './dailyBurned';
export * from './dailyIssued';
export * from './synthHolders';
12 changes: 12 additions & 0 deletions packages/data/__mocks__/synthHolders.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export const synthHoldersMock = {
response: {
balanceOf: '90038854631941946067435170',
id: '0x6c5024cd4f8a59110119c56f8933403a539555eb-sUSD',
synth: 'sUSD',
},
formatted: {
balanceOf: 90038854.63194194,
address: '0x6c5024cd4f8a59110119c56f8933403a539555eb',
synth: 'sUSD',
},
};
47 changes: 45 additions & 2 deletions packages/data/__tests__/queries.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import {
parseBinaryOptionTransactions,
parseDailyTotalActiveStakers,
parseExchangeTotals,
parseAccountsFlaggedForLiquidation,
parseSynthHolders,
} from '../queries';
import synthetixData, { calculateTimestampForPeriod, PERIOD_IN_HOURS } from '../src';
import { SynthetixData } from '../src/types';
Expand All @@ -39,9 +41,11 @@ import {
binaryOptionsTransactionsMock,
exchangeTotalsMock,
dailyTotalActiveStakersMock,
accountsFlaggedForLiquidationMock,
dailyBurnedMock,
dailyIssuedMock,
synthHoldersMock,
} from '../__mocks__';
import { dailyBurnedMock } from '../__mocks__/dailyBurned';
import { dailyIssuedMock } from '../__mocks__/dailyIssued';

describe('@synthetixio/data tests', () => {
const randomLargeSNXStaker = '0x042ed37d32b88ab6b1c2e7b8a400dcdc728050bc';
Expand Down Expand Up @@ -348,6 +352,15 @@ describe('@synthetixio/data tests', () => {
expect(Number(snxHoldersInfo![0].balanceOf)).toBeGreaterThan(0);
expect(snxHoldersInfo!.length).toEqual(5);
});

test('should accept addresses prop', async () => {
const snxHoldersInfo = await snxData.snxHolders({
addresses: ['0x49be88f0fcc3a8393a59d3688480d7d253c37d2a'],
});
expect(Number(snxHoldersInfo![0].collateral)).toBeGreaterThan(0);
expect(Number(snxHoldersInfo![0].balanceOf)).toBeGreaterThan(0);
expect(snxHoldersInfo!.length).toEqual(1);
});
});

describe('exchangeEntrySettleds query', () => {
Expand Down Expand Up @@ -436,4 +449,34 @@ describe('@synthetixio/data tests', () => {
expect(total.id).toBeGreaterThanOrEqual(1);
});
});

describe('accountsFlaggedForLiquidation query', () => {
test('should parse the response correctly', () => {
const parsedOutput = parseAccountsFlaggedForLiquidation(
accountsFlaggedForLiquidationMock.response
);
expect(accountsFlaggedForLiquidationMock.formatted).toEqual(parsedOutput);
});

test('should accept fiter options', async () => {
const accounts = await snxData.accountsFlaggedForLiquidation({ max: 100 });
expect(accounts.length).toBeGreaterThanOrEqual(1);
const account = accounts[0]!;
expect(account.deadline).toBeGreaterThanOrEqual(1);
});
});

describe('synthHolders query', () => {
test('should parse the response correctly', () => {
const parsedOutput = parseSynthHolders(synthHoldersMock.response);
expect(synthHoldersMock.formatted).toEqual(parsedOutput);
});

test('should accept fiter options', async () => {
const holders = await snxData.synthHolders({ max: 100 });
expect(holders.length).toBeGreaterThanOrEqual(1);
const holder = holders[0]!;
expect(holder.balanceOf).toBeGreaterThanOrEqual(1);
});
});
});
2 changes: 1 addition & 1 deletion packages/data/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions packages/data/queries/accountsFlaggedForLiquidation/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './query';
export * from './parse';
17 changes: 17 additions & 0 deletions packages/data/queries/accountsFlaggedForLiquidation/parse.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { AccountFlaggedForLiquidation as UnformattedAccountFlaggedForLiquidation } from '../../generated/graphql';
import { AccountFlaggedForLiquidation } from '../../src/types';
import { formatEther, formatTimestamp } from '../../src/utils';

export const parseAccountsFlaggedForLiquidation = ({
deadline,
account,
collateralRatio,
liquidatableNonEscrowSNX,
collateral,
}: UnformattedAccountFlaggedForLiquidation): AccountFlaggedForLiquidation => ({
deadline: formatTimestamp(deadline),
account,
collateral: Number(formatEther(collateral)),
collateralRatio: Number(formatEther(collateralRatio)),
liquidatableNonEscrowSNX: Number(formatEther(liquidatableNonEscrowSNX)),
});
32 changes: 32 additions & 0 deletions packages/data/queries/accountsFlaggedForLiquidation/query.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { gql } from 'graphql-request';
import { AccountsFlaggedForLiquidationParams } from '../../src/types';
import { createGQLWhereString, createGQLBlockNumberString } from '../../src/utils';

export const createAccountsFlaggedForLiquidationQuery = (
params?: AccountsFlaggedForLiquidationParams
): string => {
const whereString = createGQLWhereString(
Object.entries({
account: (params?.account ?? null) != null ? 'account' : null,
deadline_gte: (params?.minTimestamp ?? null) != null ? 'minTimestamp' : null,
deadline_lte: (params?.maxTimestamp ?? null) != null ? 'maxTimestamp' : null,
})
);

return gql`
query accountFlaggedForLiquidations($max: Int, $account: String, $minTimestamp: Int, $maxTimestamp: Int) {
accountFlaggedForLiquidations(
first: $max${createGQLBlockNumberString(params?.blockNumber ?? null)}
where: ${whereString}
orderBy: deadline
orderDirection: asc
) {
deadline
account
collateralRatio
liquidatableNonEscrowSNX
collateral
}
}
`;
};
1 change: 1 addition & 0 deletions packages/data/queries/exchangeTotals/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { createGQLBlockNumberString } from '../../src/utils';
export const ENTITY_MAP: Record<string, string> = {
'1d': 'dailyTotals',
'15m': 'fifteenMinuteTotals',
all: 'totals',
};

export const createExchangeTotalsQuery = (params: ExchangeTotalsParams): string => {
Expand Down
5 changes: 5 additions & 0 deletions packages/data/queries/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,8 @@ export {
parseExchangeTotals,
getExchangeTotalsQueryResponseAttr,
} from './exchangeTotals';
export {
createAccountsFlaggedForLiquidationQuery,
parseAccountsFlaggedForLiquidation,
} from './accountsFlaggedForLiquidation';
export { createSynthHoldersQuery, parseSynthHolders } from './synthHolders';
3 changes: 2 additions & 1 deletion packages/data/queries/snxHolders/snxHolders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,12 @@ export const createSnxHolderQuery = (params?: SnxHolderParams): string => {
(params?.autoGeneratedPaginationField ?? null) != null
? 'autoGeneratedPaginationField'
: null,
id_in: (params?.addresses ?? null) != null ? 'addresses' : null,
})
);

return gql`
query snxholders($autoGeneratedPaginationField: Int, $max: Int, $minCollateral: Int, $minClaims: Int, $minMints: Int, $maxCollateral: Int, $address: String) {
query snxholders($autoGeneratedPaginationField: Int, $max: Int, $minCollateral: Int, $minClaims: Int, $minMints: Int, $maxCollateral: Int, $addresses: [String]) {
snxholders(
first: $max${createGQLBlockNumberString(params?.blockNumber ?? null)}
where: ${whereString}
Expand Down
2 changes: 2 additions & 0 deletions packages/data/queries/synthHolders/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './query';
export * from './parse';
13 changes: 13 additions & 0 deletions packages/data/queries/synthHolders/parse.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { SynthHolder as UnformattedSynthHolder } from '../../generated/graphql';
import { SynthHolder } from '../../src/types';
import { formatEther } from '../../src/utils';

export const parseSynthHolders = ({
id,
synth,
balanceOf,
}: UnformattedSynthHolder): SynthHolder => ({
address: id.split('-')[0],
synth,
balanceOf: Number(formatEther(balanceOf)),
});
27 changes: 27 additions & 0 deletions packages/data/queries/synthHolders/query.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { gql } from 'graphql-request';
import { SynthHolderParams } from '../../src/types';
import { createGQLWhereString, createGQLBlockNumberString } from '../../src/utils';

export const createSynthHoldersQuery = (params?: SynthHolderParams): string => {
const whereString = createGQLWhereString(
Object.entries({
id: (params?.id ?? null) != null ? 'id' : null,
synth: (params?.synth ?? null) != null ? 'synth' : null,
})
);

return gql`
query synthHolders($max: Int, $id: String, $synth: String) {
synthHolders(
first: $max${createGQLBlockNumberString(params?.blockNumber ?? null)}
where: ${whereString}
orderBy: balanceOf
orderDirection: desc
) {
id
synth
balanceOf
}
}
`;
};
43 changes: 42 additions & 1 deletion packages/data/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ import {
DailyTotalActiveStakersParams,
ExchangeTotals,
ExchangeTotalsParams,
AccountsFlaggedForLiquidationParams,
AccountFlaggedForLiquidation,
SynthHolderParams,
SynthHolder,
} from './types';
import {
Synthetix,
Expand Down Expand Up @@ -301,10 +305,47 @@ const synthetixData = ({ networkId }: { networkId: NetworkId }) => ({
? response.totalDailyActiveStakers.map(queries.parseDailyTotalActiveStakers)
: null;
},
accountsFlaggedForLiquidation: async (
params?: AccountsFlaggedForLiquidationParams
): Promise<AccountFlaggedForLiquidation[] | null> => {
const response = await getData({
params,
queryMethod: queries.createAccountsFlaggedForLiquidationQuery,
networkId,
endpoints: {
[NetworkId.Mainnet]: l1Endpoints.liquidations,
},
});
return response != null
? response.accountFlaggedForLiquidations.map(queries.parseAccountsFlaggedForLiquidation)
: null;
},
synthHolders: async (params?: SynthHolderParams): Promise<SynthHolder[] | null> => {
const response = await getData({
params,
queryMethod: queries.createSynthHoldersQuery,
networkId,
endpoints: {
[NetworkId.Mainnet]: l1Endpoints.snx,
},
});
return response != null ? response.synthHolders.map(queries.parseSynthHolders) : null;
},
});

type SynthetixData = ReturnType<typeof synthetixData>;

export { Period, PERIOD_IN_HOURS, calculateTimestampForPeriod, l1Endpoints, l2Endpoints };
export type { SynthetixData };
export type {
SynthetixData,
SynthExchangeExpanded,
ExchangeTotals,
OptionsMarket,
OptionsTransaction,
DailyTotalActiveStakers,
AccountFlaggedForLiquidation,
SynthHolder,
DailyIssued,
DailyBurned,
};
export default synthetixData;
26 changes: 26 additions & 0 deletions packages/data/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ export type SnxHolderParams = {
address?: string;
minMints?: number;
minClaims?: number;
addresses?: string[];
} & BaseQueryParams;

export type RateUpdateQueryParams = {
Expand Down Expand Up @@ -91,6 +92,17 @@ export type ExchangeTotalsParams = {
timeSeries: string;
} & BaseQueryParams;

export type AccountsFlaggedForLiquidationParams = {
account?: string;
minTimestamp?: number;
maxTimestamp?: number;
} & BaseQueryParams;

export type SynthHolderParams = {
id?: string;
synth?: string;
} & BaseQueryParams;

/**
* Shorts have many relationships between entities although we are not taking advantage
* of all of them so we are removing the types we don't use
Expand Down Expand Up @@ -142,3 +154,17 @@ export type ExchangeTotals = {
exchangeUSDTally: number;
totalFeesGeneratedInUSD: number;
};

export type AccountFlaggedForLiquidation = {
deadline: number;
account: string;
collateral: number;
collateralRatio: number;
liquidatableNonEscrowSNX: number;
};

export type SynthHolder = {
address: string;
synth: string;
balanceOf: number;
};
Loading

0 comments on commit 610a424

Please sign in to comment.