Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve: relay calculator clients #1543

Merged
merged 4 commits into from
May 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"dependencies": {
"@across-protocol/constants-v2": "1.0.25",
"@across-protocol/contracts-v2": "2.5.6",
"@across-protocol/sdk-v2": "0.23.11",
"@across-protocol/sdk-v2": "0.24.0",
"@arbitrum/sdk": "^3.1.3",
"@consensys/linea-sdk": "^0.2.1",
"@defi-wonderland/smock": "^2.3.5",
Expand Down
33 changes: 3 additions & 30 deletions src/clients/ProfitClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,35 +84,6 @@ type UnprofitableFill = {
// relayer's own address. The specified address is deliberately setup by RL to have a 0 token balance.
const TEST_RECIPIENT = "0xBb23Cd0210F878Ea4CcA50e9dC307fb0Ed65Cf6B";

// These are used to simulate fills on L2s to return estimated gas costs.
// Note: the type here assumes that all of these classes take the same constructor parameters.
const QUERY_HANDLERS: {
[chainId: number]: new (
...args: ConstructorParameters<typeof relayFeeCalculator.BaseQueries>
) => relayFeeCalculator.QueryInterface;
} = {
1: relayFeeCalculator.EthereumQueries,
10: relayFeeCalculator.OptimismQueries,
137: relayFeeCalculator.PolygonQueries,
288: relayFeeCalculator.BobaQueries,
324: relayFeeCalculator.ZkSyncQueries,
8453: relayFeeCalculator.BaseQueries,
42161: relayFeeCalculator.ArbitrumQueries,
59144: relayFeeCalculator.LineaQueries,
// Testnets:
5: relayFeeCalculator.EthereumGoerliQueries,
280: relayFeeCalculator.zkSyncGoerliQueries,
420: relayFeeCalculator.OptimismGoerliQueries,
59140: relayFeeCalculator.LineaGoerliQueries,
80001: relayFeeCalculator.PolygonMumbaiQueries,
84531: relayFeeCalculator.BaseGoerliQueries,
84532: relayFeeCalculator.BaseSepoliaQueries,
421613: relayFeeCalculator.ArbitrumGoerliQueries,
421614: relayFeeCalculator.ArbitrumSepoliaQueries,
11155111: relayFeeCalculator.EthereumSepoliaQueries,
11155420: relayFeeCalculator.OptimismSepoliaQueries,
};

const { PriceClient } = priceClient;
const { acrossApi, coingecko, defiLlama } = priceClient.adapters;

Expand Down Expand Up @@ -648,7 +619,9 @@ export class ProfitClient {
const coingeckoProApiKey = undefined;
// TODO: Set this once we figure out gas markup on the API side.
const gasMarkup = 0;
return new QUERY_HANDLERS[chainId](
// Call the factory to create a new QueryBase instance.
return relayFeeCalculator.QueryBase__factory.create(
chainId,
provider,
undefined, // symbolMapping
undefined, // spokePoolAddress
Expand Down
2 changes: 1 addition & 1 deletion src/clients/TokenClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import {
TOKEN_SYMBOLS_MAP,
} from "../utils";

type TokenDataType = { [chainId: number]: { [token: string]: { balance: BigNumber; allowance: BigNumber } } };
export type TokenDataType = { [chainId: number]: { [token: string]: { balance: BigNumber; allowance: BigNumber } } };
type TokenShortfallType = {
[chainId: number]: { [token: string]: { deposits: number[]; totalRequirement: BigNumber } };
};
Expand Down
54 changes: 45 additions & 9 deletions test/Dataworker.loadData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,21 @@ import {
} from "./utils";

import { Dataworker } from "../src/dataworker/Dataworker"; // Tested
import { getCurrentTime, toBN, Event, bnZero, toBNWei, fixedPointAdjustment, assert, ZERO_ADDRESS } from "../src/utils";
import {
getCurrentTime,
toBN,
Event,
bnZero,
toBNWei,
fixedPointAdjustment,
assert,
ZERO_ADDRESS,
BigNumber,
} from "../src/utils";
import { MockHubPoolClient, MockSpokePoolClient } from "./mocks";
import { interfaces, utils as sdkUtils } from "@across-protocol/sdk-v2";
import { cloneDeep } from "lodash";
import { CombinedRefunds } from "../src/dataworker/DataworkerUtils";

let spokePool_1: Contract, erc20_1: Contract, spokePool_2: Contract, erc20_2: Contract;
let l1Token_1: Contract;
Expand Down Expand Up @@ -1188,30 +1199,55 @@ describe("Dataworker: Load data used in all functions", async function () {

// Approximate refunds should count both fills
await updateAllClients();
const refunds = await bundleDataClient.getApproximateRefundsForBlockRange(
const refunds = bundleDataClient.getApproximateRefundsForBlockRange(
[originChainId, destinationChainId],
getDefaultBlockRange(5)
);
expect(refunds).to.deep.equal({
const expectedRefunds = {
[originChainId]: {
[erc20_1.address]: {
[relayer.address]: amountToDeposit.mul(2),
[relayer.address]: BigNumber.from(amountToDeposit.mul(2)).toString(),
},
},
});
};

// Convert refunds to have a nested string instead of BigNumber. It's three levels deep
// which is a bit ugly but it's the easiest way to compare the two objects that are having
// these BN issues.
const convertToNumericStrings = (data: CombinedRefunds) =>
Object.entries(data).reduce(
(acc, [chainId, refunds]) => ({
...acc,
[chainId]: Object.entries(refunds).reduce(
(acc, [token, refunds]) => ({
...acc,
[token]: Object.entries(refunds).reduce(
(acc, [address, amount]) => ({ ...acc, [address]: amount.toString() }),
{}
),
}),
{}
),
}),
{}
);

expect(convertToNumericStrings(refunds)).to.deep.equal(expectedRefunds);

// Send an invalid fill and check it is not included.
await fillV3(spokePool_1, relayer, { ...deposit1, depositId: deposit1.depositId + 1 }, originChainId);
await updateAllClients();
expect(
await bundleDataClient.getApproximateRefundsForBlockRange(
[originChainId, destinationChainId],
getDefaultBlockRange(5)
convertToNumericStrings(
bundleDataClient.getApproximateRefundsForBlockRange(
[originChainId, destinationChainId],
getDefaultBlockRange(5)
)
)
).to.deep.equal({
[originChainId]: {
[erc20_1.address]: {
[relayer.address]: amountToDeposit.mul(2),
[relayer.address]: amountToDeposit.mul(2).toString(),
},
},
});
Expand Down
16 changes: 13 additions & 3 deletions test/TokenClient.BalanceAlowance.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ConfigStoreClient, SpokePoolClient, TokenClient } from "../src/clients"; // Tested
import { ConfigStoreClient, SpokePoolClient, TokenClient, TokenDataType } from "../src/clients"; // Tested
import { originChainId, destinationChainId, ZERO_ADDRESS } from "./constants";
import { MockHubPoolClient } from "./mocks";
import {
Expand All @@ -10,6 +10,7 @@ import {
deploySpokePoolWithToken,
ethers,
expect,
toBN,
toBNWei,
winston,
} from "./utils";
Expand Down Expand Up @@ -103,6 +104,15 @@ describe("TokenClient: Balance and Allowance", async function () {
});

it("Fetches all associated balances", async function () {
const alignTokenData = (data: TokenDataType): TokenDataType =>
Object.entries(data).reduce((acc, [chainId, tokenData]) => {
acc[chainId] = Object.entries(tokenData).reduce((acc, [token, { balance, allowance }]) => {
acc[token] = { balance: toBN(balance), allowance: toBN(allowance) };
return acc;
}, {});
return acc;
}, {});

await updateAllClients();
const expectedData = {
[originChainId]: {
Expand All @@ -115,7 +125,7 @@ describe("TokenClient: Balance and Allowance", async function () {
},
};
const tokenData = tokenClient.getAllTokenData();
expect(tokenData).to.deep.equal(expectedData);
expect(alignTokenData(tokenData)).to.deep.equal(expectedData);

// Check some balance/allowances directly.
expect(tokenClient.getBalance(originChainId, erc20_1.address)).to.equal(toBNWei(0));
Expand All @@ -139,7 +149,7 @@ describe("TokenClient: Balance and Allowance", async function () {
},
};
const tokenData1 = tokenClient.getAllTokenData();
expect(tokenData1).to.deep.equal(expectedData1);
expect(alignTokenData(tokenData1)).to.deep.equal(expectedData1);

expect(tokenClient.getBalance(originChainId, erc20_1.address)).to.equal(toBNWei(42069));
expect(tokenClient.getBalance(destinationChainId, weth_2.address)).to.equal(toBNWei(1337));
Expand Down
40 changes: 30 additions & 10 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@
resolved "https://registry.yarnpkg.com/@across-protocol/constants-v2/-/constants-v2-1.0.20.tgz#d28fb3d3be8514db51c214d92bee954d15e5f06a"
integrity sha512-DYb48kaAzv7t4/FVi4a5KYGNxJSG2rOY2mSznuWDI4n4mezM1wTTtQzK3un15tcwkTlIeO13CaDeABgR6jIVvg==

"@across-protocol/constants-v2@^1.0.20":
version "1.0.23"
resolved "https://registry.yarnpkg.com/@across-protocol/constants-v2/-/constants-v2-1.0.23.tgz#271fca7e93104c4a7591615da08d920ccab43df1"
integrity sha512-46G5pftiGqdl6W2YFQDaRxv7hYx/GMW0EvMOgbVwyjUUEJbf4s5+NbUUl0QBdlIfR5gvrsS2Vbts94UkSwrmAA==
"@across-protocol/constants-v2@^1.0.26":
version "1.0.26"
resolved "https://registry.yarnpkg.com/@across-protocol/constants-v2/-/constants-v2-1.0.26.tgz#04fff84574af9fe4128318a19d7b7990ee10f32e"
integrity sha512-XuxBGNvhevdTT+GQGreg1YP+RsRWzuqOEHNA9m/p7l3qjFkXuWUM9nj77ttccbIi0tCZQDiMR8VxwHBCAXB4LQ==

"@across-protocol/[email protected]":
version "2.5.6"
Expand All @@ -46,6 +46,26 @@
axios "^1.6.2"
zksync-web3 "^0.14.3"

"@across-protocol/[email protected]":
version "2.5.7"
resolved "https://registry.yarnpkg.com/@across-protocol/contracts-v2/-/contracts-v2-2.5.7.tgz#5bafbda17a4196593130c175fa6dc5d3b9d9f88d"
integrity sha512-MsvwUfjLiNmGHdVvTQxPHaOIt5U3bWRC99aM2cTzTw629ktH13G5PsCUgAPmPyXqm3WtyQqIOOcEuw5Sz0BAtA==
dependencies:
"@across-protocol/constants-v2" "^1.0.26"
"@defi-wonderland/smock" "^2.3.4"
"@eth-optimism/contracts" "^0.5.40"
"@ethersproject/abstract-provider" "5.7.0"
"@ethersproject/abstract-signer" "5.7.0"
"@ethersproject/bignumber" "5.7.0"
"@openzeppelin/contracts" "4.9.6"
"@openzeppelin/contracts-upgradeable" "4.9.6"
"@scroll-tech/contracts" "^0.1.0"
"@uma/common" "^2.34.0"
"@uma/contracts-node" "^0.4.17"
"@uma/core" "^2.56.0"
axios "^1.6.2"
zksync-web3 "^0.14.3"

"@across-protocol/contracts@^0.1.4":
version "0.1.4"
resolved "https://registry.yarnpkg.com/@across-protocol/contracts/-/contracts-0.1.4.tgz#64b3d91e639d2bb120ea94ddef3d160967047fa5"
Expand All @@ -55,14 +75,14 @@
"@openzeppelin/contracts" "4.1.0"
"@uma/core" "^2.18.0"

"@across-protocol/sdk-v2@0.23.11":
version "0.23.11"
resolved "https://registry.yarnpkg.com/@across-protocol/sdk-v2/-/sdk-v2-0.23.11.tgz#1deee92af37ae7fa7849b0dafadebd6f10260a8c"
integrity sha512-P1WQGJJ7+sI+l7wRGQx8Uxwn9Qmu0U8FsWmPSzGn9zbM1Jc7tUnKWJo2waPGCF3Og062oRDExD/QUwCqZxI5kw==
"@across-protocol/sdk-v2@0.24.0":
version "0.24.0"
resolved "https://registry.yarnpkg.com/@across-protocol/sdk-v2/-/sdk-v2-0.24.0.tgz#492dfe354e8bc12425243485da49c6f3a3af7122"
integrity sha512-WPG4bTffNAF6EmS24BdrUc2Du8DBKhxTd58ffTyd3M9EbCJehPZyE8qptaHqDXgMUNoSjua+hf7KznGku7LSQA==
dependencies:
"@across-protocol/across-token" "^1.0.0"
"@across-protocol/constants-v2" "^1.0.20"
"@across-protocol/contracts-v2" "2.5.6"
"@across-protocol/constants-v2" "^1.0.26"
"@across-protocol/contracts-v2" "2.5.7"
"@eth-optimism/sdk" "^3.2.2"
"@pinata/sdk" "^2.1.0"
"@types/mocha" "^10.0.1"
Expand Down
Loading