Skip to content

Commit 9c940e0

Browse files
authored
Fix Sushi-Bentobox (DefiLlama#15377)
1 parent 4c5c24a commit 9c940e0

File tree

3 files changed

+222
-311
lines changed

3 files changed

+222
-311
lines changed

projects/sushiswap-bentobox/bentobox.js

Lines changed: 42 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,49 @@
1-
const sdk = require("@defillama/sdk");
2-
const { default: BigNumber } = require("bignumber.js");
3-
const { getChainTransform } = require("../helper/portedTokens");
4-
const { getBlock } = require("../helper/http");
5-
const {
6-
getFuroTokens,
7-
getKashiTokens,
8-
getTridentTokens,
9-
getBentoboxTokensArray,
10-
} = require("./helper");
1+
const { CONFIG, getTokens } = require("./helper");
112

12-
function bentobox(chain) {
13-
return async (timestamp, ethBlock, chainBlocks) => {
14-
const balances = {};
15-
if (chain === 'moonriver') return {}
16-
const transform = await getChainTransform(chain);
17-
let block = await getBlock(timestamp, chain, chainBlocks)
18-
block = block - 1000;
3+
const abi = "function toAmount(address token, uint256 share, bool roundUp) view returns (uint256 amount)";
194

20-
const bentoTokens = await getBentoboxTokensArray(chain, block); //array with shares and amount
21-
const tridentTokens = await getTridentTokens(chain, block); //mapping with amount
22-
const kashiTokens = await getKashiTokens(chain, block); //mapping with amount
23-
const furoTokens = await getFuroTokens(chain, block); //mapping with amount
24-
bentoTokens.map((token) => {
25-
if (token.symbol === 'MIM') return;
26-
let amount = BigNumber(token.rebase.elastic);
27-
if (tridentTokens[token.id]) {
28-
amount = amount.minus(+tridentTokens[token.id]);
29-
}
30-
if (kashiTokens[token.id]) {
31-
amount = amount.minus(+kashiTokens[token.id]);
32-
}
33-
if (furoTokens[token.id]) {
34-
amount = amount.minus(+furoTokens[token.id]);
35-
}
5+
const bentobox = async (api) => {
6+
const chain = api.chain
7+
const block = await api.getBlock()
8+
if (chain === 'moonriver') return {}
369

37-
sdk.util.sumSingleBalance(balances, transform(token.id), amount.toFixed(0));
38-
});
10+
const [bentoTokens = [], tridentTokens = [], kashiTokens = [], furoTokens = []] = await Promise.all([
11+
getTokens(api, block, 'bento'),
12+
CONFIG[api.chain]?.trident ? getTokens(api, block, 'trident') : Promise.resolve([]),
13+
CONFIG[api.chain]?.kashi ? getTokens(api, block, 'kashi') : Promise.resolve([]),
14+
CONFIG[api.chain]?.furo ? getTokens(api, block, 'furo') : Promise.resolve([]),
15+
]);
3916

40-
return balances;
41-
};
17+
const shareBalances = {};
18+
kashiTokens.forEach((pair) => {
19+
const assetId = pair.asset.id.toLowerCase();
20+
const collateralId = pair.collateral.id.toLowerCase();
21+
const assetShares = Number(pair.totalAsset.elastic);
22+
const collateralShares = Number(pair.totalCollateralShare);
23+
if (assetShares > 0) shareBalances[assetId] = (shareBalances[assetId] || 0) + assetShares;
24+
if (collateralShares > 0) shareBalances[collateralId] = (shareBalances[collateralId] || 0) + collateralShares;
25+
});
26+
27+
furoTokens.forEach((token) => {
28+
const id = token.id.toLowerCase();
29+
const shares = Number(token.liquidityShares);
30+
if (shares > 0) shareBalances[id] = (shareBalances[id] || 0) + shares;
31+
});
32+
33+
const shareBalancesMap = {};
34+
const calls = Object.entries(shareBalances).map(([token, shares]) => ({ token, call: { target: CONFIG[api.chain].bentobox, params: [token, BigInt(shares), false] } }))
35+
const balances = await api.multiCall({ abi, calls: calls.map(c => c.call) });
36+
calls.forEach(({ token }, i) => { shareBalancesMap[token] = balances[i] });
37+
38+
bentoTokens.forEach(({ id, symbol, rebase}) => {
39+
const tokenId = id.toLowerCase();
40+
if (symbol === 'MIM') return;
41+
api.add(id, rebase.elastic)
42+
const tridentToken = tridentTokens.find(t => t.id.toLowerCase() === tokenId);
43+
if (tridentToken) api.add(tokenId, -tridentToken.liquidity)
44+
const shareBalance = shareBalancesMap[tokenId];
45+
if (shareBalance) api.add(id, -shareBalance)
46+
});
4247
}
4348

4449
module.exports = {

0 commit comments

Comments
 (0)