Skip to content

Commit

Permalink
improve(Dataworker): Make logs more clear when choosing to skip excha…
Browse files Browse the repository at this point in the history
…nge 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 <[email protected]>

---------

Co-authored-by: Matt Rice <[email protected]>
  • Loading branch information
nicholaspai and mrice32 authored Mar 22, 2024
1 parent 33471c4 commit 2a986a2
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 18 deletions.
25 changes: 15 additions & 10 deletions src/dataworker/Dataworker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
});
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -1776,7 +1776,7 @@ export class Dataworker {
async _updateExchangeRatesBeforeExecutingNonHubChainLeaves(
latestLiquidReserves: Record<string, BigNumber>,
balanceAllocator: BalanceAllocator,
poolRebalanceLeaves: Pick<PoolRebalanceLeaf, "netSendAmounts" | "l1Tokens">[],
poolRebalanceLeaves: Pick<PoolRebalanceLeaf, "netSendAmounts" | "l1Tokens" | "chainId">[],
submitExecution: boolean
): Promise<Set<string>> {
const updatedL1Tokens = new Set<string>();
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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) {
Expand Down
16 changes: 8 additions & 8 deletions test/Dataworker.executePoolRebalances.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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");
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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
);
Expand Down

0 comments on commit 2a986a2

Please sign in to comment.