Skip to content

Commit

Permalink
Merge branch 'master' into pxrl/importPlumbing
Browse files Browse the repository at this point in the history
  • Loading branch information
pxrl committed Sep 29, 2023
2 parents cfdb9fb + b9e6566 commit 9894393
Show file tree
Hide file tree
Showing 34 changed files with 321 additions and 567 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
},
"dependencies": {
"@across-protocol/contracts-v2": "2.4.3",
"@across-protocol/sdk-v2": "0.15.24",
"@across-protocol/sdk-v2": "0.16.4",
"@arbitrum/sdk": "^3.1.3",
"@defi-wonderland/smock": "^2.3.5",
"@eth-optimism/sdk": "^3.1.0",
Expand Down
19 changes: 18 additions & 1 deletion src/clients/HubPoolClient.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { clients } from "@across-protocol/sdk-v2";
import { Contract } from "ethers";
import { BigNumber, Contract } from "ethers";
import winston from "winston";
import { MakeOptional, EventSearchConfig } from "../utils";
import { IGNORED_HUB_EXECUTED_BUNDLES, IGNORED_HUB_PROPOSED_BUNDLES } from "../common";
import { DepositWithBlock } from "../interfaces";

export class HubPoolClient extends clients.HubPoolClient {
constructor(
Expand All @@ -18,4 +19,20 @@ export class HubPoolClient extends clients.HubPoolClient {
ignoredHubProposedBundles: IGNORED_HUB_PROPOSED_BUNDLES,
});
}

async computeRealizedLpFeePct(
deposit: Pick<
DepositWithBlock,
"quoteTimestamp" | "amount" | "destinationChainId" | "originChainId" | "blockNumber"
>,
l1Token: string
): Promise<{ realizedLpFeePct: BigNumber | undefined; quoteBlock: number }> {
if (deposit.quoteTimestamp > this.currentTime) {
throw new Error(
`Cannot compute lp fee percent for quote timestamp ${deposit.quoteTimestamp} in the future. Current time: ${this.currentTime}.`
);
}

return await super.computeRealizedLpFeePct(deposit, l1Token);
}
}
12 changes: 11 additions & 1 deletion src/clients/SpokePoolClient.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
import { clients } from "@across-protocol/sdk-v2";
import { FundsDepositedEvent } from "../interfaces";
import { isDefined } from "../utils/TypeGuards";

export class SpokePoolClient extends clients.SpokePoolClient {}
export class SpokePoolClient extends clients.SpokePoolClient {
_isEarlyDeposit(depositEvent: FundsDepositedEvent, currentTime: number): boolean {
const hubCurrentTime = this.hubPoolClient?.currentTime;
if (!isDefined(hubCurrentTime)) {
throw new Error("HubPoolClient's currentTime is not defined");
}
return depositEvent.args.quoteTimestamp > currentTime || depositEvent.args.quoteTimestamp > hubCurrentTime;
}
}
4 changes: 2 additions & 2 deletions src/clients/TokenClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,12 +93,12 @@ export class TokenClient {
// requirement to send all seen relays and the total remaining balance of the relayer.
getTokenShortfall(): {
[chainId: number]: {
[token: string]: { balance: BigNumber; needed: BigNumber; shortfall: BigNumber; deposits: BigNumber };
[token: string]: { balance: BigNumber; needed: BigNumber; shortfall: BigNumber; deposits: number[] };
};
} {
const tokenShortfall: {
[chainId: number]: {
[token: string]: { balance: BigNumber; needed: BigNumber; shortfall: BigNumber; deposits: BigNumber };
[token: string]: { balance: BigNumber; needed: BigNumber; shortfall: BigNumber; deposits: number[] };
};
} = {};
Object.entries(this.tokenShortfall).forEach(([_chainId, tokenMap]) => {
Expand Down
27 changes: 16 additions & 11 deletions src/dataworker/Dataworker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import {
RunningBalances,
PoolRebalanceLeaf,
RelayerRefundLeaf,
BigNumberForToken,
} from "../interfaces";
import { DataworkerClients } from "./DataworkerClientHelper";
import { SpokePoolClient, UBAClient, BalanceAllocator } from "../clients";
Expand Down Expand Up @@ -92,7 +91,6 @@ export class Dataworker {
readonly chainIdListForBundleEvaluationBlockNumbers: number[],
readonly maxRefundCountOverride: number | undefined,
readonly maxL1TokenCountOverride: number | undefined,
readonly tokenTransferThreshold: BigNumberForToken = {},
readonly blockRangeEndBlockBuffer: { [chainId: number]: number } = {},
readonly spokeRootsLookbackCount = 0,
readonly bufferToPropose = 0,
Expand All @@ -102,7 +100,6 @@ export class Dataworker {
if (
maxRefundCountOverride !== undefined ||
maxL1TokenCountOverride !== undefined ||
Object.keys(tokenTransferThreshold).length > 0 ||
Object.keys(blockRangeEndBlockBuffer).length > 0
) {
this.logger.debug({
Expand All @@ -111,7 +108,6 @@ export class Dataworker {
chainIdListForBundleEvaluationBlockNumbers,
maxRefundCountOverride: this.maxRefundCountOverride,
maxL1TokenCountOverride: this.maxL1TokenCountOverride,
tokenTransferThreshold: this.tokenTransferThreshold,
blockRangeEndBlockBuffer: this.blockRangeEndBlockBuffer,
});
}
Expand Down Expand Up @@ -157,8 +153,7 @@ export class Dataworker {
poolRebalanceLeaves,
runningBalances,
this.clients,
maxRefundCount,
this.tokenTransferThreshold
maxRefundCount
);
}

Expand Down Expand Up @@ -531,8 +526,7 @@ export class Dataworker {
this.clients,
this.maxRefundCountOverride
? this.maxRefundCountOverride
: this.clients.configStoreClient.getMaxRefundCountForRelayerRefundLeafForBlock(mainnetBundleEndBlock),
this.tokenTransferThreshold
: this.clients.configStoreClient.getMaxRefundCountForRelayerRefundLeafForBlock(mainnetBundleEndBlock)
);
const slowRelayRoot = _buildSlowRelayRoot(unfilledDeposits);

Expand Down Expand Up @@ -729,7 +723,6 @@ export class Dataworker {
poolRebalanceLeafData.bundleLpFees,
this.clients.configStoreClient,
this.maxL1TokenCountOverride,
this.tokenTransferThreshold,
poolRebalanceLeafData.incentivePoolBalances,
poolRebalanceLeafData.netSendAmounts,
true
Expand Down Expand Up @@ -804,7 +797,6 @@ export class Dataworker {
this.maxRefundCountOverride
? this.maxRefundCountOverride
: this.clients.configStoreClient.getMaxRefundCountForRelayerRefundLeafForBlock(mainnetBundleEndBlock),
this.tokenTransferThreshold,
true // Instruct function to always set amountToReturn = -netSendAmount iff netSendAmount < 0
);
return relayerRefundRoot;
Expand Down Expand Up @@ -1138,12 +1130,26 @@ export class Dataworker {
reason: "out-of-date-config-store-version",
};
}

let rootBundleData: ProposeRootBundleReturnType;
const mainnetBundleStartBlock = getBlockRangeForChain(
blockRangesImpliedByBundleEndBlocks,
hubPoolChainId,
this.chainIdListForBundleEvaluationBlockNumbers
)[0];

// Check if we have the right code to validate a bundle for the given block ranges.
const versionAtProposalBlock =
this.clients.configStoreClient.getConfigStoreVersionForBlock(mainnetBundleStartBlock);

// Bundles that need to be validated with older code should emit helpful error logs about which code to run.
// @dev only throw this error if the hub chain ID is 1, suggesting we're running on production.
if (versionAtProposalBlock <= sdk.constants.TRANSFER_THRESHOLD_MAX_CONFIG_STORE_VERSION && hubPoolChainId === 1) {
throw new Error(
"Must use relayer-v2 code at commit 412ddc30af72c2ac78f9e4c8dccfccfd0eb478ab to validate a bundle with transferThreshold set"
);
}

let isUBA = false;
if (
sdk.clients.isUBAActivatedAtBlock(
Expand Down Expand Up @@ -2248,7 +2254,6 @@ export class Dataworker {
spokePoolClients,
this.chainIdListForBundleEvaluationBlockNumbers,
this.maxL1TokenCountOverride,
this.tokenTransferThreshold,
logSlowFillExcessData ? this.logger : undefined
);
}
Expand Down
5 changes: 0 additions & 5 deletions src/dataworker/DataworkerConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { BigNumber, assert, toBNWei } from "../utils";
export class DataworkerConfig extends CommonConfig {
readonly maxPoolRebalanceLeafSizeOverride: number;
readonly maxRelayerRepaymentLeafSizeOverride: number;
readonly tokenTransferThresholdOverride: { [l1TokenAddress: string]: BigNumber };
readonly rootBundleExecutionThreshold: BigNumber;
readonly spokeRootsLookbackCount: number; // Consider making this configurable per chain ID.

Expand Down Expand Up @@ -42,7 +41,6 @@ export class DataworkerConfig extends CommonConfig {
constructor(env: ProcessEnv) {
const {
ROOT_BUNDLE_EXECUTION_THRESHOLD,
TOKEN_TRANSFER_THRESHOLD_OVERRIDE,
MAX_POOL_REBALANCE_LEAF_SIZE_OVERRIDE,
MAX_RELAYER_REPAYMENT_LEAF_SIZE_OVERRIDE,
DISPUTER_ENABLED,
Expand Down Expand Up @@ -76,9 +74,6 @@ export class DataworkerConfig extends CommonConfig {
if (this.maxRelayerRepaymentLeafSizeOverride !== undefined) {
assert(this.maxRelayerRepaymentLeafSizeOverride > 0, "Max leaf count set to 0");
}
this.tokenTransferThresholdOverride = TOKEN_TRANSFER_THRESHOLD_OVERRIDE
? JSON.parse(TOKEN_TRANSFER_THRESHOLD_OVERRIDE)
: {};
this.rootBundleExecutionThreshold = ROOT_BUNDLE_EXECUTION_THRESHOLD
? toBNWei(ROOT_BUNDLE_EXECUTION_THRESHOLD)
: toBNWei("500000");
Expand Down
16 changes: 2 additions & 14 deletions src/dataworker/DataworkerUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { SpokePoolClient } from "../clients";
import { spokesThatHoldEthAndWeth } from "../common/Constants";
import { CONTRACT_ADDRESSES } from "../common/ContractAddresses";
import {
BigNumberForToken,
DepositWithBlock,
FillsToRefund,
FillWithBlock,
Expand Down Expand Up @@ -192,7 +191,6 @@ export function _buildRelayerRefundRoot(
runningBalances: RunningBalances,
clients: DataworkerClients,
maxRefundCount: number,
tokenTransferThresholdOverrides: BigNumberForToken,
isUBA = false
): {
leaves: RelayerRefundLeaf[];
Expand Down Expand Up @@ -226,9 +224,6 @@ export function _buildRelayerRefundRoot(
l2TokenAddress,
endBlockForMainnet
);
const transferThreshold =
tokenTransferThresholdOverrides[l1TokenCounterpart] ||
clients.configStoreClient.getTokenTransferThresholdForBlock(l1TokenCounterpart, endBlockForMainnet);

const spokePoolTargetBalance = clients.configStoreClient.getSpokeTargetBalancesForBlock(
l1TokenCounterpart,
Expand All @@ -238,7 +233,6 @@ export function _buildRelayerRefundRoot(

// The `amountToReturn` for a { repaymentChainId, L2TokenAddress} should be set to max(-netSendAmount, 0).
amountToReturn = getAmountToReturnForRelayerRefundLeaf(
transferThreshold,
spokePoolTargetBalance,
runningBalances[repaymentChainId][l1TokenCounterpart]
);
Expand Down Expand Up @@ -285,18 +279,13 @@ export function _buildRelayerRefundRoot(
// If UBA model we don't need to do the following to figure out the amount to return:
let amountToReturn = netSendAmount.mul(-1);
if (!isUBA) {
const transferThreshold =
tokenTransferThresholdOverrides[leaf.l1Tokens[index]] ||
clients.configStoreClient.getTokenTransferThresholdForBlock(leaf.l1Tokens[index], endBlockForMainnet);

const spokePoolTargetBalance = clients.configStoreClient.getSpokeTargetBalancesForBlock(
leaf.l1Tokens[index],
leaf.chainId,
endBlockForMainnet
);

amountToReturn = getAmountToReturnForRelayerRefundLeaf(
transferThreshold,
spokePoolTargetBalance,
runningBalances[leaf.chainId][leaf.l1Tokens[index]]
);
Expand Down Expand Up @@ -334,7 +323,6 @@ export async function _buildPoolRebalanceRoot(
spokePoolClients: SpokePoolClientsByChain,
chainIdListForBundleEvaluationBlockNumbers: number[],
maxL1TokenCountOverride: number | undefined,
tokenTransferThreshold: BigNumberForToken,
logger?: winston.Logger
): Promise<PoolRebalanceRoot> {
// Running balances are the amount of tokens that we need to send to each SpokePool to pay for all instant and
Expand Down Expand Up @@ -369,6 +357,7 @@ export async function _buildPoolRebalanceRoot(
allValidFills,
allValidFillsInRange
);

if (logger && Object.keys(fillsTriggeringExcesses).length > 0) {
logger.debug({
at: "Dataworker#DataworkerUtils",
Expand Down Expand Up @@ -407,8 +396,7 @@ export async function _buildPoolRebalanceRoot(
runningBalances,
realizedLpFees,
clients.configStoreClient,
maxL1TokenCountOverride,
tokenTransferThreshold
maxL1TokenCountOverride
);

return {
Expand Down
27 changes: 4 additions & 23 deletions src/dataworker/PoolRebalanceUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { ConfigStoreClient, HubPoolClient, SpokePoolClient } from "../clients";
import { Clients } from "../common";
import * as interfaces from "../interfaces";
import {
BigNumberForToken,
PendingRootBundle,
PoolRebalanceLeaf,
RelayerRefundLeaf,
Expand Down Expand Up @@ -300,7 +299,6 @@ export function constructPoolRebalanceLeaves(
realizedLpFees: interfaces.RunningBalances,
configStoreClient: ConfigStoreClient,
maxL1TokenCount?: number,
tokenTransferThreshold?: BigNumberForToken,
incentivePoolBalances?: interfaces.RunningBalances,
netSendAmounts?: interfaces.RunningBalances,
ubaMode = false
Expand All @@ -326,12 +324,6 @@ export function constructPoolRebalanceLeaves(
for (let i = 0; i < sortedL1Tokens.length; i += maxL1TokensPerLeaf) {
const l1TokensToIncludeInThisLeaf = sortedL1Tokens.slice(i, i + maxL1TokensPerLeaf);

const transferThresholds = l1TokensToIncludeInThisLeaf.map(
(l1Token) =>
tokenTransferThreshold[l1Token] ||
configStoreClient.getTokenTransferThresholdForBlock(l1Token, latestMainnetBlock)
);

const spokeTargetBalances = l1TokensToIncludeInThisLeaf.map((l1Token) =>
configStoreClient.getSpokeTargetBalancesForBlock(l1Token, Number(chainId), latestMainnetBlock)
);
Expand All @@ -349,11 +341,7 @@ export function constructPoolRebalanceLeaves(
if (ubaMode && netSendAmounts?.[chainId] && netSendAmounts[chainId][l1Token]) {
return netSendAmounts[chainId][l1Token];
} else if (runningBalances[chainId] && runningBalances[chainId][l1Token]) {
return getNetSendAmountForL1Token(
transferThresholds[index],
spokeTargetBalances[index],
runningBalances[chainId][l1Token]
);
return getNetSendAmountForL1Token(spokeTargetBalances[index], runningBalances[chainId][l1Token]);
} else {
return toBN(0);
}
Expand All @@ -366,11 +354,7 @@ export function constructPoolRebalanceLeaves(
if (ubaMode) {
return runningBalances[chainId][l1Token];
} else {
return getRunningBalanceForL1Token(
transferThresholds[index],
spokeTargetBalances[index],
runningBalances[chainId][l1Token]
);
return getRunningBalanceForL1Token(spokeTargetBalances[index], runningBalances[chainId][l1Token]);
}
} else {
return toBN(0);
Expand Down Expand Up @@ -438,21 +422,18 @@ export function computeDesiredTransferAmountToSpoke(
// 0, indicating that we do not want the data worker to trigger a token transfer between hub pool and spoke
// pool when executing this leaf.
export function getNetSendAmountForL1Token(
transferThreshold: BigNumber,
spokePoolTargetBalance: SpokePoolTargetBalance,
runningBalance: BigNumber
): BigNumber {
const desiredTransferAmount = computeDesiredTransferAmountToSpoke(runningBalance, spokePoolTargetBalance);
return desiredTransferAmount.abs().gte(transferThreshold) ? desiredTransferAmount : toBN(0);
return computeDesiredTransferAmountToSpoke(runningBalance, spokePoolTargetBalance);
}

export function getRunningBalanceForL1Token(
transferThreshold: BigNumber,
spokePoolTargetBalance: SpokePoolTargetBalance,
runningBalance: BigNumber
): BigNumber {
const desiredTransferAmount = computeDesiredTransferAmountToSpoke(runningBalance, spokePoolTargetBalance);
return desiredTransferAmount.abs().lt(transferThreshold) ? runningBalance : runningBalance.sub(desiredTransferAmount);
return runningBalance.sub(desiredTransferAmount);
}

// This returns a possible next block range that could be submitted as a new root bundle, or used as a reference
Expand Down
7 changes: 1 addition & 6 deletions src/dataworker/RelayerRefundUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,10 @@ import { BigNumber, compareAddresses, toBN } from "../utils";
import { getNetSendAmountForL1Token } from "./PoolRebalanceUtils";

export function getAmountToReturnForRelayerRefundLeaf(
transferThreshold: BigNumber,
spokePoolTargetBalance: SpokePoolTargetBalance,
runningBalanceForLeaf: BigNumber
): BigNumber {
const netSendAmountForLeaf = getNetSendAmountForL1Token(
transferThreshold,
spokePoolTargetBalance,
runningBalanceForLeaf
);
const netSendAmountForLeaf = getNetSendAmountForL1Token(spokePoolTargetBalance, runningBalanceForLeaf);
return netSendAmountForLeaf.mul(toBN(-1)).gt(toBN(0)) ? netSendAmountForLeaf.mul(toBN(-1)) : toBN(0);
}

Expand Down
1 change: 0 additions & 1 deletion src/dataworker/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ export async function createDataworker(
clients.configStoreClient.getChainIdIndicesForBlock(),
config.maxRelayerRepaymentLeafSizeOverride,
config.maxPoolRebalanceLeafSizeOverride,
config.tokenTransferThresholdOverride,
config.blockRangeEndBlockBuffer,
config.spokeRootsLookbackCount,
config.bufferToPropose,
Expand Down
1 change: 0 additions & 1 deletion src/interfaces/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ export type BigNumberForToken = interfaces.BigNumberForToken;

// ConfigStore interfaces
export type ParsedTokenConfig = interfaces.ParsedTokenConfig;
export type L1TokenTransferThreshold = interfaces.L1TokenTransferThreshold;
export type SpokePoolTargetBalance = interfaces.SpokePoolTargetBalance;
export type SpokeTargetBalanceUpdate = interfaces.SpokeTargetBalanceUpdate;
export type RouteRateModelUpdate = interfaces.RouteRateModelUpdate;
Expand Down
Loading

0 comments on commit 9894393

Please sign in to comment.