Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master' into pxrl/fillSearch
Browse files Browse the repository at this point in the history
  • Loading branch information
pxrl committed Jan 5, 2024
2 parents 6c0798c + 1cd06de commit 9ba7387
Show file tree
Hide file tree
Showing 15 changed files with 170 additions and 125 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@across-protocol/sdk-v2",
"author": "UMA Team",
"version": "0.19.0",
"version": "0.20.0",
"license": "AGPL-3.0",
"homepage": "https://docs.across.to/v/developer-docs/developers/across-sdk",
"files": [
Expand Down
11 changes: 11 additions & 0 deletions src/clients/SpokePoolClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,17 @@ export class SpokePoolClient extends BaseAbstractClient {
};
}

/**
* Find a deposit based on its deposit ID.
* @notice If evaluating a fill, be sure to verify it against the resulting deposit.
* @param depositId The unique ID of the deposit being queried.
* @returns The corresponding deposit if found, undefined otherwise.
*/
public getDeposit(depositId: number): DepositWithBlock | undefined {
const depositHash = this.getDepositHash({ depositId, originChainId: this.chainId });
return this.depositHashes[depositHash];
}

/**
* Find a corresponding deposit for a given fill.
* @param fill The fill to find a corresponding deposit for.
Expand Down
35 changes: 12 additions & 23 deletions src/clients/mocks/MockConfigStoreClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,11 @@ import {
DEFAULT_CONFIG_STORE_VERSION,
GLOBAL_CONFIG_STORE_KEYS,
} from "../AcrossConfigStoreClient";
import { EventManager, getEventManager } from "./MockEvents";
import { EventManager, EventOverrides, getEventManager } from "./MockEvents";

export class MockConfigStoreClient extends AcrossConfigStoreClient {
public configStoreVersion = DEFAULT_CONFIG_STORE_VERSION;
private eventManager: EventManager | null;
private events: Event[] = [];
private ubaActivationBlockOverride: number | undefined;
private availableChainIdsOverride: number[] | undefined;

Expand All @@ -37,11 +36,9 @@ export class MockConfigStoreClient extends AcrossConfigStoreClient {
this.chainId = chainId;
this.eventManager = mockUpdate ? getEventManager(chainId, this.eventSignatures) : null;
if (isDefined(this.eventManager) && this.eventManager) {
this.updateGlobalConfig(
GLOBAL_CONFIG_STORE_KEYS.CHAIN_ID_INDICES,
JSON.stringify(availableChainIdsOverride),
this.eventManager.blockNumber
);
this.updateGlobalConfig(GLOBAL_CONFIG_STORE_KEYS.CHAIN_ID_INDICES, JSON.stringify(availableChainIdsOverride), {
blockNumber: this.eventManager.blockNumber,
});
}
}

Expand Down Expand Up @@ -72,10 +69,6 @@ export class MockConfigStoreClient extends AcrossConfigStoreClient {
this.configStoreVersion = version;
}

addEvent(event: Event): void {
this.events.push(event);
}

async _update(): Promise<ConfigStoreUpdate> {
// Backwards compatibility for pre-existing MockConfigStoreClient users.
if (this.eventManager === null) {
Expand All @@ -89,7 +82,7 @@ export class MockConfigStoreClient extends AcrossConfigStoreClient {
// All requested event types must be populated in the array (even if empty).
const globalConfigUpdateTimes: number[] = [];
const _events: Event[][] = eventNames.map(() => []);
for (const event of this.events.flat()) {
for (const event of this.eventManager.getEvents().flat()) {
const idx = eventNames.indexOf(event.event as string);
if (idx !== -1) {
_events[idx].push(event);
Expand All @@ -100,7 +93,6 @@ export class MockConfigStoreClient extends AcrossConfigStoreClient {
globalConfigUpdateTimes.push(block.timestamp);
}
}
this.events = [];

// Transform 2d-events array into a record.
const events = Object.fromEntries(eventNames.map((eventName, idx) => [eventName, _events[idx]]));
Expand All @@ -117,33 +109,30 @@ export class MockConfigStoreClient extends AcrossConfigStoreClient {
};
}

updateGlobalConfig(key: string, value: string, blockNumber?: number): Event {
return this.generateConfig("UpdatedGlobalConfig", utf8ToHex(key), value, blockNumber);
updateGlobalConfig(key: string, value: string, overrides: EventOverrides = {}): Event {
return this.generateConfig("UpdatedGlobalConfig", utf8ToHex(key), value, overrides);
}

updateTokenConfig(key: string, value: string, blockNumber?: number): Event {
updateTokenConfig(key: string, value: string, overrides: EventOverrides = {}): Event {
// Verify that the key is a valid address
if (ethers.utils.isAddress(key) === false) {
throw new Error(`Invalid address: ${key}`);
}
return this.generateConfig("UpdatedTokenConfig", key, value, blockNumber);
return this.generateConfig("UpdatedTokenConfig", key, value, overrides);
}

private generateConfig(event: string, key: string, value: string, blockNumber?: number): Event {
private generateConfig(event: string, key: string, value: string, overrides: EventOverrides = {}): Event {
assert(this.eventManager !== null);

const topics = [key, value];
const args = { key, value };

const configEvent = this.eventManager.generateEvent({
return this.eventManager.generateEvent({
event,
address: this.configStore.address,
topics: topics.map((topic) => topic.toString()),
args,
blockNumber,
blockNumber: overrides.blockNumber,
});

this.addEvent(configEvent);
return configEvent;
}
}
19 changes: 18 additions & 1 deletion src/clients/mocks/MockEvents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import { random } from "lodash";
import { isDefined, randomAddress, toBN } from "../../utils";

const { id, keccak256, toUtf8Bytes } = ethersUtils;
export type EventOverrides = {
blockNumber?: number;
};

type Block = providers.Block;
type TransactionResponse = providers.TransactionResponse;
Expand Down Expand Up @@ -37,6 +40,7 @@ const removeListener = (): void => {

export class EventManager {
private logIndexes: Record<string, number> = {};
public events: Event[] = [];
public readonly minBlockRange = 10;
public readonly eventSignatures: Record<string, string> = {};

Expand All @@ -51,6 +55,16 @@ export class EventManager {
});
}

addEvent(event: Event): void {
this.events.push(event);
}

getEvents(): Event[] {
const events = this.events;
this.events = [];
return events;
}

generateEvent(inputs: EthersEventTemplate): Event {
const { address, event, topics: _topics, data, args } = inputs;
const eventSignature = `${event}(${this.eventSignatures[event]})`;
Expand Down Expand Up @@ -94,7 +108,7 @@ export class EventManager {
});
};

return {
const generatedEvent = {
blockNumber,
transactionIndex,
logIndex,
Expand All @@ -113,6 +127,9 @@ export class EventManager {
getTransactionReceipt,
removeListener,
} as Event;

this.addEvent(generatedEvent);
return generatedEvent;
}
}

Expand Down
35 changes: 18 additions & 17 deletions src/clients/mocks/MockHubPoolClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { randomAddress, assign } from "../../utils";
import { L1Token, PendingRootBundle } from "../../interfaces";
import { AcrossConfigStoreClient as ConfigStoreClient } from "../AcrossConfigStoreClient";
import { HubPoolClient, HubPoolUpdate } from "../HubPoolClient";
import { EventManager, getEventManager } from "./MockEvents";
import { EventManager, EventOverrides, getEventManager } from "./MockEvents";

const emptyRootBundle: PendingRootBundle = {
poolRebalanceRoot: "",
Expand All @@ -20,7 +20,6 @@ const emptyRootBundle: PendingRootBundle = {
export class MockHubPoolClient extends HubPoolClient {
public rootBundleProposal = emptyRootBundle;

private events: Event[] = [];
private l1TokensMock: L1Token[] = []; // L1Tokens and their associated info.
private tokenInfoToReturn: L1Token = { address: "", decimals: 0, symbol: "" };

Expand Down Expand Up @@ -58,10 +57,6 @@ export class MockHubPoolClient extends HubPoolClient {
this.latestBlockSearched = blockNumber;
}

addEvent(event: Event): void {
this.events.push(event);
}

addL1Token(l1Token: L1Token) {
this.l1TokensMock.push(l1Token);
}
Expand Down Expand Up @@ -107,13 +102,15 @@ export class MockHubPoolClient extends HubPoolClient {
// Ensure an array for every requested event exists, in the requested order.
// All requested event types must be populated in the array (even if empty).
const _events: Event[][] = eventNames.map(() => []);
this.events.flat().forEach((event) => {
const idx = eventNames.indexOf(event.event as string);
if (idx !== -1) {
_events[idx].push(event);
}
});
this.events = [];
this.eventManager
.getEvents()
.flat()
.forEach((event) => {
const idx = eventNames.indexOf(event.event as string);
if (idx !== -1) {
_events[idx].push(event);
}
});

// Transform 2d-events array into a record.
const events = Object.fromEntries(eventNames.map((eventName, idx) => [eventName, _events[idx]]));
Expand All @@ -139,7 +136,7 @@ export class MockHubPoolClient extends HubPoolClient {
destinationChainId: number,
l1Token: string,
destinationToken: string,
blockNumber?: number
overrides: EventOverrides = {}
): Event {
const event = "SetPoolRebalanceRoute";

Expand All @@ -155,7 +152,7 @@ export class MockHubPoolClient extends HubPoolClient {
address: this.hubPool.address,
topics: topics.map((topic) => topic.toString()),
args,
blockNumber,
blockNumber: overrides.blockNumber,
});
}

Expand All @@ -166,7 +163,8 @@ export class MockHubPoolClient extends HubPoolClient {
poolRebalanceRoot?: string,
relayerRefundRoot?: string,
slowRelayRoot?: string,
proposer?: string
proposer?: string,
overrides: EventOverrides = {}
): Event {
const event = "ProposeRootBundle";

Expand All @@ -191,6 +189,7 @@ export class MockHubPoolClient extends HubPoolClient {
address: this.hubPool.address,
topics: topics.map((topic) => topic.toString()),
args,
blockNumber: overrides.blockNumber,
});
}

Expand All @@ -202,7 +201,8 @@ export class MockHubPoolClient extends HubPoolClient {
bundleLpFees: BigNumber[],
netSendAmounts: BigNumber[],
runningBalances: BigNumber[],
caller?: string
caller?: string,
overrides: EventOverrides = {}
): Event {
const event = "RootBundleExecuted";

Expand All @@ -225,6 +225,7 @@ export class MockHubPoolClient extends HubPoolClient {
address: this.hubPool.address,
topics: topics.map((topic) => topic.toString()),
args,
blockNumber: overrides.blockNumber,
});
}
}
18 changes: 9 additions & 9 deletions src/clients/mocks/MockSpokePoolClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,14 @@ import { ZERO_ADDRESS } from "../../constants";
import { DepositWithBlock, FillWithBlock, FundsDepositedEvent, RefundRequestWithBlock } from "../../interfaces";
import { bnZero, toBNWei, forEachAsync, randomAddress } from "../../utils";
import { SpokePoolClient, SpokePoolUpdate } from "../SpokePoolClient";
import { EventManager, getEventManager } from "./MockEvents";
import { EventManager, EventOverrides, getEventManager } from "./MockEvents";

type Block = providers.Block;

// This class replaces internal SpokePoolClient functionality, enabling the
// user to bypass on-chain queries and inject ethers Event objects directly.
export class MockSpokePoolClient extends SpokePoolClient {
public eventManager: EventManager;
private events: Event[] = [];
private realizedLpFeePct: BigNumber | undefined = bnZero;
private realizedLpFeePctOverride = false;
private destinationTokenForChainOverride: Record<number, string> = {};
Expand Down Expand Up @@ -67,10 +66,6 @@ export class MockSpokePoolClient extends SpokePoolClient {
this.latestBlockSearched = blockNumber;
}

addEvent(event: Event): void {
this.events.push(event);
}

setDepositIds(_depositIds: number[]): void {
this.depositIdAtBlock = [];
if (_depositIds.length === 0) {
Expand Down Expand Up @@ -103,14 +98,13 @@ export class MockSpokePoolClient extends SpokePoolClient {
// Ensure an array for every requested event exists, in the requested order.
// All requested event types must be populated in the array (even if empty).
const events: Event[][] = eventsToQuery.map(() => []);
await forEachAsync(this.events.flat(), async (event) => {
await forEachAsync(this.eventManager.getEvents().flat(), async (event) => {
const idx = eventsToQuery.indexOf(event.event as string);
if (idx !== -1) {
events[idx].push(event);
blocks[event.blockNumber] = await event.getBlock();
}
});
this.events = [];
this.blocks = blocks;

// Update latestDepositIdQueried.
Expand Down Expand Up @@ -258,7 +252,12 @@ export class MockSpokePoolClient extends SpokePoolClient {
});
}

generateDepositRoute(originToken: string, destinationChainId: number, enabled: boolean): Event {
generateDepositRoute(
originToken: string,
destinationChainId: number,
enabled: boolean,
overrides: EventOverrides = {}
): Event {
const event = "EnabledDepositRoute";

const topics = [originToken, destinationChainId];
Expand All @@ -269,6 +268,7 @@ export class MockSpokePoolClient extends SpokePoolClient {
address: this.spokePool.address,
topics: topics.map((topic) => topic.toString()),
args,
blockNumber: overrides.blockNumber,
});
}
}
Loading

0 comments on commit 9ba7387

Please sign in to comment.