@hyperlane-xyz/rebalancer 2.0.0 → 3.0.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 (209) hide show
  1. package/dist/bridges/LiFiBridge.d.ts +67 -0
  2. package/dist/bridges/LiFiBridge.d.ts.map +1 -0
  3. package/dist/bridges/LiFiBridge.js +386 -0
  4. package/dist/bridges/LiFiBridge.js.map +1 -0
  5. package/dist/config/RebalancerConfig.d.ts +7 -2
  6. package/dist/config/RebalancerConfig.d.ts.map +1 -1
  7. package/dist/config/RebalancerConfig.js +7 -4
  8. package/dist/config/RebalancerConfig.js.map +1 -1
  9. package/dist/config/RebalancerConfig.test.js +134 -1
  10. package/dist/config/RebalancerConfig.test.js.map +1 -1
  11. package/dist/config/types.d.ts +1016 -304
  12. package/dist/config/types.d.ts.map +1 -1
  13. package/dist/config/types.js +105 -10
  14. package/dist/config/types.js.map +1 -1
  15. package/dist/core/InventoryRebalancer.d.ts +190 -0
  16. package/dist/core/InventoryRebalancer.d.ts.map +1 -0
  17. package/dist/core/InventoryRebalancer.js +885 -0
  18. package/dist/core/InventoryRebalancer.js.map +1 -0
  19. package/dist/core/InventoryRebalancer.test.d.ts +2 -0
  20. package/dist/core/InventoryRebalancer.test.d.ts.map +1 -0
  21. package/dist/core/InventoryRebalancer.test.js +1351 -0
  22. package/dist/core/InventoryRebalancer.test.js.map +1 -0
  23. package/dist/core/Rebalancer.d.ts +11 -4
  24. package/dist/core/Rebalancer.d.ts.map +1 -1
  25. package/dist/core/Rebalancer.js +92 -9
  26. package/dist/core/Rebalancer.js.map +1 -1
  27. package/dist/core/Rebalancer.test.js +82 -49
  28. package/dist/core/Rebalancer.test.js.map +1 -1
  29. package/dist/core/RebalancerOrchestrator.d.ts +30 -9
  30. package/dist/core/RebalancerOrchestrator.d.ts.map +1 -1
  31. package/dist/core/RebalancerOrchestrator.js +79 -71
  32. package/dist/core/RebalancerOrchestrator.js.map +1 -1
  33. package/dist/core/RebalancerOrchestrator.test.d.ts +2 -0
  34. package/dist/core/RebalancerOrchestrator.test.d.ts.map +1 -0
  35. package/dist/core/RebalancerOrchestrator.test.js +714 -0
  36. package/dist/core/RebalancerOrchestrator.test.js.map +1 -0
  37. package/dist/core/RebalancerService.d.ts +7 -3
  38. package/dist/core/RebalancerService.d.ts.map +1 -1
  39. package/dist/core/RebalancerService.js +44 -24
  40. package/dist/core/RebalancerService.js.map +1 -1
  41. package/dist/core/RebalancerService.test.js +71 -109
  42. package/dist/core/RebalancerService.test.js.map +1 -1
  43. package/dist/e2e/collateral-deficit.e2e-test.js +1 -3
  44. package/dist/e2e/collateral-deficit.e2e-test.js.map +1 -1
  45. package/dist/e2e/composite.e2e-test.js.map +1 -1
  46. package/dist/e2e/harness/BridgeSetup.d.ts +6 -0
  47. package/dist/e2e/harness/BridgeSetup.d.ts.map +1 -1
  48. package/dist/e2e/harness/BridgeSetup.js +10 -1
  49. package/dist/e2e/harness/BridgeSetup.js.map +1 -1
  50. package/dist/e2e/harness/TestHelpers.d.ts.map +1 -1
  51. package/dist/e2e/harness/TestHelpers.js +1 -4
  52. package/dist/e2e/harness/TestHelpers.js.map +1 -1
  53. package/dist/e2e/harness/TestRebalancer.d.ts +1 -1
  54. package/dist/e2e/harness/TestRebalancer.d.ts.map +1 -1
  55. package/dist/e2e/harness/TestRebalancer.js +6 -7
  56. package/dist/e2e/harness/TestRebalancer.js.map +1 -1
  57. package/dist/e2e/minAmount.e2e-test.js +0 -1
  58. package/dist/e2e/minAmount.e2e-test.js.map +1 -1
  59. package/dist/e2e/weighted.e2e-test.js +0 -1
  60. package/dist/e2e/weighted.e2e-test.js.map +1 -1
  61. package/dist/factories/RebalancerContextFactory.d.ts +48 -6
  62. package/dist/factories/RebalancerContextFactory.d.ts.map +1 -1
  63. package/dist/factories/RebalancerContextFactory.js +170 -17
  64. package/dist/factories/RebalancerContextFactory.js.map +1 -1
  65. package/dist/index.d.ts +5 -5
  66. package/dist/index.d.ts.map +1 -1
  67. package/dist/index.js +1 -1
  68. package/dist/index.js.map +1 -1
  69. package/dist/interfaces/IExternalBridge.d.ts +101 -0
  70. package/dist/interfaces/IExternalBridge.d.ts.map +1 -0
  71. package/dist/interfaces/IExternalBridge.js +2 -0
  72. package/dist/interfaces/IExternalBridge.js.map +1 -0
  73. package/dist/interfaces/IMonitor.d.ts +1 -0
  74. package/dist/interfaces/IMonitor.d.ts.map +1 -1
  75. package/dist/interfaces/IRebalancer.d.ts +25 -25
  76. package/dist/interfaces/IRebalancer.d.ts.map +1 -1
  77. package/dist/interfaces/IStrategy.d.ts +36 -3
  78. package/dist/interfaces/IStrategy.d.ts.map +1 -1
  79. package/dist/interfaces/IStrategy.js +12 -1
  80. package/dist/interfaces/IStrategy.js.map +1 -1
  81. package/dist/metrics/PriceGetter.js +1 -1
  82. package/dist/metrics/PriceGetter.js.map +1 -1
  83. package/dist/metrics/scripts/metrics.d.ts +3 -3
  84. package/dist/monitor/Monitor.d.ts +12 -2
  85. package/dist/monitor/Monitor.d.ts.map +1 -1
  86. package/dist/monitor/Monitor.js +46 -1
  87. package/dist/monitor/Monitor.js.map +1 -1
  88. package/dist/service.js +40 -17
  89. package/dist/service.js.map +1 -1
  90. package/dist/strategy/BaseStrategy.d.ts +12 -6
  91. package/dist/strategy/BaseStrategy.d.ts.map +1 -1
  92. package/dist/strategy/BaseStrategy.js +56 -21
  93. package/dist/strategy/BaseStrategy.js.map +1 -1
  94. package/dist/strategy/CollateralDeficitStrategy.d.ts +1 -1
  95. package/dist/strategy/CollateralDeficitStrategy.d.ts.map +1 -1
  96. package/dist/strategy/CollateralDeficitStrategy.js +19 -11
  97. package/dist/strategy/CollateralDeficitStrategy.js.map +1 -1
  98. package/dist/strategy/CollateralDeficitStrategy.test.js +135 -2
  99. package/dist/strategy/CollateralDeficitStrategy.test.js.map +1 -1
  100. package/dist/strategy/CompositeStrategy.test.js +13 -0
  101. package/dist/strategy/CompositeStrategy.test.js.map +1 -1
  102. package/dist/strategy/MinAmountStrategy.test.js +4 -0
  103. package/dist/strategy/MinAmountStrategy.test.js.map +1 -1
  104. package/dist/strategy/StrategyFactory.d.ts +2 -1
  105. package/dist/strategy/StrategyFactory.d.ts.map +1 -1
  106. package/dist/strategy/StrategyFactory.js +24 -8
  107. package/dist/strategy/StrategyFactory.js.map +1 -1
  108. package/dist/strategy/WeightedStrategy.test.js +6 -0
  109. package/dist/strategy/WeightedStrategy.test.js.map +1 -1
  110. package/dist/test/helpers.d.ts +8 -7
  111. package/dist/test/helpers.d.ts.map +1 -1
  112. package/dist/test/helpers.js +23 -5
  113. package/dist/test/helpers.js.map +1 -1
  114. package/dist/test/lifiMocks.d.ts +51 -0
  115. package/dist/test/lifiMocks.d.ts.map +1 -0
  116. package/dist/test/lifiMocks.js +130 -0
  117. package/dist/test/lifiMocks.js.map +1 -0
  118. package/dist/tracking/ActionTracker.d.ts +33 -1
  119. package/dist/tracking/ActionTracker.d.ts.map +1 -1
  120. package/dist/tracking/ActionTracker.js +193 -22
  121. package/dist/tracking/ActionTracker.js.map +1 -1
  122. package/dist/tracking/ActionTracker.test.js +107 -19
  123. package/dist/tracking/ActionTracker.test.js.map +1 -1
  124. package/dist/tracking/IActionTracker.d.ts +47 -3
  125. package/dist/tracking/IActionTracker.d.ts.map +1 -1
  126. package/dist/tracking/InflightContextAdapter.d.ts.map +1 -1
  127. package/dist/tracking/InflightContextAdapter.js +24 -7
  128. package/dist/tracking/InflightContextAdapter.js.map +1 -1
  129. package/dist/tracking/InflightContextAdapter.test.js +7 -4
  130. package/dist/tracking/InflightContextAdapter.test.js.map +1 -1
  131. package/dist/tracking/types.d.ts +31 -2
  132. package/dist/tracking/types.d.ts.map +1 -1
  133. package/dist/utils/ExplorerClient.d.ts +2 -1
  134. package/dist/utils/ExplorerClient.d.ts.map +1 -1
  135. package/dist/utils/ExplorerClient.js +13 -8
  136. package/dist/utils/ExplorerClient.js.map +1 -1
  137. package/dist/utils/bridgeUtils.d.ts +27 -4
  138. package/dist/utils/bridgeUtils.d.ts.map +1 -1
  139. package/dist/utils/bridgeUtils.js +38 -0
  140. package/dist/utils/bridgeUtils.js.map +1 -1
  141. package/dist/utils/bridgeUtils.test.js +9 -0
  142. package/dist/utils/bridgeUtils.test.js.map +1 -1
  143. package/dist/utils/gasEstimation.d.ts +65 -0
  144. package/dist/utils/gasEstimation.d.ts.map +1 -0
  145. package/dist/utils/gasEstimation.js +176 -0
  146. package/dist/utils/gasEstimation.js.map +1 -0
  147. package/dist/utils/tokenUtils.d.ts +9 -1
  148. package/dist/utils/tokenUtils.d.ts.map +1 -1
  149. package/dist/utils/tokenUtils.js +11 -0
  150. package/dist/utils/tokenUtils.js.map +1 -1
  151. package/package.json +9 -7
  152. package/src/bridges/LiFiBridge.ts +538 -0
  153. package/src/config/RebalancerConfig.test.ts +160 -0
  154. package/src/config/RebalancerConfig.ts +14 -3
  155. package/src/config/types.ts +136 -10
  156. package/src/core/InventoryRebalancer.test.ts +1684 -0
  157. package/src/core/InventoryRebalancer.ts +1255 -0
  158. package/src/core/Rebalancer.test.ts +84 -30
  159. package/src/core/Rebalancer.ts +144 -23
  160. package/src/core/RebalancerOrchestrator.test.ts +860 -0
  161. package/src/core/RebalancerOrchestrator.ts +146 -95
  162. package/src/core/RebalancerService.test.ts +80 -123
  163. package/src/core/RebalancerService.ts +67 -33
  164. package/src/e2e/collateral-deficit.e2e-test.ts +2 -4
  165. package/src/e2e/composite.e2e-test.ts +5 -5
  166. package/src/e2e/harness/BridgeSetup.ts +28 -1
  167. package/src/e2e/harness/TestHelpers.ts +1 -4
  168. package/src/e2e/harness/TestRebalancer.ts +7 -7
  169. package/src/e2e/minAmount.e2e-test.ts +1 -2
  170. package/src/e2e/weighted.e2e-test.ts +1 -2
  171. package/src/factories/RebalancerContextFactory.ts +293 -24
  172. package/src/index.ts +20 -5
  173. package/src/interfaces/IExternalBridge.ts +115 -0
  174. package/src/interfaces/IMonitor.ts +1 -0
  175. package/src/interfaces/IRebalancer.ts +45 -29
  176. package/src/interfaces/IStrategy.ts +50 -3
  177. package/src/metrics/PriceGetter.ts +1 -1
  178. package/src/monitor/Monitor.ts +81 -2
  179. package/src/service.ts +59 -18
  180. package/src/strategy/BaseStrategy.ts +77 -24
  181. package/src/strategy/CollateralDeficitStrategy.test.ts +181 -4
  182. package/src/strategy/CollateralDeficitStrategy.ts +42 -15
  183. package/src/strategy/CompositeStrategy.test.ts +13 -0
  184. package/src/strategy/MinAmountStrategy.test.ts +4 -0
  185. package/src/strategy/StrategyFactory.ts +33 -6
  186. package/src/strategy/WeightedStrategy.test.ts +6 -0
  187. package/src/test/helpers.ts +39 -14
  188. package/src/test/lifiMocks.ts +174 -0
  189. package/src/tracking/ActionTracker.test.ts +122 -19
  190. package/src/tracking/ActionTracker.ts +284 -24
  191. package/src/tracking/IActionTracker.ts +58 -3
  192. package/src/tracking/InflightContextAdapter.test.ts +7 -4
  193. package/src/tracking/InflightContextAdapter.ts +42 -9
  194. package/src/tracking/types.ts +43 -2
  195. package/src/utils/ExplorerClient.ts +23 -10
  196. package/src/utils/bridgeUtils.test.ts +9 -0
  197. package/src/utils/bridgeUtils.ts +75 -6
  198. package/src/utils/gasEstimation.ts +272 -0
  199. package/src/utils/tokenUtils.ts +12 -0
  200. package/dist/tracking/index.d.ts +0 -7
  201. package/dist/tracking/index.d.ts.map +0 -1
  202. package/dist/tracking/index.js +0 -6
  203. package/dist/tracking/index.js.map +0 -1
  204. package/dist/utils/index.d.ts +0 -5
  205. package/dist/utils/index.d.ts.map +0 -1
  206. package/dist/utils/index.js +0 -5
  207. package/dist/utils/index.js.map +0 -1
  208. package/src/tracking/index.ts +0 -36
  209. package/src/utils/index.ts +0 -4
@@ -0,0 +1,1684 @@
1
+ import chai, { expect } from 'chai';
2
+ import chaiAsPromised from 'chai-as-promised';
3
+ import { Wallet } from 'ethers';
4
+ import { pino } from 'pino';
5
+ import Sinon, { type SinonStubbedInstance } from 'sinon';
6
+
7
+ import {
8
+ type ChainName,
9
+ type MultiProvider,
10
+ TokenStandard,
11
+ type WarpCore,
12
+ } from '@hyperlane-xyz/sdk';
13
+
14
+ import { ExternalBridgeType } from '../config/types.js';
15
+ import type { IExternalBridge } from '../interfaces/IExternalBridge.js';
16
+ import type { InventoryRoute } from '../interfaces/IStrategy.js';
17
+ import { createMockBridgeQuote } from '../test/lifiMocks.js';
18
+ import type { IActionTracker } from '../tracking/IActionTracker.js';
19
+ import type { RebalanceIntent } from '../tracking/types.js';
20
+
21
+ import {
22
+ InventoryRebalancer,
23
+ type InventoryRebalancerConfig,
24
+ } from './InventoryRebalancer.js';
25
+
26
+ chai.use(chaiAsPromised);
27
+
28
+ const testLogger = pino({ level: 'silent' });
29
+
30
+ describe('InventoryRebalancer E2E', () => {
31
+ let inventoryRebalancer: InventoryRebalancer;
32
+ let config: InventoryRebalancerConfig;
33
+ let actionTracker: SinonStubbedInstance<IActionTracker>;
34
+ let bridge: SinonStubbedInstance<IExternalBridge>;
35
+ let warpCore: any;
36
+ let multiProvider: any;
37
+ let adapterStub: any;
38
+
39
+ // Test constants
40
+ const ARBITRUM_CHAIN = 'arbitrum' as ChainName;
41
+ const SOLANA_CHAIN = 'solanamainnet' as ChainName;
42
+ const ARBITRUM_DOMAIN = 42161;
43
+ const SOLANA_DOMAIN = 1399811149;
44
+ const TEST_PRIVATE_KEY =
45
+ '0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80';
46
+ const TEST_WALLET = new Wallet(TEST_PRIVATE_KEY);
47
+ const INVENTORY_SIGNER = TEST_WALLET.address;
48
+
49
+ beforeEach(() => {
50
+ // Config
51
+ config = {
52
+ inventorySigner: INVENTORY_SIGNER,
53
+ inventoryChains: [ARBITRUM_CHAIN, SOLANA_CHAIN],
54
+ };
55
+
56
+ // Mock IActionTracker
57
+ actionTracker = {
58
+ initialize: Sinon.stub(),
59
+ syncTransfers: Sinon.stub(),
60
+ syncRebalanceIntents: Sinon.stub(),
61
+ syncRebalanceActions: Sinon.stub(),
62
+ syncInventoryMovementActions: Sinon.stub(),
63
+ getInProgressTransfers: Sinon.stub(),
64
+ getTransfersByDestination: Sinon.stub(),
65
+ getActiveRebalanceIntents: Sinon.stub(),
66
+ getRebalanceIntentsByDestination: Sinon.stub(),
67
+ createRebalanceIntent: Sinon.stub(),
68
+ completeRebalanceIntent: Sinon.stub(),
69
+ cancelRebalanceIntent: Sinon.stub(),
70
+ failRebalanceIntent: Sinon.stub(),
71
+ getActionsByType: Sinon.stub(),
72
+ getInflightInventoryMovements: Sinon.stub(),
73
+ getPartiallyFulfilledInventoryIntents: Sinon.stub(),
74
+ createRebalanceAction: Sinon.stub(),
75
+ completeRebalanceAction: Sinon.stub(),
76
+ failRebalanceAction: Sinon.stub(),
77
+ logStoreContents: Sinon.stub(),
78
+ } as SinonStubbedInstance<IActionTracker>;
79
+
80
+ // Default: No active (partial) inventory intents
81
+ actionTracker.getPartiallyFulfilledInventoryIntents.resolves([]);
82
+
83
+ bridge = {
84
+ bridgeId: 'lifi',
85
+ quote: Sinon.stub(),
86
+ execute: Sinon.stub(),
87
+ getStatus: Sinon.stub(),
88
+ getNativeTokenAddress: Sinon.stub().returns(
89
+ '0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE',
90
+ ),
91
+ } as unknown as SinonStubbedInstance<IExternalBridge>;
92
+
93
+ // Mock adapter for WarpCore tokens
94
+ adapterStub = {
95
+ quoteTransferRemoteGas: Sinon.stub().resolves({
96
+ igpQuote: { amount: 1000000n },
97
+ }),
98
+ populateTransferRemoteTx: Sinon.stub().resolves({
99
+ to: '0xRouterAddress',
100
+ data: '0xTransferRemoteData',
101
+ value: 1000000n,
102
+ }),
103
+ };
104
+
105
+ // Mock WarpCore with tokens for both chains
106
+ // Note: We need tokens for both origin (surplus) and destination (deficit) chains
107
+ // because transferRemote is called FROM destination (after direction swap)
108
+ const arbitrumToken = {
109
+ chainName: ARBITRUM_CHAIN,
110
+ standard: TokenStandard.EvmHypCollateral, // Non-native: no IGP reservation needed
111
+ addressOrDenom: '0xArbitrumToken',
112
+ getHypAdapter: Sinon.stub().returns(adapterStub),
113
+ };
114
+ const solanaToken = {
115
+ chainName: SOLANA_CHAIN,
116
+ standard: TokenStandard.EvmHypCollateral, // Non-native: no IGP reservation needed
117
+ addressOrDenom: '0xSolanaToken',
118
+ getHypAdapter: Sinon.stub().returns(adapterStub),
119
+ };
120
+
121
+ warpCore = {
122
+ tokens: [arbitrumToken, solanaToken],
123
+ multiProvider: {
124
+ getProvider: Sinon.stub(),
125
+ getSigner: Sinon.stub(),
126
+ },
127
+ };
128
+
129
+ // Mock provider with getFeeData for gas estimation, estimateGas for actual gas estimation,
130
+ // and waitForTransaction for confirmations
131
+ const mockProvider = {
132
+ getFeeData: Sinon.stub().resolves({
133
+ maxFeePerGas: 10000000000n, // 10 gwei
134
+ gasPrice: 10000000000n,
135
+ }),
136
+ estimateGas: Sinon.stub().resolves(300000n), // Mock gas estimate for transferRemote
137
+ waitForTransaction: Sinon.stub().resolves({
138
+ blockNumber: 100,
139
+ status: 1,
140
+ }),
141
+ };
142
+
143
+ // Mock MultiProvider
144
+ multiProvider = {
145
+ getDomainId: Sinon.stub().callsFake((chain: ChainName) => {
146
+ if (chain === ARBITRUM_CHAIN) return ARBITRUM_DOMAIN;
147
+ if (chain === SOLANA_CHAIN) return SOLANA_DOMAIN;
148
+ return 0;
149
+ }),
150
+ getChainId: Sinon.stub().callsFake((chain: ChainName) => {
151
+ if (chain === ARBITRUM_CHAIN) return 42161;
152
+ if (chain === SOLANA_CHAIN) return 1399811149;
153
+ return 0;
154
+ }),
155
+ getChainName: Sinon.stub().callsFake((domain: number) => {
156
+ if (domain === ARBITRUM_DOMAIN) return ARBITRUM_CHAIN;
157
+ if (domain === SOLANA_DOMAIN) return SOLANA_CHAIN;
158
+ return 'unknown';
159
+ }),
160
+ getChainMetadata: Sinon.stub().returns({
161
+ blocks: { reorgPeriod: 1 }, // Quick confirmations for tests
162
+ }),
163
+ getProvider: Sinon.stub().returns(mockProvider),
164
+ getSigner: Sinon.stub().returns(TEST_WALLET),
165
+ sendTransaction: Sinon.stub().resolves({
166
+ transactionHash: '0xTransferRemoteTxHash',
167
+ logs: [], // Required for HyperlaneCore.getDispatchedMessages
168
+ }),
169
+ };
170
+
171
+ // Create InventoryRebalancer
172
+ inventoryRebalancer = new InventoryRebalancer(
173
+ config,
174
+ actionTracker as unknown as IActionTracker,
175
+ { lifi: bridge as unknown as IExternalBridge },
176
+ warpCore as unknown as WarpCore,
177
+ multiProvider as unknown as MultiProvider,
178
+ testLogger,
179
+ );
180
+
181
+ // Set default high inventory balances - tests can override via setInventoryBalances
182
+ inventoryRebalancer.setInventoryBalances({
183
+ [ARBITRUM_CHAIN]: BigInt('1000000000000000000000'),
184
+ [SOLANA_CHAIN]: BigInt('1000000000000000000000'),
185
+ });
186
+ });
187
+
188
+ afterEach(() => {
189
+ Sinon.restore();
190
+ });
191
+
192
+ // Helper to create test routes and intents
193
+ function createTestRoute(
194
+ overrides?: Partial<InventoryRoute>,
195
+ ): InventoryRoute {
196
+ return {
197
+ origin: ARBITRUM_CHAIN,
198
+ destination: SOLANA_CHAIN,
199
+ amount: 10000000000n, // 10k USDC (6 decimals)
200
+ executionType: 'inventory',
201
+ externalBridge: ExternalBridgeType.LiFi,
202
+ ...overrides,
203
+ };
204
+ }
205
+
206
+ function createTestIntent(
207
+ overrides?: Partial<RebalanceIntent>,
208
+ ): RebalanceIntent {
209
+ const now = Date.now();
210
+ const intent: RebalanceIntent = {
211
+ id: 'intent-1',
212
+ status: 'not_started',
213
+ origin: ARBITRUM_DOMAIN,
214
+ destination: SOLANA_DOMAIN,
215
+ amount: 10000000000n,
216
+ executionMethod: 'inventory',
217
+ createdAt: now,
218
+ updatedAt: now,
219
+ ...overrides,
220
+ };
221
+
222
+ // Configure mock to return this intent when createRebalanceIntent is called
223
+ actionTracker.createRebalanceIntent.resolves(intent);
224
+
225
+ return intent;
226
+ }
227
+
228
+ describe('Basic Inventory Rebalance (Sufficient Inventory)', () => {
229
+ // NOTE: Strategy route is arbitrum (surplus) → solana (deficit)
230
+ // But execution calls transferRemote FROM solana TO arbitrum (swapped direction)
231
+ // This ADDS collateral to solana (filling deficit) and RELEASES from arbitrum (has surplus)
232
+
233
+ it('executes transferRemote when inventory is available on destination chain', async () => {
234
+ // Setup: Strategy says move from arbitrum→solana
235
+ // We need inventory on SOLANA (destination/deficit) to call transferRemote FROM there
236
+ const route = createTestRoute();
237
+ createTestIntent();
238
+
239
+ // Inventory is checked on DESTINATION (solana), not origin
240
+ inventoryRebalancer.setInventoryBalances({
241
+ [SOLANA_CHAIN]: 10000000000n,
242
+ [ARBITRUM_CHAIN]: 0n,
243
+ });
244
+
245
+ // Execute
246
+ const results = await inventoryRebalancer.rebalance([route]);
247
+
248
+ // Verify: Single successful result
249
+ expect(results).to.have.lengthOf(1);
250
+ expect(results[0].success).to.be.true;
251
+ expect(results[0].route).to.deep.equal(route);
252
+
253
+ // Verify: transferRemote was called via adapter
254
+ expect(adapterStub.quoteTransferRemoteGas.calledOnce).to.be.true;
255
+ expect(adapterStub.populateTransferRemoteTx.calledOnce).to.be.true;
256
+
257
+ // Verify: Transaction was sent FROM SOLANA (destination chain, swapped)
258
+ expect(multiProvider.sendTransaction.calledOnce).to.be.true;
259
+ const [chainArg, txArg] = multiProvider.sendTransaction.firstCall.args;
260
+ expect(chainArg).to.equal(SOLANA_CHAIN); // Called FROM destination (swapped)
261
+ expect(txArg.to).to.equal('0xRouterAddress');
262
+
263
+ // Verify: inventory_deposit action was created
264
+ expect(actionTracker.createRebalanceAction.calledOnce).to.be.true;
265
+ const actionParams =
266
+ actionTracker.createRebalanceAction.firstCall.args[0];
267
+ expect(actionParams.intentId).to.equal('intent-1');
268
+ expect(actionParams.type).to.equal('inventory_deposit');
269
+ expect(actionParams.amount).to.equal(10000000000n);
270
+ expect(actionParams.txHash).to.equal('0xTransferRemoteTxHash');
271
+ });
272
+
273
+ it('executes transferRemote with correct parameters (swapped direction)', async () => {
274
+ const route = createTestRoute({ amount: 5000000000n }); // 5k USDC
275
+ createTestIntent({ amount: 5000000000n });
276
+
277
+ // Inventory checked on DESTINATION (solana)
278
+ inventoryRebalancer.setInventoryBalances({
279
+ [SOLANA_CHAIN]: 5000000000n,
280
+ [ARBITRUM_CHAIN]: 0n,
281
+ });
282
+
283
+ await inventoryRebalancer.rebalance([route]);
284
+
285
+ // Verify populateTransferRemoteTx params
286
+ // Direction is SWAPPED: transferRemote from solana TO arbitrum
287
+ const populateParams =
288
+ adapterStub.populateTransferRemoteTx.firstCall.args[0];
289
+ expect(populateParams.destination).to.equal(ARBITRUM_DOMAIN); // Goes TO arbitrum (swapped)
290
+ expect(populateParams.recipient).to.equal(INVENTORY_SIGNER);
291
+ expect(populateParams.weiAmountOrId).to.equal(5000000000n);
292
+ });
293
+ });
294
+
295
+ describe('Partial Fulfillment (Insufficient Inventory)', () => {
296
+ // Partial transfers happen when maxTransferable >= minViableTransfer
297
+ // For non-native tokens (EvmHypCollateral), minViableTransfer = 0, so partial always viable
298
+ const PARTIAL_AMOUNT = BigInt(5e15); // 0.005 ETH - above threshold
299
+ const FULL_AMOUNT = BigInt(1e16); // 0.01 ETH
300
+
301
+ it('executes partial transferRemote when maxTransferable >= minViableTransfer', async () => {
302
+ // Setup: Need 0.01 ETH, but only 0.005 ETH available on destination
303
+ // For non-native tokens, minViableTransfer = 0, so partial transfer is viable
304
+ const route = createTestRoute({ amount: FULL_AMOUNT });
305
+ createTestIntent({ amount: FULL_AMOUNT });
306
+
307
+ // Inventory checked on DESTINATION (solana) - Only 0.005 ETH available
308
+ inventoryRebalancer.setInventoryBalances({
309
+ [SOLANA_CHAIN]: PARTIAL_AMOUNT,
310
+ [ARBITRUM_CHAIN]: 0n,
311
+ });
312
+
313
+ // Execute
314
+ const results = await inventoryRebalancer.rebalance([route]);
315
+
316
+ // Verify: Success with partial amount
317
+ expect(results).to.have.lengthOf(1);
318
+ expect(results[0].success).to.be.true;
319
+
320
+ // Verify: transferRemote was called with available amount (0.005 ETH), not full amount (0.01 ETH)
321
+ const populateParams =
322
+ adapterStub.populateTransferRemoteTx.firstCall.args[0];
323
+ expect(populateParams.weiAmountOrId).to.equal(PARTIAL_AMOUNT);
324
+
325
+ // Verify: Action created for partial amount
326
+ const actionParams =
327
+ actionTracker.createRebalanceAction.firstCall.args[0];
328
+ expect(actionParams.amount).to.equal(PARTIAL_AMOUNT);
329
+ });
330
+
331
+ it('intent remains in_progress after partial fulfillment', async () => {
332
+ const route = createTestRoute({ amount: FULL_AMOUNT });
333
+ createTestIntent({ amount: FULL_AMOUNT });
334
+
335
+ // Inventory checked on DESTINATION (solana)
336
+ inventoryRebalancer.setInventoryBalances({
337
+ [SOLANA_CHAIN]: PARTIAL_AMOUNT,
338
+ [ARBITRUM_CHAIN]: 0n,
339
+ });
340
+
341
+ const results = await inventoryRebalancer.rebalance([route]);
342
+
343
+ // Verify: Partial transfer succeeded
344
+ expect(results[0].success).to.be.true;
345
+ });
346
+ });
347
+
348
+ describe('No Inventory Available', () => {
349
+ it('returns failure when no inventory on destination chain and no other source available', async () => {
350
+ const route = createTestRoute();
351
+ createTestIntent();
352
+
353
+ // No inventory anywhere
354
+ inventoryRebalancer.setInventoryBalances({
355
+ [SOLANA_CHAIN]: 0n,
356
+ [ARBITRUM_CHAIN]: 0n,
357
+ });
358
+
359
+ // Execute
360
+ const results = await inventoryRebalancer.rebalance([route]);
361
+
362
+ // Verify: Failure result
363
+ expect(results).to.have.lengthOf(1);
364
+ expect(results[0].success).to.be.false;
365
+ expect(results[0].error).to.include('No inventory available');
366
+
367
+ // Verify: No transferRemote attempted
368
+ expect(adapterStub.populateTransferRemoteTx.called).to.be.false;
369
+ expect(multiProvider.sendTransaction.called).to.be.false;
370
+ expect(actionTracker.createRebalanceAction.called).to.be.false;
371
+ });
372
+ });
373
+
374
+ describe('Single Intent Architecture', () => {
375
+ it('takes only first route when multiple routes provided', async () => {
376
+ // Route 1: arbitrum → solana (check inventory on solana)
377
+ const route1 = createTestRoute({ amount: 5000000000n });
378
+ // Route 2: solana → arbitrum (would check inventory on arbitrum if processed)
379
+ const route2 = createTestRoute({
380
+ origin: SOLANA_CHAIN,
381
+ destination: ARBITRUM_CHAIN,
382
+ amount: 3000000000n,
383
+ });
384
+
385
+ createTestIntent({ id: 'intent-1', amount: 5000000000n });
386
+
387
+ // Inventory on DESTINATION of first route (solana)
388
+ inventoryRebalancer.setInventoryBalances({
389
+ [SOLANA_CHAIN]: 5000000000n,
390
+ [ARBITRUM_CHAIN]: 0n,
391
+ });
392
+
393
+ // Execute with multiple routes
394
+ const results = await inventoryRebalancer.rebalance([route1, route2]);
395
+
396
+ // Verify: Only ONE route processed (single-intent architecture)
397
+ expect(results).to.have.lengthOf(1);
398
+ expect(results[0].success).to.be.true;
399
+
400
+ // Verify: Only one intent created
401
+ expect(actionTracker.createRebalanceIntent.calledOnce).to.be.true;
402
+
403
+ // Verify: Only one action created
404
+ expect(actionTracker.createRebalanceAction.calledOnce).to.be.true;
405
+ });
406
+
407
+ it('continues existing intent instead of processing new routes', async () => {
408
+ // Setup: existing partial intent
409
+ const existingIntent = createTestIntent({
410
+ id: 'existing-intent',
411
+ status: 'in_progress',
412
+ amount: 10000000000n,
413
+ });
414
+
415
+ // Configure mock to return existing partial intent
416
+ actionTracker.getPartiallyFulfilledInventoryIntents.resolves([
417
+ {
418
+ intent: existingIntent,
419
+ completedAmount: 3000000000n,
420
+ remaining: 7000000000n, // 7k remaining
421
+ },
422
+ ]);
423
+
424
+ // New route that would be ignored
425
+ const newRoute = createTestRoute({ amount: 5000000000n });
426
+
427
+ // Inventory on DESTINATION of existing intent (solana) - Plenty of inventory
428
+ inventoryRebalancer.setInventoryBalances({
429
+ [SOLANA_CHAIN]: 10000000000n,
430
+ [ARBITRUM_CHAIN]: 0n,
431
+ });
432
+
433
+ // Execute with new route (should be ignored in favor of existing intent)
434
+ const results = await inventoryRebalancer.rebalance([newRoute]);
435
+
436
+ // Verify: Existing intent was continued (not new route)
437
+ expect(results).to.have.lengthOf(1);
438
+ expect(results[0].success).to.be.true;
439
+
440
+ // Verify: No new intent was created (existing was used)
441
+ expect(actionTracker.createRebalanceIntent.called).to.be.false;
442
+ });
443
+
444
+ it('returns empty results when no routes provided and no active intent', async () => {
445
+ const results = await inventoryRebalancer.rebalance([]);
446
+
447
+ expect(results).to.have.lengthOf(0);
448
+ expect(actionTracker.createRebalanceIntent.called).to.be.false;
449
+ });
450
+
451
+ it('continues existing not_started intent instead of creating new one', async () => {
452
+ // Setup: Create an intent that stays 'not_started' (simulating failed bridges)
453
+ const existingIntent = createTestIntent({
454
+ id: 'stuck-not-started-intent',
455
+ status: 'not_started', // Never transitioned to in_progress
456
+ amount: 10000000000n,
457
+ });
458
+
459
+ // Configure mock to return the not_started intent as a partial intent
460
+ // (this is the fix - getPartiallyFulfilledInventoryIntents now includes not_started)
461
+ actionTracker.getPartiallyFulfilledInventoryIntents.resolves([
462
+ {
463
+ intent: existingIntent,
464
+ completedAmount: 0n,
465
+ remaining: 10000000000n,
466
+ },
467
+ ]);
468
+
469
+ // New route that would be ignored in favor of existing intent
470
+ const newRoute = createTestRoute({ amount: 5000000000n });
471
+
472
+ // Provide sufficient inventory for execution
473
+ inventoryRebalancer.setInventoryBalances({
474
+ [SOLANA_CHAIN]: 10000000000n,
475
+ [ARBITRUM_CHAIN]: 0n,
476
+ });
477
+
478
+ const results = await inventoryRebalancer.rebalance([newRoute]);
479
+
480
+ // Verify: Existing not_started intent was continued (not new route)
481
+ expect(results).to.have.lengthOf(1);
482
+ expect(results[0].success).to.be.true;
483
+
484
+ // Verify: No new intent was created (existing was continued)
485
+ expect(actionTracker.createRebalanceIntent.called).to.be.false;
486
+ });
487
+ });
488
+
489
+ describe('Error Handling', () => {
490
+ it('handles transaction send failure', async () => {
491
+ const route = createTestRoute();
492
+ createTestIntent();
493
+
494
+ // Inventory on DESTINATION (solana)
495
+ inventoryRebalancer.setInventoryBalances({
496
+ [SOLANA_CHAIN]: 10000000000n,
497
+ [ARBITRUM_CHAIN]: 0n,
498
+ });
499
+ multiProvider.sendTransaction.rejects(new Error('Transaction failed'));
500
+
501
+ const results = await inventoryRebalancer.rebalance([route]);
502
+
503
+ expect(results).to.have.lengthOf(1);
504
+ expect(results[0].success).to.be.false;
505
+ expect(results[0].error).to.include('Transaction failed');
506
+ });
507
+
508
+ it('handles missing token for chain', async () => {
509
+ // Clear tokens to simulate missing token
510
+ warpCore.tokens = [];
511
+
512
+ const route = createTestRoute();
513
+ createTestIntent();
514
+
515
+ // Even with inventory, if no token for destination, it should fail
516
+ inventoryRebalancer.setInventoryBalances({
517
+ [SOLANA_CHAIN]: 10000000000n,
518
+ [ARBITRUM_CHAIN]: 0n,
519
+ });
520
+
521
+ const results = await inventoryRebalancer.rebalance([route]);
522
+
523
+ expect(results).to.have.lengthOf(1);
524
+ expect(results[0].success).to.be.false;
525
+ expect(results[0].error).to.include('No token found');
526
+ });
527
+
528
+ it('handles adapter quoteTransferRemoteGas failure', async () => {
529
+ const route = createTestRoute();
530
+ createTestIntent();
531
+
532
+ // Inventory on DESTINATION (solana)
533
+ inventoryRebalancer.setInventoryBalances({
534
+ [SOLANA_CHAIN]: 10000000000n,
535
+ [ARBITRUM_CHAIN]: 0n,
536
+ });
537
+ adapterStub.quoteTransferRemoteGas.rejects(new Error('Gas quote failed'));
538
+
539
+ const results = await inventoryRebalancer.rebalance([route]);
540
+
541
+ expect(results).to.have.lengthOf(1);
542
+ expect(results[0].success).to.be.false;
543
+ expect(results[0].error).to.include('Gas quote failed');
544
+ });
545
+
546
+ it('throws when signer is not a Wallet instance', async () => {
547
+ multiProvider.getSigner = Sinon.stub().returns({
548
+ getAddress: Sinon.stub().resolves(INVENTORY_SIGNER),
549
+ });
550
+
551
+ inventoryRebalancer = new InventoryRebalancer(
552
+ config,
553
+ actionTracker as unknown as IActionTracker,
554
+ { lifi: bridge as unknown as IExternalBridge },
555
+ warpCore as unknown as WarpCore,
556
+ multiProvider as unknown as MultiProvider,
557
+ testLogger,
558
+ );
559
+
560
+ const route = createTestRoute({ amount: BigInt(1e18) });
561
+ createTestIntent({ amount: BigInt(1e18) });
562
+
563
+ inventoryRebalancer.setInventoryBalances({
564
+ [ARBITRUM_CHAIN]: BigInt(10e18),
565
+ [SOLANA_CHAIN]: 0n,
566
+ });
567
+
568
+ bridge.quote.resolves(
569
+ createMockBridgeQuote({
570
+ fromAmount: BigInt(1e18),
571
+ toAmount: BigInt(0.98e18),
572
+ }),
573
+ );
574
+
575
+ const results = await inventoryRebalancer.rebalance([route]);
576
+
577
+ expect(results).to.have.lengthOf(1);
578
+ expect(results[0].success).to.be.false;
579
+ expect(results[0].error).to.include('Wallet');
580
+ expect(bridge.execute.called).to.be.false;
581
+ });
582
+ });
583
+
584
+ describe('Native Token IGP Reservation', () => {
585
+ // Gas estimation: 300,000 gas × 10 gwei = 3,000,000,000,000 wei
586
+ // Buffered gas limit (10%): 330,000 gas
587
+ // Buffered gas cost: 330,000 × 10 gwei = 3,300,000,000,000 wei
588
+ // IGP quote: 1,000,000 wei
589
+ // Total reservation: IGP + buffered gas = 3,300,001,000,000 wei (~0.0033 ETH)
590
+ // Total cost (for min viable): IGP + buffered gas = 3,300,001,000,000 wei
591
+ // Min viable transfer (2x total cost): 6,600,002,000,000 wei (~0.0066 ETH)
592
+ const GAS_LIMIT = 300000n;
593
+ const BUFFERED_GAS_LIMIT = (GAS_LIMIT * 110n) / 100n; // 10% buffer
594
+ const GAS_PRICE = 10000000000n; // 10 gwei
595
+ const BUFFERED_GAS_COST = GAS_PRICE * BUFFERED_GAS_LIMIT;
596
+ const IGP_COST = 1000000n;
597
+ const TOTAL_RESERVATION = IGP_COST + BUFFERED_GAS_COST;
598
+ // Note: MIN_VIABLE_TRANSFER = TOTAL_COST * 2n = ~6.6e12 wei (~0.0066 ETH)
599
+
600
+ it('reserves IGP and gas cost when transferring native tokens', async () => {
601
+ // Setup: Native token on DESTINATION (solana) where IGP and gas must be reserved
602
+ // Strategy: arbitrum → solana, so transferRemote is called FROM solana
603
+ const arbitrumToken = {
604
+ chainName: ARBITRUM_CHAIN,
605
+ standard: TokenStandard.EvmHypNative,
606
+ getHypAdapter: Sinon.stub().returns(adapterStub),
607
+ };
608
+ const solanaToken = {
609
+ chainName: SOLANA_CHAIN,
610
+ standard: TokenStandard.EvmHypNative, // Native token: reservation needed
611
+ getHypAdapter: Sinon.stub().returns(adapterStub),
612
+ };
613
+ warpCore.tokens = [arbitrumToken, solanaToken];
614
+
615
+ const requestedAmount = 10000000000000000n; // 0.01 ETH
616
+ const availableInventory = requestedAmount + TOTAL_RESERVATION; // Enough for amount + costs
617
+
618
+ const route = createTestRoute({ amount: requestedAmount });
619
+ createTestIntent({ amount: requestedAmount });
620
+
621
+ // Inventory on DESTINATION (solana) where transferRemote is called FROM
622
+ inventoryRebalancer.setInventoryBalances({
623
+ [SOLANA_CHAIN]: availableInventory,
624
+ [ARBITRUM_CHAIN]: 0n,
625
+ });
626
+
627
+ // Execute
628
+ const results = await inventoryRebalancer.rebalance([route]);
629
+
630
+ // Verify: Success with full requested amount (since we have enough for amount + costs)
631
+ expect(results).to.have.lengthOf(1);
632
+ expect(results[0].success).to.be.true;
633
+
634
+ // Verify: transferRemote was called with full amount (costs are separate)
635
+ // Note: populateTransferRemoteTx is called multiple times:
636
+ // - First calls: gas estimation with minimal amount (1n)
637
+ // - Last call: actual transfer with requested amount
638
+ const populateParams =
639
+ adapterStub.populateTransferRemoteTx.lastCall.args[0];
640
+ expect(populateParams.weiAmountOrId).to.equal(requestedAmount);
641
+ });
642
+
643
+ it('reduces transfer amount when inventory is limited', async () => {
644
+ // Setup: Native token on DESTINATION where we have less inventory than needed
645
+ const arbitrumToken = {
646
+ chainName: ARBITRUM_CHAIN,
647
+ standard: TokenStandard.EvmHypNative,
648
+ getHypAdapter: Sinon.stub().returns(adapterStub),
649
+ };
650
+ const solanaToken = {
651
+ chainName: SOLANA_CHAIN,
652
+ standard: TokenStandard.EvmHypNative,
653
+ getHypAdapter: Sinon.stub().returns(adapterStub),
654
+ };
655
+ warpCore.tokens = [arbitrumToken, solanaToken];
656
+
657
+ // Request more than we have available
658
+ const requestedAmount = 20000000000000000n; // 0.02 ETH
659
+ // Have enough for costs + partial transfer that exceeds min viable threshold
660
+ // availableInventory = TOTAL_RESERVATION + partialAmount
661
+ // where partialAmount >= MIN_VIABLE_TRANSFER (2x base cost)
662
+ const partialAmount = 7000000000000000n; // 0.007 ETH (> MIN_VIABLE_TRANSFER of ~0.006 ETH)
663
+ const availableInventory = TOTAL_RESERVATION + partialAmount;
664
+
665
+ const route = createTestRoute({ amount: requestedAmount });
666
+ createTestIntent({ amount: requestedAmount });
667
+
668
+ // Inventory on DESTINATION (solana)
669
+ // Since 100% is consolidated on destination, partial transfer should happen
670
+ inventoryRebalancer.setInventoryBalances({
671
+ [SOLANA_CHAIN]: availableInventory,
672
+ [ARBITRUM_CHAIN]: 0n,
673
+ });
674
+
675
+ // Execute
676
+ const results = await inventoryRebalancer.rebalance([route]);
677
+
678
+ // Verify: Success with reduced amount
679
+ expect(results).to.have.lengthOf(1);
680
+ expect(results[0].success).to.be.true;
681
+
682
+ // Verify: transferRemote was called with reduced amount (inventory - costs)
683
+ // Note: populateTransferRemoteTx is called multiple times:
684
+ // - First in estimateTransferRemoteGas (for calculateMaxTransferable)
685
+ // - Second in estimateTransferRemoteGas (for calculateMinViableTransfer)
686
+ // - Third in executeTransferRemote (the actual transfer)
687
+ // We check the last call which is the actual execution
688
+ const populateParams =
689
+ adapterStub.populateTransferRemoteTx.lastCall.args[0];
690
+ expect(populateParams.weiAmountOrId).to.equal(partialAmount);
691
+ });
692
+
693
+ it('returns failure when inventory cannot cover costs', async () => {
694
+ // Setup: Native token on DESTINATION where inventory is less than total reservation
695
+ const arbitrumToken = {
696
+ chainName: ARBITRUM_CHAIN,
697
+ standard: TokenStandard.EvmHypNative,
698
+ getHypAdapter: Sinon.stub().returns(adapterStub),
699
+ };
700
+ const solanaToken = {
701
+ chainName: SOLANA_CHAIN,
702
+ standard: TokenStandard.EvmHypNative,
703
+ getHypAdapter: Sinon.stub().returns(adapterStub),
704
+ };
705
+ warpCore.tokens = [arbitrumToken, solanaToken];
706
+
707
+ const route = createTestRoute({ amount: 10000000000000000n });
708
+ createTestIntent({ amount: 10000000000000000n });
709
+
710
+ // Available inventory on DESTINATION (solana) is less than total reservation (IGP + gas)
711
+ // Just under the threshold, no inventory anywhere else
712
+ inventoryRebalancer.setInventoryBalances({
713
+ [SOLANA_CHAIN]: TOTAL_RESERVATION - 1n,
714
+ [ARBITRUM_CHAIN]: 0n,
715
+ });
716
+
717
+ // Execute
718
+ const results = await inventoryRebalancer.rebalance([route]);
719
+
720
+ // Verify: Failure due to insufficient funds
721
+ expect(results).to.have.lengthOf(1);
722
+ expect(results[0].success).to.be.false;
723
+ expect(results[0].error).to.include('No inventory available');
724
+
725
+ // Verify: No actual transferRemote executed (only gas estimation calls allowed)
726
+ // Note: With gas estimation, populateTransferRemoteTx IS called for estimation,
727
+ // so we can't check that it wasn't called at all. Instead, verify no action was created.
728
+ expect(actionTracker.createRebalanceAction.called).to.be.false;
729
+ });
730
+
731
+ it('does not reserve IGP for non-native tokens', async () => {
732
+ // Setup: Collateral tokens on both chains (IGP paid separately)
733
+ const arbitrumToken = {
734
+ chainName: ARBITRUM_CHAIN,
735
+ standard: TokenStandard.EvmHypCollateral, // Non-native: no IGP reservation
736
+ getHypAdapter: Sinon.stub().returns(adapterStub),
737
+ };
738
+ const solanaToken = {
739
+ chainName: SOLANA_CHAIN,
740
+ standard: TokenStandard.EvmHypCollateral, // Non-native: no IGP reservation
741
+ getHypAdapter: Sinon.stub().returns(adapterStub),
742
+ };
743
+ warpCore.tokens = [arbitrumToken, solanaToken];
744
+
745
+ const route = createTestRoute({ amount: 10000000000n });
746
+ createTestIntent({ amount: 10000000000n });
747
+
748
+ // Inventory on DESTINATION (solana)
749
+ inventoryRebalancer.setInventoryBalances({
750
+ [SOLANA_CHAIN]: 10000000000n,
751
+ [ARBITRUM_CHAIN]: 0n,
752
+ });
753
+
754
+ // Execute
755
+ const results = await inventoryRebalancer.rebalance([route]);
756
+
757
+ // Verify: Success with full amount (no IGP deduction)
758
+ expect(results).to.have.lengthOf(1);
759
+ expect(results[0].success).to.be.true;
760
+
761
+ const populateParams =
762
+ adapterStub.populateTransferRemoteTx.firstCall.args[0];
763
+ expect(populateParams.weiAmountOrId).to.equal(10000000000n); // Full amount
764
+ });
765
+ });
766
+
767
+ describe('Native Token Fee Quote Reservation', () => {
768
+ // Token fee quote scenarios: tests verify that tokenFeeQuote is properly deducted
769
+ // from maxTransferable when it represents a native token fee
770
+ const GAS_LIMIT = 300000n;
771
+ const BUFFERED_GAS_LIMIT = (GAS_LIMIT * 110n) / 100n; // 10% buffer
772
+ const GAS_PRICE = 10000000000n; // 10 gwei
773
+ const BUFFERED_GAS_COST = GAS_PRICE * BUFFERED_GAS_LIMIT;
774
+ const IGP_COST = 1000000n;
775
+ const TOKEN_FEE_AMOUNT = 500000n; // Significant token fee
776
+ const TOTAL_RESERVATION = IGP_COST + BUFFERED_GAS_COST;
777
+
778
+ it('deducts tokenFeeQuote when addressOrDenom is undefined (native token)', async () => {
779
+ // Setup: Native token with tokenFeeQuote but no addressOrDenom (undefined = native)
780
+ // Override the global adapterStub to include tokenFeeQuote
781
+ adapterStub.quoteTransferRemoteGas.resolves({
782
+ igpQuote: { amount: IGP_COST },
783
+ tokenFeeQuote: { amount: TOKEN_FEE_AMOUNT }, // No addressOrDenom = native
784
+ });
785
+
786
+ const arbitrumToken = {
787
+ chainName: ARBITRUM_CHAIN,
788
+ standard: TokenStandard.EvmHypNative,
789
+ getHypAdapter: Sinon.stub().returns(adapterStub),
790
+ };
791
+ const solanaToken = {
792
+ chainName: SOLANA_CHAIN,
793
+ standard: TokenStandard.EvmHypNative,
794
+ getHypAdapter: Sinon.stub().returns(adapterStub),
795
+ };
796
+ warpCore.tokens = [arbitrumToken, solanaToken];
797
+
798
+ const requestedAmount = 10000000000000000n; // 0.01 ETH
799
+ // Available = requested + IGP + gas + tokenFee (enough for full transfer)
800
+ const availableInventory =
801
+ requestedAmount + TOTAL_RESERVATION + TOKEN_FEE_AMOUNT;
802
+
803
+ const route = createTestRoute({ amount: requestedAmount });
804
+ createTestIntent({ amount: requestedAmount });
805
+
806
+ inventoryRebalancer.setInventoryBalances({
807
+ [SOLANA_CHAIN]: availableInventory,
808
+ [ARBITRUM_CHAIN]: 0n,
809
+ });
810
+
811
+ const results = await inventoryRebalancer.rebalance([route]);
812
+
813
+ expect(results).to.have.lengthOf(1);
814
+ expect(results[0].success).to.be.true;
815
+ // Verify tokenFee was deducted from maxTransferable
816
+ const populateParams =
817
+ adapterStub.populateTransferRemoteTx.lastCall.args[0];
818
+ expect(populateParams.weiAmountOrId).to.equal(requestedAmount);
819
+ });
820
+
821
+ it('deducts tokenFeeQuote when addressOrDenom is zero address', async () => {
822
+ // Setup: Native token with tokenFeeQuote and zero-address denom (isZeroishAddress)
823
+ const zeroAddressAdapter = {
824
+ quoteTransferRemoteGas: Sinon.stub().resolves({
825
+ igpQuote: { amount: IGP_COST },
826
+ tokenFeeQuote: {
827
+ addressOrDenom: '0x0000000000000000000000000000000000000000',
828
+ amount: TOKEN_FEE_AMOUNT,
829
+ },
830
+ }),
831
+ populateTransferRemoteTx: Sinon.stub().resolves({
832
+ to: '0xRouterAddress',
833
+ data: '0xTransferRemoteData',
834
+ value: 1000000n,
835
+ }),
836
+ };
837
+
838
+ const arbitrumToken = {
839
+ chainName: ARBITRUM_CHAIN,
840
+ standard: TokenStandard.EvmHypNative,
841
+ getHypAdapter: Sinon.stub().returns(zeroAddressAdapter),
842
+ };
843
+ const solanaToken = {
844
+ chainName: SOLANA_CHAIN,
845
+ standard: TokenStandard.EvmHypNative,
846
+ getHypAdapter: Sinon.stub().returns(zeroAddressAdapter),
847
+ };
848
+ warpCore.tokens = [arbitrumToken, solanaToken];
849
+
850
+ const requestedAmount = 10000000000000000n;
851
+ const availableInventory =
852
+ requestedAmount + TOTAL_RESERVATION + TOKEN_FEE_AMOUNT;
853
+
854
+ const route = createTestRoute({ amount: requestedAmount });
855
+ createTestIntent({ amount: requestedAmount });
856
+
857
+ inventoryRebalancer.setInventoryBalances({
858
+ [SOLANA_CHAIN]: availableInventory,
859
+ [ARBITRUM_CHAIN]: 0n,
860
+ });
861
+
862
+ const results = await inventoryRebalancer.rebalance([route]);
863
+
864
+ expect(results).to.have.lengthOf(1);
865
+ expect(results[0].success).to.be.true;
866
+ // Verify tokenFee was deducted (zero address is treated as native)
867
+ const populateParams =
868
+ zeroAddressAdapter.populateTransferRemoteTx.lastCall.args[0];
869
+ expect(populateParams.weiAmountOrId).to.equal(requestedAmount);
870
+ });
871
+
872
+ it('does NOT deduct tokenFeeQuote when addressOrDenom is ERC20 address', async () => {
873
+ // Setup: Native token with tokenFeeQuote but ERC20 denom (not native)
874
+ const erc20Address = '0x1234567890abcdef1234567890abcdef12345678';
875
+ const erc20Adapter = {
876
+ quoteTransferRemoteGas: Sinon.stub().resolves({
877
+ igpQuote: { amount: IGP_COST },
878
+ tokenFeeQuote: {
879
+ addressOrDenom: erc20Address,
880
+ amount: TOKEN_FEE_AMOUNT,
881
+ },
882
+ }),
883
+ populateTransferRemoteTx: Sinon.stub().resolves({
884
+ to: '0xRouterAddress',
885
+ data: '0xTransferRemoteData',
886
+ value: 1000000n,
887
+ }),
888
+ };
889
+
890
+ const arbitrumToken = {
891
+ chainName: ARBITRUM_CHAIN,
892
+ standard: TokenStandard.EvmHypNative,
893
+ getHypAdapter: Sinon.stub().returns(erc20Adapter),
894
+ };
895
+ const solanaToken = {
896
+ chainName: SOLANA_CHAIN,
897
+ standard: TokenStandard.EvmHypNative,
898
+ getHypAdapter: Sinon.stub().returns(erc20Adapter),
899
+ };
900
+ warpCore.tokens = [arbitrumToken, solanaToken];
901
+
902
+ const requestedAmount = 10000000000000000n;
903
+ // Available = requested + IGP + gas (NO tokenFee deduction since it's ERC20)
904
+ const availableInventory = requestedAmount + TOTAL_RESERVATION;
905
+
906
+ const route = createTestRoute({ amount: requestedAmount });
907
+ createTestIntent({ amount: requestedAmount });
908
+
909
+ inventoryRebalancer.setInventoryBalances({
910
+ [SOLANA_CHAIN]: availableInventory,
911
+ [ARBITRUM_CHAIN]: 0n,
912
+ });
913
+
914
+ const results = await inventoryRebalancer.rebalance([route]);
915
+
916
+ expect(results).to.have.lengthOf(1);
917
+ expect(results[0].success).to.be.true;
918
+ // Verify tokenFee was NOT deducted (ERC20 denom, not native)
919
+ const populateParams =
920
+ erc20Adapter.populateTransferRemoteTx.lastCall.args[0];
921
+ expect(populateParams.weiAmountOrId).to.equal(requestedAmount);
922
+ });
923
+
924
+ it('handles undefined tokenFeeQuote (backward compatibility with v<10)', async () => {
925
+ // Setup: Native token without tokenFeeQuote (old contract version)
926
+ const oldAdapter = {
927
+ quoteTransferRemoteGas: Sinon.stub().resolves({
928
+ igpQuote: { amount: IGP_COST },
929
+ // No tokenFeeQuote field
930
+ }),
931
+ populateTransferRemoteTx: Sinon.stub().resolves({
932
+ to: '0xRouterAddress',
933
+ data: '0xTransferRemoteData',
934
+ value: 1000000n,
935
+ }),
936
+ };
937
+
938
+ const arbitrumToken = {
939
+ chainName: ARBITRUM_CHAIN,
940
+ standard: TokenStandard.EvmHypNative,
941
+ getHypAdapter: Sinon.stub().returns(oldAdapter),
942
+ };
943
+ const solanaToken = {
944
+ chainName: SOLANA_CHAIN,
945
+ standard: TokenStandard.EvmHypNative,
946
+ getHypAdapter: Sinon.stub().returns(oldAdapter),
947
+ };
948
+ warpCore.tokens = [arbitrumToken, solanaToken];
949
+
950
+ const requestedAmount = 10000000000000000n;
951
+ // Available = requested + IGP + gas (no tokenFee since undefined)
952
+ const availableInventory = requestedAmount + TOTAL_RESERVATION;
953
+
954
+ const route = createTestRoute({ amount: requestedAmount });
955
+ createTestIntent({ amount: requestedAmount });
956
+
957
+ inventoryRebalancer.setInventoryBalances({
958
+ [SOLANA_CHAIN]: availableInventory,
959
+ [ARBITRUM_CHAIN]: 0n,
960
+ });
961
+
962
+ const results = await inventoryRebalancer.rebalance([route]);
963
+
964
+ expect(results).to.have.lengthOf(1);
965
+ expect(results[0].success).to.be.true;
966
+ // Verify transfer succeeded without tokenFee deduction
967
+ const populateParams =
968
+ oldAdapter.populateTransferRemoteTx.lastCall.args[0];
969
+ expect(populateParams.weiAmountOrId).to.equal(requestedAmount);
970
+ });
971
+
972
+ it('reduces maxTransferable when large tokenFeeQuote is present', async () => {
973
+ // Setup: Native token with large tokenFeeQuote that significantly reduces maxTransferable
974
+ adapterStub.quoteTransferRemoteGas.resolves({
975
+ igpQuote: { amount: IGP_COST },
976
+ tokenFeeQuote: { amount: 5000000000000000n }, // Large fee (5e15 wei)
977
+ });
978
+
979
+ const arbitrumToken = {
980
+ chainName: ARBITRUM_CHAIN,
981
+ standard: TokenStandard.EvmHypNative,
982
+ getHypAdapter: Sinon.stub().returns(adapterStub),
983
+ };
984
+ const solanaToken = {
985
+ chainName: SOLANA_CHAIN,
986
+ standard: TokenStandard.EvmHypNative,
987
+ getHypAdapter: Sinon.stub().returns(adapterStub),
988
+ };
989
+ warpCore.tokens = [arbitrumToken, solanaToken];
990
+
991
+ const requestedAmount = 10000000000000000n; // 0.01 ETH
992
+ // Available = requested + IGP + gas + large tokenFee
993
+ const largeTokenFee = 5000000000000000n;
994
+ const availableInventory =
995
+ requestedAmount + TOTAL_RESERVATION + largeTokenFee;
996
+
997
+ const route = createTestRoute({ amount: requestedAmount });
998
+ createTestIntent({ amount: requestedAmount });
999
+
1000
+ inventoryRebalancer.setInventoryBalances({
1001
+ [SOLANA_CHAIN]: availableInventory,
1002
+ [ARBITRUM_CHAIN]: 0n,
1003
+ });
1004
+
1005
+ const results = await inventoryRebalancer.rebalance([route]);
1006
+
1007
+ // Verify: Transfer succeeds with full amount (we have enough for amount + all costs)
1008
+ // This demonstrates that tokenFeeQuote is properly deducted from maxTransferable
1009
+ expect(results).to.have.lengthOf(1);
1010
+ expect(results[0].success).to.be.true;
1011
+ });
1012
+ });
1013
+
1014
+ describe('LiFi Bridge Integration (for future inventory_movement)', () => {
1015
+ // These tests validate the mock utilities are working correctly
1016
+ // and prepare for when inventory_movement is implemented
1017
+
1018
+ it('mock utilities create valid quote', () => {
1019
+ const quote = createMockBridgeQuote({
1020
+ fromAmount: 10000000000n,
1021
+ toAmount: 9950000000n,
1022
+ });
1023
+
1024
+ expect(quote.id).to.equal('quote-123');
1025
+ expect(quote.tool).to.equal('across');
1026
+ expect(quote.fromAmount).to.equal(10000000000n);
1027
+ expect(quote.toAmount).to.equal(9950000000n);
1028
+ });
1029
+
1030
+ it('bridge mock can be configured for quote', async () => {
1031
+ bridge.quote.resolves(
1032
+ createMockBridgeQuote({
1033
+ fromAmount: 10000000000n,
1034
+ toAmount: 9950000000n,
1035
+ }),
1036
+ );
1037
+
1038
+ const quote = await bridge.quote({
1039
+ fromChain: ARBITRUM_DOMAIN,
1040
+ toChain: SOLANA_DOMAIN,
1041
+ fromToken: '0xUSDC',
1042
+ toToken: '0xUSDC',
1043
+ fromAmount: 10000000000n,
1044
+ fromAddress: INVENTORY_SIGNER,
1045
+ });
1046
+
1047
+ expect(quote.fromAmount).to.equal(10000000000n);
1048
+ expect(quote.toAmount).to.equal(9950000000n);
1049
+ });
1050
+ });
1051
+
1052
+ describe('Smart Partial Transfer Threshold', () => {
1053
+ // Test the 90% consolidation threshold for partial transfers
1054
+ // Tests use non-native tokens (EvmHypCollateral) so minViableTransfer = 0
1055
+
1056
+ it('does partial transfer when inventory is available on destination', async () => {
1057
+ // amount = 1 ETH, availableOnDestination = 0.5 ETH
1058
+ // With simplified logic: if maxTransferable >= minViableTransfer, do partial transfer
1059
+ // For non-native tokens, minViableTransfer = 0, so partial transfer happens
1060
+ const amount = BigInt(1e18); // 1 ETH
1061
+ const availableOnDestination = BigInt(0.5e18); // 0.5 ETH on destination
1062
+
1063
+ const route = createTestRoute({ amount });
1064
+ createTestIntent({ amount });
1065
+
1066
+ // Inventory on destination (SOLANA)
1067
+ inventoryRebalancer.setInventoryBalances({
1068
+ [SOLANA_CHAIN]: availableOnDestination,
1069
+ [ARBITRUM_CHAIN]: 0n,
1070
+ });
1071
+
1072
+ const results = await inventoryRebalancer.rebalance([route]);
1073
+
1074
+ expect(results).to.have.lengthOf(1);
1075
+ expect(results[0].success).to.be.true;
1076
+
1077
+ // Verify: transferRemote WAS called (partial transfer happened)
1078
+ const populateParams =
1079
+ adapterStub.populateTransferRemoteTx.lastCall.args[0];
1080
+ expect(populateParams.weiAmountOrId).to.equal(availableOnDestination);
1081
+
1082
+ // Verify: Bridge was NOT called (no need to bridge when partial transfer is viable)
1083
+ expect(bridge.execute.called).to.be.false;
1084
+ });
1085
+
1086
+ it('does partial transfer when maxTransferable >= minViableTransfer', async () => {
1087
+ // For non-native tokens (EvmHypCollateral), minViableTransfer = 0
1088
+ // So any positive maxTransferable triggers partial transfer
1089
+ const amount = BigInt(2e18); // 2 ETH requested
1090
+ const maxTransferable = BigInt(0.6e18); // 0.6 ETH available
1091
+
1092
+ const route = createTestRoute({ amount });
1093
+ createTestIntent({ amount });
1094
+
1095
+ // 0.6 ETH available on destination
1096
+ inventoryRebalancer.setInventoryBalances({
1097
+ [SOLANA_CHAIN]: maxTransferable,
1098
+ [ARBITRUM_CHAIN]: 0n,
1099
+ });
1100
+
1101
+ const results = await inventoryRebalancer.rebalance([route]);
1102
+
1103
+ expect(results).to.have.lengthOf(1);
1104
+ expect(results[0].success).to.be.true;
1105
+
1106
+ // Verify: transferRemote WAS called with partial amount
1107
+ const populateParams =
1108
+ adapterStub.populateTransferRemoteTx.lastCall.args[0];
1109
+ expect(populateParams.weiAmountOrId).to.equal(maxTransferable);
1110
+ });
1111
+
1112
+ it('does NOT do partial transfer when maxTransferable < minViableTransfer (native tokens)', async () => {
1113
+ // For native tokens (EvmHypNative), minViableTransfer = totalCost * 2
1114
+ // When available inventory minus costs is below minViableTransfer, falls through to bridging
1115
+ const arbitrumToken = {
1116
+ chainName: ARBITRUM_CHAIN,
1117
+ standard: TokenStandard.EvmHypNative,
1118
+ getHypAdapter: Sinon.stub().returns(adapterStub),
1119
+ };
1120
+ const solanaToken = {
1121
+ chainName: SOLANA_CHAIN,
1122
+ standard: TokenStandard.EvmHypNative,
1123
+ getHypAdapter: Sinon.stub().returns(adapterStub),
1124
+ };
1125
+ warpCore.tokens = [arbitrumToken, solanaToken];
1126
+
1127
+ const amount = BigInt(2e18); // 2 ETH
1128
+ // Available inventory is small - after subtracting costs, maxTransferable < minViableTransfer
1129
+ // minViableTransfer = ~0.0066 ETH (totalCost * 2)
1130
+ // If available = 0.005 ETH, after costs ~0, maxTransferable < minViableTransfer
1131
+ const availableOnDestination = BigInt(0.003e18); // 0.003 ETH - below minViableTransfer
1132
+ const availableOnSource = BigInt(0.6e18); // 0.6 ETH on ARBITRUM
1133
+
1134
+ const route = createTestRoute({ amount });
1135
+ createTestIntent({ amount });
1136
+
1137
+ // Inventory on destination - too small after costs, but source has inventory
1138
+ inventoryRebalancer.setInventoryBalances({
1139
+ [SOLANA_CHAIN]: availableOnDestination,
1140
+ [ARBITRUM_CHAIN]: availableOnSource,
1141
+ });
1142
+
1143
+ // Mock bridge
1144
+ bridge.quote.resolves(
1145
+ createMockBridgeQuote({
1146
+ fromAmount: BigInt(0.5e18),
1147
+ toAmount: BigInt(0.48e18),
1148
+ }),
1149
+ );
1150
+ bridge.execute.resolves({
1151
+ txHash: '0xBridgeTxHash',
1152
+ fromChain: 42161,
1153
+ toChain: 1399811149,
1154
+ });
1155
+
1156
+ const results = await inventoryRebalancer.rebalance([route]);
1157
+
1158
+ expect(results).to.have.lengthOf(1);
1159
+ expect(results[0].success).to.be.true;
1160
+
1161
+ // Verify: inventory movement via bridge happened (NOT partial transferRemote)
1162
+ expect(bridge.execute.called).to.be.true;
1163
+ });
1164
+ });
1165
+
1166
+ describe('Early Exit for Small Amounts', () => {
1167
+ // Gas estimation: 300,000 gas × 10 gwei = 3,000,000,000,000 wei
1168
+ // Buffered gas limit (10%): 330,000 gas
1169
+ // Buffered gas cost: 330,000 × 10 gwei = 3,300,000,000,000 wei
1170
+ // IGP quote: 1,000,000 wei
1171
+ // Total cost: IGP + buffered gas = 3,300,001,000,000 wei (~0.0033 ETH)
1172
+ // Min viable transfer (2x total cost): ~0.0066 ETH
1173
+ const MIN_VIABLE = BigInt(6.6e12); // ~0.0066 ETH
1174
+
1175
+ it('completes intent when amount < minViableTransfer', async () => {
1176
+ // Use native token to get non-zero minViableTransfer
1177
+ const arbitrumToken = {
1178
+ chainName: ARBITRUM_CHAIN,
1179
+ standard: TokenStandard.EvmHypNative,
1180
+ getHypAdapter: Sinon.stub().returns(adapterStub),
1181
+ };
1182
+ const solanaToken = {
1183
+ chainName: SOLANA_CHAIN,
1184
+ standard: TokenStandard.EvmHypNative,
1185
+ getHypAdapter: Sinon.stub().returns(adapterStub),
1186
+ };
1187
+ warpCore.tokens = [arbitrumToken, solanaToken];
1188
+
1189
+ // Amount smaller than minViableTransfer
1190
+ const smallAmount = MIN_VIABLE / 2n; // 0.0033 ETH < minViable 0.0066 ETH
1191
+ const route = createTestRoute({ amount: smallAmount });
1192
+ createTestIntent({ amount: smallAmount });
1193
+
1194
+ // Even with plenty of inventory, small amount triggers early exit
1195
+ inventoryRebalancer.setInventoryBalances({
1196
+ [SOLANA_CHAIN]: BigInt(10e18),
1197
+ [ARBITRUM_CHAIN]: BigInt(10e18),
1198
+ });
1199
+
1200
+ const results = await inventoryRebalancer.rebalance([route]);
1201
+
1202
+ expect(results).to.have.lengthOf(1);
1203
+ expect(results[0].success).to.be.true;
1204
+ expect((results[0] as any).reason).to.equal(
1205
+ 'completed_with_acceptable_loss',
1206
+ );
1207
+
1208
+ // Verify: Intent was completed (not left in progress)
1209
+ expect(actionTracker.completeRebalanceIntent.calledOnce).to.be.true;
1210
+ expect(actionTracker.completeRebalanceIntent.calledWith('intent-1')).to.be
1211
+ .true;
1212
+
1213
+ // Verify: No transferRemote was attempted
1214
+ expect(actionTracker.createRebalanceAction.called).to.be.false;
1215
+ });
1216
+ });
1217
+
1218
+ describe('Parallel Multi-Source Bridging', () => {
1219
+ // Third chain for multi-source tests
1220
+ const BASE_CHAIN = 'base' as ChainName;
1221
+ const BASE_DOMAIN = 8453;
1222
+
1223
+ beforeEach(() => {
1224
+ // Extend multiProvider to handle BASE_CHAIN
1225
+ multiProvider.getDomainId.callsFake((chain: ChainName) => {
1226
+ if (chain === ARBITRUM_CHAIN) return ARBITRUM_DOMAIN;
1227
+ if (chain === SOLANA_CHAIN) return SOLANA_DOMAIN;
1228
+ if (chain === BASE_CHAIN) return BASE_DOMAIN;
1229
+ return 0;
1230
+ });
1231
+
1232
+ multiProvider.getChainId = Sinon.stub().callsFake((chain: ChainName) => {
1233
+ if (chain === ARBITRUM_CHAIN) return 42161;
1234
+ if (chain === SOLANA_CHAIN) return 1399811149;
1235
+ if (chain === BASE_CHAIN) return 8453;
1236
+ return 0;
1237
+ });
1238
+
1239
+ // Add token for BASE_CHAIN
1240
+ const baseToken = {
1241
+ chainName: BASE_CHAIN,
1242
+ standard: TokenStandard.EvmHypCollateral,
1243
+ getHypAdapter: Sinon.stub().returns(adapterStub),
1244
+ addressOrDenom: '0xBaseToken',
1245
+ };
1246
+ warpCore.tokens.push(baseToken);
1247
+
1248
+ multiProvider.getSigner = Sinon.stub().returns(TEST_WALLET);
1249
+ });
1250
+
1251
+ it('bridges from multiple sources in parallel', async () => {
1252
+ // Need 1 ETH on solana, have 0.6 ETH on arbitrum and 0.6 ETH on base
1253
+ const amount = BigInt(1e18);
1254
+ const perChainInventory = BigInt(0.6e18);
1255
+
1256
+ const route = createTestRoute({ amount });
1257
+ createTestIntent({ amount });
1258
+
1259
+ // No inventory on destination, inventory on sources
1260
+ inventoryRebalancer.setInventoryBalances({
1261
+ [SOLANA_CHAIN]: 0n,
1262
+ [ARBITRUM_CHAIN]: perChainInventory,
1263
+ [BASE_CHAIN]: perChainInventory,
1264
+ });
1265
+
1266
+ // Mock bridge quotes and execution
1267
+ bridge.quote.resolves(
1268
+ createMockBridgeQuote({
1269
+ fromAmount: BigInt(0.55e18),
1270
+ toAmount: BigInt(0.525e18),
1271
+ }),
1272
+ );
1273
+ bridge.execute.resolves({
1274
+ txHash: '0xBridgeTxHash',
1275
+ fromChain: 42161,
1276
+ toChain: 1399811149,
1277
+ });
1278
+
1279
+ const results = await inventoryRebalancer.rebalance([route]);
1280
+
1281
+ expect(results).to.have.lengthOf(1);
1282
+ expect(results[0].success).to.be.true;
1283
+
1284
+ // Verify: Bridge was called twice (once for each source)
1285
+ expect(bridge.execute.callCount).to.equal(2);
1286
+ });
1287
+
1288
+ it('applies 5% buffer to total bridge amount', async () => {
1289
+ // Need 1 ETH -> should plan to bridge 1.05 ETH total (with 5% buffer)
1290
+ const amount = BigInt(1e18); // 1 ETH
1291
+ const availableInventory = BigInt(2e18); // 2 ETH on source
1292
+
1293
+ const route = createTestRoute({ amount });
1294
+ createTestIntent({ amount });
1295
+
1296
+ // No inventory on destination, plenty on source
1297
+ inventoryRebalancer.setInventoryBalances({
1298
+ [SOLANA_CHAIN]: 0n,
1299
+ [ARBITRUM_CHAIN]: availableInventory,
1300
+ });
1301
+
1302
+ // Capture the quote amount from executeInventoryMovement
1303
+ // (calculateMaxViableBridgeAmount doesn't quote for ERC20 tokens)
1304
+ let quotedFromAmount: bigint | undefined;
1305
+ bridge.quote.callsFake(async (params: any) => {
1306
+ quotedFromAmount = params.fromAmount;
1307
+ return createMockBridgeQuote({
1308
+ fromAmount: params.fromAmount ?? params.toAmount,
1309
+ toAmount: params.fromAmount ?? params.toAmount,
1310
+ });
1311
+ });
1312
+ bridge.execute.resolves({
1313
+ txHash: '0xBridgeTxHash',
1314
+ fromChain: 42161,
1315
+ toChain: 1399811149,
1316
+ });
1317
+
1318
+ await inventoryRebalancer.rebalance([route]);
1319
+
1320
+ // Verify: 5% buffer applied (1 ETH * 1.05 = 1.05 ETH)
1321
+ // The bridge plan uses pre-validated amounts (for ERC20, full inventory available)
1322
+ // But the target is (amount * 105%), so if source has >= target, we bridge exactly target
1323
+ const expectedWithBuffer = (amount * 105n) / 100n;
1324
+ expect(quotedFromAmount).to.equal(expectedWithBuffer);
1325
+ });
1326
+
1327
+ it('continues when some bridges fail', async () => {
1328
+ const amount = BigInt(1e18);
1329
+ const perChainInventory = BigInt(0.6e18);
1330
+
1331
+ const route = createTestRoute({ amount });
1332
+ createTestIntent({ amount });
1333
+
1334
+ // No inventory on destination, inventory on sources
1335
+ inventoryRebalancer.setInventoryBalances({
1336
+ [SOLANA_CHAIN]: 0n,
1337
+ [ARBITRUM_CHAIN]: perChainInventory,
1338
+ [BASE_CHAIN]: perChainInventory,
1339
+ });
1340
+
1341
+ bridge.quote.resolves(
1342
+ createMockBridgeQuote({
1343
+ fromAmount: BigInt(0.55e18),
1344
+ toAmount: BigInt(0.525e18),
1345
+ }),
1346
+ );
1347
+
1348
+ // First bridge succeeds, second fails
1349
+ bridge.execute
1350
+ .onFirstCall()
1351
+ .resolves({
1352
+ txHash: '0xSuccessTxHash',
1353
+ fromChain: 42161,
1354
+ toChain: 1399811149,
1355
+ })
1356
+ .onSecondCall()
1357
+ .rejects(new Error('Bridge execution failed'));
1358
+
1359
+ const results = await inventoryRebalancer.rebalance([route]);
1360
+
1361
+ // Verify: Overall success (at least one bridge succeeded)
1362
+ expect(results).to.have.lengthOf(1);
1363
+ expect(results[0].success).to.be.true;
1364
+ });
1365
+
1366
+ it('returns failure when all bridges fail', async () => {
1367
+ const amount = BigInt(1e18);
1368
+ const perChainInventory = BigInt(0.6e18);
1369
+
1370
+ const route = createTestRoute({ amount });
1371
+ createTestIntent({ amount });
1372
+
1373
+ // No inventory on destination, inventory on sources
1374
+ inventoryRebalancer.setInventoryBalances({
1375
+ [SOLANA_CHAIN]: 0n,
1376
+ [ARBITRUM_CHAIN]: perChainInventory,
1377
+ [BASE_CHAIN]: perChainInventory,
1378
+ });
1379
+
1380
+ bridge.quote.resolves(
1381
+ createMockBridgeQuote({
1382
+ fromAmount: BigInt(0.55e18),
1383
+ toAmount: BigInt(0.525e18),
1384
+ }),
1385
+ );
1386
+
1387
+ // All bridges fail
1388
+ bridge.execute.rejects(new Error('Bridge execution failed'));
1389
+
1390
+ const results = await inventoryRebalancer.rebalance([route]);
1391
+
1392
+ // Verify: Failure when all bridges fail
1393
+ expect(results).to.have.lengthOf(1);
1394
+ expect(results[0].success).to.be.false;
1395
+ expect(results[0].error).to.include('All inventory movements failed');
1396
+ });
1397
+ });
1398
+
1399
+ describe('Bridge Viability Check', () => {
1400
+ // Tests for the gas-aware planning approach that prevents "insufficient funds for gas * price + value" errors
1401
+ // by calculating max viable bridge amounts BEFORE creating bridge plans.
1402
+ // Uses calculateMaxViableBridgeAmount which:
1403
+ // 1. Gets a quote to determine gas costs
1404
+ // 2. Applies 20x multiplier on quoted gas (LiFi underestimates)
1405
+ // 3. Returns 0 if gas exceeds 10% of inventory (not economically viable)
1406
+
1407
+ it('filters out sources where gas cost exceeds 10% of inventory', async () => {
1408
+ // Setup: Native token bridge where gas cost is too high relative to balance
1409
+ // Scenario: Arbitrum has 0.00219 ETH, estimated gas (with 20x buffer) exceeds 10% threshold
1410
+ const arbitrumToken = {
1411
+ chainName: ARBITRUM_CHAIN,
1412
+ standard: TokenStandard.EvmHypNative, // Native token for gas check
1413
+ addressOrDenom: '0xArbitrumNative',
1414
+ getHypAdapter: Sinon.stub().returns(adapterStub),
1415
+ };
1416
+ const solanaToken = {
1417
+ chainName: SOLANA_CHAIN,
1418
+ standard: TokenStandard.EvmHypNative,
1419
+ addressOrDenom: '0xSolanaNative',
1420
+ getHypAdapter: Sinon.stub().returns(adapterStub),
1421
+ };
1422
+ warpCore.tokens = [arbitrumToken, solanaToken];
1423
+
1424
+ const amount = BigInt(1e18); // 1 ETH requested on destination
1425
+
1426
+ const route = createTestRoute({ amount });
1427
+ createTestIntent({ amount });
1428
+
1429
+ // Raw balance on source chain (ARBITRUM) - the limiting factor
1430
+ const rawBalance = BigInt('2194632084196208'); // ~0.00219 ETH
1431
+
1432
+ // No inventory on destination, low balance on source
1433
+ inventoryRebalancer.setInventoryBalances({
1434
+ [SOLANA_CHAIN]: 0n,
1435
+ [ARBITRUM_CHAIN]: rawBalance,
1436
+ });
1437
+
1438
+ // Mock quote with gas costs that exceed 10% threshold when multiplied by 20
1439
+ // rawBalance = 0.00219 ETH
1440
+ // 10% threshold = 0.000219 ETH
1441
+ // gasCosts = 0.00005 ETH, estimated = 0.001 ETH (20x multiplier)
1442
+ // 0.001 > 0.000219 → not viable
1443
+ bridge.quote.resolves(
1444
+ createMockBridgeQuote({
1445
+ fromAmount: rawBalance,
1446
+ toAmount: rawBalance - BigInt(1e14), // Some output
1447
+ gasCosts: BigInt('50000000000000'), // 0.00005 ETH, becomes 0.001 ETH with 20x
1448
+ feeCosts: 0n,
1449
+ }),
1450
+ );
1451
+
1452
+ const results = await inventoryRebalancer.rebalance([route]);
1453
+
1454
+ // Verify: Should fail because no sources pass viability check at planning time
1455
+ expect(results).to.have.lengthOf(1);
1456
+ expect(results[0].success).to.be.false;
1457
+ // New behavior: Error is "No viable bridge sources" (filtered at planning time)
1458
+ // rather than "Insufficient funds" (which was at execution time)
1459
+ expect(results[0].error).to.include('No viable bridge sources');
1460
+
1461
+ // Verify: Bridge.execute should NOT have been called (filtered during planning)
1462
+ expect(bridge.execute.called).to.be.false;
1463
+ });
1464
+
1465
+ it('proceeds with bridge when total cost is within available balance', async () => {
1466
+ // Setup: Native token bridge where we have enough balance
1467
+ const arbitrumToken = {
1468
+ chainName: ARBITRUM_CHAIN,
1469
+ standard: TokenStandard.EvmHypNative,
1470
+ addressOrDenom: '0xArbitrumNative',
1471
+ getHypAdapter: Sinon.stub().returns(adapterStub),
1472
+ };
1473
+ const solanaToken = {
1474
+ chainName: SOLANA_CHAIN,
1475
+ standard: TokenStandard.EvmHypNative,
1476
+ addressOrDenom: '0xSolanaNative',
1477
+ getHypAdapter: Sinon.stub().returns(adapterStub),
1478
+ };
1479
+ warpCore.tokens = [arbitrumToken, solanaToken];
1480
+
1481
+ const amount = BigInt(1e18); // 1 ETH requested
1482
+
1483
+ const route = createTestRoute({ amount });
1484
+ createTestIntent({ amount });
1485
+
1486
+ // Plenty of balance on source chain
1487
+ const rawBalance = BigInt(2e18); // 2 ETH - more than enough
1488
+
1489
+ inventoryRebalancer.setInventoryBalances({
1490
+ [SOLANA_CHAIN]: 0n,
1491
+ [ARBITRUM_CHAIN]: rawBalance,
1492
+ });
1493
+
1494
+ // Quote with reasonable costs well under the balance
1495
+ bridge.quote.resolves(
1496
+ createMockBridgeQuote({
1497
+ fromAmount: BigInt(1.05e18), // 1.05 ETH (with buffer)
1498
+ toAmount: BigInt(1e18),
1499
+ gasCosts: BigInt(1e15), // 0.001 ETH gas
1500
+ feeCosts: 0n,
1501
+ }),
1502
+ );
1503
+
1504
+ // Mock successful execution
1505
+ bridge.execute.resolves({
1506
+ txHash: '0xSuccessBridgeTxHash',
1507
+ fromChain: 42161,
1508
+ toChain: 1399811149,
1509
+ });
1510
+
1511
+ const results = await inventoryRebalancer.rebalance([route]);
1512
+
1513
+ // Verify: Should succeed
1514
+ expect(results).to.have.lengthOf(1);
1515
+ expect(results[0].success).to.be.true;
1516
+
1517
+ // Verify: Bridge.execute WAS called (not abandoned)
1518
+ expect(bridge.execute.calledOnce).to.be.true;
1519
+ });
1520
+
1521
+ it('viability check only applies to native tokens (not ERC20)', async () => {
1522
+ // Setup: ERC20 token bridge - gas is paid separately in ETH, not from token balance
1523
+ const arbitrumToken = {
1524
+ chainName: ARBITRUM_CHAIN,
1525
+ standard: TokenStandard.EvmHypCollateral, // ERC20, not native
1526
+ addressOrDenom: '0xArbitrumToken',
1527
+ getHypAdapter: Sinon.stub().returns(adapterStub),
1528
+ };
1529
+ const solanaToken = {
1530
+ chainName: SOLANA_CHAIN,
1531
+ standard: TokenStandard.EvmHypCollateral,
1532
+ addressOrDenom: '0xSolanaToken',
1533
+ getHypAdapter: Sinon.stub().returns(adapterStub),
1534
+ };
1535
+ warpCore.tokens = [arbitrumToken, solanaToken];
1536
+
1537
+ const amount = BigInt(1e18); // 1 token
1538
+ const route = createTestRoute({ amount });
1539
+ createTestIntent({ amount });
1540
+
1541
+ // Small token balance (but gas is paid in ETH, so this shouldn't matter for viability)
1542
+ const tokenBalance = BigInt(1e18);
1543
+
1544
+ inventoryRebalancer.setInventoryBalances({
1545
+ [SOLANA_CHAIN]: 0n,
1546
+ [ARBITRUM_CHAIN]: tokenBalance,
1547
+ });
1548
+
1549
+ // Quote with high gas costs (but for ERC20, this shouldn't trigger viability check)
1550
+ bridge.quote.resolves(
1551
+ createMockBridgeQuote({
1552
+ fromAmount: BigInt(1.05e18),
1553
+ toAmount: BigInt(1e18),
1554
+ gasCosts: BigInt(1e18), // Very high gas (would fail viability if this were native)
1555
+ feeCosts: 0n,
1556
+ }),
1557
+ );
1558
+
1559
+ bridge.execute.resolves({
1560
+ txHash: '0xERC20BridgeTxHash',
1561
+ fromChain: 42161,
1562
+ toChain: 1399811149,
1563
+ });
1564
+
1565
+ const results = await inventoryRebalancer.rebalance([route]);
1566
+
1567
+ // Verify: Should succeed because ERC20 viability check doesn't include gasCosts
1568
+ expect(results).to.have.lengthOf(1);
1569
+ expect(results[0].success).to.be.true;
1570
+ expect(bridge.execute.calledOnce).to.be.true;
1571
+ });
1572
+
1573
+ it('calculates max viable amount as inventory minus (gasCosts * 20) for native tokens', async () => {
1574
+ // Setup: Native token with enough balance for viable bridge
1575
+ const arbitrumToken = {
1576
+ chainName: ARBITRUM_CHAIN,
1577
+ standard: TokenStandard.EvmHypNative,
1578
+ addressOrDenom: '0xArbitrumNative',
1579
+ getHypAdapter: Sinon.stub().returns(adapterStub),
1580
+ };
1581
+ const solanaToken = {
1582
+ chainName: SOLANA_CHAIN,
1583
+ standard: TokenStandard.EvmHypNative,
1584
+ addressOrDenom: '0xSolanaNative',
1585
+ getHypAdapter: Sinon.stub().returns(adapterStub),
1586
+ };
1587
+ warpCore.tokens = [arbitrumToken, solanaToken];
1588
+
1589
+ const amount = BigInt(0.5e18); // 0.5 ETH requested
1590
+
1591
+ const route = createTestRoute({ amount });
1592
+ createTestIntent({ amount });
1593
+
1594
+ // Large balance - should be viable
1595
+ const rawBalance = BigInt(1e18); // 1 ETH
1596
+
1597
+ inventoryRebalancer.setInventoryBalances({
1598
+ [SOLANA_CHAIN]: 0n,
1599
+ [ARBITRUM_CHAIN]: rawBalance,
1600
+ });
1601
+
1602
+ // gasCosts = 0.001 ETH, estimated = 0.02 ETH (20x multiplier)
1603
+ // maxViable = 1 ETH - 0.02 ETH = 0.98 ETH
1604
+ // 10% threshold = 0.1 ETH, estimatedGas (0.02) < threshold → viable
1605
+ const gasCosts = BigInt(0.001e18); // 0.001 ETH
1606
+ bridge.quote.resolves(
1607
+ createMockBridgeQuote({
1608
+ fromAmount: rawBalance,
1609
+ toAmount: rawBalance - BigInt(1e15),
1610
+ gasCosts,
1611
+ feeCosts: 0n,
1612
+ }),
1613
+ );
1614
+
1615
+ bridge.execute.resolves({
1616
+ txHash: '0xSuccessBridgeTxHash',
1617
+ fromChain: 42161,
1618
+ toChain: 1399811149,
1619
+ });
1620
+
1621
+ const results = await inventoryRebalancer.rebalance([route]);
1622
+
1623
+ // Verify: Should succeed - bridge is viable
1624
+ expect(results).to.have.lengthOf(1);
1625
+ expect(results[0].success).to.be.true;
1626
+ expect(bridge.execute.calledOnce).to.be.true;
1627
+
1628
+ // Verify: The quoted fromAmount should be the target (since maxViable > target)
1629
+ // For the execution quote (second quote call):
1630
+ // targetWithBuffer = (0.5 ETH) * 1.05 = 0.525 ETH (for non-inventory execution, costs are 0)
1631
+ const executionQuoteCall = bridge.quote
1632
+ .getCalls()
1633
+ .find(
1634
+ (call: any) =>
1635
+ call.args[0].fromAmount !== undefined &&
1636
+ call.args[0].fromAmount !== rawBalance,
1637
+ );
1638
+ // Since maxViable (0.98 ETH) > targetWithBuffer (0.525 ETH), we bridge exactly targetWithBuffer
1639
+ expect(executionQuoteCall).to.exist;
1640
+ });
1641
+
1642
+ it('handles quote failures gracefully by skipping the source chain', async () => {
1643
+ // Setup: Native token where quote fails
1644
+ const arbitrumToken = {
1645
+ chainName: ARBITRUM_CHAIN,
1646
+ standard: TokenStandard.EvmHypNative,
1647
+ addressOrDenom: '0xArbitrumNative',
1648
+ getHypAdapter: Sinon.stub().returns(adapterStub),
1649
+ };
1650
+ const solanaToken = {
1651
+ chainName: SOLANA_CHAIN,
1652
+ standard: TokenStandard.EvmHypNative,
1653
+ addressOrDenom: '0xSolanaNative',
1654
+ getHypAdapter: Sinon.stub().returns(adapterStub),
1655
+ };
1656
+ warpCore.tokens = [arbitrumToken, solanaToken];
1657
+
1658
+ const amount = BigInt(1e18);
1659
+
1660
+ const route = createTestRoute({ amount });
1661
+ createTestIntent({ amount });
1662
+
1663
+ const rawBalance = BigInt(2e18); // 2 ETH
1664
+
1665
+ inventoryRebalancer.setInventoryBalances({
1666
+ [SOLANA_CHAIN]: 0n,
1667
+ [ARBITRUM_CHAIN]: rawBalance,
1668
+ });
1669
+
1670
+ // Quote fails for viability check
1671
+ bridge.quote.rejects(new Error('LiFi API timeout'));
1672
+
1673
+ const results = await inventoryRebalancer.rebalance([route]);
1674
+
1675
+ // Verify: Should fail because quote error means no viable sources
1676
+ expect(results).to.have.lengthOf(1);
1677
+ expect(results[0].success).to.be.false;
1678
+ expect(results[0].error).to.include('No viable bridge sources');
1679
+
1680
+ // Verify: Bridge.execute should NOT have been called
1681
+ expect(bridge.execute.called).to.be.false;
1682
+ });
1683
+ });
1684
+ });