From 2a986a267c72af02390124ffee545840f14f7b0a Mon Sep 17 00:00:00 2001 From: nicholaspai <9457025+nicholaspai@users.noreply.github.com> Date: Fri, 22 Mar 2024 17:57:07 -0400 Subject: [PATCH] improve(Dataworker): Make logs more clear when choosing to skip exchange rate updates (#1349) * improve(Dataworker): Make logs more clear when choosing to skip exchange rate updates - Add details to logs - Fix one bug to skip duplicate tokens in `_updateOldExchangeRates`. * Update Dataworker.ts * Update src/dataworker/Dataworker.ts Co-authored-by: Matt Rice --------- Co-authored-by: Matt Rice --- src/dataworker/Dataworker.ts | 25 ++++++++++++++---------- test/Dataworker.executePoolRebalances.ts | 16 +++++++-------- 2 files changed, 23 insertions(+), 18 deletions(-) diff --git a/src/dataworker/Dataworker.ts b/src/dataworker/Dataworker.ts index 44b243078..82473c4c6 100644 --- a/src/dataworker/Dataworker.ts +++ b/src/dataworker/Dataworker.ts @@ -1554,7 +1554,7 @@ export class Dataworker { const l1TokensWithPotentiallyOlderUpdate = expectedTrees.poolRebalanceTree.leaves.reduce((l1TokenSet, leaf) => { const currLeafL1Tokens = leaf.l1Tokens; currLeafL1Tokens.forEach((l1Token) => { - if (!l1TokenSet[l1Token] && !updatedL1Tokens.has(l1Token)) { + if (!l1TokenSet.includes(l1Token) && !updatedL1Tokens.has(l1Token)) { l1TokenSet.push(l1Token); } }); @@ -1727,7 +1727,7 @@ export class Dataworker { if (currentLiquidReserves.gte(netSendAmounts[idx])) { this.logger.debug({ at: "Dataworker#_updateExchangeRatesBeforeExecutingHubChainLeaves", - message: `Skipping exchange rate update for ${tokenSymbol} because current liquid reserves > netSendAmount`, + message: `Skipping exchange rate update for ${tokenSymbol} because current liquid reserves > netSendAmount for hubChain`, currentLiquidReserves, netSendAmount: netSendAmounts[idx], l1Token, @@ -1751,7 +1751,7 @@ export class Dataworker { this.logger.debug({ at: "Dataworker#_updateExchangeRatesBeforeExecutingHubChainLeaves", - message: `Updating exchange rate update for ${tokenSymbol} because we need to update the liquid reserves of the contract to execute the poolRebalanceLeaf.`, + message: `Updating exchange rate update for ${tokenSymbol} because we need to update the liquid reserves of the contract to execute the hubChain poolRebalanceLeaf.`, poolRebalanceLeaf, netSendAmount: netSendAmounts[idx], currentPooledTokens, @@ -1776,7 +1776,7 @@ export class Dataworker { async _updateExchangeRatesBeforeExecutingNonHubChainLeaves( latestLiquidReserves: Record, balanceAllocator: BalanceAllocator, - poolRebalanceLeaves: Pick[], + poolRebalanceLeaves: Pick[], submitExecution: boolean ): Promise> { const updatedL1Tokens = new Set(); @@ -1813,7 +1813,8 @@ export class Dataworker { if (currHubPoolLiquidReserves.gte(leaf.netSendAmounts[idx])) { this.logger.debug({ at: "Dataworker#_updateExchangeRatesBeforeExecutingNonHubChainLeaves", - message: `Skipping exchange rate update for ${tokenSymbol} because current liquid reserves > netSendAmount`, + message: `Skipping exchange rate update for ${tokenSymbol} because current liquid reserves > netSendAmount for chain ${leaf.chainId}`, + l2ChainId: leaf.chainId, currHubPoolLiquidReserves, netSendAmount: leaf.netSendAmounts[idx], l1Token, @@ -1876,16 +1877,18 @@ export class Dataworker { const tokenSymbol = this.clients.hubPoolClient.getTokenInfo(chainId, l1Token)?.symbol; // Exit early if we recently synced this token. - const lastestFeesCompoundedTime = + const latestFeesCompoundedTime = this.clients.hubPoolClient.getLpTokenInfoForL1Token(l1Token)?.lastLpFeeUpdate ?? 0; + // Force update every 2 days: if ( this.clients.hubPoolClient.currentTime === undefined || - this.clients.hubPoolClient.currentTime - lastestFeesCompoundedTime <= 2 * 24 * 60 * 60 // 2 day + this.clients.hubPoolClient.currentTime - latestFeesCompoundedTime <= 2 * 24 * 60 * 60 ) { + const timeToNextUpdate = 2 * 24 * 60 * 60 - (this.clients.hubPoolClient.currentTime - latestFeesCompoundedTime); this.logger.debug({ at: "Dataworker#_updateOldExchangeRates", - message: `Skipping exchange rate update for ${tokenSymbol} because it was recently updated`, - lastUpdateTime: lastestFeesCompoundedTime, + message: `Skipping exchange rate update for ${tokenSymbol} because it was recently updated. Seconds to next update: ${timeToNextUpdate}s`, + lastUpdateTime: latestFeesCompoundedTime, }); return; } @@ -1913,7 +1916,9 @@ export class Dataworker { this.logger.debug({ at: "Dataworker#_updateOldExchangeRates", message: `Updating exchange rate for ${tokenSymbol}`, - lastUpdateTime: lastestFeesCompoundedTime, + lastUpdateTime: latestFeesCompoundedTime, + currentLiquidReserves, + updatedLiquidReserves, l1Token, }); if (submitExecution) { diff --git a/test/Dataworker.executePoolRebalances.ts b/test/Dataworker.executePoolRebalances.ts index d80df387e..eebd8ea61 100644 --- a/test/Dataworker.executePoolRebalances.ts +++ b/test/Dataworker.executePoolRebalances.ts @@ -244,7 +244,7 @@ describe("Dataworker: Execute pool rebalances", async function () { const updated = await dataworkerInstance._updateExchangeRatesBeforeExecutingNonHubChainLeaves( {}, balanceAllocator, - [{ netSendAmounts: [toBNWei(-1)], l1Tokens: [l1Token_1.address] }], + [{ netSendAmounts: [toBNWei(-1)], l1Tokens: [l1Token_1.address], chainId: 1 }], true ); expect(updated.size).to.equal(0); @@ -259,7 +259,7 @@ describe("Dataworker: Execute pool rebalances", async function () { const updated = await dataworkerInstance._updateExchangeRatesBeforeExecutingNonHubChainLeaves( {}, balanceAllocator, - [{ netSendAmounts: [netSendAmount], l1Tokens: [l1Token_1.address] }], + [{ netSendAmounts: [netSendAmount], l1Tokens: [l1Token_1.address], chainId: 1 }], true ); expect(updated.size).to.equal(0); @@ -274,7 +274,7 @@ describe("Dataworker: Execute pool rebalances", async function () { [l1Token_1.address]: liquidReserves, }, balanceAllocator, - [{ netSendAmounts: [netSendAmount], l1Tokens: [l1Token_1.address] }], + [{ netSendAmounts: [netSendAmount], l1Tokens: [l1Token_1.address], chainId: 1 }], true ); expect(updated.size).to.equal(0); @@ -288,7 +288,7 @@ describe("Dataworker: Execute pool rebalances", async function () { const updated = await dataworkerInstance._updateExchangeRatesBeforeExecutingNonHubChainLeaves( {}, balanceAllocator, - [{ netSendAmounts: [netSendAmount], l1Tokens: [l1Token_1.address] }], + [{ netSendAmounts: [netSendAmount], l1Tokens: [l1Token_1.address], chainId: 1 }], true ); expect(lastSpyLogLevel(spy)).to.equal("error"); @@ -307,7 +307,7 @@ describe("Dataworker: Execute pool rebalances", async function () { const updated = await dataworkerInstance._updateExchangeRatesBeforeExecutingNonHubChainLeaves( {}, balanceAllocator, - [{ netSendAmounts: [netSendAmount], l1Tokens: [l1Token_1.address] }], + [{ netSendAmounts: [netSendAmount], l1Tokens: [l1Token_1.address], chainId: 1 }], true ); expect(updated.size).to.equal(1); @@ -326,7 +326,7 @@ describe("Dataworker: Execute pool rebalances", async function () { [l1Token_1.address]: liquidReserves, }, balanceAllocator, - [{ netSendAmounts: [netSendAmount], l1Tokens: [l1Token_1.address] }], + [{ netSendAmounts: [netSendAmount], l1Tokens: [l1Token_1.address], chainId: 1 }], true ); expect(updated.size).to.equal(1); @@ -346,8 +346,8 @@ describe("Dataworker: Execute pool rebalances", async function () { }, balanceAllocator, [ - { netSendAmounts: [netSendAmount], l1Tokens: [l1Token_1.address] }, - { netSendAmounts: [netSendAmount], l1Tokens: [l1Token_1.address] }, + { netSendAmounts: [netSendAmount], l1Tokens: [l1Token_1.address], chainId: 1 }, + { netSendAmounts: [netSendAmount], l1Tokens: [l1Token_1.address], chainId: 1 }, ], true );