Skip to content

Commit

Permalink
Merge branch 'main' of github.com:DefiLlama/DefiLlama-Adapters
Browse files Browse the repository at this point in the history
  • Loading branch information
g1nt0ki committed Dec 26, 2022
2 parents 45693e5 + 4a8f48d commit 014e933
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 71 deletions.
3 changes: 3 additions & 0 deletions projects/exactly/.prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"printWidth": 120
}
136 changes: 65 additions & 71 deletions projects/exactly/index.js
Original file line number Diff line number Diff line change
@@ -1,90 +1,80 @@
const sdk = require('@defillama/sdk')
const sdk = require("@defillama/sdk");

module.exports = {
methodology:
"Counts the tokens locked in the contracts to be used as collateral to borrow or to earn yield. Borrowed coins are not counted towards the TVL, so only the coins actually locked in the contracts are counted. There are multiple reasons behind this but one of the main ones is to avoid inflating the TVL through cycled lending.",
};

/** @type {Record<string, { auditor: string, start: number }>} */
const config = {
ethereum: {
auditor: '0x310A2694521f75C7B2b64b5937C16CE65C3EFE01',
startTimestamp: 1667223731,
}
}
auditor: "0x310A2694521f75C7B2b64b5937C16CE65C3EFE01",
start: 15_868_410,
},
};

Object.keys(config).forEach(chain => {
const { auditor, startTimestamp, } = config[chain]
Object.entries(config).forEach(([chain, { auditor, start }]) => {
module.exports[chain] = {
tvl: async (_, _b, _cb, { api, }) => {

const balances = {}
const data = await markets(api, auditor, startTimestamp)

data[0].forEach((_, i) => {
const asset = data[0][i]
const totalAssets = data[1][i]
const totalFloatingBorrowAssets = data[2][i]
const fixedPools = data[4][i]

sdk.util.sumSingleBalance(balances, asset, totalAssets, chain)
sdk.util.sumSingleBalance(balances, asset, totalFloatingBorrowAssets * -1, chain)
start,
/** @type {(timestamp: number, block: number, chainBlocks: Record<string, number>, { api: ChainApi }) => Promise<Balances>} */
tvl: async (_, __, ___, { api }) => {
/** @type {Balances} */
const balances = {};
const data = await markets(api, auditor);
data.forEach(([asset, totalAssets, totalFloatingBorrowAssets, fixedPools]) => {
sdk.util.sumSingleBalance(balances, asset, totalAssets, chain);
sdk.util.sumSingleBalance(balances, asset, -1 * +totalFloatingBorrowAssets, chain);
fixedPools.forEach(({ borrowed, supplied }) => {
sdk.util.sumSingleBalance(balances, asset, supplied, chain)
sdk.util.sumSingleBalance(balances, asset, borrowed * -1, chain)
})
})
return balances
sdk.util.sumSingleBalance(balances, asset, supplied, chain);
sdk.util.sumSingleBalance(balances, asset, -1 * +borrowed, chain);
});
});
return balances;
},
borrowed: async (_, _b, _cb, { api, }) => {

const balances = {}
const data = await markets(api, auditor, startTimestamp)

data[0].forEach((_, i) => {
const asset = data[0][i]
const totalFloatingBorrowAssets = data[2][i]
const fixedPools = data[4][i]

sdk.util.sumSingleBalance(balances, asset, totalFloatingBorrowAssets, chain)
/** @type {(timestamp: number, block: number, chainBlocks: Record<string, number>, { api: ChainApi }) => Promise<Balances>} */
borrowed: async (_, __, ___, { api }) => {
/** @type {Balances} */
const balances = {};
const data = await markets(api, auditor);
data.forEach(([asset, , totalFloatingBorrowAssets, fixedPools]) => {
sdk.util.sumSingleBalance(balances, asset, totalFloatingBorrowAssets, chain);
fixedPools.forEach(({ borrowed }) => {
sdk.util.sumSingleBalance(balances, asset, borrowed, chain)
})
})
return balances
sdk.util.sumSingleBalance(balances, asset, borrowed, chain);
});
});
return balances;
},
}
})
};
});

const INTERVAL = 86_400 * 7 * 4;

async function markets(api, target, startTimestamp) {
const markets = await api.call({ abi: abis.allMarkets, target })
const timestamp = api.timestamp

const getters = [
"asset",
"totalAssets",
"totalFloatingBorrowAssets",
"maxFuturePools",
]
const gettersData = await Promise.all(getters.map(key => api.multiCall({ abi: abis[key], calls: markets })))
const params = []
const futurePoolsMax = gettersData[3].reduce((a, i) => a > +i ? a : +i, 0)

markets.forEach((_, i) => {
params[i] = []
const minMaturity = startTimestamp - (startTimestamp % INTERVAL) + INTERVAL;
const maxMaturity = timestamp - (timestamp % INTERVAL) + INTERVAL * futurePoolsMax
const fixedPoolCount = (maxMaturity - minMaturity) / INTERVAL + 1;

for (let j = 0; j < fixedPoolCount; j++)
params[i].push(minMaturity + INTERVAL * j)
})

const fixedPools = await Promise.all(markets.map((target, i) => api.multiCall({ target, abi: abis.fixedPools, calls: params[i]})))

gettersData.push(fixedPools)
return gettersData
/** @type {(api: ChainApi, auditor: string) => Promise<[string, string, string, FixedPool[]][]>} */
async function markets(api, auditor) {
/** @type {string[]} */
const markets = await api.call({ abi: abis.allMarkets, target: auditor });
const timestamp = api.timestamp ?? 0;

/** @type {string[][]} */
const [asset, totalAssets, totalFloatingBorrowAssets, maxFuturePools] = await Promise.all(
["asset", "totalAssets", "totalFloatingBorrowAssets", "maxFuturePools"].map((key) =>
api.multiCall({ abi: abis[key], calls: markets })
)
);
const maxPools = maxFuturePools.reduce((max, n) => Math.max(max, +n), 0);
const minMaturity = timestamp - (timestamp % INTERVAL) - INTERVAL * (maxPools - 1);
const maturities = [...Array(2 * maxPools)].map((_, i) => minMaturity + INTERVAL * i);
/** @type {FixedPool[]} */
const fixedPools = await api.multiCall({
abi: abis.fixedPools,
calls: markets.flatMap((target) => maturities.map((params) => ({ target, params }))),
});
return markets.map((_, i) => [
asset[i],
totalAssets[i],
totalFloatingBorrowAssets[i],
fixedPools.slice(i * maturities.length, (i + 1) * maturities.length),
]);
}

const abis = {
Expand All @@ -94,4 +84,8 @@ const abis = {
maxFuturePools: "function maxFuturePools() view returns (uint8)",
totalAssets: "function totalAssets() view returns (uint256)",
totalFloatingBorrowAssets: "function totalFloatingBorrowAssets() view returns (uint256)",
}
};

/** @typedef {import("@defillama/sdk").ChainApi} ChainApi */
/** @typedef {import("@defillama/sdk/build/types").Balances} Balances */
/** @typedef {{ borrowed: string, supplied: string }} FixedPool */

0 comments on commit 014e933

Please sign in to comment.