Skip to content

Commit

Permalink
refactor: Cleanup test import/exports (#551)
Browse files Browse the repository at this point in the history
Primarily, avoid re-exporting the entirety of contracts-v2 from multiple
places in the test directory. This gives better control over what is
imported, and where from. Additionally, re-organise so that
test/constants only exports actual constants and not utility functions,
then relocate the functions to test/utils/utils.ts.
  • Loading branch information
pxrl committed Feb 12, 2024
1 parent 713f993 commit a201093
Show file tree
Hide file tree
Showing 13 changed files with 82 additions and 78 deletions.
4 changes: 2 additions & 2 deletions test/HubPoolClient.DepositToDestinationToken.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { Event } from "ethers";
import {
CONFIG_STORE_VERSION,
destinationChainId,
originChainId,
randomDestinationToken,
randomDestinationToken2,
randomL1Token,
Expand All @@ -12,11 +14,9 @@ import {
SignerWithAddress,
createSpyLogger,
deployConfigStore,
destinationChainId,
ethers,
expect,
getContractFactory,
originChainId,
zeroAddress,
} from "./utils";

Expand Down
6 changes: 3 additions & 3 deletions test/HubPoolClient.Utilization.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import {
amountToLp,
originChainId,
repaymentChainId,
destinationChainId,
mockTreeRoot,
randomAddress,
refundProposalLiveness,
totalBond,
} from "./constants";
Expand All @@ -20,8 +21,7 @@ import {
expect,
hubPoolFixture,
mineRandomBlocks,
originChainId,
repaymentChainId,
randomAddress,
setupTokensForWallet,
toBN,
toBNWei,
Expand Down
14 changes: 2 additions & 12 deletions test/SpokePoolClient.DepositRoutes.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,6 @@
import {
Contract,
createSpyLogger,
deploySpokePool,
destinationChainId,
enableRoutes,
ethers,
expect,
originChainId,
randomAddress,
} from "./utils";

import { SpokePoolClient } from "../src/clients"; // tested
import { originChainId, destinationChainId } from "./constants";
import { Contract, createSpyLogger, deploySpokePool, enableRoutes, ethers, expect, randomAddress } from "./utils";

let spokePool: Contract;

Expand Down
4 changes: 2 additions & 2 deletions test/SpokePoolClient.SpeedUp.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { depositRelayerFeePct, destinationChainId, modifyRelayHelper } from "./constants";
import { depositRelayerFeePct, destinationChainId, originChainId } from "./constants";
import {
Contract,
SignerWithAddress,
Expand All @@ -8,7 +8,7 @@ import {
enableRoutes,
ethers,
expect,
originChainId,
modifyRelayHelper,
setupTokensForWallet,
simpleDeposit,
toBNWei,
Expand Down
4 changes: 1 addition & 3 deletions test/SpokePoolClient.ValidateFill.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
validateFillForDeposit,
queryHistoricalDepositForFill,
} from "../src/utils";
import { CHAIN_ID_TEST_LIST, originChainId, destinationChainId, repaymentChainId } from "./constants";
import {
assert,
expect,
Expand All @@ -23,8 +24,6 @@ import {
buildModifiedFill,
deploySpokePoolWithToken,
Contract,
originChainId,
destinationChainId,
createSpyLogger,
zeroAddress,
deployAndConfigureHubPool,
Expand All @@ -38,7 +37,6 @@ import {
winston,
lastSpyLogIncludes,
} from "./utils";
import { CHAIN_ID_TEST_LIST, repaymentChainId } from "./constants";
import { MockConfigStoreClient, MockHubPoolClient, MockSpokePoolClient } from "./mocks";

let spokePool_1: Contract, erc20_1: Contract, spokePool_2: Contract, erc20_2: Contract, hubPool: Contract;
Expand Down
3 changes: 1 addition & 2 deletions test/SpokePoolClient.deposits.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
import { SpokePoolClient } from "../src/clients";
import { Deposit } from "../src/interfaces";
import { originChainId, destinationChainId } from "./constants";
import {
Contract,
SignerWithAddress,
buildFill,
createSpyLogger,
deploySpokePoolWithToken,
destinationChainId,
ethers,
expect,
originChainId,
setupTokensForWallet,
toBNWei,
} from "./utils";
Expand Down
3 changes: 1 addition & 2 deletions test/SpokePoolClient.fills.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { ZERO_ADDRESS, EMPTY_MESSAGE } from "../src/constants";
import { SpokePoolClient } from "../src/clients";
import { Deposit, V3Deposit } from "../src/interfaces";
import { bnOne, findFillBlock, getCurrentTime, getNetworkName } from "../src/utils";
import { originChainId, destinationChainId } from "./constants";
import {
assertPromiseError,
Contract,
Expand All @@ -11,10 +12,8 @@ import {
fillV3Relay,
createSpyLogger,
deploySpokePoolWithToken,
destinationChainId,
ethers,
expect,
originChainId,
setupTokensForWallet,
toBNWei,
} from "./utils";
Expand Down
6 changes: 3 additions & 3 deletions test/SpokePoolClient.v3Events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@ import {
hubPoolFixture,
deploySpokePool,
ethers,
contractsV2Utils,
modifyRelayHelper,
SignerWithAddress,
toBNWei,
} from "./utils";
import { modifyRelayHelper } from "./constants";

type EventSearchConfig = sdkUtils.EventSearchConfig;

Expand All @@ -53,7 +53,7 @@ describe("SpokePoolClient: Event Filtering", function () {
const filledRelayEvents = ["FilledRelay", "FilledV3Relay"];
const executedRelayerRefundEvents = ["ExecutedRelayerRefundRoot", "ExecutedV3RelayerRefundRoot"];

let owner: contractsV2Utils.SignerWithAddress, depositor: contractsV2Utils.SignerWithAddress;
let owner: SignerWithAddress, depositor: SignerWithAddress;
let chainIds: number[];
let originChainId: number, destinationChainId: number, repaymentChainId: number;
let hubPoolClient: MockHubPoolClient;
Expand Down
31 changes: 20 additions & 11 deletions test/constants.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,22 @@
import * as contracts from "@across-protocol/contracts-v2/dist/test-utils"; // Contains all the Across contract constants.
import { DEFAULT_CONFIG_STORE_VERSION } from "./mocks";
import { destinationChainId, originChainId, randomAddress, repaymentChainId, toWei } from "./utils";

export const {
amountToDeposit,
amountToLp,
depositRelayerFeePct,
originChainId,
destinationChainId,
repaymentChainId,
mockTreeRoot,
refundProposalLiveness,
totalBond,
} = contracts;
export const CONFIG_STORE_VERSION = DEFAULT_CONFIG_STORE_VERSION;
export const randomL1Token = randomAddress();
export const randomOriginToken = randomAddress();
export const randomDestinationToken = randomAddress();
export const randomDestinationToken2 = randomAddress();
export const randomL1Token = contracts.randomAddress();
export const randomOriginToken = contracts.randomAddress();
export const randomDestinationToken = contracts.randomAddress();
export const randomDestinationToken2 = contracts.randomAddress();

// Max number of refunds in relayer refund leaf for a { repaymentChainId, L2TokenAddress }.
export const MAX_REFUNDS_PER_RELAYER_REFUND_LEAF = 3;
Expand All @@ -15,14 +26,12 @@ export const MAX_L1_TOKENS_PER_POOL_REBALANCE_LEAF = 3;

// DAI's Rate model.
export const sampleRateModel = {
UBar: toWei(0.8).toString(),
R0: toWei(0.04).toString(),
R1: toWei(0.07).toString(),
R2: toWei(0.75).toString(),
UBar: contracts.toWei(0.8).toString(),
R0: contracts.toWei(0.04).toString(),
R1: contracts.toWei(0.07).toString(),
R2: contracts.toWei(0.75).toString(),
};

// Add Mainnet chain ID 1 to the chain ID list because the dataworker uses this chain to look up latest GlobalConfig
// updates for config variables like MAX_REFUND_COUNT_FOR_RELAYER_REPAYMENT_LEAF.
export const CHAIN_ID_TEST_LIST = [originChainId, destinationChainId, repaymentChainId, 1];

export * from "@across-protocol/contracts-v2/dist/test-utils"; // Contains all the Across contract constants.
21 changes: 10 additions & 11 deletions test/fixtures/UmaEcosystemFixture.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import * as utils from "@across-protocol/contracts-v2/dist/test-utils";
import { Contract } from "ethers";
import { SignerWithAddress } from "@nomiclabs/hardhat-ethers/signers";
import { utf8ToHex, identifier, refundProposalLiveness } from "@across-protocol/contracts-v2/dist/test-utils";
import { identifier } from "@across-protocol/contracts-v2/dist/test-utils";
import { refundProposalLiveness } from "../constants";
import { Contract, getContractFactory, SignerWithAddress, utf8ToHex } from "../utils";

export async function setupUmaEcosystem(owner: SignerWithAddress): Promise<{
timer: Contract;
Expand All @@ -11,18 +10,18 @@ export async function setupUmaEcosystem(owner: SignerWithAddress): Promise<{
}> {
// Setup minimum UMA ecosystem contracts. Note that we don't use the umaEcosystemFixture because Hardhat Fixture's
// seem to produce non-deterministic behavior between tests.
const timer = await (await utils.getContractFactory("Timer", owner)).deploy();
const finder = await (await utils.getContractFactory("Finder", owner)).deploy();
const identifierWhitelist = await (await utils.getContractFactory("IdentifierWhitelist", owner)).deploy();
const timer = await (await getContractFactory("Timer", owner)).deploy();
const finder = await (await getContractFactory("Finder", owner)).deploy();
const identifierWhitelist = await (await getContractFactory("IdentifierWhitelist", owner)).deploy();
const mockOracle = await (
await utils.getContractFactory("MockOracleAncillary", owner)
await getContractFactory("MockOracleAncillary", owner)
).deploy(finder.address, timer.address);
const optimisticOracle = await (
await utils.getContractFactory("SkinnyOptimisticOracle", owner)
await getContractFactory("SkinnyOptimisticOracle", owner)
).deploy(refundProposalLiveness, finder.address, timer.address);
const collateralWhitelist = await (await utils.getContractFactory("AddressWhitelist", owner)).deploy();
const collateralWhitelist = await (await getContractFactory("AddressWhitelist", owner)).deploy();
const store = await (
await utils.getContractFactory("Store", owner)
await getContractFactory("Store", owner)
).deploy({ rawValue: "0" }, { rawValue: "0" }, timer.address);
await finder.changeImplementationAddress(utf8ToHex("CollateralWhitelist"), collateralWhitelist.address);
await finder.changeImplementationAddress(utf8ToHex("IdentifierWhitelist"), identifierWhitelist.address);
Expand Down
10 changes: 3 additions & 7 deletions test/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
export * from "@across-protocol/contracts-v2/dist/test-utils";
export * as contractsV2Utils from "@across-protocol/contracts-v2/dist/test-utils";
export { ethers } from "hardhat";
export { smock } from "@defi-wonderland/smock";

export * from "./utils";
export * from "./BlockchainUtils";
export * from "./SpokePoolUtils";
export * from "./SpyTransport";

export { smock } from "@defi-wonderland/smock";

export { ethers } from "hardhat";

export * as contractsV2Utils from "@across-protocol/contracts-v2/dist/test-utils";
50 changes: 32 additions & 18 deletions test/utils/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as utils from "@across-protocol/contracts-v2/dist/test-utils";
import { BigNumberish, providers } from "ethers";
import { BigNumber, BigNumberish, Contract, providers } from "ethers";
import {
AcrossConfigStoreClient as ConfigStoreClient,
GLOBAL_CONFIG_STORE_KEYS,
Expand All @@ -15,19 +15,17 @@ import {
resolveContractFromSymbol,
toBN,
toBNWei,
toWei,
utf8ToHex,
} from "../../src/utils";
import {
MAX_L1_TOKENS_PER_POOL_REBALANCE_LEAF,
MAX_REFUNDS_PER_RELAYER_REFUND_LEAF,
amountToDeposit,
depositRelayerFeePct,
destinationChainId as defaultDestinationChainId,
MAX_L1_TOKENS_PER_POOL_REBALANCE_LEAF,
MAX_REFUNDS_PER_RELAYER_REFUND_LEAF,
sampleRateModel,
zeroAddress,
} from "../constants";
import { BigNumber, Contract, SignerWithAddress } from "./index";
export { sinon, winston };

import { AcrossConfigStore } from "@across-protocol/contracts-v2";
import chai, { expect } from "chai";
import chaiExclude from "chai-exclude";
Expand All @@ -39,9 +37,25 @@ import { EMPTY_MESSAGE, PROTOCOL_DEFAULT_CHAIN_ID_INDICES } from "../../src/cons
import { SpyTransport } from "./SpyTransport";

chai.use(chaiExclude);

const assert = chai.assert;
export { assert, chai };

export type SignerWithAddress = utils.SignerWithAddress;

export const {
buildPoolRebalanceLeafTree,
buildPoolRebalanceLeaves,
deploySpokePool,
depositV2,
enableRoutes,
getContractFactory,
getDepositParams,
hubPoolFixture,
modifyRelayHelper,
randomAddress,
zeroAddress,
} = utils;

export { assert, BigNumber, expect, chai, Contract, sinon, toBN, toBNWei, toWei, utf8ToHex, winston };

const TokenRolesEnum = {
OWNER: "0",
Expand Down Expand Up @@ -230,18 +244,18 @@ export async function simpleDeposit(
token: utils.Contract,
recipient: utils.SignerWithAddress,
depositor: utils.SignerWithAddress,
destinationChainId: number = utils.destinationChainId,
amountToDeposit: utils.BigNumber = utils.amountToDeposit,
depositRelayerFeePct: utils.BigNumber = utils.depositRelayerFeePct
destinationChainId = defaultDestinationChainId,
amount = amountToDeposit,
relayerFeePct = depositRelayerFeePct
): Promise<V2Deposit> {
const depositObject = await utils.depositV2(
spokePool,
token,
recipient,
depositor,
destinationChainId,
amountToDeposit,
depositRelayerFeePct
amount,
relayerFeePct
);
// Sanity Check: Ensure that the deposit was successful.
expect(depositObject).to.not.be.null;
Expand Down Expand Up @@ -449,8 +463,8 @@ export async function buildDeposit(
tokenToDeposit: Contract,
recipientAndDepositor: SignerWithAddress,
destinationChainId: number,
_amountToDeposit: BigNumber = amountToDeposit,
relayerFeePct: BigNumber = depositRelayerFeePct
_amountToDeposit = amountToDeposit,
relayerFeePct = depositRelayerFeePct
): Promise<V2Deposit> {
const _deposit = await utils.depositV2(
spokePool,
Expand Down Expand Up @@ -692,8 +706,8 @@ export function buildDepositForRelayerFeeTest(
return {
amount: toBN(amount),
depositId: bnUint32Max.toNumber(),
depositor: utils.randomAddress(),
recipient: utils.randomAddress(),
depositor: randomAddress(),
recipient: randomAddress(),
relayerFeePct: bnZero,
message: EMPTY_MESSAGE,
originChainId: 1,
Expand Down
4 changes: 2 additions & 2 deletions test/validatorUtils.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { expect } from "chai";
import { utils, interfaces } from "../src";
import { ZERO_ADDRESS } from "../src/constants";
import { toBN } from "@across-protocol/contracts-v2";
import { randomAddress, toBN } from "./utils";
import { cloneDeep } from "lodash";
import { objectWithBigNumberReviver } from "../src/utils";
import { Deposit } from "../src/interfaces";
Expand Down Expand Up @@ -39,7 +39,7 @@ describe("validatorUtils", () => {
// We should be able to return true for the default deposit
expect(utils.isDepositFormedCorrectly(deposit)).to.be.true;
// Let's change the recipient to a valid address
deposit.recipient = utils.randomAddress();
deposit.recipient = randomAddress();
expect(utils.isDepositFormedCorrectly(deposit)).to.be.true;
});
it("should return false if deposit is not close to being formed correctly", () => {
Expand Down

0 comments on commit a201093

Please sign in to comment.