Skip to content

Commit

Permalink
feat: Added PoolTogether V5 TVL (#7828)
Browse files Browse the repository at this point in the history
* Cleaned up gitignore (why was this here?)

* Refactored PT V3 and V4

* Added V5 TVL

* code refactor

---------

Co-authored-by: llama <[email protected]>
  • Loading branch information
Ncookiez and llama authored Oct 25, 2023
1 parent 13ab4ea commit decd07a
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 151 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,4 @@ historical-data.js
/.idea
yarn.lock
.DS_Store
projects/pooltogether/index.js
.vscode
8 changes: 6 additions & 2 deletions projects/pooltogether/abi.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
{
"accountedBalance": "uint256:accountedBalance"
}
"accountedBalance": "uint256:accountedBalance",
"totalVaults": "uint256:totalVaults",
"allVaults": "function allVaults(uint256) view returns (address)",
"asset": "address:asset",
"totalAssets": "uint256:totalAssets"
}
164 changes: 16 additions & 148 deletions projects/pooltogether/index.js
Original file line number Diff line number Diff line change
@@ -1,153 +1,21 @@
const ADDRESSES = require('../helper/coreAssets.json')
const sdk = require("@defillama/sdk");
const { request, gql } = require("graphql-request");
const abi = require('./abi.json')
const { getChainTransform } = require("../helper/portedTokens");
const { getBlock } = require("../helper/http");
const { sumTokens } = require("../helper/unwrapLPs");
const { default: BigNumber } = require("bignumber.js");

const graphUrls = ['https://api.thegraph.com/subgraphs/name/pooltogether/pooltogether-v3_1_0',
'https://api.thegraph.com/subgraphs/name/pooltogether/pooltogether-v3_3_2',
'https://api.thegraph.com/subgraphs/name/pooltogether/pooltogether-v3_3_8',
'https://api.thegraph.com/subgraphs/name/pooltogether/pooltogether-v3_4_3']
const celoGraphUrl = 'https://api.thegraph.com/subgraphs/name/pooltogether/celo-v3_4_5'
const bscGraphUrl = 'https://api.thegraph.com/subgraphs/name/pooltogether/bsc-v3_4_3'

const graphQuery = gql`
query GET_POOLS($block: Int) {
prizePools{
id
underlyingCollateralSymbol
underlyingCollateralToken
compoundPrizePool{
cToken
}
}
}
`;

const v4pools={
ethereum:[
["0xbcca60bb61934080951369a648fb03df4f96263c", "0x32e8d4c9d1b711bc958d0ce8d14b41f77bb03a64"]
],
polygon:[
["0x1a13f4ca1d028320a707d99520abfefca3998b7f", "0xD4F6d570133401079D213EcF4A14FA0B4bfB5b9C"]
],
avax:[
['0x46a51127c3ce23fb7ab1de06226147f446e4a857', '0x7437db21A0dEB844Fa64223e2d6Db569De9648Ff']
],
optimism:[
['0x625E7708f30cA75bfd92586e17077590C60eb4cD', '0x4ecB5300D9ec6BCA09d66bfd8Dcb532e3192dDA1']
]
}

async function getChainBalances(allPrizePools, chain, block, transform) {
const balances = {};
transform = transform || (addr=>`${chain}:${addr}`);
const lockedTokens = await sdk.api.abi.multiCall({
abi: abi['accountedBalance'],
calls: allPrizePools.map(pool => ({
target: pool.id
})),
block,
chain
})
lockedTokens.output.forEach(call => {
const underlyingToken = transform(allPrizePools.find(pool =>
pool.id === call.input.target).underlyingCollateralToken);
const underlyingTokenBalance = ((underlyingToken.includes('0x')) ?
call.output : call.output / 10 ** 18)
sdk.util.sumSingleBalance(balances, underlyingToken, BigNumber(underlyingTokenBalance).toFixed(0))
})
if(v4pools[chain]!== undefined){
await sumTokens(balances, v4pools[chain], block, chain, transform)
}
return balances
}

async function eth(timestamp, block) {
let allPrizePools = []
let combinedPrizePools = []
for (const graphUrl of graphUrls) {
const { prizePools } = await request(
graphUrl,
graphQuery,
{
block,
}
);
allPrizePools = allPrizePools.concat(prizePools)
}
combinedPrizePools = allPrizePools.flat()
allPrizePools = [...new Set(combinedPrizePools.map(a => JSON.stringify(a)))].map(a => JSON.parse(a))
return getChainBalances(allPrizePools, 'ethereum', block, addr=>{
if(addr === "0x04f2694c8fcee23e8fd0dfea1d4f5bb8c352111f") return "0x383518188c0c6d7730d91b2c03a03c837814a899" // OHM
return addr
})
}

async function polygon(timestamp, block, chainBlocks) {
return getChainBalances([{
id: "0x887E17D791Dcb44BfdDa3023D26F7a04Ca9C7EF4",
underlyingCollateralToken: ADDRESSES.ethereum.USDT
},
{
id: "0xee06abe9e2af61cabcb13170e01266af2defa946",
underlyingCollateralToken: ADDRESSES.ethereum.USDC
}], 'polygon', chainBlocks.polygon)
}

async function avax(timestamp, block, chainBlocks) {
return getChainBalances([], 'avax', chainBlocks.avax, ()=>`avax:0xa7d7079b0fead91f3e65f86e8915cb59c1a4c664`)
}

async function optimism(timestamp, block, chainBlocks) {
return getChainBalances([], 'optimism', chainBlocks.optimism, ()=>`optimism:0x7F5c764cBc14f9669B88837ca1490cCa17c31607`)
}

async function celo(timestamp, block, chainBlocks) {
const transform = await getChainTransform('celo')
let allPrizePools = []
block = chainBlocks.celo
const { prizePools } = await request(
celoGraphUrl, graphQuery, { block })
allPrizePools = allPrizePools.concat(prizePools)
return getChainBalances(allPrizePools, 'celo', block, transform)
}

async function bsc(timestamp, _, chainBlocks) {
const transform = await getChainTransform('bsc')
let allPrizePools = []
const blockG = await getBlock(timestamp, 'bsc', chainBlocks) - 1000
const { prizePools } = await request(bscGraphUrl, graphQuery, { block: blockG })
allPrizePools = allPrizePools.concat(prizePools)
return getChainBalances(allPrizePools, 'bsc', chainBlocks.bsc, transform)
}
const v3 = require('./v3.js').tvl
const v4 = require('./v4.js').tvl
const v5 = require('./v5.js').tvl
const sdk = require('@defillama/sdk')

module.exports = {
doublecounted: true,
ethereum: {
tvl: eth
},
polygon: {
tvl: polygon
},
avax:{
tvl: avax
},
optimism:{
tvl: optimism
},
celo: {
tvl: celo
},
bsc: {
tvl: bsc,
},
hallmarks:[
[1658872800, "OP Rewards Start"],
[1669615200, "OP Rewards Start"],
ethereum: { tvl: sdk.util.sumChainTvls([v3, v4]) },
avax: { tvl: sdk.util.sumChainTvls([ v4]) },
polygon: { tvl: sdk.util.sumChainTvls([v3, v4]) },
optimism: { tvl: sdk.util.sumChainTvls([v4, v5]) },
bsc: { tvl: sdk.util.sumChainTvls([v3]) },
celo: { tvl: sdk.util.sumChainTvls([v3]) },
hallmarks: [
[1_634_320_800, 'V4 Launch'],
[1_658_872_800, 'V4 OP Rewards Begin'],
[1_669_615_200, 'V4 OP Rewards Extended'],
[1_697_738_400, 'V5 Launch']
],
methodology: `TVL is the total quantity of tokens locked in PoolTogether pools on Ethereum, Polygon, Avalanche, Optimism, Celo, and BSC`
methodology: `TVL is the total tokens deposited in PoolTogether amongst V3, V4 and V5 on Ethereum, Polygon, Avalanche, Optimism, Celo and BSC`
}
36 changes: 36 additions & 0 deletions projects/pooltogether/v3.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
const { cachedGraphQuery } = require('../helper/cache')
const abi = require('./abi.json')

const GRAPH_URLS = {
ethereum: [
'https://api.thegraph.com/subgraphs/name/pooltogether/pooltogether-v3_1_0',
'https://api.thegraph.com/subgraphs/name/pooltogether/pooltogether-v3_3_2',
'https://api.thegraph.com/subgraphs/name/pooltogether/pooltogether-v3_3_8',
'https://api.thegraph.com/subgraphs/name/pooltogether/pooltogether-v3_4_3'
],
celo: ['https://api.thegraph.com/subgraphs/name/pooltogether/celo-v3_4_5'],
bsc: ['https://api.thegraph.com/subgraphs/name/pooltogether/bsc-v3_4_3']
}
const GRAPH_QUERY = `
query GET_POOLS {
prizePools { id }
}
`
async function tvl(_, _b, _cb, { api, }) {
const graphUrls = GRAPH_URLS[api.chain] ?? []
const pools = []
if (api.chain === 'polygon') pools.push('0x887E17D791Dcb44BfdDa3023D26F7a04Ca9C7EF4', '0xee06abe9e2af61cabcb13170e01266af2defa946')
for (const endpoint of graphUrls) {
const key = `pooltogether/${api.chain}/${endpoint.split('pooltogether/')[1]}`
const { prizePools } = await cachedGraphQuery(key, endpoint, GRAPH_QUERY,)
pools.push(...prizePools.map(i => i.id))
}
const tokens = await api.multiCall({ abi: 'address:token', calls: pools})
const bals = await api.multiCall({ abi: abi.accountedBalance, calls: pools})
api.addTokens(tokens, bals)
return api.getBalances()
}

module.exports = {
tvl,
}
24 changes: 24 additions & 0 deletions projects/pooltogether/v4.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
const { sumTokens2 } = require('../helper/unwrapLPs')

const V4_POOLS = {
ethereum: [
['0xbcca60bb61934080951369a648fb03df4f96263c', '0x32e8d4c9d1b711bc958d0ce8d14b41f77bb03a64']
],
polygon: [
['0x1a13f4ca1d028320a707d99520abfefca3998b7f', '0xD4F6d570133401079D213EcF4A14FA0B4bfB5b9C']
],
avax: [
['0x46a51127c3ce23fb7ab1de06226147f446e4a857', '0x7437db21A0dEB844Fa64223e2d6Db569De9648Ff']
],
optimism: [
['0x625E7708f30cA75bfd92586e17077590C60eb4cD', '0x4ecB5300D9ec6BCA09d66bfd8Dcb532e3192dDA1']
]
}

async function tvl(_, _b, _cb, { api, }) {
return sumTokens2({ api, tokensAndOwners: V4_POOLS[api.chain] })
}

module.exports = {
tvl
}
19 changes: 19 additions & 0 deletions projects/pooltogether/v5.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const abi = require('./abi.json')

const V5_VAULT_FACTORIES = {
optimism: '0xF65FA202907D6046D1eF33C521889B54BdE08081'
}

async function tvl(_, _b, _cb, { api, }) {
const factory = V5_VAULT_FACTORIES[api.chain]
if (!factory) return {}
const vaults = await api.fetchList({ lengthAbi: abi.totalVaults, itemAbi: abi.allVaults, target: factory })
const tokens = await api.multiCall({ abi: abi.asset, calls: vaults })
const bals = await api.multiCall({ abi: abi.totalAssets, calls: vaults })
api.addTokens(tokens, bals)
return api.getBalances()
}

module.exports = {
tvl
}

0 comments on commit decd07a

Please sign in to comment.