-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: new queries, increasing epoch close limit, fix link (#250)
* chore: increase gas limit for epoch close to 5M * feat: implement daily tvl query * fix: cleanup * feat: daily token prices query * fix: add missing pools * fix: remove console logs * fix: linter errors
- Loading branch information
Jeroen Offerijns
authored
Jun 15, 2021
1 parent
eabddf2
commit c82d875
Showing
6 changed files
with
225 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
import { baseToDisplay, toPrecision } from '@centrifuge/tinlake-js' | ||
import BN from 'bn.js' | ||
import gql from 'graphql-tag' | ||
import Apollo from '../../services/apollo' | ||
import { downloadCSV } from '../../utils/export' | ||
import { csvName } from './queries' | ||
|
||
interface DailyPoolData { | ||
day: { | ||
id: number | ||
} | ||
pool: { | ||
id: string | ||
} | ||
reserve: string | ||
assetValue: string | ||
} | ||
|
||
const fetch = async (skip: number, first: number, blockHash: string | null): Promise<any> => { | ||
return await Apollo.runCustomQuery(gql` | ||
{ | ||
dailyPoolDatas(first: ${first}, skip: ${skip} ${blockHash ? `, block: { hash: "${blockHash}" }` : ''}) { | ||
day { | ||
id | ||
} | ||
pool { | ||
id | ||
} | ||
reserve | ||
assetValue | ||
} | ||
_meta { | ||
block { | ||
hash | ||
number | ||
} | ||
} | ||
} | ||
`) | ||
} | ||
|
||
export async function dailyTVL() { | ||
let start = 0 | ||
const limit = 1000 | ||
|
||
const results: any[] = [] | ||
let blockHash: string | null = null | ||
let blockNumber: number | null = null | ||
|
||
// subgraph only returns 1000 entries, fetch until no more entries are returned | ||
while (true) { | ||
const response: any = await fetch(start, limit, blockHash) | ||
if (blockHash === null) { | ||
blockHash = response._meta.block.hash | ||
} | ||
if (blockNumber === null) { | ||
blockNumber = response._meta.block.number | ||
} | ||
results.push(...response.dailyPoolDatas) | ||
if (response.dailyPoolDatas.length < limit) { | ||
break | ||
} | ||
start += limit | ||
} | ||
|
||
const groupedResults: { [day: string]: DailyPoolData[] } = results.reduce( | ||
(output: { [day: string]: DailyPoolData[] }, input: DailyPoolData) => { | ||
const day = input.day.id.toString() | ||
if (day in output) return { ...output, [day]: [...output[day], input] } | ||
return { ...output, [day]: [input] } | ||
}, | ||
{} | ||
) | ||
|
||
const summedResults = Object.keys(groupedResults).map((day: string) => { | ||
const data = groupedResults[day].reduce( | ||
(sum: { day: string; reserve: BN; assetValue: BN }, input: DailyPoolData) => { | ||
return { | ||
day: sum.day, | ||
reserve: sum.reserve.add(new BN(input.reserve)), | ||
assetValue: sum.assetValue.add(new BN(input.assetValue)), | ||
} | ||
}, | ||
{ day, reserve: new BN(0), assetValue: new BN(0) } | ||
) | ||
return data | ||
}) | ||
|
||
const headers = ['Day', 'NAV', 'Reserve', 'TVL'] | ||
const rows: string[][] = [ | ||
headers, | ||
...summedResults.map((el: any) => [ | ||
el.day ? new Date(parseInt(el.day, 10) * 1000).toISOString().substr(0, 10) : '-', | ||
el.assetValue ? toPrecision(baseToDisplay(el.assetValue, 18), 0) : '-', | ||
el.reserve ? toPrecision(baseToDisplay(el.reserve, 18), 0) : '-', | ||
el.reserve && el.assetValue ? toPrecision(baseToDisplay(el.assetValue.add(el.reserve), 18), 0) : '-', | ||
]), | ||
] | ||
|
||
downloadCSV(rows, csvName(`Daily tvl`)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
import { baseToDisplay, toPrecision } from '@centrifuge/tinlake-js' | ||
import BN from 'bn.js' | ||
import gql from 'graphql-tag' | ||
import Apollo from '../../services/apollo' | ||
import { downloadCSV } from '../../utils/export' | ||
import { csvName } from './queries' | ||
|
||
interface DailyPoolData { | ||
day: { | ||
id: number | ||
} | ||
pool: { | ||
shortName: string | ||
} | ||
juniorTokenPrice: string | ||
seniorTokenPrice: string | ||
} | ||
|
||
const fetch = async (skip: number, first: number, blockHash: string | null): Promise<any> => { | ||
return await Apollo.runCustomQuery(gql` | ||
{ | ||
dailyPoolDatas(first: ${first}, skip: ${skip} ${blockHash ? `, block: { hash: "${blockHash}" }` : ''}) { | ||
day { | ||
id | ||
} | ||
pool { | ||
shortName | ||
} | ||
juniorTokenPrice | ||
seniorTokenPrice | ||
} | ||
_meta { | ||
block { | ||
hash | ||
number | ||
} | ||
} | ||
} | ||
`) | ||
} | ||
|
||
export async function dailyTokenPrices() { | ||
let start = 0 | ||
const limit = 1000 | ||
|
||
const results: any[] = [] | ||
let blockHash: string | null = null | ||
let blockNumber: number | null = null | ||
|
||
// subgraph only returns 1000 entries, fetch until no more entries are returned | ||
while (true) { | ||
const response: any = await fetch(start, limit, blockHash) | ||
if (blockHash === null) { | ||
blockHash = response._meta.block.hash | ||
} | ||
if (blockNumber === null) { | ||
blockNumber = response._meta.block.number | ||
} | ||
results.push(...response.dailyPoolDatas) | ||
if (response.dailyPoolDatas.length < limit) { | ||
break | ||
} | ||
start += limit | ||
} | ||
|
||
const groupedResults: { [day: string]: DailyPoolData[] } = results.reduce( | ||
(output: { [day: string]: DailyPoolData[] }, input: DailyPoolData) => { | ||
const day = input.day.id.toString() | ||
if (day in output) return { ...output, [day]: [...output[day], input] } | ||
return { ...output, [day]: [input] } | ||
}, | ||
{} | ||
) | ||
|
||
const tokens = results.reduce((pools: { [key: string]: BN }, data: DailyPoolData) => { | ||
if (!pools[`${data.pool.shortName} DROP`]) { | ||
return { | ||
...pools, | ||
[`${data.pool.shortName} DROP`]: new BN(10).pow(new BN(27)), | ||
[`${data.pool.shortName} TIN`]: new BN(10).pow(new BN(27)), | ||
} | ||
} | ||
return pools | ||
}, {}) | ||
|
||
const summedResults = Object.keys(groupedResults).map((day: string) => { | ||
const data: any = groupedResults[day].reduce( | ||
(sum: any, input: DailyPoolData) => { | ||
return { | ||
...sum, | ||
[`${input.pool.shortName} DROP`]: new BN(input.seniorTokenPrice).isZero() | ||
? new BN(10).pow(new BN(27)) | ||
: new BN(input.seniorTokenPrice), | ||
[`${input.pool.shortName} TIN`]: new BN(input.juniorTokenPrice).isZero() | ||
? new BN(10).pow(new BN(27)) | ||
: new BN(input.juniorTokenPrice), | ||
} | ||
}, | ||
{ day, ...tokens } | ||
) | ||
return data | ||
}) | ||
|
||
const headers = ['Day', ...Object.keys(tokens)] | ||
const rows: string[][] = [ | ||
headers, | ||
...summedResults.map((el: any) => [ | ||
el.day ? new Date(parseInt(el.day, 10) * 1000).toISOString().substr(0, 10) : '-', | ||
...Object.keys(tokens).map((token: string) => { | ||
return el[token] ? toPrecision(baseToDisplay(el[token], 27), 8) : '0' | ||
}), | ||
]), | ||
] | ||
|
||
downloadCSV(rows, csvName(`Daily token prices`)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters