Skip to content

Commit

Permalink
improve: use utility function directly
Browse files Browse the repository at this point in the history
Signed-off-by: james-a-morris <jaamorris@cs.stonybrook.edu>
  • Loading branch information
james-a-morris committed Sep 5, 2024
1 parent 89074c2 commit 5df05e7
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 31 deletions.
22 changes: 15 additions & 7 deletions src/caching/Arweave/ArweaveClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,13 +175,21 @@ export class ArweaveClient {
});
const results = await Promise.all(
entries.map(async (edge) => {
const data = await this.get<T>(edge.node.id, validator);
return isDefined(data)
? {
data,
hash: edge.node.id,
}
: null;
try {
const data = await this.get<T>(edge.node.id, validator);
return isDefined(data)
? {
data,
hash: edge.node.id,
}
: null;
} catch (e) {
this.logger.warn({
at: "ArweaveClient:getByTopic",
message: `Bad request for Arweave topic ${edge.node.id}: ${e}`,
});
return null;
}
})
);
return results.filter(isDefined);
Expand Down
16 changes: 3 additions & 13 deletions src/clients/SpokePoolClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ import {
} from "../interfaces";
import { SpokePool } from "../typechain";
import { getNetworkName } from "../utils/NetworkUtils";
import { getBlockRangeForDepositId, getDepositIdAtBlock } from "../utils/SpokeUtils";
import { getBlockRangeForDepositId, getDepositIdAtBlock, relayFillStatus } from "../utils/SpokeUtils";
import { BaseAbstractClient, isUpdateFailureReason, UpdateFailureReason } from "./BaseAbstractClient";
import { HubPoolClient } from "./HubPoolClient";
import { AcrossConfigStoreClient } from "./AcrossConfigStoreClient";
Expand Down Expand Up @@ -896,21 +896,11 @@ export class SpokePoolClient extends BaseAbstractClient {
* @param blockTag Block tag (numeric or "latest") to query at.
* @returns The amount filled for the specified deposit at the requested block (or latest).
*/
public async relayFillStatus(
public relayFillStatus(
relayData: RelayData,
blockTag?: number | "latest",
destinationChainId?: number
): Promise<FillStatus> {
destinationChainId ??= this.chainId;
const hash = getRelayDataHash(relayData, destinationChainId!);
const _fillStatus = await this.spokePool.fillStatuses(hash, { blockTag });
const fillStatus = Number(_fillStatus);
if (![FillStatus.Unfilled, FillStatus.RequestedSlowFill, FillStatus.Filled].includes(fillStatus)) {
const { originChainId, depositId } = relayData;
throw new Error(
`relayFillStatus: Unexpected fillStatus for ${originChainId} deposit ${depositId} (${fillStatus})`
);
}
return fillStatus;
return relayFillStatus(this.spokePool, relayData, blockTag, destinationChainId);
}
}
35 changes: 30 additions & 5 deletions src/utils/SpokeUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,32 @@ export function getRelayHashFromEvent(e: Deposit | Fill | SlowFillRequest): stri
return getRelayDataHash(e, e.destinationChainId);
}

/**
* Find the amount filled for a deposit at a particular block.
* @param spokePool SpokePool contract instance.
* @param relayData Deposit information that is used to complete a fill.
* @param blockTag Block tag (numeric or "latest") to query at.
* @returns The amount filled for the specified deposit at the requested block (or latest).
*/
export async function relayFillStatus(
spokePool: Contract,
relayData: RelayData,
blockTag?: number | "latest",
destinationChainId?: number
): Promise<FillStatus> {
destinationChainId ??= await spokePool.chainId();
const hash = getRelayDataHash(relayData, destinationChainId!);
const _fillStatus = await spokePool.fillStatuses(hash, { blockTag });
const fillStatus = Number(_fillStatus);

if (![FillStatus.Unfilled, FillStatus.RequestedSlowFill, FillStatus.Filled].includes(fillStatus)) {
const { originChainId, depositId } = relayData;
throw new Error(`relayFillStatus: Unexpected fillStatus for ${originChainId} deposit ${depositId} (${fillStatus})`);
}

return fillStatus;
}

export async function fillStatusArray(
spokePool: Contract,
relayData: RelayData[],
Expand Down Expand Up @@ -290,12 +316,11 @@ export async function fillStatusArray(
* @returns The block number at which the relay was completed, or undefined.
*/
export async function findFillBlock(
spokePoolClient: SpokePoolClient,
spokePool: Contract,
relayData: RelayData,
lowBlockNumber: number,
highBlockNumber?: number
): Promise<number | undefined> {
const { spokePool } = spokePoolClient;
const { provider } = spokePool;
highBlockNumber ??= await provider.getBlockNumber();
assert(highBlockNumber > lowBlockNumber, `Block numbers out of range (${lowBlockNumber} > ${highBlockNumber})`);
Expand All @@ -315,8 +340,8 @@ export async function findFillBlock(
// Make sure the relay war completed within the block range supplied by the caller.
const [initialFillStatus, finalFillStatus] = (
await Promise.all([
spokePoolClient.relayFillStatus(relayData, lowBlockNumber, destinationChainId),
spokePoolClient.relayFillStatus(relayData, highBlockNumber, destinationChainId),
relayFillStatus(spokePool, relayData, lowBlockNumber, destinationChainId),
relayFillStatus(spokePool, relayData, highBlockNumber, destinationChainId),
])
).map(Number);

Expand All @@ -334,7 +359,7 @@ export async function findFillBlock(
// Find the leftmost block where filledAmount equals the deposit amount.
do {
const midBlockNumber = Math.floor((highBlockNumber + lowBlockNumber) / 2);
const fillStatus = await spokePoolClient.relayFillStatus(relayData, midBlockNumber, destinationChainId);
const fillStatus = await relayFillStatus(spokePool, relayData, midBlockNumber, destinationChainId);

if (fillStatus === FillStatus.Filled) {
highBlockNumber = midBlockNumber;
Expand Down
11 changes: 10 additions & 1 deletion test/SpokePoolClient.ValidateFill.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
bnOne,
InvalidFill,
fillStatusArray,
relayFillStatus,
validateFillForDeposit,
queryHistoricalDepositForFill,
DepositSearchResult,
Expand Down Expand Up @@ -126,10 +127,18 @@ describe("SpokePoolClient: Fill Validation", function () {
outputAmount
);

let filled = await spokePoolClient2.relayFillStatus(deposit);
let filled = await relayFillStatus(spokePool_2, deposit);
expect(filled).to.equal(FillStatus.Unfilled);

// Also test spoke client variant
filled = await spokePoolClient2.relayFillStatus(deposit);
expect(filled).to.equal(FillStatus.Unfilled);

await fillV3Relay(spokePool_2, deposit, relayer);
filled = await relayFillStatus(spokePool_2, deposit);
expect(filled).to.equal(FillStatus.Filled);

// Also test spoke client variant
filled = await spokePoolClient2.relayFillStatus(deposit);
expect(filled).to.equal(FillStatus.Filled);
});
Expand Down
8 changes: 4 additions & 4 deletions test/SpokePoolClient.fills.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ describe("SpokePoolClient: Fills", function () {
await hre.network.provider.send("evm_mine");
}

const fillBlock = await findFillBlock(spokePoolClient, deposit, startBlock);
const fillBlock = await findFillBlock(spokePool, deposit, startBlock);
expect(fillBlock).to.equal(targetFillBlock);
});

Expand All @@ -122,7 +122,7 @@ describe("SpokePoolClient: Fills", function () {
}

// No fill has been made, so expect an undefined fillBlock.
const fillBlock = await findFillBlock(spokePoolClient, deposit, startBlock);
const fillBlock = await findFillBlock(spokePool, deposit, startBlock);
expect(fillBlock).to.be.undefined;

const { blockNumber: lateBlockNumber } = await fillV3Relay(spokePool, deposit, relayer1);
Expand All @@ -131,13 +131,13 @@ describe("SpokePoolClient: Fills", function () {
// Now search for the fill _after_ it was filled and expect an exception.
const srcChain = getNetworkName(deposit.originChainId);
await assertPromiseError(
findFillBlock(spokePoolClient, deposit, lateBlockNumber),
findFillBlock(spokePool, deposit, lateBlockNumber),
`${srcChain} deposit ${deposit.depositId} filled on `
);

// Should assert if highBlock <= lowBlock.
await assertPromiseError(
findFillBlock(spokePoolClient, deposit, await spokePool.provider.getBlockNumber()),
findFillBlock(spokePool, deposit, await spokePool.provider.getBlockNumber()),
"Block numbers out of range"
);
});
Expand Down
2 changes: 1 addition & 1 deletion test/common.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ describe("Utils test", () => {
await estimateTotalGasRequiredByUnsignedTransaction(fill, relayerAddress, provider, 0.0, gasPrice);
expect(toBN(refGasEstimate).eq(toBN(refGasCost).mul(gasPrice))).to.be.true;

for (let gasMarkup = -0.99; gasMarkup <= 4.0; gasMarkup += 0.33) {
for (let gasMarkup = -0.99; gasMarkup <= 1.0; gasMarkup += 0.33) {
const { nativeGasCost, tokenGasCost } = await estimateTotalGasRequiredByUnsignedTransaction(
fill,
relayerAddress,
Expand Down

0 comments on commit 5df05e7

Please sign in to comment.