@across-protocol/sdk 4.3.56 → 4.3.58

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (132) hide show
  1. package/dist/cjs/clients/SpokePoolClient/EVMSpokePoolClient.d.ts +1 -0
  2. package/dist/cjs/clients/SpokePoolClient/EVMSpokePoolClient.js +48 -30
  3. package/dist/cjs/clients/SpokePoolClient/EVMSpokePoolClient.js.map +1 -1
  4. package/dist/cjs/clients/SpokePoolClient/SVMSpokePoolClient.js +7 -2
  5. package/dist/cjs/clients/SpokePoolClient/SVMSpokePoolClient.js.map +1 -1
  6. package/dist/cjs/interfaces/SpokePool.d.ts +5 -0
  7. package/dist/cjs/pool/poolClient.d.ts +9 -13
  8. package/dist/cjs/pool/poolClient.js +1 -1
  9. package/dist/cjs/pool/poolClient.js.map +1 -1
  10. package/dist/cjs/pool/uma/across/constants.d.ts +2 -0
  11. package/dist/cjs/pool/uma/across/constants.js +6 -0
  12. package/dist/cjs/pool/uma/across/constants.js.map +1 -0
  13. package/dist/cjs/pool/uma/across/index.d.ts +2 -0
  14. package/dist/cjs/pool/uma/across/index.js +8 -0
  15. package/dist/cjs/pool/uma/across/index.js.map +1 -0
  16. package/dist/cjs/pool/uma/across/transactionManager.d.ts +12 -0
  17. package/dist/cjs/pool/uma/across/transactionManager.js +121 -0
  18. package/dist/cjs/pool/uma/across/transactionManager.js.map +1 -0
  19. package/dist/cjs/pool/uma/clients/erc20/client.d.ts +26 -0
  20. package/dist/cjs/pool/uma/clients/erc20/client.js +38 -0
  21. package/dist/cjs/pool/uma/clients/erc20/client.js.map +1 -0
  22. package/dist/cjs/pool/uma/clients/erc20/index.d.ts +1 -0
  23. package/dist/cjs/pool/uma/clients/erc20/index.js +5 -0
  24. package/dist/cjs/pool/uma/clients/erc20/index.js.map +1 -0
  25. package/dist/cjs/pool/uma/clients/index.d.ts +1 -0
  26. package/dist/cjs/pool/uma/clients/index.js +6 -0
  27. package/dist/cjs/pool/uma/clients/index.js.map +1 -0
  28. package/dist/cjs/pool/uma/index.d.ts +13 -0
  29. package/dist/cjs/pool/uma/index.js +9 -0
  30. package/dist/cjs/pool/uma/index.js.map +1 -0
  31. package/dist/cjs/pool/uma/oracle/index.d.ts +1 -0
  32. package/dist/cjs/pool/uma/oracle/index.js +6 -0
  33. package/dist/cjs/pool/uma/oracle/index.js.map +1 -0
  34. package/dist/cjs/pool/uma/oracle/utils.d.ts +7 -0
  35. package/dist/cjs/pool/uma/oracle/utils.js +20 -0
  36. package/dist/cjs/pool/uma/oracle/utils.js.map +1 -0
  37. package/dist/cjs/pool/uma/utils.d.ts +17 -0
  38. package/dist/cjs/pool/uma/utils.js +69 -0
  39. package/dist/cjs/pool/uma/utils.js.map +1 -0
  40. package/dist/cjs/utils/SpokeUtils.d.ts +5 -1
  41. package/dist/cjs/utils/SpokeUtils.js +67 -0
  42. package/dist/cjs/utils/SpokeUtils.js.map +1 -1
  43. package/dist/esm/clients/SpokePoolClient/EVMSpokePoolClient.d.ts +1 -0
  44. package/dist/esm/clients/SpokePoolClient/EVMSpokePoolClient.js +48 -30
  45. package/dist/esm/clients/SpokePoolClient/EVMSpokePoolClient.js.map +1 -1
  46. package/dist/esm/clients/SpokePoolClient/SVMSpokePoolClient.js +7 -2
  47. package/dist/esm/clients/SpokePoolClient/SVMSpokePoolClient.js.map +1 -1
  48. package/dist/esm/interfaces/SpokePool.d.ts +5 -0
  49. package/dist/esm/pool/poolClient.d.ts +9 -13
  50. package/dist/esm/pool/poolClient.js +1 -1
  51. package/dist/esm/pool/poolClient.js.map +1 -1
  52. package/dist/esm/pool/uma/across/constants.d.ts +2 -0
  53. package/dist/esm/pool/uma/across/constants.js +3 -0
  54. package/dist/esm/pool/uma/across/constants.js.map +1 -0
  55. package/dist/esm/pool/uma/across/index.d.ts +2 -0
  56. package/dist/esm/pool/uma/across/index.js +4 -0
  57. package/dist/esm/pool/uma/across/index.js.map +1 -0
  58. package/dist/esm/pool/uma/across/transactionManager.d.ts +12 -0
  59. package/dist/esm/pool/uma/across/transactionManager.js +122 -0
  60. package/dist/esm/pool/uma/across/transactionManager.js.map +1 -0
  61. package/dist/esm/pool/uma/clients/erc20/client.d.ts +26 -0
  62. package/dist/esm/pool/uma/clients/erc20/client.js +35 -0
  63. package/dist/esm/pool/uma/clients/erc20/client.js.map +1 -0
  64. package/dist/esm/pool/uma/clients/erc20/index.d.ts +1 -0
  65. package/dist/esm/pool/uma/clients/erc20/index.js +2 -0
  66. package/dist/esm/pool/uma/clients/erc20/index.js.map +1 -0
  67. package/dist/esm/pool/uma/clients/index.d.ts +1 -0
  68. package/dist/esm/pool/uma/clients/index.js +3 -0
  69. package/dist/esm/pool/uma/clients/index.js.map +1 -0
  70. package/dist/esm/pool/uma/index.d.ts +13 -0
  71. package/dist/esm/pool/uma/index.js +9 -0
  72. package/dist/esm/pool/uma/index.js.map +1 -0
  73. package/dist/esm/pool/uma/oracle/index.d.ts +1 -0
  74. package/dist/esm/pool/uma/oracle/index.js +3 -0
  75. package/dist/esm/pool/uma/oracle/index.js.map +1 -0
  76. package/dist/esm/pool/uma/oracle/utils.d.ts +23 -0
  77. package/dist/esm/pool/uma/oracle/utils.js +33 -0
  78. package/dist/esm/pool/uma/oracle/utils.js.map +1 -0
  79. package/dist/esm/pool/uma/utils.d.ts +17 -0
  80. package/dist/esm/pool/uma/utils.js +68 -0
  81. package/dist/esm/pool/uma/utils.js.map +1 -0
  82. package/dist/esm/utils/SpokeUtils.d.ts +5 -1
  83. package/dist/esm/utils/SpokeUtils.js +72 -3
  84. package/dist/esm/utils/SpokeUtils.js.map +1 -1
  85. package/dist/types/clients/SpokePoolClient/EVMSpokePoolClient.d.ts +1 -0
  86. package/dist/types/clients/SpokePoolClient/EVMSpokePoolClient.d.ts.map +1 -1
  87. package/dist/types/clients/SpokePoolClient/SVMSpokePoolClient.d.ts.map +1 -1
  88. package/dist/types/interfaces/SpokePool.d.ts +5 -0
  89. package/dist/types/interfaces/SpokePool.d.ts.map +1 -1
  90. package/dist/types/pool/poolClient.d.ts +9 -13
  91. package/dist/types/pool/poolClient.d.ts.map +1 -1
  92. package/dist/types/pool/uma/across/constants.d.ts +3 -0
  93. package/dist/types/pool/uma/across/constants.d.ts.map +1 -0
  94. package/dist/types/pool/uma/across/index.d.ts +3 -0
  95. package/dist/types/pool/uma/across/index.d.ts.map +1 -0
  96. package/dist/types/pool/uma/across/transactionManager.d.ts +13 -0
  97. package/dist/types/pool/uma/across/transactionManager.d.ts.map +1 -0
  98. package/dist/types/pool/uma/clients/erc20/client.d.ts +27 -0
  99. package/dist/types/pool/uma/clients/erc20/client.d.ts.map +1 -0
  100. package/dist/types/pool/uma/clients/erc20/index.d.ts +2 -0
  101. package/dist/types/pool/uma/clients/erc20/index.d.ts.map +1 -0
  102. package/dist/types/pool/uma/clients/index.d.ts +2 -0
  103. package/dist/types/pool/uma/clients/index.d.ts.map +1 -0
  104. package/dist/types/pool/uma/index.d.ts +14 -0
  105. package/dist/types/pool/uma/index.d.ts.map +1 -0
  106. package/dist/types/pool/uma/oracle/index.d.ts +2 -0
  107. package/dist/types/pool/uma/oracle/index.d.ts.map +1 -0
  108. package/dist/types/pool/uma/oracle/utils.d.ts +24 -0
  109. package/dist/types/pool/uma/oracle/utils.d.ts.map +1 -0
  110. package/dist/types/pool/uma/utils.d.ts +18 -0
  111. package/dist/types/pool/uma/utils.d.ts.map +1 -0
  112. package/dist/types/utils/SpokeUtils.d.ts +5 -1
  113. package/dist/types/utils/SpokeUtils.d.ts.map +1 -1
  114. package/package.json +2 -2
  115. package/src/clients/SpokePoolClient/EVMSpokePoolClient.ts +54 -39
  116. package/src/clients/SpokePoolClient/SVMSpokePoolClient.ts +5 -0
  117. package/src/interfaces/SpokePool.ts +6 -0
  118. package/src/pool/poolClient.ts +1 -1
  119. package/src/pool/uma/across/constants.ts +2 -0
  120. package/src/pool/uma/across/index.ts +2 -0
  121. package/src/pool/uma/across/transactionManager.ts +73 -0
  122. package/src/pool/uma/clients/erc20/README.md +29 -0
  123. package/src/pool/uma/clients/erc20/client.e2e.ts +26 -0
  124. package/src/pool/uma/clients/erc20/client.ts +67 -0
  125. package/src/pool/uma/clients/erc20/index.ts +1 -0
  126. package/src/pool/uma/clients/index.ts +1 -0
  127. package/src/pool/uma/index.ts +33 -0
  128. package/src/pool/uma/oracle/index.ts +2 -0
  129. package/src/pool/uma/oracle/utils.ts +38 -0
  130. package/src/pool/uma/utils.test.ts +24 -0
  131. package/src/pool/uma/utils.ts +53 -0
  132. package/src/utils/SpokeUtils.ts +63 -2
@@ -7,7 +7,7 @@ import {
7
7
  relayFillStatus,
8
8
  getTimestampForBlock as _getTimestampForBlock,
9
9
  } from "../../arch/evm";
10
- import { DepositWithBlock, FillStatus, RelayData } from "../../interfaces";
10
+ import { DepositWithBlock, FillStatus, Log, RelayData } from "../../interfaces";
11
11
  import {
12
12
  BigNumber,
13
13
  DepositSearchResult,
@@ -155,45 +155,14 @@ export class EVMSpokePoolClient extends SpokePoolClient {
155
155
  }
156
156
 
157
157
  // No deposit found; revert to searching for it.
158
- const upperBound = this.latestHeightSearched || undefined; // Don't permit block 0 as the high block.
159
- const from = await findDepositBlock(this.spokePool, depositId, this.deploymentBlock, upperBound);
160
- const chain = getNetworkName(this.chainId);
161
- if (!from) {
162
- const reason =
163
- `Unable to find ${chain} depositId ${depositId}` +
164
- ` within blocks [${this.deploymentBlock}, ${upperBound ?? "latest"}].`;
165
- return { found: false, code: InvalidFill.DepositIdNotFound, reason };
166
- }
158
+ const result = await this.queryDepositEvents(depositId);
167
159
 
168
- const to = from;
169
- const tStart = Date.now();
170
- // Check both V3FundsDeposited and FundsDeposited events to look for a specified depositId.
171
- const { maxLookBack } = this.eventSearchConfig;
172
- const query = (
173
- await Promise.all([
174
- paginatedEventQuery(
175
- this.spokePool,
176
- this.spokePool.filters.V3FundsDeposited(null, null, null, null, null, depositId),
177
- { from, to, maxLookBack }
178
- ),
179
- paginatedEventQuery(
180
- this.spokePool,
181
- this.spokePool.filters.FundsDeposited(null, null, null, null, null, depositId),
182
- { from, to, maxLookBack }
183
- ),
184
- ])
185
- ).flat();
186
- const tStop = Date.now();
187
-
188
- const event = query.find(({ args }) => args["depositId"].eq(depositId));
189
- if (event === undefined) {
190
- return {
191
- found: false,
192
- code: InvalidFill.DepositIdNotFound,
193
- reason: `${chain} depositId ${depositId} not found at block ${from}.`,
194
- };
160
+ if ("reason" in result) {
161
+ return { found: false, code: InvalidFill.DepositIdNotFound, reason: result.reason };
195
162
  }
196
163
 
164
+ const { event, elapsedMs } = result;
165
+
197
166
  const partialDeposit = unpackDepositEvent(spreadEventWithBlockNumber(event), this.chainId);
198
167
  const quoteBlockNumber = await this.getBlockNumber(partialDeposit.quoteTimestamp);
199
168
  const outputToken = partialDeposit.outputToken.isZeroAddress()
@@ -212,13 +181,59 @@ export class EVMSpokePoolClient extends SpokePoolClient {
212
181
  at: "SpokePoolClient#findDeposit",
213
182
  message: "Located deposit outside of SpokePoolClient's search range",
214
183
  deposit,
215
- elapsedMs: tStop - tStart,
184
+ elapsedMs,
216
185
  });
217
-
218
186
  return { found: true, deposit };
219
187
  }
220
188
 
221
189
  public override getTimestampForBlock(blockNumber: number): Promise<number> {
222
190
  return _getTimestampForBlock(this.spokePool.provider, blockNumber);
223
191
  }
192
+
193
+ private async queryDepositEvents(
194
+ depositId: BigNumber
195
+ ): Promise<{ event: Log; elapsedMs: number } | { reason: string }> {
196
+ const tStart = Date.now();
197
+ const upperBound = this.latestHeightSearched || undefined;
198
+ const from = await findDepositBlock(this.spokePool, depositId, this.deploymentBlock, upperBound);
199
+ const chain = getNetworkName(this.chainId);
200
+
201
+ if (!from) {
202
+ return {
203
+ reason: `Unable to find ${chain} depositId ${depositId} within blocks [${this.deploymentBlock}, ${
204
+ upperBound ?? "latest"
205
+ }].`,
206
+ };
207
+ }
208
+
209
+ const to = from;
210
+
211
+ const { maxLookBack } = this.eventSearchConfig;
212
+ const events = (
213
+ await Promise.all([
214
+ paginatedEventQuery(
215
+ this.spokePool,
216
+ this.spokePool.filters.V3FundsDeposited(null, null, null, null, null, depositId),
217
+ { from, to, maxLookBack }
218
+ ),
219
+ paginatedEventQuery(
220
+ this.spokePool,
221
+ this.spokePool.filters.FundsDeposited(null, null, null, null, null, depositId),
222
+ { from, to, maxLookBack }
223
+ ),
224
+ ])
225
+ )
226
+ .flat()
227
+ .filter(({ args }) => args["depositId"].eq(depositId));
228
+
229
+ const tStop = Date.now();
230
+ const [event] = events;
231
+ if (!event) {
232
+ return {
233
+ reason: `Unable to find ${chain} depositId ${depositId} within blocks [${from}, ${upperBound ?? "latest"}].`,
234
+ };
235
+ }
236
+
237
+ return { event, elapsedMs: tStop - tStart };
238
+ }
224
239
  }
@@ -218,6 +218,11 @@ export class SVMSpokePoolClient extends SpokePoolClient {
218
218
  * Finds a deposit based on its deposit ID on the SVM chain.
219
219
  */
220
220
  public async findDeposit(depositId: BigNumber): Promise<DepositSearchResult> {
221
+ // First check memory for deposits
222
+ const memoryDeposit = this.getDeposit(depositId);
223
+ if (memoryDeposit) {
224
+ return { found: true, deposit: memoryDeposit };
225
+ }
221
226
  const deposit = await findDeposit(this.svmEventsClient, depositId, this.logger);
222
227
  if (!deposit) {
223
228
  return {
@@ -175,3 +175,9 @@ export interface SpokePoolClientsByChain {
175
175
  export interface RelayDataWithMessageHash extends RelayData {
176
176
  messageHash?: string;
177
177
  }
178
+
179
+ export interface InvalidFill {
180
+ fill: FillWithBlock;
181
+ reason: string;
182
+ deposit?: DepositWithBlock;
183
+ }
@@ -1,5 +1,5 @@
1
1
  import assert from "assert";
2
- import * as uma from "@uma/sdk";
2
+ import * as uma from "./uma";
3
3
  import {
4
4
  bnZero,
5
5
  toBNWei,
@@ -0,0 +1,2 @@
1
+ export const SECONDS_PER_YEAR = 31557600; // based on 365.25 days per year
2
+ export const DEFAULT_BLOCK_DELTA = 10; // look exchange rate up based on 10 block difference by default
@@ -0,0 +1,2 @@
1
+ export { default as TransactionManager } from "./transactionManager";
2
+ export * as constants from "./constants";
@@ -0,0 +1,73 @@
1
+ import assert from "assert";
2
+ import { Signer } from "ethers";
3
+ import { TransactionRequest, TransactionReceipt } from "@ethersproject/abstract-provider";
4
+
5
+ function makeKey(tx: TransactionRequest) {
6
+ return JSON.stringify(
7
+ Object.entries(tx).map(([key, value]) => {
8
+ return [key, (value || "").toString()];
9
+ })
10
+ );
11
+ }
12
+
13
+ type Config = {
14
+ confirmations?: number;
15
+ };
16
+ export type Emit = (event: string, key: string, data: TransactionReceipt | string | TransactionRequest | Error) => void;
17
+ export default (config: Config, signer: Signer, emit: Emit = () => null) => {
18
+ assert(signer.provider, "signer requires a provider, use signer.connect(provider)");
19
+ const { confirmations = 3 } = config;
20
+ const requests = new Map<string, TransactionRequest>();
21
+ const submissions = new Map<string, string>();
22
+ const mined = new Map<string, TransactionReceipt>();
23
+ function request(unsignedTx: TransactionRequest) {
24
+ // this no longer calls signer.populateTransaction, to allow metamask to fill in missing details instead
25
+ // use overrides if you want to manually fill in other tx details, including the overrides.customData field.
26
+ const populated = unsignedTx;
27
+ const key = makeKey(populated);
28
+ assert(!requests.has(key), "Transaction already in progress");
29
+ requests.set(key, populated);
30
+ return key;
31
+ }
32
+ async function processRequest(key: string) {
33
+ const request = requests.get(key);
34
+ assert(request, "invalid request");
35
+ // always delete request, it should only be submitted once
36
+ requests.delete(key);
37
+ try {
38
+ const sent = await signer.sendTransaction(request);
39
+ submissions.set(key, sent.hash);
40
+ emit("submitted", key, sent.hash);
41
+ } catch (err) {
42
+ emit("error", key, err as Error);
43
+ }
44
+ }
45
+ async function processSubmission(key: string) {
46
+ const hash = submissions.get(key);
47
+ assert(hash, "invalid submission");
48
+ assert(signer.provider, "signer requires a provider, use signer.connect(provider)");
49
+ // we look for this transaction, but it may never find it if its sped up
50
+ const receipt = await signer.provider.getTransactionReceipt(hash).catch(() => undefined);
51
+ if (receipt == null) return;
52
+ if (receipt.confirmations < confirmations) return;
53
+ submissions.delete(key);
54
+ mined.set(key, receipt);
55
+ emit("mined", key, receipt);
56
+ }
57
+ function isMined(key: string) {
58
+ return Promise.resolve(mined.get(key));
59
+ }
60
+ async function update() {
61
+ for (const key of Array.from(requests.keys())) {
62
+ await processRequest(key);
63
+ }
64
+ for (const key of Array.from(submissions.keys())) {
65
+ await processSubmission(key);
66
+ }
67
+ }
68
+ return {
69
+ request,
70
+ isMined,
71
+ update,
72
+ };
73
+ };
@@ -0,0 +1,29 @@
1
+ # UMA SDK ERC20 Client
2
+
3
+ Client to interface with ERC20 style tokens, based on Ethers and typechain.
4
+
5
+ ## Usage
6
+
7
+ ```js
8
+ import {ethers} from 'ethers'
9
+
10
+ // assume you have a url injected from env
11
+ const provider = new ethers.providers.WebSocketProvider(env.CUSTOM_NODE_URL)
12
+
13
+ // get the contract instance
14
+ const erc20Address:string = // assume you have an emp address you want to connect to
15
+ const erc20Instance:uma.clients.erc20.Instance = uma.clients.erc20.connect(erc20Address,provider)
16
+
17
+ // gets all emp events, see ethers queryFilter for details on contructing the query.
18
+ const events = await erc20Instance.queryFilter({})
19
+
20
+ // returns EventState, defined in the emp client. This can contain user balances as well as approval limits.
21
+ const state:uma.clients.erc20.EventState = uma.clients.erc20.getEventState(events)
22
+
23
+ // Types
24
+ types {Transfer,Approval, Instance, EventState} = uma.clients.erc20
25
+ // Transfer and Approval are event types
26
+ // Instance is the contract instance once connected
27
+ // Event state describes what the state reconstruction based on contract events will look like
28
+
29
+ ```
@@ -0,0 +1,26 @@
1
+ import assert from "assert";
2
+ import { ethers } from "ethers";
3
+ import * as Client from "./client";
4
+
5
+ const address = "0xeca82185adCE47f39c684352B0439f030f860318";
6
+ // these require integration testing, skip for ci
7
+ describe("erc20", function () {
8
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
9
+ let events: any;
10
+ let client: Client.Instance;
11
+ test("inits", function () {
12
+ const provider = ethers.providers.getDefaultProvider(process.env.CUSTOM_NODE_URL);
13
+ client = Client.connect(address, provider);
14
+ assert.ok(client);
15
+ });
16
+ test("getEventState between", async function () {
17
+ events = await client.queryFilter({}, 12477952, 12477952 + 1000);
18
+ assert.ok(events.length);
19
+ });
20
+ test("getEventState", async function () {
21
+ const state = await Client.getEventState(events);
22
+ assert.ok(state.balances);
23
+ assert.ok(state.approvalsByOwner);
24
+ assert.ok(state.approvalsBySpender);
25
+ });
26
+ });
@@ -0,0 +1,67 @@
1
+ import { ERC20Ethers, ERC20Ethers__factory } from "@uma/contracts-node";
2
+ // eslint-disable-next-line no-restricted-imports
3
+ import { Event } from "ethers";
4
+ import { set } from "lodash";
5
+ import { Balances } from "../../utils";
6
+ import type { Provider, GetEventType } from "../..";
7
+
8
+ export type Instance = ERC20Ethers;
9
+ const Factory = ERC20Ethers__factory;
10
+
11
+ export function connect(address: string, provider: Provider): Instance {
12
+ return Factory.connect(address, provider);
13
+ }
14
+
15
+ export interface EventState {
16
+ // any address that created a position, regardless of if they have closed it
17
+ balances?: Balances;
18
+ // approvals are keyed both ways here for ease of lookup by either owner or spender
19
+ approvalsByOwner?: {
20
+ [owner: string]: {
21
+ [spender: string]: {
22
+ amount: string;
23
+ };
24
+ };
25
+ };
26
+ approvalsBySpender?: {
27
+ [spender: string]: {
28
+ [owner: string]: {
29
+ amount: string;
30
+ };
31
+ };
32
+ };
33
+ }
34
+
35
+ export type Transfer = GetEventType<Instance, "Transfer">;
36
+ export type Approval = GetEventType<Instance, "Approval">;
37
+
38
+ // takes all events and returns user balances and approvals
39
+ function reduceEvents(state: EventState = {}, event: Event): EventState {
40
+ switch (event.event) {
41
+ case "Transfer": {
42
+ const typedEvent = event as Transfer;
43
+ const { from, to, value } = typedEvent.args;
44
+ const balances = Balances(state.balances || {});
45
+ balances.sub(from, value);
46
+ balances.add(to, value);
47
+ return {
48
+ ...state,
49
+ balances: balances.balances,
50
+ };
51
+ }
52
+ case "Approval": {
53
+ const typedEvent = event as Approval;
54
+ const { owner, spender, value } = typedEvent.args;
55
+ set(state, ["approvalsByOwner", owner, spender], value.toString());
56
+ set(state, ["approvalsBySpender", spender, owner], value.toString());
57
+ return {
58
+ ...state,
59
+ };
60
+ }
61
+ }
62
+ return state;
63
+ }
64
+
65
+ export function getEventState(events: Event[], initialState: EventState = {}): EventState {
66
+ return events.reduce(reduceEvents, initialState);
67
+ }
@@ -0,0 +1 @@
1
+ export * from "./client";
@@ -0,0 +1 @@
1
+ export * as erc20 from "./erc20";
@@ -0,0 +1,33 @@
1
+ // eslint-disable-next-line no-restricted-imports
2
+ import { Contract, ethers, Event } from "ethers";
3
+ import type { TypedEventFilterEthers as TypedEventFilter, TypedEventEthers as TypedEvent } from "@uma/contracts-node";
4
+
5
+ export * as across from "./across";
6
+ export * as clients from "./clients";
7
+ export * as oracle from "./oracle";
8
+ export * as utils from "./utils";
9
+
10
+ export { type Provider } from "@ethersproject/providers";
11
+
12
+ type Result = ethers.utils.Result;
13
+
14
+ export interface Callable {
15
+ (...args: unknown[]): unknown;
16
+ }
17
+
18
+ export type SerializableEvent = Omit<
19
+ Event,
20
+ "decode" | "removeListener" | "getBlock" | "getTransaction" | "getTransactionReceipt"
21
+ >;
22
+
23
+ // this convoluted type is meant to cast events to the types you need based on the contract and event name
24
+ // example: type NewContractRegistered = GetEventType<Registry,"NewContractRegistered">;
25
+ // TODO: the any below is a hacky solution because some typechain types fail to resolve due to
26
+ // incompatible TypedEventFilter and TypedEvent types. This will be fixed by upgrading typechain
27
+ // to a version where Ethers events are exported as first class types.
28
+ export type GetEventType<ContractType extends Contract, EventName extends string> = ReturnType<
29
+ ContractType["filters"][EventName] extends Callable ? ContractType["filters"][EventName] : never
30
+ > extends TypedEventFilter<infer T, infer S>
31
+ ? // eslint-disable-next-line @typescript-eslint/no-explicit-any
32
+ TypedEvent<T & S extends Result ? T & S : any>
33
+ : never;
@@ -0,0 +1,2 @@
1
+ // Minimal oracle module - only export utils needed by poolClient
2
+ export * as utils from "./utils";
@@ -0,0 +1,38 @@
1
+ import { BigNumberish } from "../../../utils";
2
+ import sortedLastIndexBy from "lodash/sortedLastIndexBy";
3
+
4
+ /**
5
+ * eventKey. Make a unique and sortable identifier string for an event
6
+ * Used by poolClient.ts lines 166, 246
7
+ *
8
+ * @param {Event} event
9
+ * @returns {string} - the unique id
10
+ */
11
+ export function eventKey(event: {
12
+ blockNumber: BigNumberish;
13
+ transactionIndex: BigNumberish;
14
+ logIndex: BigNumberish;
15
+ }): string {
16
+ return [
17
+ // we pad these because numbers of varying lengths will not sort correctly, ie "10" will incorrectly sort before "9", but "09" will be correct.
18
+ event.blockNumber.toString().padStart(16, "0"),
19
+ event.transactionIndex.toString().padStart(16, "0"),
20
+ event.logIndex?.toString().padStart(16, "0"),
21
+ // ~ is the last printable ascii char, so it does not interfere with sorting
22
+ ].join("~");
23
+ }
24
+
25
+ /**
26
+ * insertOrdered. Inserts items in an array maintaining sorted order, in this case lowest to highest. Does not check duplicates.
27
+ * Mainly used for caching all known events, in order of oldest to newest.
28
+ * Used by poolClient.ts line 181
29
+ *
30
+ * @param {T[]} array
31
+ * @param {T} element
32
+ * @param {Function} orderBy
33
+ */
34
+ export function insertOrderedAscending<T>(array: T[], element: T, orderBy: (element: T) => string | number): T[] {
35
+ const index = sortedLastIndexBy(array, element, orderBy);
36
+ array.splice(index, 0, element);
37
+ return array;
38
+ }
@@ -0,0 +1,24 @@
1
+ import * as utils from "./utils";
2
+ import assert from "assert";
3
+ test("Balances", function () {
4
+ const balances = utils.Balances();
5
+ assert.ok(balances);
6
+ balances.create("a", "100");
7
+ balances.create("b", "99");
8
+ let result = balances.get("a");
9
+ assert.equal(result, "100");
10
+ result = balances.get("b");
11
+ assert.equal(result, "99");
12
+
13
+ result = balances.sub("a", 1);
14
+ assert.equal(result, "99");
15
+
16
+ result = balances.sub("b", 1);
17
+ assert.equal(result, "98");
18
+
19
+ result = balances.add("b", 2);
20
+ assert.equal(result, "100");
21
+
22
+ const bals = balances.balances;
23
+ assert.equal(Object.keys(bals).length, 2);
24
+ });
@@ -0,0 +1,53 @@
1
+ import assert from "assert";
2
+ import { BigNumber, BigNumberish, delay as sleep } from "../../utils";
3
+
4
+ export { delay as sleep } from "../../utils";
5
+
6
+ // check if a value is not null or undefined, useful for numbers which could be 0.
7
+ // "is" syntax: https://stackoverflow.com/questions/40081332/what-does-the-is-keyword-do-in-typescript
8
+ /* eslint-disable-next-line @typescript-eslint/ban-types */
9
+ export function exists<T>(value: T | null | undefined): value is NonNullable<T> {
10
+ return value !== null && value !== undefined;
11
+ }
12
+
13
+ // useful for maintaining balances from events
14
+ export type Balances = { [key: string]: string };
15
+ export function Balances(balances: Balances = {}) {
16
+ function create(id: string, amount = "0") {
17
+ assert(!has(id), "balance already exists");
18
+ return set(id, amount);
19
+ }
20
+ function has(id: string) {
21
+ return exists(balances[id]);
22
+ }
23
+ function set(id: string, amount: string) {
24
+ balances[id] = amount;
25
+ return amount;
26
+ }
27
+ function add(id: string, amount: BigNumberish) {
28
+ return set(id, BigNumber.from(amount).add(getOrCreate(id)).toString());
29
+ }
30
+ function sub(id: string, amount: BigNumberish) {
31
+ return set(id, BigNumber.from(getOrCreate(id)).sub(amount).toString());
32
+ }
33
+ function get(id: string) {
34
+ assert(has(id), "balance does not exist");
35
+ return balances[id];
36
+ }
37
+ function getOrCreate(id: string) {
38
+ if (has(id)) return get(id);
39
+ return create(id);
40
+ }
41
+ return { create, add, sub, get, balances, set, has, getOrCreate };
42
+ }
43
+
44
+ // Loop forever but wait until execution is finished before starting next timer. Throw an error to break this
45
+ // or add another utlity function if you need it to end on condition.
46
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
47
+ export async function loop(fn: (...args: any[]) => any, delay: number, ...args: any[]) {
48
+ do {
49
+ await fn(...args);
50
+ await sleep(delay);
51
+ /* eslint-disable-next-line no-constant-condition */
52
+ } while (true);
53
+ }
@@ -6,6 +6,7 @@ import {
6
6
  Fill,
7
7
  FillWithBlock,
8
8
  FillType,
9
+ InvalidFill,
9
10
  RelayData,
10
11
  RelayExecutionEventInfo,
11
12
  SlowFillLeaf,
@@ -13,9 +14,10 @@ import {
13
14
  } from "../interfaces";
14
15
  import { svm } from "../arch";
15
16
  import { BigNumber } from "./BigNumberUtils";
16
- import { isMessageEmpty } from "./DepositUtils";
17
- import { chainIsSvm } from "./NetworkUtils";
17
+ import { isMessageEmpty, validateFillForDeposit } from "./DepositUtils";
18
+ import { chainIsSvm, getNetworkName } from "./NetworkUtils";
18
19
  import { toAddressType } from "./AddressUtils";
20
+ import { SpokePoolClient } from "../clients";
19
21
 
20
22
  export function isSlowFill(fill: Fill): boolean {
21
23
  return fill.relayExecutionInfo.fillType === FillType.SlowFill;
@@ -171,3 +173,62 @@ export function isUnsafeDepositId(depositId: BigNumber): boolean {
171
173
  export function getMessageHash(message: string): string {
172
174
  return isMessageEmpty(message) ? ZERO_BYTES : keccak256(message as Hex);
173
175
  }
176
+
177
+ export async function findInvalidFills(spokePoolClients: {
178
+ [chainId: number]: SpokePoolClient;
179
+ }): Promise<InvalidFill[]> {
180
+ const invalidFills: InvalidFill[] = [];
181
+
182
+ // Iterate through each spoke pool client
183
+ for (const spokePoolClient of Object.values(spokePoolClients)) {
184
+ // Get all fills for this client
185
+ const fills = spokePoolClient.getFills();
186
+
187
+ // Process fills in parallel for this client
188
+ const fillDepositPairs = await Promise.all(
189
+ fills.map(async (fill) => {
190
+ // Skip fills with unsafe deposit IDs
191
+ if (isUnsafeDepositId(fill.depositId)) {
192
+ return null; // Return null for unsafe deposits
193
+ }
194
+
195
+ // Get all deposits (including duplicates) for this fill's depositId, both in memory and on-chain
196
+ const depositResult = await spokePoolClients[fill.originChainId]?.findDeposit(fill.depositId);
197
+
198
+ // If no deposits found at all
199
+ if (!depositResult?.found) {
200
+ return {
201
+ fill,
202
+ deposit: null,
203
+ reason: `No ${getNetworkName(fill.originChainId)} deposit with depositId ${fill.depositId} found`,
204
+ };
205
+ }
206
+
207
+ // Check if fill is valid for deposit
208
+ const validationResult = validateFillForDeposit(fill, depositResult.deposit);
209
+ if (!validationResult.valid) {
210
+ return {
211
+ fill,
212
+ deposit: depositResult.deposit,
213
+ reason: validationResult.reason,
214
+ };
215
+ }
216
+
217
+ // Valid fill with deposit - return null to filter out
218
+ return null;
219
+ })
220
+ );
221
+
222
+ for (const pair of fillDepositPairs) {
223
+ if (pair) {
224
+ invalidFills.push({
225
+ fill: pair.fill,
226
+ reason: pair.reason,
227
+ deposit: pair.deposit || undefined,
228
+ });
229
+ }
230
+ }
231
+ }
232
+
233
+ return invalidFills;
234
+ }