@across-protocol/sdk 4.3.113 → 4.3.114-alpha.1

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 (165) hide show
  1. package/dist/cjs/arch/svm/SpokeUtils.d.ts +17 -24
  2. package/dist/cjs/arch/svm/SpokeUtils.js.map +1 -1
  3. package/dist/cjs/arch/svm/types.d.ts +2 -1
  4. package/dist/cjs/arch/svm/types.js.map +1 -1
  5. package/dist/cjs/arch/svm/utils.d.ts +4 -9
  6. package/dist/cjs/arch/svm/utils.js.map +1 -1
  7. package/dist/cjs/gasPriceOracle/adapters/solana.js.map +1 -1
  8. package/dist/cjs/index.d.ts +0 -1
  9. package/dist/cjs/index.js +1 -2
  10. package/dist/cjs/index.js.map +1 -1
  11. package/dist/cjs/relayFeeCalculator/chain-queries/svmQuery.d.ts +4 -4
  12. package/dist/cjs/relayFeeCalculator/chain-queries/svmQuery.js +1 -1
  13. package/dist/cjs/relayFeeCalculator/chain-queries/svmQuery.js.map +1 -1
  14. package/dist/esm/arch/svm/SpokeUtils.d.ts +17 -24
  15. package/dist/esm/arch/svm/SpokeUtils.js.map +1 -1
  16. package/dist/esm/arch/svm/types.d.ts +6 -1
  17. package/dist/esm/arch/svm/types.js.map +1 -1
  18. package/dist/esm/arch/svm/utils.d.ts +4 -9
  19. package/dist/esm/arch/svm/utils.js.map +1 -1
  20. package/dist/esm/gasPriceOracle/adapters/solana.js +1 -1
  21. package/dist/esm/gasPriceOracle/adapters/solana.js.map +1 -1
  22. package/dist/esm/index.d.ts +0 -1
  23. package/dist/esm/index.js +0 -2
  24. package/dist/esm/index.js.map +1 -1
  25. package/dist/esm/relayFeeCalculator/chain-queries/svmQuery.d.ts +4 -4
  26. package/dist/esm/relayFeeCalculator/chain-queries/svmQuery.js +3 -3
  27. package/dist/esm/relayFeeCalculator/chain-queries/svmQuery.js.map +1 -1
  28. package/dist/types/arch/svm/SpokeUtils.d.ts +17 -24
  29. package/dist/types/arch/svm/SpokeUtils.d.ts.map +1 -1
  30. package/dist/types/arch/svm/types.d.ts +6 -1
  31. package/dist/types/arch/svm/types.d.ts.map +1 -1
  32. package/dist/types/arch/svm/utils.d.ts +4 -9
  33. package/dist/types/arch/svm/utils.d.ts.map +1 -1
  34. package/dist/types/gasPriceOracle/adapters/solana.d.ts.map +1 -1
  35. package/dist/types/index.d.ts +0 -1
  36. package/dist/types/index.d.ts.map +1 -1
  37. package/dist/types/relayFeeCalculator/chain-queries/svmQuery.d.ts +4 -4
  38. package/dist/types/relayFeeCalculator/chain-queries/svmQuery.d.ts.map +1 -1
  39. package/package.json +17 -8
  40. package/src/arch/svm/SpokeUtils.ts +24 -26
  41. package/src/arch/svm/types.ts +9 -0
  42. package/src/arch/svm/utils.ts +4 -4
  43. package/src/gasPriceOracle/adapters/solana.ts +8 -3
  44. package/src/index.ts +0 -1
  45. package/src/relayFeeCalculator/chain-queries/svmQuery.ts +6 -7
  46. package/dist/cjs/pool/TransactionManager.d.ts +0 -12
  47. package/dist/cjs/pool/TransactionManager.js +0 -121
  48. package/dist/cjs/pool/TransactionManager.js.map +0 -1
  49. package/dist/cjs/pool/index.d.ts +0 -1
  50. package/dist/cjs/pool/index.js +0 -5
  51. package/dist/cjs/pool/index.js.map +0 -1
  52. package/dist/cjs/pool/poolClient.d.ts +0 -228
  53. package/dist/cjs/pool/poolClient.js +0 -869
  54. package/dist/cjs/pool/poolClient.js.map +0 -1
  55. package/dist/cjs/pool/uma/across/constants.d.ts +0 -2
  56. package/dist/cjs/pool/uma/across/constants.js +0 -6
  57. package/dist/cjs/pool/uma/across/constants.js.map +0 -1
  58. package/dist/cjs/pool/uma/across/index.d.ts +0 -2
  59. package/dist/cjs/pool/uma/across/index.js +0 -8
  60. package/dist/cjs/pool/uma/across/index.js.map +0 -1
  61. package/dist/cjs/pool/uma/across/transactionManager.d.ts +0 -12
  62. package/dist/cjs/pool/uma/across/transactionManager.js +0 -121
  63. package/dist/cjs/pool/uma/across/transactionManager.js.map +0 -1
  64. package/dist/cjs/pool/uma/clients/erc20/client.d.ts +0 -26
  65. package/dist/cjs/pool/uma/clients/erc20/client.js +0 -38
  66. package/dist/cjs/pool/uma/clients/erc20/client.js.map +0 -1
  67. package/dist/cjs/pool/uma/clients/erc20/index.d.ts +0 -1
  68. package/dist/cjs/pool/uma/clients/erc20/index.js +0 -5
  69. package/dist/cjs/pool/uma/clients/erc20/index.js.map +0 -1
  70. package/dist/cjs/pool/uma/clients/index.d.ts +0 -1
  71. package/dist/cjs/pool/uma/clients/index.js +0 -6
  72. package/dist/cjs/pool/uma/clients/index.js.map +0 -1
  73. package/dist/cjs/pool/uma/index.d.ts +0 -13
  74. package/dist/cjs/pool/uma/index.js +0 -9
  75. package/dist/cjs/pool/uma/index.js.map +0 -1
  76. package/dist/cjs/pool/uma/oracle/index.d.ts +0 -1
  77. package/dist/cjs/pool/uma/oracle/index.js +0 -6
  78. package/dist/cjs/pool/uma/oracle/index.js.map +0 -1
  79. package/dist/cjs/pool/uma/oracle/utils.d.ts +0 -7
  80. package/dist/cjs/pool/uma/oracle/utils.js +0 -20
  81. package/dist/cjs/pool/uma/oracle/utils.js.map +0 -1
  82. package/dist/cjs/pool/uma/utils.d.ts +0 -17
  83. package/dist/cjs/pool/uma/utils.js +0 -69
  84. package/dist/cjs/pool/uma/utils.js.map +0 -1
  85. package/dist/esm/pool/TransactionManager.d.ts +0 -12
  86. package/dist/esm/pool/TransactionManager.js +0 -122
  87. package/dist/esm/pool/TransactionManager.js.map +0 -1
  88. package/dist/esm/pool/index.d.ts +0 -1
  89. package/dist/esm/pool/index.js +0 -2
  90. package/dist/esm/pool/index.js.map +0 -1
  91. package/dist/esm/pool/poolClient.d.ts +0 -238
  92. package/dist/esm/pool/poolClient.js +0 -899
  93. package/dist/esm/pool/poolClient.js.map +0 -1
  94. package/dist/esm/pool/uma/across/constants.d.ts +0 -2
  95. package/dist/esm/pool/uma/across/constants.js +0 -3
  96. package/dist/esm/pool/uma/across/constants.js.map +0 -1
  97. package/dist/esm/pool/uma/across/index.d.ts +0 -2
  98. package/dist/esm/pool/uma/across/index.js +0 -4
  99. package/dist/esm/pool/uma/across/index.js.map +0 -1
  100. package/dist/esm/pool/uma/across/transactionManager.d.ts +0 -12
  101. package/dist/esm/pool/uma/across/transactionManager.js +0 -122
  102. package/dist/esm/pool/uma/across/transactionManager.js.map +0 -1
  103. package/dist/esm/pool/uma/clients/erc20/client.d.ts +0 -26
  104. package/dist/esm/pool/uma/clients/erc20/client.js +0 -35
  105. package/dist/esm/pool/uma/clients/erc20/client.js.map +0 -1
  106. package/dist/esm/pool/uma/clients/erc20/index.d.ts +0 -1
  107. package/dist/esm/pool/uma/clients/erc20/index.js +0 -2
  108. package/dist/esm/pool/uma/clients/erc20/index.js.map +0 -1
  109. package/dist/esm/pool/uma/clients/index.d.ts +0 -1
  110. package/dist/esm/pool/uma/clients/index.js +0 -3
  111. package/dist/esm/pool/uma/clients/index.js.map +0 -1
  112. package/dist/esm/pool/uma/index.d.ts +0 -13
  113. package/dist/esm/pool/uma/index.js +0 -9
  114. package/dist/esm/pool/uma/index.js.map +0 -1
  115. package/dist/esm/pool/uma/oracle/index.d.ts +0 -1
  116. package/dist/esm/pool/uma/oracle/index.js +0 -3
  117. package/dist/esm/pool/uma/oracle/index.js.map +0 -1
  118. package/dist/esm/pool/uma/oracle/utils.d.ts +0 -23
  119. package/dist/esm/pool/uma/oracle/utils.js +0 -33
  120. package/dist/esm/pool/uma/oracle/utils.js.map +0 -1
  121. package/dist/esm/pool/uma/utils.d.ts +0 -17
  122. package/dist/esm/pool/uma/utils.js +0 -68
  123. package/dist/esm/pool/uma/utils.js.map +0 -1
  124. package/dist/types/pool/TransactionManager.d.ts +0 -13
  125. package/dist/types/pool/TransactionManager.d.ts.map +0 -1
  126. package/dist/types/pool/index.d.ts +0 -2
  127. package/dist/types/pool/index.d.ts.map +0 -1
  128. package/dist/types/pool/poolClient.d.ts +0 -239
  129. package/dist/types/pool/poolClient.d.ts.map +0 -1
  130. package/dist/types/pool/uma/across/constants.d.ts +0 -3
  131. package/dist/types/pool/uma/across/constants.d.ts.map +0 -1
  132. package/dist/types/pool/uma/across/index.d.ts +0 -3
  133. package/dist/types/pool/uma/across/index.d.ts.map +0 -1
  134. package/dist/types/pool/uma/across/transactionManager.d.ts +0 -13
  135. package/dist/types/pool/uma/across/transactionManager.d.ts.map +0 -1
  136. package/dist/types/pool/uma/clients/erc20/client.d.ts +0 -27
  137. package/dist/types/pool/uma/clients/erc20/client.d.ts.map +0 -1
  138. package/dist/types/pool/uma/clients/erc20/index.d.ts +0 -2
  139. package/dist/types/pool/uma/clients/erc20/index.d.ts.map +0 -1
  140. package/dist/types/pool/uma/clients/index.d.ts +0 -2
  141. package/dist/types/pool/uma/clients/index.d.ts.map +0 -1
  142. package/dist/types/pool/uma/index.d.ts +0 -14
  143. package/dist/types/pool/uma/index.d.ts.map +0 -1
  144. package/dist/types/pool/uma/oracle/index.d.ts +0 -2
  145. package/dist/types/pool/uma/oracle/index.d.ts.map +0 -1
  146. package/dist/types/pool/uma/oracle/utils.d.ts +0 -24
  147. package/dist/types/pool/uma/oracle/utils.d.ts.map +0 -1
  148. package/dist/types/pool/uma/utils.d.ts +0 -18
  149. package/dist/types/pool/uma/utils.d.ts.map +0 -1
  150. package/src/pool/TransactionManager.ts +0 -73
  151. package/src/pool/index.ts +0 -1
  152. package/src/pool/poolClient.ts +0 -849
  153. package/src/pool/uma/across/constants.ts +0 -2
  154. package/src/pool/uma/across/index.ts +0 -2
  155. package/src/pool/uma/across/transactionManager.ts +0 -73
  156. package/src/pool/uma/clients/erc20/README.md +0 -29
  157. package/src/pool/uma/clients/erc20/client.e2e.ts +0 -26
  158. package/src/pool/uma/clients/erc20/client.ts +0 -67
  159. package/src/pool/uma/clients/erc20/index.ts +0 -1
  160. package/src/pool/uma/clients/index.ts +0 -1
  161. package/src/pool/uma/index.ts +0 -33
  162. package/src/pool/uma/oracle/index.ts +0 -2
  163. package/src/pool/uma/oracle/utils.ts +0 -38
  164. package/src/pool/uma/utils.test.ts +0 -24
  165. package/src/pool/uma/utils.ts +0 -53
@@ -1,2 +0,0 @@
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
@@ -1,2 +0,0 @@
1
- export { default as TransactionManager } from "./transactionManager";
2
- export * as constants from "./constants";
@@ -1,73 +0,0 @@
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
- };
@@ -1,29 +0,0 @@
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 constructing 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
- ```
@@ -1,26 +0,0 @@
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
- });
@@ -1,67 +0,0 @@
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
- }
@@ -1 +0,0 @@
1
- export * from "./client";
@@ -1 +0,0 @@
1
- export * as erc20 from "./erc20";
@@ -1,33 +0,0 @@
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;
@@ -1,2 +0,0 @@
1
- // Minimal oracle module - only export utils needed by poolClient
2
- export * as utils from "./utils";
@@ -1,38 +0,0 @@
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
- }
@@ -1,24 +0,0 @@
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
- });
@@ -1,53 +0,0 @@
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 utility 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
- }