@gauntlet-xyz/sdk 1.0.6 → 2.3.0

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 (145) hide show
  1. package/README.md +82 -1
  2. package/dist/api/activity.d.ts +70 -0
  3. package/dist/api/activity.d.ts.map +1 -0
  4. package/dist/api/activity.js +154 -0
  5. package/dist/api/activity.js.map +1 -0
  6. package/dist/api/caip.d.ts +29 -0
  7. package/dist/api/caip.d.ts.map +1 -0
  8. package/dist/api/caip.js +38 -0
  9. package/dist/api/caip.js.map +1 -0
  10. package/dist/api/client.d.ts +114 -0
  11. package/dist/api/client.d.ts.map +1 -0
  12. package/dist/api/client.js +109 -0
  13. package/dist/api/client.js.map +1 -0
  14. package/dist/api/decimal.d.ts +21 -0
  15. package/dist/api/decimal.d.ts.map +1 -0
  16. package/dist/api/decimal.js +44 -0
  17. package/dist/api/decimal.js.map +1 -0
  18. package/dist/api/errors.d.ts +32 -0
  19. package/dist/api/errors.d.ts.map +1 -0
  20. package/dist/api/errors.js +41 -0
  21. package/dist/api/errors.js.map +1 -0
  22. package/dist/api/generated.d.ts +1461 -0
  23. package/dist/api/generated.d.ts.map +1 -0
  24. package/dist/api/generated.js +6 -0
  25. package/dist/api/generated.js.map +1 -0
  26. package/dist/api/http.d.ts +16 -0
  27. package/dist/api/http.d.ts.map +1 -0
  28. package/dist/api/http.js +39 -0
  29. package/dist/api/http.js.map +1 -0
  30. package/dist/api/index.d.ts +14 -0
  31. package/dist/api/index.d.ts.map +1 -0
  32. package/dist/api/index.js +8 -0
  33. package/dist/api/index.js.map +1 -0
  34. package/dist/api/positionHistory.d.ts +44 -0
  35. package/dist/api/positionHistory.d.ts.map +1 -0
  36. package/dist/api/positionHistory.js +126 -0
  37. package/dist/api/positionHistory.js.map +1 -0
  38. package/dist/client.d.ts +6 -0
  39. package/dist/client.d.ts.map +1 -1
  40. package/dist/client.js +2 -0
  41. package/dist/client.js.map +1 -1
  42. package/dist/constants.d.ts +16 -0
  43. package/dist/constants.d.ts.map +1 -0
  44. package/dist/constants.js +28 -0
  45. package/dist/constants.js.map +1 -0
  46. package/dist/errors.d.ts +18 -3
  47. package/dist/errors.d.ts.map +1 -1
  48. package/dist/errors.js +20 -2
  49. package/dist/errors.js.map +1 -1
  50. package/dist/evm/abis/priceAndFeeCalculatorV2.d.ts +233 -0
  51. package/dist/evm/abis/priceAndFeeCalculatorV2.d.ts.map +1 -0
  52. package/dist/evm/abis/priceAndFeeCalculatorV2.js +119 -0
  53. package/dist/evm/abis/priceAndFeeCalculatorV2.js.map +1 -0
  54. package/dist/evm/abis/provisionerV2.d.ts +684 -0
  55. package/dist/evm/abis/provisionerV2.d.ts.map +1 -0
  56. package/dist/evm/abis/provisionerV2.js +262 -0
  57. package/dist/evm/abis/provisionerV2.js.map +1 -0
  58. package/dist/evm/adapters/aera.d.ts +3 -3
  59. package/dist/evm/adapters/aera.d.ts.map +1 -1
  60. package/dist/evm/adapters/aera.js +179 -103
  61. package/dist/evm/adapters/aera.js.map +1 -1
  62. package/dist/evm/adapters/types.d.ts +12 -1
  63. package/dist/evm/adapters/types.d.ts.map +1 -1
  64. package/dist/evm/aeraContracts/priceAndFeeCalculator.d.ts +41 -0
  65. package/dist/evm/aeraContracts/priceAndFeeCalculator.d.ts.map +1 -0
  66. package/dist/evm/aeraContracts/priceAndFeeCalculator.js +140 -0
  67. package/dist/evm/aeraContracts/priceAndFeeCalculator.js.map +1 -0
  68. package/dist/evm/aeraContracts/v1.d.ts +2317 -0
  69. package/dist/evm/aeraContracts/v1.d.ts.map +1 -0
  70. package/dist/evm/aeraContracts/v1.js +59 -0
  71. package/dist/evm/aeraContracts/v1.js.map +1 -0
  72. package/dist/evm/aeraContracts/v2.d.ts +4146 -0
  73. package/dist/evm/aeraContracts/v2.d.ts.map +1 -0
  74. package/dist/evm/aeraContracts/v2.js +217 -0
  75. package/dist/evm/aeraContracts/v2.js.map +1 -0
  76. package/dist/evm/aeraContracts.d.ts +23 -2316
  77. package/dist/evm/aeraContracts.d.ts.map +1 -1
  78. package/dist/evm/aeraContracts.js +117 -24
  79. package/dist/evm/aeraContracts.js.map +1 -1
  80. package/dist/evm/deposit.d.ts +5 -1
  81. package/dist/evm/deposit.d.ts.map +1 -1
  82. package/dist/evm/deposit.js +28 -14
  83. package/dist/evm/deposit.js.map +1 -1
  84. package/dist/evm/depositReceiverApproval.d.ts +17 -0
  85. package/dist/evm/depositReceiverApproval.d.ts.map +1 -0
  86. package/dist/evm/depositReceiverApproval.js +32 -0
  87. package/dist/evm/depositReceiverApproval.js.map +1 -0
  88. package/dist/evm/index.d.ts +8 -1
  89. package/dist/evm/index.d.ts.map +1 -1
  90. package/dist/evm/index.js +4 -0
  91. package/dist/evm/index.js.map +1 -1
  92. package/dist/evm/operationMode.d.ts +9 -0
  93. package/dist/evm/operationMode.d.ts.map +1 -0
  94. package/dist/evm/operationMode.js +41 -0
  95. package/dist/evm/operationMode.js.map +1 -0
  96. package/dist/evm/types.d.ts +5 -3
  97. package/dist/evm/types.d.ts.map +1 -1
  98. package/dist/evm/types.js +4 -1
  99. package/dist/evm/types.js.map +1 -1
  100. package/dist/evm/userCurrentBalance.d.ts.map +1 -1
  101. package/dist/evm/userCurrentBalance.js +68 -58
  102. package/dist/evm/userCurrentBalance.js.map +1 -1
  103. package/dist/evm/withdraw.d.ts +5 -1
  104. package/dist/evm/withdraw.d.ts.map +1 -1
  105. package/dist/evm/withdraw.js +25 -13
  106. package/dist/evm/withdraw.js.map +1 -1
  107. package/dist/index.d.ts +9 -1
  108. package/dist/index.d.ts.map +1 -1
  109. package/dist/index.js +6 -1
  110. package/dist/index.js.map +1 -1
  111. package/dist/privy/index.d.ts +52 -0
  112. package/dist/privy/index.d.ts.map +1 -0
  113. package/dist/privy/index.js +51 -0
  114. package/dist/privy/index.js.map +1 -0
  115. package/manifest/vaults.json +316 -132
  116. package/package.json +2 -2
  117. package/src/api/activity.ts +219 -0
  118. package/src/api/caip.ts +61 -0
  119. package/src/api/client.ts +201 -0
  120. package/src/api/decimal.ts +49 -0
  121. package/src/api/errors.ts +64 -0
  122. package/src/api/generated.ts +1461 -0
  123. package/src/api/http.ts +62 -0
  124. package/src/api/index.ts +65 -0
  125. package/src/api/positionHistory.ts +167 -0
  126. package/src/client.ts +7 -1
  127. package/src/constants.ts +32 -0
  128. package/src/errors.ts +44 -5
  129. package/src/evm/abis/priceAndFeeCalculatorV2.ts +118 -0
  130. package/src/evm/abis/provisionerV2.ts +261 -0
  131. package/src/evm/adapters/aera.ts +419 -137
  132. package/src/evm/adapters/types.ts +19 -1
  133. package/src/evm/aeraContracts/priceAndFeeCalculator.ts +223 -0
  134. package/src/evm/aeraContracts/v1.ts +157 -0
  135. package/src/evm/aeraContracts/v2.ts +466 -0
  136. package/src/evm/aeraContracts.ts +173 -52
  137. package/src/evm/deposit.ts +48 -16
  138. package/src/evm/depositReceiverApproval.ts +51 -0
  139. package/src/evm/index.ts +23 -1
  140. package/src/evm/operationMode.ts +60 -0
  141. package/src/evm/types.ts +7 -3
  142. package/src/evm/userCurrentBalance.ts +80 -55
  143. package/src/evm/withdraw.ts +42 -15
  144. package/src/index.ts +21 -0
  145. package/src/privy/index.ts +95 -0
@@ -0,0 +1,62 @@
1
+ import { GauntletApiError } from './errors';
2
+
3
+ export const DEFAULT_API_URL = 'https://api.gauntlet.xyz';
4
+
5
+ export interface GauntletApiConfig {
6
+ /** Kong consumer key sent as `x-api-key`. Anonymous access is rate-limited. */
7
+ apiKey?: string;
8
+ /**
9
+ * Override the API origin. Defaults to `https://api.gauntlet.xyz`. In a
10
+ * browser this may be a relative path (e.g. a Next.js rewrite like
11
+ * `/gauntlet-api`), which resolves against the page origin.
12
+ */
13
+ apiUrl?: string;
14
+ /** Custom fetch implementation (testing, instrumentation). Defaults to global fetch. */
15
+ fetch?: typeof fetch;
16
+ }
17
+
18
+ export type QueryParams = Record<string, string | number | boolean | undefined>;
19
+
20
+ export async function apiGet<T>(
21
+ config: GauntletApiConfig,
22
+ path: string,
23
+ query?: QueryParams
24
+ ): Promise<T> {
25
+ // Concatenate instead of `new URL(path, base)`: the paths are absolute, so
26
+ // URL resolution would silently drop any path prefix on a proxy apiUrl.
27
+ const base = (config.apiUrl ?? DEFAULT_API_URL).replace(/\/+$/, '');
28
+ // Relative bases resolve against the browser origin.
29
+ const origin = (globalThis as { location?: { origin: string } }).location?.origin;
30
+ if (origin === undefined && !/^[a-z][a-z0-9+.-]*:\/\//i.test(base)) {
31
+ throw new Error(
32
+ `relative apiUrl "${base}" requires a browser origin; pass an absolute apiUrl outside the browser`
33
+ );
34
+ }
35
+ const url = new URL(base + path, origin);
36
+ for (const [key, value] of Object.entries(query ?? {})) {
37
+ if (value !== undefined) url.searchParams.set(key, String(value));
38
+ }
39
+
40
+ const fetchImpl = config.fetch ?? fetch;
41
+ const response = await fetchImpl(url, {
42
+ headers: {
43
+ accept: 'application/json',
44
+ ...(config.apiKey ? { 'x-api-key': config.apiKey } : {}),
45
+ },
46
+ });
47
+
48
+ if (!response.ok) {
49
+ let message = response.statusText || 'request failed';
50
+ let code: string | undefined;
51
+ try {
52
+ const body = (await response.json()) as { error?: { code?: string; message?: string } };
53
+ message = body.error?.message ?? message;
54
+ code = body.error?.code;
55
+ } catch {
56
+ // non-JSON error body — keep the status text
57
+ }
58
+ throw new GauntletApiError({ status: response.status, path, message, code });
59
+ }
60
+
61
+ return (await response.json()) as T;
62
+ }
@@ -0,0 +1,65 @@
1
+ export { GauntletApi } from './client';
2
+ export type {
3
+ ActivityOptions,
4
+ AmountPair,
5
+ ChainSyncStatus,
6
+ HealthResponse,
7
+ LatestPriceOptions,
8
+ LatestPriceResponse,
9
+ PageOptions,
10
+ PartialResponseError,
11
+ PositionTimeseriesPoint,
12
+ PriceTimeseriesOptions,
13
+ PriceTimeseriesResponse,
14
+ TimeseriesMeta,
15
+ TimeWindowOptions,
16
+ TokenRef,
17
+ TvlResponse,
18
+ UserActivity,
19
+ UserActivityResponse,
20
+ UserAllPositionsResponse,
21
+ UserPosition,
22
+ UserPositionLatestResponse,
23
+ UserPositionsTimeseriesResponse,
24
+ VaultDetail,
25
+ VaultDetailResponse,
26
+ VaultMetrics,
27
+ VaultTimeseriesPoint,
28
+ VaultTimeseriesResponse,
29
+ } from './client';
30
+
31
+ export { DEFAULT_API_URL } from './http';
32
+ export type { GauntletApiConfig } from './http';
33
+
34
+ export { bigIntToDecimal, decimalToBigInt, SHARE_DECIMALS, sharesToBigInt } from './decimal';
35
+
36
+ export {
37
+ apiVaultIdFromVaultId,
38
+ formatApiVaultId,
39
+ parseApiVaultId,
40
+ vaultIdFromApiVaultId,
41
+ } from './caip';
42
+ export type { ApiVaultId } from './caip';
43
+
44
+ export { getActivityFlows, stitchActivityFlows, waitForRequestSettlement } from './activity';
45
+ export type {
46
+ ActivityFlow,
47
+ ActivityFlowKind,
48
+ ActivityFlowsOptions,
49
+ ActivityFlowStatus,
50
+ AssetAmount,
51
+ WaitForSettlementOptions,
52
+ } from './activity';
53
+
54
+ export { buildPositionHistory, getPositionHistory } from './positionHistory';
55
+ export type { PositionHistory, PositionHistoryPoint } from './positionHistory';
56
+
57
+ export {
58
+ DecimalPrecisionError,
59
+ GauntletApiError,
60
+ InvalidCaipIdError,
61
+ InvalidDecimalError,
62
+ SettlementTimeoutError,
63
+ } from './errors';
64
+
65
+ export type { components as GaiaApiComponents, paths as GaiaApiPaths } from './generated';
@@ -0,0 +1,167 @@
1
+ import { GauntletSDKError } from '../errors';
2
+ import type { GauntletApi, TokenRef, UserActivity } from './client';
3
+ import { bigIntToDecimal, decimalToBigInt, sharesToBigInt } from './decimal';
4
+
5
+ /**
6
+ * Internal accounting scale for asset amounts. The API emits human-unit
7
+ * decimal strings with at most `token.decimals` fractional digits, so 18
8
+ * digits is exact for every supported asset; a token with more than 18
9
+ * decimals would throw `DecimalPrecisionError` rather than lose precision.
10
+ */
11
+ const ASSET_SCALE = 18;
12
+
13
+ /** The wallet's reconstructed position state immediately after one activity row. */
14
+ export interface PositionHistoryPoint {
15
+ timestamp: Date;
16
+ txHash: string;
17
+ type: UserActivity['type'];
18
+ /** Signed share movement of this row, 18-decimal base units. */
19
+ sharesDelta: bigint;
20
+ /** Signed asset movement of this row (deposit-ledger convention), human decimal string. */
21
+ assetsDelta: string;
22
+ /** Shares held by the wallet after this row (escrowed redeem shares excluded). */
23
+ sharesBalance: bigint;
24
+ /** Assets escrowed at the Provisioner awaiting share mint, human decimal string. */
25
+ pendingDepositAssets: string;
26
+ /** Shares escrowed at the Provisioner awaiting asset return, 18-decimal base units. */
27
+ pendingRedeemShares: bigint;
28
+ /** Cumulative settled deposits minus settled withdrawals, human decimal string. */
29
+ netAssetsIn: string;
30
+ }
31
+
32
+ export interface PositionHistory {
33
+ /** CAIP-10 vault id the history belongs to. */
34
+ vaultId: string;
35
+ /** The vault's asset token, when the indexer reports it on any row. */
36
+ token: TokenRef | null;
37
+ /** Chronological (oldest-first) position states, one per activity row. */
38
+ points: PositionHistoryPoint[];
39
+ }
40
+
41
+ /**
42
+ * Pure builder: replays a single vault's activity rows (any order) into a
43
+ * chronological position timeline — running share balance, escrowed pending
44
+ * amounts, and cumulative net asset flows.
45
+ *
46
+ * Async lifecycles are paired via `request_hash` so settled deposits count
47
+ * the requested asset amount and terminal rows release the matching escrow.
48
+ */
49
+ export function buildPositionHistory(rows: UserActivity[]): PositionHistory {
50
+ if (rows.length === 0) {
51
+ throw new GauntletSDKError('Cannot build position history from zero activity rows');
52
+ }
53
+ const vaultIds = new Set(rows.map((r) => r.vault_id));
54
+ if (vaultIds.size > 1) {
55
+ throw new GauntletSDKError(
56
+ `Position history requires rows from a single vault; got ${[...vaultIds].join(', ')}`
57
+ );
58
+ }
59
+
60
+ const ordered = [...rows].sort((a, b) => a.block_timestamp - b.block_timestamp);
61
+ const token = ordered.find((r) => r.assets_delta.token)?.assets_delta.token ?? null;
62
+
63
+ let sharesBalance = 0n;
64
+ let pendingDepositAssets = 0n;
65
+ let pendingRedeemShares = 0n;
66
+ let netAssetsIn = 0n;
67
+ const pendingDepositByHash = new Map<string, bigint>();
68
+ const pendingRedeemByHash = new Map<string, bigint>();
69
+
70
+ const points: PositionHistoryPoint[] = [];
71
+ for (const row of ordered) {
72
+ const sharesDelta = sharesToBigInt(row.shares_delta);
73
+ const assetsDelta = decimalToBigInt(row.assets_delta.native, ASSET_SCALE);
74
+ sharesBalance += sharesDelta;
75
+
76
+ switch (row.type) {
77
+ case 'deposit':
78
+ if (row.request_hash != null) {
79
+ // Async terminal solve: assets moved at request time; release escrow.
80
+ const requested = pendingDepositByHash.get(row.request_hash);
81
+ if (requested === undefined) {
82
+ throw new GauntletSDKError(
83
+ `Deposit settlement ${row.tx_hash} references request_hash "${row.request_hash}" with no matching deposit_pending row`
84
+ );
85
+ }
86
+ pendingDepositByHash.delete(row.request_hash);
87
+ pendingDepositAssets -= requested;
88
+ netAssetsIn += requested;
89
+ } else {
90
+ netAssetsIn += assetsDelta;
91
+ }
92
+ break;
93
+ case 'deposit_pending':
94
+ pendingDepositAssets += assetsDelta;
95
+ if (row.request_hash != null) pendingDepositByHash.set(row.request_hash, assetsDelta);
96
+ break;
97
+ case 'deposit_refunded':
98
+ // Row carries the unwound request as a negative delta.
99
+ pendingDepositAssets += assetsDelta;
100
+ if (row.request_hash != null) pendingDepositByHash.delete(row.request_hash);
101
+ break;
102
+ case 'withdraw':
103
+ // Sync withdraws and async terminal solves both carry the asset
104
+ // payout as a negative delta on this row.
105
+ netAssetsIn += assetsDelta;
106
+ if (row.request_hash != null) {
107
+ const escrowed = pendingRedeemByHash.get(row.request_hash);
108
+ if (escrowed === undefined) {
109
+ throw new GauntletSDKError(
110
+ `Withdraw settlement ${row.tx_hash} references request_hash "${row.request_hash}" with no matching withdraw_pending row`
111
+ );
112
+ }
113
+ pendingRedeemByHash.delete(row.request_hash);
114
+ pendingRedeemShares -= escrowed;
115
+ }
116
+ break;
117
+ case 'withdraw_pending': {
118
+ const escrowed = -sharesDelta;
119
+ pendingRedeemShares += escrowed;
120
+ if (row.request_hash != null) pendingRedeemByHash.set(row.request_hash, escrowed);
121
+ break;
122
+ }
123
+ case 'withdraw_refunded':
124
+ pendingRedeemShares -= sharesDelta;
125
+ if (row.request_hash != null) pendingRedeemByHash.delete(row.request_hash);
126
+ break;
127
+ case 'transfer_in':
128
+ case 'transfer_out':
129
+ break;
130
+ default:
131
+ throw new GauntletSDKError(`Unknown activity row type "${row.type}"`);
132
+ }
133
+
134
+ points.push({
135
+ timestamp: new Date(row.block_timestamp * 1000),
136
+ txHash: row.tx_hash,
137
+ type: row.type,
138
+ sharesDelta,
139
+ assetsDelta: row.assets_delta.native,
140
+ sharesBalance,
141
+ pendingDepositAssets: bigIntToDecimal(pendingDepositAssets, ASSET_SCALE),
142
+ pendingRedeemShares,
143
+ netAssetsIn: bigIntToDecimal(netAssetsIn, ASSET_SCALE),
144
+ });
145
+ }
146
+
147
+ return { vaultId: ordered[0].vault_id, token, points };
148
+ }
149
+
150
+ /**
151
+ * Fetches a wallet's complete activity for one vault and replays it into a
152
+ * position timeline. `vaultId` is the CAIP-10 id; use `apiVaultIdFromVaultId`
153
+ * to convert a manifest vault id. A wallet that has never touched the vault
154
+ * gets an empty timeline, not an error.
155
+ */
156
+ export async function getPositionHistory(
157
+ api: GauntletApi,
158
+ walletAddress: string,
159
+ vaultId: string
160
+ ): Promise<PositionHistory> {
161
+ const rows: UserActivity[] = [];
162
+ for await (const row of api.activityRows(walletAddress, { vaultId, order: 'asc', limit: 1000 })) {
163
+ rows.push(row);
164
+ }
165
+ if (rows.length === 0) return { vaultId, token: null, points: [] };
166
+ return buildPositionHistory(rows);
167
+ }
package/src/client.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  import { type PublicClient, type WalletClient } from 'viem';
2
2
  import bundledManifest from '../manifest/vaults.json';
3
+ import { GauntletApi } from './api/client';
3
4
  import { RpcNotConfiguredError, UnimplementedFeatureError } from './errors';
4
5
  import type { VaultManifest } from './evm/types';
5
6
  import { base } from 'viem/chains';
@@ -20,7 +21,10 @@ export enum AttributionMode {
20
21
  export type ChainId = string | number;
21
22
 
22
23
  export interface GauntletClientConfig {
24
+ /** Gaia API key, sent as `x-api-key` on `client.api` requests. Anonymous access is rate-limited. */
23
25
  apiKey?: string;
26
+ /** Override the Gaia API origin (e.g. a same-origin proxy route). Defaults to `https://api.gauntlet.xyz`. */
27
+ apiUrl?: string;
24
28
  /** Map of chainId → PublicClient, e.g. { [base.id]: createPublicClient(...) } */
25
29
  evmClients?: Record<ChainId, PublicClient>;
26
30
  attributionMode?: AttributionMode;
@@ -56,12 +60,13 @@ export interface GauntletClientConfig {
56
60
  * ```
57
61
  */
58
62
  export class GauntletClient {
59
- // used for future core api and encoding attribution
60
63
  readonly apiKey?: string;
61
64
  readonly builderCode?: string;
62
65
  readonly attributionMode: AttributionMode;
63
66
  readonly evmClients: Record<ChainId, PublicClient>;
64
67
  readonly wallet?: WalletClient;
68
+ /** Typed client for the Gaia REST API (vaults, positions, activity, tvl, prices). */
69
+ readonly api: GauntletApi;
65
70
 
66
71
  private _manifest: VaultManifest = bundledManifest as VaultManifest;
67
72
 
@@ -71,6 +76,7 @@ export class GauntletClient {
71
76
  this.attributionMode = config.attributionMode ?? AttributionMode.PUBLIC;
72
77
  this.builderCode = config.builderCode;
73
78
  this.wallet = config.wallet;
79
+ this.api = new GauntletApi({ apiKey: config.apiKey, apiUrl: config.apiUrl });
74
80
  }
75
81
 
76
82
  get manifest(): Promise<VaultManifest> {
@@ -0,0 +1,32 @@
1
+ export const MAX_BPS = 10000n;
2
+ export const DEFAULT_BPS = 100;
3
+
4
+ export const SECOND = 1;
5
+ export const MINUTE = SECOND * 60;
6
+ export const HOUR = MINUTE * 60;
7
+ export const DAY = HOUR * 24;
8
+
9
+ export const REQUEST_DEADLINE_BUFFER_DAYS = 3;
10
+ export const DEFAULT_MAX_PRICE_AGE_DAYS = 10;
11
+ export const DEFAULT_SOLVER_TIP = 0n;
12
+ export const DEFAULT_REQUEST_DEADLINE_BUFFER = DAY * REQUEST_DEADLINE_BUFFER_DAYS;
13
+ export const DEFAULT_MAX_PRICE_AGE = BigInt(DAY * DEFAULT_MAX_PRICE_AGE_DAYS);
14
+
15
+ export const CHAIN_NAMES: Record<number, string> = {
16
+ 1: 'ethereum',
17
+ 8453: 'base',
18
+ 42161: 'arbitrum',
19
+ 10: 'optimism',
20
+ };
21
+
22
+ // Approximate 3-day block lookback per chain based on avg block time.
23
+ export const BLOCKS_3_DAYS: Record<number, bigint> = {
24
+ 1: 21_600n,
25
+ 8453: 129_600n,
26
+ 42161: 1_036_800n,
27
+ 10: 129_600n,
28
+ };
29
+ export const DEFAULT_BLOCKS_3_DAYS = 129_600n;
30
+
31
+ // Many RPC providers cap eth_getLogs to 10,000 blocks per request.
32
+ export const LOG_PAGE_SIZE = 9_999n;
package/src/errors.ts CHANGED
@@ -1,4 +1,5 @@
1
- import { ChainId } from './client';
1
+ import type { ChainId } from './client';
2
+ import { MAX_BPS } from './constants';
2
3
 
3
4
  export class GauntletSDKError extends Error {
4
5
  constructor(message: string) {
@@ -48,10 +49,10 @@ export class ChainMismatchError extends GauntletSDKError {
48
49
 
49
50
  export class UnsupportedDepositModeError extends GauntletSDKError {
50
51
  readonly vaultId: string;
51
- readonly requested: 'sync' | 'async';
52
+ readonly requested: string;
52
53
  readonly available: string;
53
54
 
54
- constructor(vaultId: string, requested: 'sync' | 'async', available: string) {
55
+ constructor(vaultId: string, requested: string, available: string) {
55
56
  super(`Vault "${vaultId}" does not support ${requested} deposits (available: ${available})`);
56
57
  this.name = 'UnsupportedDepositModeError';
57
58
  this.vaultId = vaultId;
@@ -109,7 +110,7 @@ export class UnsupportedFeatureError extends GauntletSDKError {
109
110
 
110
111
  constructor(feature: string) {
111
112
  super(`Feature not supported: "${feature}"`);
112
- this.name = 'UnimplementedFeatureError';
113
+ this.name = 'UnsupportedFeatureError';
113
114
  this.feature = feature;
114
115
  }
115
116
  }
@@ -124,11 +125,49 @@ export class UnitConversionError extends GauntletSDKError {
124
125
  }
125
126
  }
126
127
 
128
+ export class StalePriceError extends GauntletSDKError {
129
+ readonly blockTimestamp: bigint;
130
+ readonly maxPriceAge: bigint;
131
+ readonly priceTimestamp: bigint;
132
+
133
+ constructor({
134
+ blockTimestamp,
135
+ maxPriceAge,
136
+ priceTimestamp,
137
+ }: {
138
+ blockTimestamp: bigint;
139
+ maxPriceAge: bigint;
140
+ priceTimestamp: bigint;
141
+ }) {
142
+ super(
143
+ `Price is stale: timestamp "${priceTimestamp}" with max age "${maxPriceAge}" is older than block timestamp "${blockTimestamp}"`
144
+ );
145
+ this.name = 'StalePriceError';
146
+ this.blockTimestamp = blockTimestamp;
147
+ this.maxPriceAge = maxPriceAge;
148
+ this.priceTimestamp = priceTimestamp;
149
+ }
150
+ }
151
+
152
+ export class InvalidSolverTipError extends GauntletSDKError {
153
+ readonly solverTip: bigint;
154
+ readonly availableAmount: bigint;
155
+
156
+ constructor(solverTip: bigint, availableAmount: bigint) {
157
+ super(
158
+ `Solver tip "${solverTip}" must be less than available token amount "${availableAmount}"`
159
+ );
160
+ this.name = 'InvalidSolverTipError';
161
+ this.solverTip = solverTip;
162
+ this.availableAmount = availableAmount;
163
+ }
164
+ }
165
+
127
166
  export class InvalidSlippageBPSError extends GauntletSDKError {
128
167
  readonly slippage: number;
129
168
 
130
169
  constructor(slippage: number) {
131
- super(`Slippage not in range of 10000-0; argumet passed: ${slippage}`);
170
+ super(`Slippage not in range of 0-${Number(MAX_BPS)}; argument passed: ${slippage}`);
132
171
  this.name = 'IncorrectArgument';
133
172
  this.slippage = slippage;
134
173
  }
@@ -0,0 +1,118 @@
1
+ // Compact ABI derived from aera-finance/aera-contracts-v3@f4e3433058c29c3a0b1348ffbd358cc036a1687f
2
+ // source: src/core/interfaces/IPriceAndFeeCalculatorV2.sol
3
+ export const priceAndFeeCalculatorV2Abi = [
4
+ {
5
+ type: 'function',
6
+ inputs: [
7
+ { name: 'vault', internalType: 'address', type: 'address' },
8
+ { name: 'token', internalType: 'contract IERC20', type: 'address' },
9
+ { name: 'tokenAmount', internalType: 'uint256', type: 'uint256' },
10
+ ],
11
+ name: 'convertTokenToUnits',
12
+ outputs: [{ name: 'unitsAmount', internalType: 'uint256', type: 'uint256' }],
13
+ stateMutability: 'view',
14
+ },
15
+ {
16
+ type: 'function',
17
+ inputs: [
18
+ { name: 'vault', internalType: 'address', type: 'address' },
19
+ { name: 'token', internalType: 'contract IERC20', type: 'address' },
20
+ { name: 'tokenAmount', internalType: 'uint256', type: 'uint256' },
21
+ { name: 'rounding', internalType: 'enum Math.Rounding', type: 'uint8' },
22
+ ],
23
+ name: 'convertTokenToUnitsIfActive',
24
+ outputs: [{ name: 'unitsAmount', internalType: 'uint256', type: 'uint256' }],
25
+ stateMutability: 'view',
26
+ },
27
+ {
28
+ type: 'function',
29
+ inputs: [
30
+ { name: 'vault', internalType: 'address', type: 'address' },
31
+ { name: 'token', internalType: 'contract IERC20', type: 'address' },
32
+ { name: 'unitsAmount', internalType: 'uint256', type: 'uint256' },
33
+ ],
34
+ name: 'convertUnitsToToken',
35
+ outputs: [{ name: 'tokenAmount', internalType: 'uint256', type: 'uint256' }],
36
+ stateMutability: 'view',
37
+ },
38
+ {
39
+ type: 'function',
40
+ inputs: [
41
+ { name: 'vault', internalType: 'address', type: 'address' },
42
+ { name: 'token', internalType: 'contract IERC20', type: 'address' },
43
+ { name: 'unitsAmount', internalType: 'uint256', type: 'uint256' },
44
+ { name: 'rounding', internalType: 'enum Math.Rounding', type: 'uint8' },
45
+ ],
46
+ name: 'convertUnitsToTokenIfActive',
47
+ outputs: [{ name: 'tokenAmount', internalType: 'uint256', type: 'uint256' }],
48
+ stateMutability: 'view',
49
+ },
50
+ {
51
+ type: 'function',
52
+ inputs: [{ name: 'vault', internalType: 'address', type: 'address' }],
53
+ name: 'getAnchorTimestamp',
54
+ outputs: [{ name: 'timestamp', internalType: 'uint32', type: 'uint32' }],
55
+ stateMutability: 'view',
56
+ },
57
+ {
58
+ type: 'function',
59
+ inputs: [{ name: 'vault', internalType: 'address', type: 'address' }],
60
+ name: 'getVaultState',
61
+ outputs: [
62
+ {
63
+ name: '',
64
+ internalType: 'struct VaultPriceStateV2',
65
+ type: 'tuple',
66
+ components: [
67
+ { name: 'paused', internalType: 'bool', type: 'bool' },
68
+ { name: 'pauseOnBadAnchorUpdate', internalType: 'bool', type: 'bool' },
69
+ { name: 'maxPriceAge', internalType: 'uint16', type: 'uint16' },
70
+ { name: 'minUpdateIntervalMinutes', internalType: 'uint16', type: 'uint16' },
71
+ { name: 'maxPriceToleranceRatio', internalType: 'uint16', type: 'uint16' },
72
+ { name: 'minPriceToleranceRatio', internalType: 'uint16', type: 'uint16' },
73
+ { name: 'maxUpdateDelayDays', internalType: 'uint8', type: 'uint8' },
74
+ { name: 'accrualLag', internalType: 'uint32', type: 'uint32' },
75
+ { name: 'anchorTimestamp', internalType: 'uint32', type: 'uint32' },
76
+ { name: 'driftTimestamp', internalType: 'uint32', type: 'uint32' },
77
+ { name: 'anchorPrice', internalType: 'uint128', type: 'uint128' },
78
+ { name: 'driftPrice', internalType: 'uint128', type: 'uint128' },
79
+ { name: 'highestPrice', internalType: 'uint128', type: 'uint128' },
80
+ { name: 'lastTotalSupply', internalType: 'uint128', type: 'uint128' },
81
+ ],
82
+ },
83
+ {
84
+ name: '',
85
+ internalType: 'struct VaultAccruals',
86
+ type: 'tuple',
87
+ components: [
88
+ {
89
+ name: 'fees',
90
+ internalType: 'struct Fee',
91
+ type: 'tuple',
92
+ components: [
93
+ { name: 'tvl', internalType: 'uint16', type: 'uint16' },
94
+ { name: 'performance', internalType: 'uint16', type: 'uint16' },
95
+ ],
96
+ },
97
+ { name: 'accruedFees', internalType: 'uint112', type: 'uint112' },
98
+ { name: 'accruedProtocolFees', internalType: 'uint112', type: 'uint112' },
99
+ ],
100
+ },
101
+ ],
102
+ stateMutability: 'view',
103
+ },
104
+ {
105
+ type: 'function',
106
+ inputs: [{ name: 'vault', internalType: 'address', type: 'address' }],
107
+ name: 'isVaultPaused',
108
+ outputs: [{ name: '', internalType: 'bool', type: 'bool' }],
109
+ stateMutability: 'view',
110
+ },
111
+ {
112
+ type: 'function',
113
+ inputs: [],
114
+ name: 'version',
115
+ outputs: [{ name: '', internalType: 'string', type: 'string' }],
116
+ stateMutability: 'pure',
117
+ },
118
+ ] as const;