Skip to content

Commit

Permalink
Fix: Darkcrypto (masterChef logic) (#11729)
Browse files Browse the repository at this point in the history
  • Loading branch information
0xpeluche authored Sep 24, 2024
1 parent 68b1892 commit 78fecf3
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 110 deletions.
112 changes: 30 additions & 82 deletions projects/darkcrypto/farm-utils.js
Original file line number Diff line number Diff line change
@@ -1,90 +1,38 @@
const sdk = require('@defillama/sdk');
const BigNumber = require('bignumber.js');
const farmCronos = require('./farm-cronos.json');
const abi = {
getReserves: "function getReserves() view returns (uint112 _reserve0, uint112 _reserve1, uint32 _blockTimestampLast)",
poolInfo: "function poolInfo(uint256) view returns (address token, uint256 allocPoint, uint256 lastRewardTime, uint256 accSkyPerShare, bool isStarted)"
}

const farmLPBalance = async (
chain,
block,
masterChef,
lpToken,
token0,
token1,
) => {
const balances = (
await sdk.api.abi.multiCall({
abi: 'erc20:balanceOf',
calls: [
{
target: token0,
params: [lpToken],
},
{
target: token1,
params: [lpToken],
},
{
target: lpToken,
params: [masterChef],
},
],
block,
chain: chain,
})
).output;

const lpTotalSuply = (
await sdk.api.abi.call({
target: lpToken,
abi: 'erc20:totalSupply',
chain: chain,
block,
})
).output;
const token0Locked = (balances[2].output * balances[0].output) / lpTotalSuply;
const token1Locked = (balances[2].output * balances[1].output) / lpTotalSuply;
return [
{ token: `${chain}:${token0}`, locked: token0Locked },
{ token: `${chain}:${token1}`, locked: token1Locked },
];
};
const pool2Balances = async (api, masterChef) => {
const calls = Array.from({ length: 9 }, (_, i) => ({ target: masterChef, params: [i] }));
const poolsInfos = await api.multiCall({ calls, abi: abi.poolInfo })
const pools = poolsInfos.map(({ token }) => token)

const [token0s, token1s, balances, reserves, supplies] = await Promise.all([
api.multiCall({ calls: pools.map((p) => ({ target: p })), abi: 'address:token0', permitFailure: true }),
api.multiCall({ calls: pools.map((p) => ({ target: p })), abi: 'address:token1', permitFailure: true }),
api.multiCall({ calls: pools.map((p) => ({ target: p, params: [masterChef] })), abi: 'erc20:balanceOf', permitFailure:true }),
api.multiCall({ calls: pools.map((p) => ({ target: p })), abi: abi.getReserves, permitFailure:true }),
api.multiCall({ calls: pools.map((p) => ({ target: p })), abi: 'erc20:totalSupply', permitFailure:true })
])

const farmLocked = async (block) => {
const balances = {};
const tokens = farmCronos.tokens;
pools.forEach((_, i) => {
const token0 = token0s[i]
const token1 = token1s[i]
const balance = balances[i]
const reserve = reserves[i]
const supply = supplies[i]
if (!token0 || !token1 || !balance || !reserve || !supply) return

const allPools = farmCronos.farms
.map((t) => {
return t.pools.map((pool) => {
return Object.assign(pool, {
masterChef: t.masterChef,
});
});
})
.reduce((acc, current) => [...acc, ...current], []);
const promises = allPools.map((item) => {
return farmLPBalance(
'cronos',
block,
item.masterChef,
item.lpToken,
tokens[item.token0],
tokens[item.token1],
);
});
const _balance0 = Math.round(reserve[0] * balance / supply)
const _balance1 = Math.round(reserve[1] * balance / supply)

const data = await Promise.all(promises);
data.forEach((farm) => {
farm.forEach((item) => {
balances[item.token] = new BigNumber(balances[item.token] || 0)
.plus(item.locked || 0)
.toFixed(0);
});
});
api.add(token0, _balance0)
api.add(token1, _balance1)
})
}

return balances;
};

module.exports = {
farmLocked,
};
pool2Balances
}
31 changes: 3 additions & 28 deletions projects/darkcrypto/index.js
Original file line number Diff line number Diff line change
@@ -1,34 +1,10 @@
const ADDRESSES = require('../helper/coreAssets.json')
const { stakingUnknownPricedLP } = require("../helper/staking");
const farmUtils = require("./farm-utils");
const vaultUtils = require("./vault-utils")
const { pool2Balances } = require("./farm-utils");

const sdk = require("@defillama/sdk");
const sky = "0x9D3BBb0e988D9Fb2d55d07Fe471Be2266AD9c81c";
const boardroom = "0x2e7d17ABCb9a2a40ec482B2ac9a9F811c12Bf630";


async function pool2(timestamp, block, chainBlocks) {
// SKY POOL
const farmTvl = await farmUtils.farmLocked(chainBlocks["cronos"]);
let balances = {};

//add CRO balance in LP pool
sdk.util.sumSingleBalance(
balances,
"cronos:" + ADDRESSES.cronos.WCRO_1,
farmTvl["cronos:" + ADDRESSES.cronos.WCRO_1]
);

//add Dark and Sky balance in LP pool
sdk.util.sumSingleBalance(
balances,
"cronos:" + ADDRESSES.cronos.WCRO_1,
farmTvl["cronos:" + ADDRESSES.cronos.WCRO_1],
);

return balances;
}
const masterChef = "0x42B652A523367e7407Fb4BF2fA1F430781e7db8C"

async function vault(api){
return vaultUtils.vaultLocked(api)
Expand All @@ -38,13 +14,12 @@ module.exports = {
doublecounted: true,
cronos: {
tvl:vault,
pool2,
pool2: (api) => pool2Balances(api, masterChef),
staking: stakingUnknownPricedLP(
boardroom,
sky,
"cronos",
"0xaA0845EE17e4f1D4F3A8c22cB1e8102baCf56a77"
),

},
};

0 comments on commit 78fecf3

Please sign in to comment.