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