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

feat: new queries, increasing epoch close limit, fix link #250

Merged
merged 7 commits into from
Jun 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 101 additions & 0 deletions tinlake-ui/containers/DataQuery/dailyTVL.ts
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`))
}
116 changes: 116 additions & 0 deletions tinlake-ui/containers/DataQuery/dailyTokenPrices.ts
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`))
}
4 changes: 4 additions & 0 deletions tinlake-ui/containers/DataQuery/queries.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { assetList } from './assetList'
import { dailyTokenPrices } from './dailyTokenPrices'
import { dailyTVL } from './dailyTVL'
import { fortunaFiDailyInvestorBalances } from './fortunaFiDailyInvestorBalances'
import { poolList } from './poolList'

Expand All @@ -7,6 +9,8 @@ export type Query = () => Promise<void>
const queries: { [name: string]: Query } = {
'Pool list': poolList,
'Asset list': assetList,
'Daily TVL': dailyTVL,
'Daily token prices': dailyTokenPrices,
'FortunaFi Daily Investor Balances': fortunaFiDailyInvestorBalances,
}

Expand Down
2 changes: 1 addition & 1 deletion tinlake-ui/containers/Onboarding/InfoBox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ const InfoBox: React.FC<Props> = (props: Props) => {
require a minimum investment amount of 5000 DAI. Residents of some countries may also be excluded from investing.
<br />
<br />
<Anchor href="https://centrifuge.hackmd.io/QXUCI0wRSLepfABBivNruw?view" target="_blank">
<Anchor href="https://docs.centrifuge.io/use/invest/#onboarding-guide" target="_blank">
Read the onboarding guide
</Anchor>
<Anchor onClick={() => openModal()}>See list of excluded countries</Anchor>
Expand Down
2 changes: 1 addition & 1 deletion tinlake-ui/utils/export.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export const saveAsCSV = (loans: SortableLoan[]) => {
}

export const downloadCSV = (rows: any[], filename: string) => {
const csvContent = `data:text/csv;charset=utf-8,${rows.map((e) => e.join(',')).join('\n')}`
const csvContent = `data:text/csv;charset=utf-8,${rows.map((e) => e.join(';')).join('\n')}`
const encodedUri = encodeURI(csvContent)
const link = document.createElement('a')
link.setAttribute('href', encodedUri)
Expand Down
4 changes: 2 additions & 2 deletions tinlake.js/src/actions/coordinator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,15 +86,15 @@ export function CoordinatorActions<ActionsBase extends Constructor<TinlakeParams

closeEpoch = async () => {
const coordinator = this.contract('COORDINATOR')
return this.pending(coordinator.closeEpoch({ ...this.overrides, gasLimit: 3000000 }))
return this.pending(coordinator.closeEpoch({ ...this.overrides, gasLimit: 5000000 }))
}

solveEpoch = async () => {
const coordinator = this.contract('COORDINATOR')

if ((await coordinator.submissionPeriod()) === false) {
// The epoch is can be closed, but is not closed yet
const closeTx = await coordinator.closeEpoch({ ...this.overrides, gasLimit: 3000000 })
const closeTx = await coordinator.closeEpoch({ ...this.overrides, gasLimit: 5000000 })
const closeResult = await this.getTransactionReceipt(closeTx)

if (closeResult.status === 0) {
Expand Down