Skip to content

Commit

Permalink
Fetch pool safely
Browse files Browse the repository at this point in the history
  • Loading branch information
peterpolman committed Jun 26, 2024
1 parent f00850e commit ccbed18
Showing 1 changed file with 24 additions and 17 deletions.
41 changes: 24 additions & 17 deletions apps/api/src/app/services/BalancerService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import { logger } from '../util/logger';
import { WalletDocument } from '../models';
import { ChainId } from '@thxnetwork/common/enums';
import { contractArtifacts, contractNetworks } from '@thxnetwork/api/hardhat';
import { formatUnits } from 'ethers/lib/utils';
import { BigNumber } from 'alchemy-sdk';

class BalancerService {
Expand Down Expand Up @@ -98,27 +97,35 @@ class BalancerService {
}
}

async fetchPool() {
try {
return await this.balancer.pools.find(BALANCER_POOL_ID);
} catch (error) {
logger.error(error);
return;
}
}

async updatePricesJob() {
// const pool = await this.balancer.pools.find(BALANCER_POOL_ID);
// const [usdc, thx] = pool.tokens as unknown as {
// symbol: string;
// balance: number;
// token: { latestUSDPrice: number };
// }[];
// const totalShares = pool.totalShares as unknown as number;
// const thxValue = thx.balance * thx.token.latestUSDPrice;
// const usdcValue = usdc.balance * usdc.token.latestUSDPrice;
// const btpPrice = (thxValue + usdcValue) / totalShares;
const pool = await this.fetchPool();
if (!pool) return;

const [usdc, thx] = pool.tokens as unknown as {
symbol: string;
balance: number;
token: { latestUSDPrice: number };
}[];
const totalShares = pool.totalShares as unknown as number;
const thxValue = thx.balance * thx.token.latestUSDPrice;
const usdcValue = usdc.balance * usdc.token.latestUSDPrice;
const btpPrice = (thxValue + usdcValue) / totalShares;
const balPrice = await this.fetchPrice('BAL', 'USDC');

this.pricing = {
'BAL': Number(balPrice),
'20USDC-80THX': 0,
'USDC': 0,
'THX': 0,
// '20USDC-80THX': btpPrice,
// 'USDC': Number(usdc.token.latestUSDPrice),
// 'THX': Number(thx.token.latestUSDPrice),
'20USDC-80THX': btpPrice,
'USDC': Number(usdc.token.latestUSDPrice),
'THX': Number(thx.token.latestUSDPrice),
};
}

Expand Down

0 comments on commit ccbed18

Please sign in to comment.