Skip to content

Commit

Permalink
track apebond
Browse files Browse the repository at this point in the history
  • Loading branch information
g1nt0ki committed Feb 21, 2024
1 parent d6e3ef9 commit 6e1fb7f
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 1 deletion.
67 changes: 67 additions & 0 deletions projects/apebond/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
const { sumTokens2 } = require('../helper/unwrapLPs')
const { getLogs } = require('../helper/cache/getLogs')

const config = {
ethereum: { factory: '0x34ef7df17a7e9a51e0b308ac3280b08379deda33', fromBlock: 18745159, },
polygon: { factory: '0x1ce1b8e28e4d4c1c4da01b69f5434a9fb7be023d', fromBlock: 50910011, },
bsc: '0x9D09925567FA16f8b9E646C5d35475cc235C66d9',
}

const ownTokens = {
ethereum: '0xe6828D65bf5023AE1851D90D8783Cc821ba7eeE1',
bsc: '0x34294afabcbaffc616ac6614f6d2e17260b78bed',
polygon: '0xe6828d65bf5023ae1851d90d8783cc821ba7eee1',
}

Object.keys(config).forEach(chain => {
const _config = config[chain]

function getTvl(api, isStaking) {
const ownToken = ownTokens[chain]
if (!isStaking) {
api.removeTokenBalance(ownToken)
} else {
const regex = new RegExp(ownToken, 'i')
Object.keys(api.getBalances()).forEach(key => {
if (!regex.test(key))
api.removeTokenBalance(key)
})
}

return api.getBalances()
}

const poolManagerTvl = (isStaking) => async (_, _b, _cb, { api, }) => {
const factory = _config
const manager = await api.call({ abi: 'address:poolManager', target: factory })
const allLegacyPools = await api.call({ abi: 'address[]:allLegacyPools', target: manager })
const allNewPools = await api.call({ abi: 'address[]:allNewPools', target: manager })
const tokensLegacy = await api.multiCall({ abi: 'address:stakeToken', calls: allLegacyPools })
const tokensNew = await api.multiCall({ abi: 'address:STAKE_TOKEN', calls: allNewPools })
const tokens = [...tokensLegacy, ...tokensNew]
const owners = [...allLegacyPools, ...allNewPools]
await sumTokens2({ api, tokensAndOwners2: [tokens, owners], resolveLP: true, resolveIchiVault: true, })
return getTvl(api, isStaking)
}

const logTvl = (isStaking) => async (_, _b, _cb, { api, }) => {
const { factory, fromBlock } = _config
const logs = await getLogs({
api,
target: factory,
eventAbi: 'event DeployedPoolContract (address indexed pool, address stakeToken, address rewardToken, uint256 rewardPerSecond, uint256 startTime, uint256 bonusEndTime, address owner)',
onlyArgs: true,
fromBlock,
})
const tokensAndOwners = logs.map(log => [log.stakeToken, log.pool])
await sumTokens2({ api, tokensAndOwners, resolveLP: true, resolveIchiVault: true, })
return getTvl(api, isStaking)
}
const tvlFunction = typeof _config === 'string' ? poolManagerTvl : logTvl
module.exports[chain] = {
tvl: tvlFunction(false),
staking: tvlFunction(true),
}


})
42 changes: 41 additions & 1 deletion projects/helper/unwrapLPs.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const { requery } = require('./requery')
const { getChainTransform, getFixBalances } = require('./portedTokens')
const { getUniqueAddresses, normalizeAddress } = require('./tokenMapping')
const creamAbi = require('./abis/cream.json')
const { isLP, log, sliceIntoChunks, } = require('./utils')
const { isLP, log, sliceIntoChunks, isICHIVaultToken, } = require('./utils')
const { sumArtBlocks, whitelistedNFTs, } = require('./nft')
const wildCreditABI = require('../wildcredit/abi.json');
const { covalentGetTokens, } = require("./token");
Expand Down Expand Up @@ -693,6 +693,7 @@ async function sumTokens2({
tokenConfig = {},
sumChunkSize = undefined,
uniV3ExtraConfig = {},
resolveICHIVault = false,
}) {
if (api) {
chain = api.chain ?? chain
Expand Down Expand Up @@ -751,8 +752,23 @@ async function sumTokens2({
tokensAndOwners = getUniqueToA(tokensAndOwners)
log(chain, 'summing tokens', tokensAndOwners.length)


let ichiVaultToAs = []
if (resolveICHIVault) {
const symbols = (await api.multiCall({ abi: 'erc20:symbol', calls: tokensAndOwners.map(t => t[0]) }))
tokensAndOwners.filter(([token, owner], i) => {
if (isICHIVaultToken(symbols[i], token, api.chain)) {
ichiVaultToAs.push([token, owner])
return false
}
return true
})
}

await sumTokens(balances, tokensAndOwners, block, chain, transformAddress, { resolveLP, unwrapAll, blacklistedLPs, skipFixBalances: true, abis, permitFailure, sumChunkSize, })

if (ichiVaultToAs.length)
await unwrapICHIVaults()

if (!skipFixBalances) {
const fixBalances = await getFixBalances(chain)
Expand All @@ -765,6 +781,30 @@ async function sumTokens2({
toa = toa.map(i => i.join('-'))
return getUniqueAddresses(toa, chain).map(i => i.split('-'))
}


async function unwrapICHIVaults() {
const lps = ichiVaultToAs.map(i => i[0])
const balanceOfCalls = ichiVaultToAs.map(t => ({ params: t[1], target: t[0] }))
const [
token0s, token1s, supplies, uBalances, tokenBalances
] = await Promise.all([
api.multiCall({ abi: 'address:token0', calls: lps }),
api.multiCall({ abi: 'address:token1', calls: lps }),
api.multiCall({ abi: 'uint256:totalSupply', calls: lps }),
api.multiCall({ abi: 'function getTotalAmounts() view returns (uint256 token0Bal, uint256 token1Bal)', calls: lps }),
api.multiCall({ abi: 'erc20:balanceOf', calls: balanceOfCalls }),
])

tokenBalances.forEach((bal, i) => {
const ratio = bal / supplies[i]
const token0Bal = uBalances[i][0] * ratio
const token1Bal = uBalances[i][1] * ratio
sdk.util.sumSingleBalance(balances, token0s[i], token0Bal)
sdk.util.sumSingleBalance(balances, token1s[i], token1Bal)
})
return balances
}
}

function sumTokensExport({ balances, tokensAndOwners, tokensAndOwners2, tokens, owner, owners, transformAddress, unwrapAll, resolveLP, blacklistedLPs, blacklistedTokens, skipFixBalances, ownerTokens, resolveUniV3, resolveArtBlocks, resolveNFTs, fetchCoValentTokens, logCalls, ...args }) {
Expand Down
7 changes: 7 additions & 0 deletions projects/helper/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@ const blacklisted_LPS = [
'0x253f67aacaf0213a750e3b1704e94ff9accee10b',
].map(i => i.toLowerCase())

function isICHIVaultToken(symbol, token, chain) {
if (symbol === 'ICHI_Vault_LP') return true
if (chain === 'bsc' && symbol.startsWith('IV-') && symbol.endsWith('-THE')) return true
return false
}

function isLP(symbol, token, chain) {
// sdk.log(symbol, chain, token)
if (!symbol) return false
Expand Down Expand Up @@ -327,4 +333,5 @@ module.exports = {
getDecimals,
getParamCalls,
once,
isICHIVaultToken,
}

0 comments on commit 6e1fb7f

Please sign in to comment.