@aegis-engine/core 0.2.0 → 0.3.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 (42) hide show
  1. package/README.md +109 -4
  2. package/THIRD_PARTY_NOTICES.md +211 -0
  3. package/dist/actions/kyberSwap.d.ts +3 -2
  4. package/dist/address.d.ts +3 -1
  5. package/dist/aegis-core.js +8565 -5305
  6. package/dist/backendAdapters.d.ts +25 -1
  7. package/dist/config/constants.d.ts +4 -0
  8. package/dist/config/types.d.ts +5 -0
  9. package/dist/contract/backendClient.d.ts +16 -6
  10. package/dist/contract/generated/aegis-swap.d.ts +2682 -96
  11. package/dist/contract/schemas.d.ts +27 -20
  12. package/dist/engine/actions.d.ts +56 -0
  13. package/dist/engine/borrowCostProjection.d.ts +1 -0
  14. package/dist/engine/dataSource.d.ts +63 -0
  15. package/dist/engine/index.d.ts +8 -0
  16. package/dist/engine/marketAdapter.d.ts +8 -0
  17. package/dist/engine/positionAmounts.d.ts +37 -0
  18. package/dist/engine/quotes.d.ts +9 -0
  19. package/dist/engine/reads.d.ts +43 -0
  20. package/dist/engine/registry.d.ts +27 -0
  21. package/dist/engine/restSchemas.d.ts +409 -0
  22. package/dist/engine/sdk.d.ts +1 -0
  23. package/dist/engine/sdkRuntime.d.ts +1 -0
  24. package/dist/engine/types.d.ts +1000 -0
  25. package/dist/errors.d.ts +10 -0
  26. package/dist/historicalPrices.d.ts +78 -0
  27. package/dist/index.d.ts +13 -8
  28. package/dist/internal/requestSignal.d.ts +9 -0
  29. package/dist/internal/ttlCache.d.ts +19 -0
  30. package/dist/metadata.d.ts +4 -0
  31. package/dist/permit.d.ts +1 -0
  32. package/dist/quote/aggregatedSwapQuote.d.ts +12 -0
  33. package/dist/quote/kyberCalldata.d.ts +211 -0
  34. package/dist/quote.d.ts +1 -0
  35. package/dist/sdkRuntime-CCRVvSwD.js +16052 -0
  36. package/dist/services/kyberSwap.d.ts +22 -2
  37. package/dist/swap/receiptWait.d.ts +17 -0
  38. package/dist/swap/sendSwapTransaction.d.ts +40 -6
  39. package/dist/swap/settlement.d.ts +15 -0
  40. package/dist/transactionProgress.d.ts +8 -1
  41. package/dist/types/aggregatedSwap.d.ts +18 -0
  42. package/package.json +15 -7
package/README.md CHANGED
@@ -1,10 +1,11 @@
1
1
  # Aegis Core
2
2
 
3
- React-free swap operations for Aegis integrations.
3
+ React-free Swap and Engine operations for Aegis integrations.
4
4
 
5
5
  `@aegis-engine/core` is the operations layer behind the Aegis swap experience.
6
- Use it when you want Aegis direct-pool swaps or KyberSwap aggregated routes
7
- inside your own UI instead of using a prebuilt widget.
6
+ Use it when you want Aegis direct-pool swaps, KyberSwap aggregated routes, or
7
+ Aegis Engine lend/borrow/position flows inside your own UI instead of using a
8
+ prebuilt widget.
8
9
 
9
10
  ## Install
10
11
 
@@ -13,7 +14,8 @@ npm install @aegis-engine/core viem
13
14
  ```
14
15
 
15
16
  `viem` is a peer dependency because hosts usually already own their wallet and
16
- RPC setup.
17
+ RPC setup. The protocol implementation used by Engine quotes is bundled into
18
+ the published Core package; consumers do not need GitHub Packages credentials.
17
19
 
18
20
  ## What It Supports
19
21
 
@@ -23,6 +25,14 @@ RPC setup.
23
25
  - ERC-20 approvals, Permit2 signing, Permit2 on-chain fallback, wallet chain
24
26
  switching, transaction submission, replacement tracking, and receipt summaries.
25
27
  - Token, market, chain, metadata, polling, and progress helpers for custom UIs.
28
+ - Display-only historical token prices through the public REST backend,
29
+ normalized into chronological typed points.
30
+ - Engine REST market and wallet-position snapshots with runtime validation,
31
+ bounded session caches, and explicit indexed freshness.
32
+ - Engine 12-hour market-metric history for optional rate, utilization, and
33
+ liquidity charts.
34
+ - Engine lend and borrow quotes, exact rounded LP-shaped debt/carry
35
+ projections, collateral sizing, Permit2 preparation, and guarded execution.
26
36
 
27
37
  Direct pool swaps support `exactIn` and `exactOut`. KyberSwap routes currently
28
38
  support `exactIn` only because Kyber's route/build flow is exact-input.
@@ -30,6 +40,101 @@ support `exactIn` only because Kyber's route/build flow is exact-input.
30
40
  Core does not ship a hardcoded pool list. The widget or host app should own pool
31
41
  selection and pass the selected market into the operation functions.
32
42
 
43
+ ## Engine REST and protocol boundary
44
+
45
+ Engine discovery and wallet positions default to partner-safe REST:
46
+
47
+ ```ts
48
+ import {
49
+ fetchEngineMarketHistory,
50
+ fetchEngineMarkets,
51
+ fetchEnginePositionsSnapshot
52
+ } from '@aegis-engine/core'
53
+
54
+ const source = {
55
+ kind: 'rest' as const,
56
+ baseUrl: 'https://your-engine-api.example',
57
+ timeoutMs: 10_000
58
+ }
59
+
60
+ const markets = await fetchEngineMarkets({ source, chainId: 1 })
61
+ const positions = await fetchEnginePositionsSnapshot({
62
+ source,
63
+ chainId: 1,
64
+ account
65
+ })
66
+ const marketHistory = await fetchEngineMarketHistory({
67
+ source,
68
+ marketId: markets[0].id,
69
+ window: '30d'
70
+ })
71
+ ```
72
+
73
+ Hosts that already own deterministic market/position metadata can instead use
74
+ the typed `static` source. Engine market history remains a REST capability.
75
+
76
+ The REST service supplies facts and provenance. Core owns live RPC reads,
77
+ collateral/debt math, quote freshness, recipe construction, simulation,
78
+ approval, and send. `positions.freshness` is indexed provenance:
79
+ `lastLockedPips` / `lastLockedPct` are not current health. A current health
80
+ claim requires a fresh StateView/RPC snapshot.
81
+
82
+ Market snapshots preserve the backend's current Engine rate, fee, surge, and
83
+ freshness fields, including `borrowAprPct`, `lendApyPct`, `rateSource`,
84
+ `baseFeePpm`, `activeFeePpm`, `surgeFeePpm`, `surgeActive`, `inCapEvent`, and
85
+ the associated read timestamps/block. Market history comes from
86
+ `GET /v1/engine/markets/{marketId}/history`; pass the exact `market.id` returned
87
+ by discovery because Engine market IDs are case-sensitive. Its status is
88
+ `ready`, `warming_up`, or `no_data`, and unavailable metrics remain `null`.
89
+
90
+ Position reads prefer `GET /v1/engine/positions/v2`, which returns borrow
91
+ vaults and indexed lender-share positions. Core exposes them separately as
92
+ `positions` and `lendPositions`. If a backend returns `404` for v2, Core falls
93
+ back to the frozen v1 route; check `schemaVersion` before claiming lender
94
+ coverage. Lender share balances are indexed raw share units and require a
95
+ current share price before valuation.
96
+
97
+ Shared REST reads have bounded in-memory caches and a 10-second default request
98
+ timeout. A consumer abort only detaches that caller; it does not cancel a
99
+ request shared by another mounted widget. Set `timeoutMs` on the REST source
100
+ when a host needs a stricter boundary.
101
+
102
+ Borrowing always requires collateral. Engine debt is a two-token full-range LP
103
+ obligation, so carry is calculated from the exact rounded LP debt frame rather
104
+ than only the token amount received. Repayment requires both pool tokens;
105
+ remaining collateral stays in the vault until a separate withdrawal. A
106
+ one-token lend is a zap into a two-sided full-range LP, not single-token
107
+ lending.
108
+
109
+ ## Historical token prices
110
+
111
+ Historical prices are intentionally separate from Engine market and
112
+ transaction truth. A host reads one exact token through the public REST
113
+ backend without placing private data-service credentials in the client:
114
+
115
+ ```ts
116
+ import {
117
+ AEGIS_BACKEND_PROD_BASE_URL,
118
+ fetchTokenPriceHistory
119
+ } from '@aegis-engine/core'
120
+
121
+ const history = await fetchTokenPriceHistory({
122
+ baseUrl: AEGIS_BACKEND_PROD_BASE_URL,
123
+ chainId: 8453,
124
+ tokenAddress,
125
+ window: '30d'
126
+ })
127
+ ```
128
+
129
+ The helper calls
130
+ `GET /v1/tokens/{chainId}/{address}/price-history?window=30d`, validates the
131
+ response identity and status, and returns ascending `{ timestamp, priceUsd }`
132
+ observations. Native-token sentinels are looked up under the backend's zero
133
+ address key. `ready`, `warming_up`, and `no_data` are explicit; malformed or
134
+ identity-mismatched responses reject so a UI can log them while failing soft.
135
+ Use the series for display trends only; current quotes and executable prices
136
+ still come from the live market/RPC boundary.
137
+
33
138
  ## Wallet Sessions
34
139
 
35
140
  Execution functions take a `WalletSession`:
@@ -0,0 +1,211 @@
1
+ # Third-party notices
2
+
3
+ The lazy Engine implementation includes code from these MIT-licensed works:
4
+
5
+ - `@labs-solo/aegis-engine-sdk` 0.30.0 — Copyright (c) 2026 Solo Labs
6
+ - `@uniswap/sdk-core` 6.1.1 and 7.18.0 — Copyright (c) 2021 Uniswap Labs
7
+ - `@uniswap/v3-sdk` 3.27.0 — Copyright (c) 2021 Uniswap Labs
8
+ - `tiny-invariant` 1.3.3 — Copyright (c) 2019 Alexander Reardon
9
+ - `big.js` 5.2.2 — Copyright (c) 2018 Michael Mclaughlin
10
+ - `decimal.js-light` 2.5.1 — Copyright (c) 2020 Michael Mclaughlin
11
+ - `toformat` 2.0.0 — Copyright (c) 2017 Michael Mclaughlin
12
+
13
+ The following MIT License applies to each work listed above:
14
+
15
+ Permission is hereby granted, free of charge, to any person obtaining a copy
16
+ of this software and associated documentation files (the "Software"), to deal
17
+ in the Software without restriction, including without limitation the rights
18
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
19
+ copies of the Software, and to permit persons to whom the Software is
20
+ furnished to do so, subject to the following conditions:
21
+
22
+ The above copyright notice and this permission notice shall be included in all
23
+ copies or substantial portions of the Software.
24
+
25
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
26
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
27
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
28
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
29
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
30
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
31
+ SOFTWARE.
32
+
33
+ The lazy Engine implementation also includes `jsbi` 3.2.5 under the Apache
34
+ License, Version 2.0:
35
+
36
+ Apache License
37
+ Version 2.0, January 2004
38
+ https://www.apache.org/licenses/
39
+
40
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
41
+
42
+ 1. Definitions.
43
+
44
+ "License" shall mean the terms and conditions for use, reproduction,
45
+ and distribution as defined by Sections 1 through 9 of this document.
46
+
47
+ "Licensor" shall mean the copyright owner or entity authorized by
48
+ the copyright owner that is granting the License.
49
+
50
+ "Legal Entity" shall mean the union of the acting entity and all
51
+ other entities that control, are controlled by, or are under common
52
+ control with that entity. For the purposes of this definition,
53
+ "control" means (i) the power, direct or indirect, to cause the
54
+ direction or management of such entity, whether by contract or
55
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
56
+ outstanding shares, or (iii) beneficial ownership of such entity.
57
+
58
+ "You" (or "Your") shall mean an individual or Legal Entity
59
+ exercising permissions granted by this License.
60
+
61
+ "Source" form shall mean the preferred form for making modifications,
62
+ including but not limited to software source code, documentation
63
+ source, and configuration files.
64
+
65
+ "Object" form shall mean any form resulting from mechanical
66
+ transformation or translation of a Source form, including but
67
+ not limited to compiled object code, generated documentation,
68
+ and conversions to other media types.
69
+
70
+ "Work" shall mean the work of authorship, whether in Source or
71
+ Object form, made available under the License, as indicated by a
72
+ copyright notice that is included in or attached to the work
73
+ (an example is provided in the Appendix below).
74
+
75
+ "Derivative Works" shall mean any work, whether in Source or Object
76
+ form, that is based on (or derived from) the Work and for which the
77
+ editorial revisions, annotations, elaborations, or other modifications
78
+ represent, as a whole, an original work of authorship. For the purposes
79
+ of this License, Derivative Works shall not include works that remain
80
+ separable from, or merely link (or bind by name) to the interfaces of,
81
+ the Work and Derivative Works thereof.
82
+
83
+ "Contribution" shall mean any work of authorship, including
84
+ the original version of the Work and any modifications or additions
85
+ to that Work or Derivative Works thereof, that is intentionally
86
+ submitted to Licensor for inclusion in the Work by the copyright owner
87
+ or by an individual or Legal Entity authorized to submit on behalf of
88
+ the copyright owner. For the purposes of this definition, "submitted"
89
+ means any form of electronic, verbal, or written communication sent
90
+ to the Licensor or its representatives, including but not limited to
91
+ communication on electronic mailing lists, source code control systems,
92
+ and issue tracking systems that are managed by, or on behalf of, the
93
+ Licensor for the purpose of discussing and improving the Work, but
94
+ excluding communication that is conspicuously marked or otherwise
95
+ designated in writing by the copyright owner as "Not a Contribution."
96
+
97
+ "Contributor" shall mean Licensor and any individual or Legal Entity
98
+ on behalf of whom a Contribution has been received by Licensor and
99
+ subsequently incorporated within the Work.
100
+
101
+ 2. Grant of Copyright License. Subject to the terms and conditions of
102
+ this License, each Contributor hereby grants to You a perpetual,
103
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
104
+ copyright license to reproduce, prepare Derivative Works of,
105
+ publicly display, publicly perform, sublicense, and distribute the
106
+ Work and such Derivative Works in Source or Object form.
107
+
108
+ 3. Grant of Patent License. Subject to the terms and conditions of
109
+ this License, each Contributor hereby grants to You a perpetual,
110
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
111
+ (except as stated in this section) patent license to make, have made,
112
+ use, offer to sell, sell, import, and otherwise transfer the Work,
113
+ where such license applies only to those patent claims licensable
114
+ by such Contributor that are necessarily infringed by their
115
+ Contribution(s) alone or by combination of their Contribution(s)
116
+ with the Work to which such Contribution(s) was submitted. If You
117
+ institute patent litigation against any entity (including a
118
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
119
+ or a Contribution incorporated within the Work constitutes direct
120
+ or contributory patent infringement, then any patent licenses
121
+ granted to You under this License for that Work shall terminate
122
+ as of the date such litigation is filed.
123
+
124
+ 4. Redistribution. You may reproduce and distribute copies of the
125
+ Work or Derivative Works thereof in any medium, with or without
126
+ modifications, and in Source or Object form, provided that You
127
+ meet the following conditions:
128
+
129
+ (a) You must give any other recipients of the Work or
130
+ Derivative Works a copy of this License; and
131
+
132
+ (b) You must cause any modified files to carry prominent notices
133
+ stating that You changed the files; and
134
+
135
+ (c) You must retain, in the Source form of any Derivative Works
136
+ that You distribute, all copyright, patent, trademark, and
137
+ attribution notices from the Source form of the Work,
138
+ excluding those notices that do not pertain to any part of
139
+ the Derivative Works; and
140
+
141
+ (d) If the Work includes a "NOTICE" text file as part of its
142
+ distribution, then any Derivative Works that You distribute must
143
+ include a readable copy of the attribution notices contained
144
+ within such NOTICE file, excluding those notices that do not
145
+ pertain to any part of the Derivative Works, in at least one
146
+ of the following places: within a NOTICE text file distributed
147
+ as part of the Derivative Works; within the Source form or
148
+ documentation, if provided along with the Derivative Works; or,
149
+ within a display generated by the Derivative Works, if and
150
+ wherever such third-party notices normally appear. The contents
151
+ of the NOTICE file are for informational purposes only and
152
+ do not modify the License. You may add Your own attribution
153
+ notices within Derivative Works that You distribute, alongside
154
+ or as an addendum to the NOTICE text from the Work, provided
155
+ that such additional attribution notices cannot be construed
156
+ as modifying the License.
157
+
158
+ You may add Your own copyright statement to Your modifications and
159
+ may provide additional or different license terms and conditions
160
+ for use, reproduction, or distribution of Your modifications, or
161
+ for any such Derivative Works as a whole, provided Your use,
162
+ reproduction, and distribution of the Work otherwise complies with
163
+ the conditions stated in this License.
164
+
165
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
166
+ any Contribution intentionally submitted for inclusion in the Work
167
+ by You to the Licensor shall be under the terms and conditions of
168
+ this License, without any additional terms or conditions.
169
+ Notwithstanding the above, nothing herein shall supersede or modify
170
+ the terms of any separate license agreement you may have executed
171
+ with Licensor regarding such Contributions.
172
+
173
+ 6. Trademarks. This License does not grant permission to use the trade
174
+ names, trademarks, service marks, or product names of the Licensor,
175
+ except as required for reasonable and customary use in describing the
176
+ origin of the Work and reproducing the content of the NOTICE file.
177
+
178
+ 7. Disclaimer of Warranty. Unless required by applicable law or
179
+ agreed to in writing, Licensor provides the Work (and each
180
+ Contributor provides its Contributions) on an "AS IS" BASIS,
181
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
182
+ implied, including, without limitation, any warranties or conditions
183
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
184
+ PARTICULAR PURPOSE. You are solely responsible for determining the
185
+ appropriateness of using or redistributing the Work and assume any
186
+ risks associated with Your exercise of permissions under this License.
187
+
188
+ 8. Limitation of Liability. In no event and under no legal theory,
189
+ whether in tort (including negligence), contract, or otherwise,
190
+ unless required by applicable law (such as deliberate and grossly
191
+ negligent acts) or agreed to in writing, shall any Contributor be
192
+ liable to You for damages, including any direct, indirect, special,
193
+ incidental, or consequential damages of any character arising as a
194
+ result of this License or out of the use or inability to use the
195
+ Work (including but not limited to damages for loss of goodwill,
196
+ work stoppage, computer failure or malfunction, or any and all
197
+ other commercial damages or losses), even if such Contributor
198
+ has been advised of the possibility of such damages.
199
+
200
+ 9. Accepting Warranty or Additional Liability. While redistributing
201
+ the Work or Derivative Works thereof, You may choose to offer,
202
+ and charge a fee for, acceptance of support, warranty, indemnity,
203
+ or other liability obligations and/or rights consistent with this
204
+ License. However, in accepting such obligations, You may act only
205
+ on Your own behalf and on Your sole responsibility, not on behalf
206
+ of any other Contributor, and only if You agree to indemnify,
207
+ defend, and hold each Contributor harmless for any liability
208
+ incurred by, or claims asserted against, such Contributor by reason
209
+ of your accepting any such warranty or additional liability.
210
+
211
+ END OF TERMS AND CONDITIONS
@@ -1,11 +1,12 @@
1
1
  import { type Address, type Hex, type PublicClient } from 'viem';
2
+ import { UserFacingError } from '../errors.js';
2
3
  import type { AegisSwapChainConfig, AegisSwapToken } from '../config/index.js';
3
4
  import type { AegisSwapApprovalMode, WalletSession } from '../swap/types.js';
4
5
  import type { AggregatedSwapQuote } from '../types/aggregatedSwap.js';
5
6
  import type { AegisSwapActionCallbacks, AegisSwapActionResult } from './types.js';
6
- export declare const NATIVE_TOKEN_ADDRESS: Address;
7
+ export { NATIVE_TOKEN_ADDRESS } from '../address.js';
7
8
  export declare function isNativeToken(token: AegisSwapToken): boolean;
8
- export declare class KyberSwapPriceMovedError extends Error {
9
+ export declare class KyberSwapPriceMovedError extends UserFacingError {
9
10
  readonly quote: AggregatedSwapQuote;
10
11
  constructor(quote: AggregatedSwapQuote);
11
12
  }
package/dist/address.d.ts CHANGED
@@ -1,4 +1,6 @@
1
- import type { Address, Hex } from 'viem';
1
+ import { type Address, type Hex } from 'viem';
2
+ export declare const NATIVE_TOKEN_ADDRESS: Address;
3
+ export declare function isNativeAssetAddress(address: Address): boolean;
2
4
  export declare function addressKey(address: Address | null | undefined): string;
3
5
  export declare function sameAddress(a: Address, b: Address): boolean;
4
6
  export declare function sameOptionalAddress(a: Address | null, b: Address | null): boolean;