Created
October 19, 2021 08:36
-
-
Save sc0Vu/ebfe4c49c8458bfdee67201413a1397b to your computer and use it in GitHub Desktop.
AAVE APR
This file contains hidden or 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
import { fetchMultiReserveInfo, DECIMAL, RAY, YEAR_TO_SECONDS, toBN } from "./subgraph" | |
const main = async () => { | |
const ids = [ | |
"0x6b175474e89094c44da98b954eedeac495271d0f0xb53c1a33016b2dc2ff3653530bff1848a515c8c5", | |
"0x7fc66500c84a76ad7e9c93437bfc5ac33e2ddae90xb53c1a33016b2dc2ff3653530bff1848a515c8c5" | |
] | |
const rewardToken = "0x7fc66500c84a76ad7e9c93437bfc5ac33e2ddae9" | |
const { reserves } = (await fetchMultiReserveInfo(ids)) | |
const reward = reserves.filter((r: Record<string, any>) => r.underlyingAsset === rewardToken)[0] | |
return reserves.filter((r: Record<string, any>) => r.underlyingAsset !== rewardToken) | |
.map((r: Record<string, any>) => { | |
let totalVariableDebt = toBN(r.variableBorrowIndex).multipliedBy(toBN(r.totalScaledVariableDebt)).dividedBy(DECIMAL).dividedBy(RAY) | |
let totalStableDebt = toBN(r.totalPrincipalStableDebt).dividedBy(DECIMAL) | |
let availableLiquidity = toBN(r.availableLiquidity).dividedBy(DECIMAL) | |
let totalLiquidity = totalVariableDebt.plus(totalStableDebt).plus(availableLiquidity) | |
let rewardPrice = toBN(reward.price.priceInEth) | |
let tokenPrice = toBN(r.price.priceInEth) | |
let emissionRewards = toBN(r.aEmissionPerSecond).multipliedBy(YEAR_TO_SECONDS).multipliedBy(rewardPrice).dividedBy(DECIMAL) | |
let total = totalLiquidity.multipliedBy(tokenPrice) | |
return emissionRewards.dividedBy(total).toString() | |
}) | |
} | |
main() | |
.then(console.log) | |
.catch(console.error) |
This file contains hidden or 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
{ | |
"name": "aaveapr", | |
"version": "1.0.0", | |
"description": "only for test", | |
"main": "aave.ts", | |
"keywords": [], | |
"author": "", | |
"license": "", | |
"dependencies": { | |
"@typescript-eslint/eslint-plugin": "^5.1.0", | |
"@typescript-eslint/parser": "^5.1.0", | |
"abort-controller": "^3.0.0", | |
"bignumber.js": "^9.0.1", | |
"graphql": "^15.6.1", | |
"graphql-request": "^3.6.1", | |
"typescript": "^4.4.4" | |
} | |
} |
This file contains hidden or 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
import { GraphQLClient, gql } from 'graphql-request' | |
import BN from 'bignumber.js' | |
import AbortController from 'abort-controller' | |
const AAVE_THE_GRAPH = 'https://api.thegraph.com/subgraphs/name/aave/protocol-v2' | |
export const DECIMAL = new BN(10 ** 18) | |
export const RAY = new BN(10 ** 27) | |
export const YEAR_TO_SECONDS = new BN(31536000) | |
const timeout = 2 * 60 * 1000 | |
const request = async (url: string, query: any, variables: any = {}) => { | |
const controller = new AbortController(); | |
const timeoutId = setTimeout(() => { | |
controller.abort() | |
}, timeout) | |
const client = new GraphQLClient(url, { | |
signal: controller.signal | |
}) | |
try { | |
const resp = await client.request(query, variables) | |
clearTimeout(timeoutId) | |
return resp | |
} catch (err) { | |
clearTimeout(timeoutId) | |
throw err | |
} | |
} | |
export const fetchMultiReserveInfo = async (reserveIds: string[]) => { | |
const query = gql` | |
query QueryMultiReserveInfo( | |
$reserveIds: [ID!] | |
) { | |
reserves( | |
where: { | |
id_in: $reserveIds | |
} | |
) { | |
id | |
aEmissionPerSecond | |
underlyingAsset | |
price { | |
priceInEth | |
} | |
underlyingAsset | |
totalScaledVariableDebt | |
totalPrincipalStableDebt | |
variableBorrowIndex | |
stableBorrowRate | |
availableLiquidity | |
lastUpdateTimestamp | |
} | |
} | |
` | |
const resp = await request(AAVE_THE_GRAPH, query, { | |
reserveIds: reserveIds.map(reserveId => reserveId.toLowerCase()) | |
}) | |
return { | |
reserves: resp.reserves | |
} | |
} | |
export const toBN = (n: any): BN => { | |
return new BN(n) | |
} |
This file contains hidden or 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
Show hidden characters
{ | |
"compilerOptions": { | |
"baseUrl": "./", | |
"outDir": "dist", | |
"strictNullChecks": true, | |
"noImplicitAny": true, | |
"noImplicitThis": true, | |
"resolveJsonModule": true, | |
"declaration": true, | |
"declarationDir": "lib", | |
"experimentalDecorators": true, | |
"allowSyntheticDefaultImports": true, | |
"forceConsistentCasingInFileNames": true, | |
"esModuleInterop": true, | |
"moduleResolution": "node", | |
"pretty": true, | |
"lib": ["ESNext", "DOM"] | |
}, | |
"exclude": ["node_modules"] | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment