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