Skip to content

Commit

Permalink
Add vBIFI token + price
Browse files Browse the repository at this point in the history
  • Loading branch information
ReflectiveChimp committed Jul 7, 2023
1 parent cebb323 commit e3f7e53
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 19 deletions.
11 changes: 11 additions & 0 deletions packages/address-book/address-book/bsc/tokens/tokens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4132,5 +4132,16 @@ const _tokens = {
'Radiant aims to be the first omnichain money market, where users can deposit any major asset on any major chain and borrow a variety of supported assets across multiple chains.',
logoURI: '',
},
vBIFI: {
name: 'Venus BIFI',
symbol: 'BIFI',
address: '0xC718c51958d3fd44f5F9580c9fFAC2F89815C909',
chainId: 56,
decimals: 8,
website: 'https://venus.io/',
description:
'Venus wrapped BIFI. Venus is an algorithmic money market and synthetic stablecoin protocol launched exclusively on BNB Chain.',
logoURI: '',
},
} as const;
export const tokens: ConstRecord<typeof _tokens, Token> = _tokens;
84 changes: 65 additions & 19 deletions src/api/stats/bsc/venus/getVenusPrices.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,39 @@ import { getContractWithProvider } from '../../../../utils/contractHelper';
import { isFiniteNumber } from '../../../../utils/number';
import BigNumber from 'bignumber.js';
import { bscWeb3 } from '../../../../utils/web3';
import { isFiniteBigNumber } from '../../../../utils/big-number';

type vToken = {
oracleId: string;
address: string;
decimals: number;
underlying: {
oracleId: string;
decimals: number;
};
};

const vUSDT = {
oracle: 'vUSDT',
oracleId: 'vUSDT',
address: '0xfD5840Cd36d94D7229439859C0112a4185BC0255',
decimals: 8,
underlying: {
oracle: 'tokens',
oracleId: 'USDT',
decimals: 18,
},
};
} as const satisfies vToken;

const vBIFI = {
oracleId: 'vBIFI',
address: '0xC718c51958d3fd44f5F9580c9fFAC2F89815C909',
decimals: 8,
underlying: {
oracleId: 'BIFI',
decimals: 18,
},
} as const satisfies vToken;

const vTokens = [vUSDT, vBIFI];

const abi = [
{
Expand All @@ -29,24 +51,48 @@ const abi = [
export const fetchVenusPrices = async (
tokenPrices: Record<string, number>
): Promise<Record<string, number>> => {
const contract = getContractWithProvider(abi, vUSDT.address, bscWeb3);
const exchangeRates = await Promise.all(
vTokens.map(async vToken => {
try {
const contract = getContractWithProvider(abi, vToken.address, bscWeb3); // TODO viem
const currentExchangeRate = new BigNumber(
await contract.methods.exchangeRateCurrent().call()
);
if (isFiniteBigNumber(currentExchangeRate)) {
return currentExchangeRate;
} else {
console.log(
`Error fetching venus price for ${vToken.oracleId}: invalid exchangeRateCurrent`
);
}
} catch (err) {
console.log(`Error fetching venus price for ${vToken.oracleId}: ${err.message}`);
}

try {
const currentExchangeRate = new BigNumber(await contract.methods.exchangeRateCurrent().call());
const divisor = new BigNumber(10).pow(18 + vUSDT.underlying.decimals - vUSDT.decimals);
return undefined;
})
);

const exchangeRate = currentExchangeRate.div(divisor);
const underlyingPrice = tokenPrices[vUSDT.underlying.oracleId];
return vTokens.reduce((prices, vToken, i) => {
const exchangeRateWei = exchangeRates[i];
if (!exchangeRateWei) {
return prices;
}

if (isFiniteNumber(underlyingPrice)) {
return {
vUSDT: exchangeRate.times(underlyingPrice).toNumber(),
};
} else {
throw new Error('missing underlying price for ' + vUSDT.underlying.oracleId);
const underlyingPrice = tokenPrices[vToken.underlying.oracleId];
if (!isFiniteNumber(underlyingPrice)) {
console.log(
`Error fetching venus price for ${vToken.oracleId}: invalid underlying price for ${vToken.underlying.oracleId}}`
);
return prices;
}
} catch (err) {
console.log('Error fetching venus prices: ' + err.message);
}
return {};

const exchangeRate = exchangeRateWei.shiftedBy(
-(18 + vToken.underlying.decimals - vToken.decimals)
);

prices[vToken.oracleId] = exchangeRate.times(underlyingPrice).toNumber();

return prices;
}, {} as Record<string, number>);
};

0 comments on commit e3f7e53

Please sign in to comment.