Skip to content

Commit

Permalink
watcher/tests: increase timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
panoel committed Sep 20, 2023
1 parent 143428a commit 40cc772
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 49 deletions.
90 changes: 42 additions & 48 deletions cloud_functions/src/computeTvlTvm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,21 @@ const ACCOUNTANT_CONTRACT_ADDRESS =
'wormhole14hj2tavq8fpesdwxxcu44rty3hh90vhujrvcmstl4zr3txmfvw9srrg465';
const PAGE_LIMIT = 2000; // throws a gas limit error over this

type Mega = {
account: AccountEntry;
type FullMetadata = {
metadata: TokenMetaDatum;
priceInfo: TokenPrice;
decimalDivisor: number;
};

const megaMap: Map<string, Mega> = new Map<string, Mega>();
type Key = {
chain_id: number;
token_chain: number;
token_address: string;
};

const metadataMap: Map<string, FullMetadata> = new Map<string, FullMetadata>();
const geckoIdMap: Map<string, string> = new Map<string, string>();
const priceMap: Map<string, TokenPrice> = new Map<string, TokenPrice>();
const accountMap: Map<Key, AccountEntry> = new Map<Key, AccountEntry>();

async function getAccountantAccounts(): Promise<AccountEntry[]> {
const cosmWasmClient = await CosmWasmClient.connect(WORMCHAIN_URL);
Expand Down Expand Up @@ -86,21 +91,8 @@ async function getTokenPrices(): Promise<TokenPrice[]> {
}

async function populateMaps() {
const emptyAccount: AccountEntry = {
key: {
chain_id: 0,
token_chain: 0,
token_address: '',
},
balance: '0',
};
const emptyPrice: TokenPrice = {
date: '',
coin_gecko_coin_id: '',
price_usd: 0,
};

// Get token metadata to start the ball rolling...
// Populate the metadata map and the geckoIdMap
const metaData: TokenMetaDatum[] = await getTokenMetadata();
console.log(`Got ${metaData.length} token metadata entries`);
for (const md of metaData) {
Expand All @@ -112,10 +104,8 @@ async function populateMaps() {
}
let decimalsFloat = Math.pow(10.0, md.decimals);
let decimals = Math.floor(decimalsFloat);
megaMap.set(key, {
account: emptyAccount,
metadataMap.set(key, {
metadata: md,
priceInfo: emptyPrice,
decimalDivisor: decimals,
});
}
Expand All @@ -124,11 +114,7 @@ async function populateMaps() {
const accounts: AccountEntry[] = await getAccountantAccounts();
console.log(`Got ${accounts.length} accountant accounts`);
for (const a of accounts) {
const key = `${a.key.token_chain}/${a.key.token_address}`;
const mega = megaMap.get(key);
if (mega) {
mega.account = a;
}
accountMap.set(a.key, a);
}

// Lastly, get the price information
Expand All @@ -139,10 +125,6 @@ async function populateMaps() {
if (!tokId) {
continue;
}
const mega = megaMap.get(tokId);
if (mega) {
mega.priceInfo = p;
}
priceMap.set(tokId, p);
}
}
Expand All @@ -161,34 +143,46 @@ export async function computeTvlTvm(req: any, res: any) {
let tvmByChain = new Map<number, number>();
let tvlByChain = new Map<number, number>();

for (let [key, value] of megaMap) {
if (value.account.balance === '0') {
for (let [key, value] of accountMap) {
if (value.balance === '0') {
continue;
}
if (!tvmByChain.has(value.account.key.chain_id)) {
tvmByChain.set(value.account.key.chain_id, 0);
if (!tvmByChain.has(value.key.chain_id)) {
tvmByChain.set(value.key.chain_id, 0);
}
if (!tvlByChain.has(value.account.key.chain_id)) {
tvlByChain.set(value.account.key.chain_id, 0);
if (!tvlByChain.has(value.key.chain_id)) {
tvlByChain.set(value.key.chain_id, 0);
}
const mapKey = `${value.key.token_chain}/${value.key.token_address}`;
const metadata = metadataMap.get(mapKey);
if (!metadata) {
// console.error(`Metadata not found for ${JSON.stringify(key)}`);
continue;
}
const priceInfo = priceMap.get(mapKey);
if (!priceInfo) {
// console.error(`Price info not found for ${JSON.stringify(key)}`);
continue;
}

let acctBalance = Number(value.account.balance);
acctBalance = acctBalance / value.decimalDivisor;
let acctBalance = Number(value.balance);
if (metadata.decimalDivisor === 0) {
console.error(`Decimal divisor is 0 for ${metadata.metadata.name}`);
continue;
}
acctBalance = acctBalance / metadata.decimalDivisor;

let notional = acctBalance * value.priceInfo.price_usd;
if (priceInfo.price_usd === 0) {
continue;
}
let notional = acctBalance * priceInfo.price_usd;

if (value.account.key.chain_id === value.account.key.token_chain) {
if (value.key.chain_id === value.key.token_chain) {
// TVL
tvlByChain.set(
value.account.key.chain_id,
tvlByChain.get(value.account.key.chain_id)! + notional
);
tvlByChain.set(value.key.chain_id, tvlByChain.get(value.key.chain_id)! + notional);
} else {
// TVM
tvmByChain.set(
value.account.key.chain_id,
tvmByChain.get(value.account.key.chain_id)! + notional
);
tvmByChain.set(value.key.chain_id, tvmByChain.get(value.key.chain_id)! + notional);
}
}
await storeLatestTvlTvm(tvlByChain, tvmByChain);
Expand Down
2 changes: 1 addition & 1 deletion watcher/src/watchers/__tests__/EVMWatcher.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const initialCeloBlock = Number(INITIAL_DEPLOYMENT_BLOCK_BY_CHAIN.celo);
const initialOasisBlock = Number(INITIAL_DEPLOYMENT_BLOCK_BY_CHAIN.oasis);
const initialKaruraBlock = Number(INITIAL_DEPLOYMENT_BLOCK_BY_CHAIN.karura);

jest.setTimeout(30000);
jest.setTimeout(60000);

test('getBlock by tag', async () => {
const watcher = new EVMWatcher('avalanche');
Expand Down

0 comments on commit 40cc772

Please sign in to comment.