code refactor

This commit is contained in:
g1nt0ki
2023-12-29 12:20:28 +01:00
parent 4049653c94
commit ee8d55188f
17 changed files with 38 additions and 238 deletions

View File

@@ -13,7 +13,6 @@ const sanitize = require("./sanitizeWeb3Response.js");
const { standardPoolInfoAbi, addFundsInMasterChef } = require('../helper/masterchef')
const sdk = require('@defillama/sdk')
const { default: BigNumber } = require('bignumber.js')
const shareValue = "uint256:getShareValue"
const xSCREAM = "0xe3D17C7e840ec140a7A51ACA351a482231760824"
@@ -41,7 +40,7 @@ async function tvl(time, ethBlock, chainBlocks) {
abi: shareValue
})
sdk.util.sumSingleBalance(balances, transform("0xe0654C8e6fd4D733349ac7E09f6f23DA256bF475"),
BigNumber(screamShare.output).times(balances[transform(xSCREAM)]).div(1e18).toFixed(0))
screamShare.output *balances[transform(xSCREAM)] /1e18)
delete balances[transform(xSCREAM)]
const creditShare = await sdk.api.abi.call({
@@ -50,7 +49,7 @@ async function tvl(time, ethBlock, chainBlocks) {
abi: shareValue
})
sdk.util.sumSingleBalance(balances, transform("0x77128dfdd0ac859b33f44050c6fa272f34872b5e"),
BigNumber(creditShare.output).times(balances[transform(xCREDIT)]).div(1e18).toFixed(0))
creditShare.output * balances[transform(xCREDIT)] / 1e18)
delete balances[transform(xCREDIT)]
const tarotShare = await sdk.api.abi.call({
@@ -95,10 +94,10 @@ async function tvl(time, ethBlock, chainBlocks) {
...pool.poolData,
...reserveData,
};
const shareOfTotalSupply = new BigNumber(newPool.totalSupply).div(newPool.poolData.totalSupply).toFixed()
const shareOfTotalSupply = newPool.totalSupply / newPool.poolData.totalSupply
newPool.shareOfTotalSupply = shareOfTotalSupply;
let token0Reserve = new BigNumber(newPool.poolData.token0Reserve).times(shareOfTotalSupply).toFixed(0);
let token1Reserve = new BigNumber(newPool.poolData.token1Reserve).times(shareOfTotalSupply).toFixed(0);
let token0Reserve = newPool.poolData.token0Reserve * shareOfTotalSupply
let token1Reserve = newPool.poolData.token1Reserve * shareOfTotalSupply
if (isNaN(token0Reserve)) {
token0Reserve = "0"
}
@@ -145,12 +144,7 @@ async function tvl(time, ethBlock, chainBlocks) {
// Add TVL from pools to balances
const addBalance = (tokenAddress, amount) => {
const fantomTokenAddress = `fantom:${tokenAddress}`
const existingBalance = balances[fantomTokenAddress];
if (existingBalance) {
balances[fantomTokenAddress] = new BigNumber(existingBalance).plus(amount).toFixed(0)
} else {
balances[fantomTokenAddress] = amount;
}
sdk.util.sumSingleBalance(balances, fantomTokenAddress, amount)
}
pools.forEach(pool => {
const token0 = pool.poolData.token0Address;

View File

@@ -1,7 +1,4 @@
const sdk = require('@defillama/sdk');
const { default: BigNumber } = require('bignumber.js');
const Abis = require('./abi.json');
const {getTokenId} = require('./utils')
const Contracts = {
moonbeam: {
@@ -9,64 +6,21 @@ const Contracts = {
'ob3p': '0x04e274f709e1ae71aff4f994b4267143ec6a381a',
'ob3pbusd': '0x7e758319d46E0A053220e3728B3eE47a1979316a',
},
ignoredLps: ['0xe7a7dfb89f84a0cf850bcd399d0ec906ab232e9d'],
}
};
}
const poolTvl = async (chain, poolAddress, block) => {
const [balances, tokens] = await Promise.all([
sdk.api.abi.call({
target: poolAddress,
abi: Abis.getTokenBalances,
chain: chain,
block,
}),
sdk.api.abi.call({
target: poolAddress,
abi: Abis.getTokens,
chain: chain,
block,
}),
]);
const sum = {};
tokens.output.forEach((token, i) => {
if (
Contracts[chain].ignoredLps &&
Contracts[chain].ignoredLps.includes(token.toLowerCase())
) {
return;
}
const [symbol, decimals] = getTokenId(token.toLowerCase());
sum[symbol] = new BigNumber(balances.output[i]).div(new BigNumber(10).pow(decimals)).toNumber()
const moonbeamTvl = async (timestamp, ethBlock, chainBlocks, { api }) => {
const pools = Object.values(Contracts.moonbeam.pools)
const tokens = await api.multiCall({ abi: Abis.getTokens, calls: pools })
const bals = await api.multiCall({ abi: Abis.getTokenBalances, calls: pools })
tokens.forEach((token, i) => {
api.addTokens(token, bals[i])
});
return sum;
};
const moonbeamTvl = async (timestamp, ethBlock, chainBlocks) => {
let block = chainBlocks['moonbeam'];
const tvl = {};
for (let address of Object.values(Contracts.moonbeam.pools)) {
const balances = await poolTvl(
'moonbeam',
address,
block,
);
Object.entries(balances).forEach(([token, value]) => {
sdk.util.sumSingleBalance(tvl, token, value);
});
}
return tvl;
api.removeTokenBalance('0xe7a7dfb89f84a0cf850bcd399d0ec906ab232e9d')
};
module.exports = {
moonbeam: {
tvl: moonbeamTvl,
},
};

View File

@@ -1,20 +0,0 @@
const ADDRESSES = require('../helper/coreAssets.json')
const TokenMaps = {
[ADDRESSES.shiden.ETH]: ['dai', 18],
[ADDRESSES.telos.USDC]: ['usd-coin', 6],
[ADDRESSES.telos.USDT]: ['tether', 6],
'0xa649325aa7c5093d12d6f98eb4378deae68ce23f': ['binance-usd', 18]
}
/**
*
* @param {string} address token address in lower case
* @returns coingecko id and decimals
*/
function getTokenId(address) {
return TokenMaps[address]
}
module.exports = {
getTokenId
}

View File

@@ -1,5 +1,3 @@
const sdk = require('@defillama/sdk');
const BigNumber = require("bignumber.js");
const abi = require('../helper/abis/aave.json');
async function ammMarket(api, borrowed) {
@@ -29,7 +27,7 @@ async function ammMarket(api, borrowed) {
})
));
supplyStabledebt.map((ssd, i) => {
balanceOfTokens[i] = BigNumber(ssd).plus(supplyVariableDebt[i]).toFixed(0)
balanceOfTokens[i] = Number(BigInt(ssd) + BigInt(supplyVariableDebt[i]))
})
}

View File

@@ -1,7 +1,4 @@
const sdk = require("@defillama/sdk")
const BigNumber = require("bignumber.js")
const stARCx = '0x9bffad7a6d5f52dbc51cae33e419793c72fd7d9d'
const stakingContract = '0x9bffad7a6d5f52dbc51cae33e419793c72fd7d9d'
const ARCx = '0x1321f1f1aa541a56c31682c57b80ecfccd9bb288'

View File

@@ -1,64 +1,13 @@
const sdk = require("@defillama/sdk");
const getReserves = 'function getReserves() view returns (uint112 _reserve0, uint112 _reserve1, uint32 _blockTimestampLast)'
const BigNumber = require("bignumber.js");
const { sumTokensExport } = require("./helper/unwrapLPs");
const CollateralSystemAddress = "0x78D4664408c06F2BeDc4f108f3Fc8f0AB017a0AE";
const MovrChaosPoolAddress = "0x8d341E5E955E936B45B29dbF49960b8538FCA978";
const MovrUsdcPoolAddress = "0xe537f70a8b62204832B8Ba91940B77d3f79AEb81";
const tokens = {
CHAOS: "0xf4c6850B6e3288E81Be542909b76865a0BdF9585",
};
async function tvl(timestamp, block, chainBlocks) {
const balances = {};
const moonriverBlock = chainBlocks.moonriver;
const stakedChaos = await sdk.api.abi.call({
chain: "moonriver",
block: moonriverBlock,
target: tokens["CHAOS"],
params: CollateralSystemAddress,
abi: "erc20:balanceOf",
});
// token0: WMOVR. token1: CHAOS
const movrChaosPoolReserves = await sdk.api.abi.call({
chain: "moonriver",
block: moonriverBlock,
target: MovrChaosPoolAddress,
params: [],
abi: getReserves,
});
// token0: WMOVR. token1: USDC
const movrUsdcPoolReserves = await sdk.api.abi.call({
chain: "moonriver",
block: moonriverBlock,
target: MovrUsdcPoolAddress,
params: [],
abi: getReserves,
});
const totalChaos = new BigNumber(stakedChaos.output)
// Calculate USD value of CHAOS via Solarbeam
const totalMovrValue = totalChaos
.times(movrChaosPoolReserves.output._reserve0)
.div(movrChaosPoolReserves.output._reserve1);
const totalUsdcValue = totalMovrValue
.times(movrUsdcPoolReserves.output._reserve1)
.div(movrUsdcPoolReserves.output._reserve0);
balances["usd-coin"] = totalUsdcValue.div("1000000").toNumber();
return balances;
}
module.exports = {
misrepresentedTokens: true,
moonriver: {
tvl,
tvl: sumTokensExport({ owner: CollateralSystemAddress, tokens: [tokens.CHAOS]}),
},
};

View File

@@ -1,4 +1,3 @@
const { default: BigNumber } = require('bignumber.js');
const { get } = require('./helper/http')
let data
@@ -10,11 +9,11 @@ async function getData() {
async function fetch() {
const tvl = await getData()
let totalTvl = new BigNumber(0)
let totalTvl = 0
tvl.data.tvls.forEach(item => {
totalTvl = totalTvl.plus(new BigNumber(item.tvl))
totalTvl += +item.tvl
});
return totalTvl.toFixed();
return totalTvl
}
const chains = {

View File

@@ -16,11 +16,6 @@ async function polygon(timestamp, block, chainBlocks) {
block: chainBlocks['polygon'],
chain: 'polygon'
})
//conditionalTokensUSDC = BigNumber(conditionalTokensUSDC.output)
// Total open interest: the conditional tokens are held at 0x4D97DCd97eC945f40cF65F87097ACe5EA0476045 and then each market has it's own contract, the address of which is the id of the FixedProductMarketMaker
//const tvl = marketsLiquidity.plus(conditionalTokensUSDC).toFixed(0)
//sdk.log(`-----\n${marketsLiquidity.div(1e12).toFixed(8)}M of marketsLiquidity \n${conditionalTokensUSDC.div(1e12).toFixed(8)}M of conditionalTokensUSDC \nTVL: ${BigNumber(tvl).div(1e12).toFixed(2)}M\n`)
return {['polygon:' + polygonUsdcContract]: conditionalTokensUSDC.output};
}

View File

@@ -1,55 +1,11 @@
// /*==================================================
// Modules
// ==================================================*/
const abi = require("./abi");
const sdk = require("@defillama/sdk");
const BigNumber = require("bignumber.js");
// /*==================================================
// Addresses
// ==================================================*/
const AssetAddress = "0x6a2469944d3F0AA54531DfA6dCB4350F4A150b67";
const EnvAddress = "0x3719C6ff935623A7B125952df5D849ef53B08cAc";
const BalanceAddress = "0x2336817CCC263B17725D7c15D687510D1d10a1b6";
// /*==================================================
// Helpers
// ==================================================*/
async function _tokens() {
return (
await sdk.api.abi.call({
target: EnvAddress,
abi: abi.env.tokens,
})
).output;
}
// /*==================================================
// Main
// ==================================================*/
async function tvl(timestamp, block) {
let balances = {};
let tokens = await _tokens();
for (let i in tokens) {
let token = tokens[i];
let output = (
await sdk.api.abi.call({
block,
target: AssetAddress,
params: token,
abi: abi.asset.balances,
})
).output;
balances[token] = balances[token]
? balances[token].plus(output)
: new BigNumber(output);
}
return balances;
async function tvl(timestamp, block, _1, { api }) {
const tokens= await api.call({ abi: abi.env.tokens, target: EnvAddress})
return api.sumTokens({ owner: AssetAddress, tokens })
}
module.exports = {

View File

@@ -1,7 +1,6 @@
const ADDRESSES = require('../helper/coreAssets.json')
const sdk = require("@defillama/sdk");
const abi = require("./abi.json");
const BigNumber = require("bignumber.js");
const qBnb = "0xbE1B5D17777565D67A5D2793f879aBF59Ae5D351"; // qBNB
const wBnb = ADDRESSES.bsc.WBNB; //wBNB
@@ -19,9 +18,6 @@ const qTokensKlaytn = [
"0x3dB032090A06e3dEaC905543C0AcC92B8f827a70", // qKQBT
];
const ZERO = new BigNumber(0);
const ETHER = new BigNumber(10).pow(18);
function tvl(borrowed) {
return async (timestamp, ethBlock, chainBlocks) => {
const chain = "bsc";
@@ -72,7 +68,7 @@ function tvlKlaytn() {
});
return {
tether: new BigNumber(data.output).dividedBy(ETHER).toNumber(),
tether: data.output/ 1e18,
};
};
}

View File

@@ -1,6 +1,4 @@
const ADDRESSES = require('../helper/coreAssets.json')
const sdk = require('@defillama/sdk');
const { ethers: { BigNumber } } = require("ethers")
const { staking } = require("../helper/staking");
const abi = require('./abi.json');
const voterProxy = '0xe96c48C5FddC0DC1Df5Cf21d68A3D8b3aba68046';

View File

@@ -1,6 +1,5 @@
const ADDRESSES = require('./helper/coreAssets.json')
const sdk = require("@defillama/sdk")
const BigNumber = require("bignumber.js")
const { getConfig } = require('./helper/cache')
// Loop through all RealT tokens listed by realt.community API and accumulate tokenprice * supply, where supply is biggest of xdai or mainnet
@@ -16,7 +15,7 @@ async function xdaiTvl(timestamp, block, chainBlocks) {
const calls_xdai = realt_tokens.map((token) => ({
target: token['xDaiContract'],
})).filter(t => t.target)
const tokenSupplies_xdai = (
await sdk.api.abi.multiCall({
calls: calls_xdai,
@@ -33,23 +32,19 @@ async function xdaiTvl(timestamp, block, chainBlocks) {
'contract': tokenContract,
'supply': supply.output,
'tokenPrice': token['tokenPrice'],
'propertyPrice': BigNumber(supply.output).div(1e18).times(BigNumber(token['tokenPrice']))
'propertyPrice': (supply.output / 1e18) * token['tokenPrice']
}
})
// Accumulate to TVL in USD and log
let tvl = tokenProperties.reduce(
(acc, token) => acc.plus(BigNumber(token['propertyPrice'])),
BigNumber(0)
)
tvl = tvl.times(1e6).toFixed(0)
return {[xdai_usdc]: tvl}
let tvl = tokenProperties.reduce((acc, token) => acc + token.propertyPrice, 0)
return { [xdai_usdc]: tvl * 1e6 }
}
module.exports = {
methodology: `TVL for RealT consists of the accumulation of all properties prices, each being tokenSupply * tokenPrice where tokenPrice is given by community API`,
methodology: `TVL for RealT consists of the accumulation of all properties prices, each being tokenSupply * tokenPrice where tokenPrice is given by community API`,
xdai: {
tvl: xdaiTvl
tvl: xdaiTvl
},
}

View File

@@ -1,5 +1,4 @@
const sdk = require('@defillama/sdk');
const BigNumber = require('bignumber.js');
const farmPolygon = require('./farms-polygon.json');
const farmLPBalance = async (
@@ -94,9 +93,7 @@ const polygonFarmLocked = async (block) => {
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);
sdk.util.sumSingleBalance(balances, item.token, item.locked);
});
});

View File

@@ -1,9 +1,6 @@
const ADDRESSES = require('../helper/coreAssets.json')
const sdk = require('@defillama/sdk');
const farmUtils = require('./farm-utils');
const BigNumber = require('bignumber.js');
const url = "https://api.safedollar.fi/api/public/getAllCollateral";
const utils = require('../helper/utils');
/**
* calculate collateral locked in safedollar
*/
@@ -48,9 +45,7 @@ const polygonTvl = async (timestamp, ethBlock, chainBlocks) => {
block: chainBlocks['polygon'],
params: [item.collateralAddress],
}).then(x => {
balances[`polygon:${item.address}`] = new BigNumber(balances[`polygon:${item.address}`] || 0)
.plus(x.output || 0)
.toFixed(0);
sdk.util.sumSingleBalance(balances, `polygon:${item.address}`, x.output)
})
});
const collateralBalance = await Promise.all(promises$)

View File

@@ -1,5 +1,4 @@
const sdk = require("@defillama/sdk");
const { default: BigNumber } = require("bignumber.js");
const { request, } = require("graphql-request"); // GraphQLClient
const { isStableToken } = require('./helper/streamingHelper')
const { getBlock } = require('./helper/http')
@@ -72,7 +71,7 @@ async function getChainBalances(allTokens, chain, block, isVesting) {
symbol,
isNativeAssetSuperToken,
} = allTokens[i]
let underlyingTokenBalance = BigNumber(totalSupply * (10 ** (underlyingToken || { decimals: 18 }).decimals) / (10 ** decimals)).toFixed(0)
let underlyingTokenBalance = totalSupply * (10 ** (underlyingToken || { decimals: 18 }).decimals) / (10 ** decimals)
// Accumulate to balances, the balance for tokens on mainnet or sidechain
let prefixedUnderlyingAddress = underlyingAddress
// if (!underlyingToken && underlyingTokenBalance/1e24 > 1) sdk.log(name, symbol, chain, Math.floor(underlyingTokenBalance/1e24))

View File

@@ -1,6 +1,5 @@
const sdk = require("@defillama/sdk");
const axios = require('axios');
const BigNumber = require("bignumber.js");
const burl = 'https://token-indexer.broxus.com/v1/root_contract/root_address/0:';
const tokenMap = {
'eb2ccad2020d9af9cec137d3146dde067039965c13a27d97293c931dae22b2b9': 'dai',
@@ -29,7 +28,7 @@ async function tvl() {
const tokenAddresses = Object.keys(tokenMap);
for (let i = 0; i < tokenAddresses.length; i++) {
const supply = (await axios.get(burl + tokenAddresses[i])).data.totalSupply;
balances[tokenMap[tokenAddresses[i]]] = new BigNumber(supply);
balances[tokenMap[tokenAddresses[i]]] = supply
}
return balances;
}

View File

@@ -1,5 +1,4 @@
const { default: BigNumber } = require('bignumber.js');
const { get } = require('./helper/http')
const nft_url = "https://nftapi.wing.finance/backend/nft-pool/pool-overview"
@@ -46,23 +45,23 @@ Object.keys(config).forEach(chain => {
if (result_nft.nftCollateralTVL !=undefined && !result_nft.nftCollateralTVL) result.totalSupply += result_nft.nftCollateralTVL
}
return {
tether: BigNumber(result.totalSupply - result.totalBorrow).toFixed(0)
tether: result.totalSupply - result.totalBorrow
}
},
staking: async () => {
const { result } = await getData(chain)
if (!result.totalLockedWingDollar) result.totalLockedWingDollar = result.TotalLockedWingDollar
if (result.totalLockedWingDollar == undefined) result.totalLockedWingDollar = BigNumber(0)
if (result.totalLockedWingDollar == undefined) result.totalLockedWingDollar = 0
if (result.totalInsurance != undefined && !result.totalInsurance) result.totalLockedWingDollar += result.totalInsurance
return {
tether: BigNumber(result.totalLockedWingDollar).toFixed(0)
tether: result.totalLockedWingDollar
}
},
borrowed: async () => {
const { result } = await getData(chain)
if (!result.totalBorrow) result.totalBorrow = result.TotalBorrow
return {
tether:new BigNumber(result.totalBorrow).toFixed(0)
tether: result.totalBorrow
}
},
}