Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
Signed-off-by: bennett <[email protected]>
  • Loading branch information
bmzig committed Feb 28, 2025
1 parent 7db66bd commit 3a453f1
Show file tree
Hide file tree
Showing 9 changed files with 171 additions and 127 deletions.
14 changes: 11 additions & 3 deletions src/clients/HubPoolClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,11 @@ export class HubPoolClient extends BaseAbstractClient {
return await this.hubPool.callStatic.liquidityUtilizationCurrent(hubPoolToken.toAddress(), overrides);
}

return await this.hubPool.callStatic.liquidityUtilizationPostRelay(hubPoolToken.toAddress(), depositAmount, overrides);
return await this.hubPool.callStatic.liquidityUtilizationPostRelay(
hubPoolToken.toAddress(),
depositAmount,
overrides
);
};

// Resolve the cache locally so that we can appease typescript
Expand Down Expand Up @@ -493,7 +497,10 @@ export class HubPoolClient extends BaseAbstractClient {
// For each token / quoteBlock pair, resolve the utilisation for each quoted block.
// This can be reused for each deposit with the same HubPool token and quoteTimestamp pair.
utilization = Object.fromEntries(
await mapAsync(getHubPoolTokens(), async (hubPoolToken) => [hubPoolToken, await resolveUtilization(hubPoolToken)])
await mapAsync(getHubPoolTokens(), async (hubPoolToken) => [
hubPoolToken.toAddress(),
await resolveUtilization(hubPoolToken),
])
);

// For each deposit, compute the post-relay HubPool utilisation independently.
Expand Down Expand Up @@ -965,7 +972,8 @@ export class HubPoolClient extends BaseAbstractClient {
),
Promise.all(
uniqueL1Tokens.map(
async (l1Token: EvmAddress) => await this.hubPool.pooledTokens(l1Token.toAddress(), { blockTag: update.searchEndBlock })
async (l1Token: EvmAddress) =>
await this.hubPool.pooledTokens(l1Token.toAddress(), { blockTag: update.searchEndBlock })
)
),
]);
Expand Down
42 changes: 27 additions & 15 deletions test/FillUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,19 @@ describe("FillUtils", function () {
spokeProvider = new MockedProvider(bnZero, bnZero);
deposit = {
depositId: bnOne,
depositor: EvmAddress.fromHex(ZERO_ADDRESS),
depositor: Address.fromHex(ZERO_ADDRESS),
destinationChainId,
originChainId,
inputAmount: toBN(100),
inputToken: EvmAddress.fromHex(ZERO_ADDRESS),
inputToken: Address.fromHex(ZERO_ADDRESS),
outputAmount: toBN(100),
outputToken: EvmAddress.fromHex(ZERO_ADDRESS),
outputToken: Address.fromHex(ZERO_ADDRESS),
message: "",
quoteTimestamp: 0,
recipient: EvmAddress.fromHex(ZERO_ADDRESS),
updatedRecipient: EvmAddress.fromHex(ZERO_ADDRESS),
recipient: Address.fromHex(ZERO_ADDRESS),
updatedRecipient: Address.fromHex(ZERO_ADDRESS),
fillDeadline: 100,
exclusiveRelayer: EvmAddress.fromHex(ZERO_ADDRESS),
exclusiveRelayer: Address.fromHex(ZERO_ADDRESS),
exclusivityDeadline: 100,
transactionHash: "0xa",
blockNumber: 0,
Expand All @@ -60,7 +60,7 @@ describe("FillUtils", function () {
relayExecutionInfo: {
updatedMessage: deposit.message,
updatedOutputAmount: deposit.outputAmount,
updatedRecipient: deposit.recipient,
updatedRecipient: Address.fromHex(deposit.recipient),
fillType: FillType.FastFill,
},
relayer,
Expand All @@ -70,20 +70,24 @@ describe("FillUtils", function () {
const { configStore } = await deployConfigStore(owner, []);
const configStoreClient = new MockConfigStoreClient(createSpyLogger().spyLogger, configStore);
hubPoolClient = new MockHubPoolClient(createSpyLogger().spyLogger, hubPool, configStoreClient);
hubPoolClient.setTokenMapping(ZERO_ADDRESS, deposit.originChainId, deposit.inputToken.toString());
hubPoolClient.setTokenMapping(EvmAddress.fromHex(ZERO_ADDRESS), deposit.originChainId, deposit.inputToken);
});

describe("verifyFillRepayment", function () {
it("Original repayment is valid", async function () {
hubPoolClient.setTokenMapping(ZERO_ADDRESS, fill.repaymentChainId, ZERO_ADDRESS);
hubPoolClient.setTokenMapping(
EvmAddress.fromHex(ZERO_ADDRESS),
fill.repaymentChainId,
Address.fromHex(ZERO_ADDRESS)
);
const result = await verifyFillRepayment(fill, spokeProvider, deposit, hubPoolClient);
expect(result).to.not.be.undefined;
expect(result!.repaymentChainId).to.equal(fill.repaymentChainId);
expect(result!.relayer).to.deep.equal(fill.relayer);
});
it("SlowFill always valid", async function () {
// We don't set the repayment chain mapping for the input token because a slow fill should always be valid.
hubPoolClient.deleteTokenMapping(ZERO_ADDRESS, fill.repaymentChainId);
hubPoolClient.deleteTokenMapping(EvmAddress.fromHex(ZERO_ADDRESS), fill.repaymentChainId);
const slowFill = { ...fill };
slowFill.relayExecutionInfo.fillType = FillType.SlowFill;
const result = await verifyFillRepayment(slowFill, spokeProvider, deposit, hubPoolClient);
Expand All @@ -92,7 +96,7 @@ describe("FillUtils", function () {
});
it("Lite chain originChain used as repayment and relayer address is valid", async function () {
// We don't set repayment chain mapping since repayment happens on origin chain.
hubPoolClient.deleteTokenMapping(ZERO_ADDRESS, fill.repaymentChainId);
hubPoolClient.deleteTokenMapping(EvmAddress.fromHex(ZERO_ADDRESS), fill.repaymentChainId);
const liteChainDeposit = {
...deposit,
fromLiteChain: true,
Expand All @@ -106,7 +110,7 @@ describe("FillUtils", function () {
});
it("Lite chain deposit and relayer is not valid EVM address; relayer gets overwritten to msg.sender", async function () {
// We don't set repayment chain mapping since repayment happens on origin chain.
hubPoolClient.deleteTokenMapping(ZERO_ADDRESS, fill.repaymentChainId);
hubPoolClient.deleteTokenMapping(EvmAddress.fromHex(ZERO_ADDRESS), fill.repaymentChainId);
const liteChainDeposit = {
...deposit,
fromLiteChain: true,
Expand All @@ -124,7 +128,11 @@ describe("FillUtils", function () {
expect(result!.repaymentChainId).to.equal(originChainId);
});
it("Relayer is not valid EVM address, relayer gets overwritten to msg.sender", async function () {
hubPoolClient.setTokenMapping(ZERO_ADDRESS, fill.repaymentChainId, ZERO_ADDRESS);
hubPoolClient.setTokenMapping(
EvmAddress.fromHex(ZERO_ADDRESS),
fill.repaymentChainId,
EvmAddress.fromHex(ZERO_ADDRESS)
);
// valid chain ID's doesn't contain repayment chain.
const invalidRepaymentFill = {
...fill,
Expand All @@ -141,7 +149,7 @@ describe("FillUtils", function () {
});
it("Lite chain deposit and relayer is not valid EVM address; msg.sender is invalid", async function () {
// We don't set repayment chain mapping since repayment happens on origin chain.
hubPoolClient.deleteTokenMapping(ZERO_ADDRESS, fill.repaymentChainId);
hubPoolClient.deleteTokenMapping(EvmAddress.fromHex(ZERO_ADDRESS), fill.repaymentChainId);
const liteChainDeposit = {
...deposit,
fromLiteChain: true,
Expand All @@ -157,7 +165,11 @@ describe("FillUtils", function () {
expect(result).to.be.undefined;
});
it("Relayer is not valid EVM address, and msg.sender is invalid", async function () {
hubPoolClient.setTokenMapping(ZERO_ADDRESS, fill.repaymentChainId, ZERO_ADDRESS);
hubPoolClient.setTokenMapping(
EvmAddress.fromHex(ZERO_ADDRESS),
fill.repaymentChainId,
Address.fromHex(ZERO_ADDRESS)
);
// Simulate what happens if the repayment chain is an EVM chain, the repayment address is not a vaid EVM address,
// and the msg.sender is not a valid EVM address. This could happen if the fill was sent on Solana and the
// repayment chain is Ethereum and the repayment address is an SVM address.
Expand Down
7 changes: 4 additions & 3 deletions test/HubPoolClient.L1Tokens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
createSpyLogger,
} from "./utils";
import { AcrossConfigStoreClient as ConfigStoreClient, HubPoolClient } from "../src/clients";
import { EvmAddress } from "../src/utils";
import { CONFIG_STORE_VERSION } from "./constants";

let hubPool: Contract, lpTokenFactory: Contract;
Expand Down Expand Up @@ -49,9 +50,9 @@ describe("HubPoolClient: L1Tokens", function () {

await hubPoolClient.update();
expect(hubPoolClient.getL1Tokens()).to.deep.equal([
{ address: l1Token1.address, symbol: "Coin1", decimals: 18 },
{ address: l1Token2.address, symbol: "Coin2", decimals: 17 },
{ address: l1Token3.address, symbol: "Coin3", decimals: 16 },
{ address: EvmAddress.fromHex(l1Token1.address), symbol: "Coin1", decimals: 18 },
{ address: EvmAddress.fromHex(l1Token2.address), symbol: "Coin2", decimals: 17 },
{ address: EvmAddress.fromHex(l1Token3.address), symbol: "Coin3", decimals: 16 },
]);
});
});
6 changes: 1 addition & 5 deletions test/HubPoolClient.RootBundleEvents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -215,11 +215,7 @@ describe("HubPoolClient: RootBundle Events", function () {
expect(runningBalance.eq(toBNWei(100))).to.be.true;

// Target block is before event.
({ runningBalance } = hubPoolClient.getRunningBalanceBeforeBlockForChain(
0,
constants.originChainId,
l1Token
));
({ runningBalance } = hubPoolClient.getRunningBalanceBeforeBlockForChain(0, constants.originChainId, l1Token));
expect(runningBalance.eq(0)).to.be.true;

// chain ID and L1 token combination not found.
Expand Down
6 changes: 0 additions & 6 deletions test/HubPoolClient.Utilization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,14 +130,12 @@ describe("HubPool Utilization", function () {
// Quote time needs to be >= first rate model event time
};

console.log("MARKER");
// Relayed amount being 10% of total LP amount should give exact same results as this test in v1:
// - https://github.com/UMAprotocol/protocol/blob/3b1a88ead18088e8056ecfefb781c97fce7fdf4d/packages/financial-templates-lib/test/clients/InsuredBridgeL1Client.js#L1037
expect((await hubPoolClient.computeRealizedLpFeePct(depositData)).realizedLpFeePct).to.equal(
toBNWei("0.000117987509354032")
);

console.log("MARKER");
// Next, let's increase the pool utilization from 0% to 60% by sending 60% of the pool's liquidity to
// another chain.
const leaves = buildPoolRebalanceLeaves(
Expand All @@ -148,15 +146,12 @@ describe("HubPool Utilization", function () {
[[toBN(0)]],
[0]
);
console.log("MARKER");
const tree = await buildPoolRebalanceLeafTree(leaves);
await weth.approve(hubPool.address, totalBond);
await hubPool.proposeRootBundle([1], 1, tree.getHexRoot(), mockTreeRoot, mockTreeRoot);
await timer.setCurrentTime(Number(await timer.getCurrentTime()) + refundProposalLiveness + 1);
console.log("MARKER");
await hubPool.executeRootBundle(...Object.values(leaves[0]), tree.getHexProof(leaves[0]));

console.log("MARKER");
// Submit a deposit with a de minimis amount of tokens so we can isolate the computed realized lp fee % to the
// pool utilization factor.
expect(
Expand All @@ -171,7 +166,6 @@ describe("HubPool Utilization", function () {
).realizedLpFeePct
).to.equal(toBNWei("0.001371068779697899"));

console.log("MARKER");
// Relaying 10% of pool should give exact same result as this test, which sends a relay that is 10% of the pool's
// size when the pool is already at 60% utilization. The resulting post-relay utilization is therefore 70%.
// - https://github.com/UMAprotocol/protocol/blob/3b1a88ead18088e8056ecfefb781c97fce7fdf4d/packages/financial-templates-lib/test/clients/InsuredBridgeL1Client.js#L1064
Expand Down
14 changes: 10 additions & 4 deletions test/SpokePoolClient.DepositRoutes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,13 @@ describe("SpokePoolClient: Deposit Routes", function () {
it("Fetches enabled deposit routes", async function () {
await spokePoolClient.update();
expect(spokePoolClient.getDepositRoutes()).to.deep.equal({});

const originToken = Address.fromHex(randomAddress());
await enableRoutes(spokePool, [{ originToken: originToken.toAddress(), destinationChainId }]);
await spokePoolClient.update();
expect(spokePoolClient.getDepositRoutes()).to.deep.equal({ [originToken.toString()]: { [destinationChainId]: true } });
expect(spokePoolClient.getDepositRoutes()).to.deep.equal({
[originToken.toString()]: { [destinationChainId]: true },
});

// Enable another destination chain with the same origin token should append to the previous structure.
const destinationChainId2 = destinationChainId + 1;
Expand All @@ -46,11 +48,15 @@ describe("SpokePoolClient: Deposit Routes", function () {
const originToken = Address.fromHex(randomAddress());
await enableRoutes(spokePool, [{ originToken: originToken.toAddress(), destinationChainId }]);
await spokePoolClient.update();
expect(spokePoolClient.getDepositRoutes()).to.deep.equal({ [originToken.toString()]: { [destinationChainId]: true } });
expect(spokePoolClient.getDepositRoutes()).to.deep.equal({
[originToken.toString()]: { [destinationChainId]: true },
});

await spokePool.setEnableRoute(originToken.toAddress(), destinationChainId, false); // Disable the route.
await spokePoolClient.update();
expect(spokePoolClient.getDepositRoutes()).to.deep.equal({ [originToken.toString()]: { [destinationChainId]: false } });
expect(spokePoolClient.getDepositRoutes()).to.deep.equal({
[originToken.toString()]: { [destinationChainId]: false },
});
const destinationChainId2 = destinationChainId + 1;
await enableRoutes(spokePool, [{ originToken: originToken.toAddress(), destinationChainId: destinationChainId2 }]);
await spokePoolClient.update();
Expand Down
11 changes: 6 additions & 5 deletions test/SpokePoolClient.ValidateFill.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
queryHistoricalDepositForFill,
DepositSearchResult,
getBlockRangeForDepositId,
EvmAddress,
} from "../src/utils";
import { ZERO_BYTES } from "../src/constants";
import { CHAIN_ID_TEST_LIST, originChainId, destinationChainId, repaymentChainId } from "./constants";
Expand Down Expand Up @@ -87,8 +88,8 @@ describe("SpokePoolClient: Fill Validation", function () {
await configStoreClient.update();

hubPoolClient = new MockHubPoolClient(spyLogger, hubPool, configStoreClient);
hubPoolClient.setTokenMapping(l1Token.address, originChainId, erc20_1.address);
hubPoolClient.setTokenMapping(l1Token.address, destinationChainId, erc20_2.address);
hubPoolClient.setTokenMapping(EvmAddress.fromHex(l1Token.address), originChainId, erc20_1.address);
hubPoolClient.setTokenMapping(EvmAddress.fromHex(l1Token.address), destinationChainId, erc20_2.address);

await hubPoolClient.update();
spokePoolClient1 = new SpokePoolClient(
Expand Down Expand Up @@ -701,7 +702,7 @@ describe("SpokePoolClient: Fill Validation", function () {
spokePool_2,
{
..._deposit_1,
recipient: relayer.address,
recipient: EvmAddress.fromHex(relayer.address),
outputAmount: _deposit_1.outputAmount.div(2),
message: "0x12",
},
Expand All @@ -714,8 +715,8 @@ describe("SpokePoolClient: Fill Validation", function () {
throw new Error("fill_2 is undefined");
}

expect(fill_1.relayExecutionInfo.updatedRecipient === depositor.address).to.be.true;
expect(fill_2.relayExecutionInfo.updatedRecipient === relayer.address).to.be.true;
expect(fill_1.relayExecutionInfo.updatedRecipient).to.deep.eq(EvmAddress.fromHex(depositor.address));
expect(fill_2.relayExecutionInfo.updatedRecipient.eq(EvmAddress.fromHex(relayer.address))).to.be.true;
expect(fill_2.relayExecutionInfo.updatedMessageHash === ethers.utils.keccak256("0x12")).to.be.true;
expect(fill_1.relayExecutionInfo.updatedMessageHash === ZERO_BYTES).to.be.true;
expect(fill_1.relayExecutionInfo.updatedOutputAmount.eq(fill_2.relayExecutionInfo.updatedOutputAmount)).to.be.false;
Expand Down
Loading

0 comments on commit 3a453f1

Please sign in to comment.