@pushchain/core 5.1.4-alpha.7 → 5.1.4-alpha.9

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pushchain/core",
3
- "version": "5.1.4-alpha.7",
3
+ "version": "5.1.4-alpha.9",
4
4
  "homepage": "https://push.org",
5
5
  "keywords": [
6
6
  "pushchain",
@@ -263,44 +263,70 @@ function executeUoaToCea(ctx, params, executeFn) {
263
263
  let gasFee = BigInt(0);
264
264
  let nativeValueForGas = BigInt(0);
265
265
  let gasToken = selectors_1.ZERO_ADDRESS;
266
+ let universalCoreAddress;
266
267
  if (prc20Token !== selectors_1.ZERO_ADDRESS) {
267
268
  try {
268
269
  const result = yield (0, gas_calculator_1.queryOutboundGasFee)(ctx, prc20Token, outboundReq.gasLimit);
269
270
  gasFee = result.gasFee;
270
271
  gasToken = result.gasToken;
271
272
  nativeValueForGas = result.nativeValueForGas;
273
+ universalCoreAddress = result.universalCoreAddress;
272
274
  (0, context_1.printLog)(ctx, `executeUoaToCea — queried gas fee: ${gasFee.toString()}, gasToken: ${gasToken}, nativeValueForGas: ${nativeValueForGas.toString()}`);
273
275
  }
274
276
  catch (err) {
275
277
  throw new Error(`Failed to query outbound gas fee: ${err}`);
276
278
  }
277
279
  }
278
- // Adjust nativeValueForGas: the 1Mx multiplier from queryOutboundGasFee produces
279
- // a value far too low for the actual WPC/gasToken swap price. Set it to 200 UPC
280
- // (capped by balance) — enough for the swap, but avoids draining thin pools.
281
- // The contract's swapAndBurnGas does exactOutputSingle and refunds excess PC.
280
+ // Estimate the actual WPC needed for the swap using on-chain pool price.
281
+ // The static 1Mx multiplier from queryOutboundGasFee doesn't reflect the real
282
+ // WPC/gasToken exchange rate. estimateNativeValueForSwap reads the Uniswap V3
283
+ // pool's slot0, computes the true cost, and adds a 2x buffer.
284
+ // This matches the SVM path (executeUoaToCeaSvm) which already uses this.
285
+ // Excess msg.value is refunded by the contract's swapAndBurnGas.
282
286
  const EVM_NATIVE_VALUE_TARGET = BigInt(200e18); // 200 UPC
283
287
  const EVM_GAS_RESERVE = BigInt(3e18); // 3 UPC for tx overhead
284
- // Reuse ueaBalance fetched earlier (line 383) — no tx sent between, balance is stable
288
+ if (universalCoreAddress && gasFee > BigInt(0)) {
289
+ // When UEA balance is 0, pass EVM_NATIVE_VALUE_TARGET as the balance hint
290
+ // so the pool estimate isn't capped/fallback'd to zero.
291
+ const balanceHint = ueaBalance > BigInt(0) ? ueaBalance : EVM_NATIVE_VALUE_TARGET;
292
+ const estimated = yield (0, gas_calculator_1.estimateNativeValueForSwap)(ctx, universalCoreAddress, gasToken, gasFee, balanceHint);
293
+ if (estimated > nativeValueForGas) {
294
+ (0, context_1.printLog)(ctx, `executeUoaToCea — pool-price estimate: ${estimated.toString()} ` +
295
+ `(was ${nativeValueForGas.toString()}), UEA balance: ${ueaBalance.toString()}`);
296
+ nativeValueForGas = estimated;
297
+ }
298
+ }
299
+ // Balance-aware adjustment: avoid draining the entire UEA balance for gas.
300
+ // The contract's swapAndBurnGas does exactOutputSingle and refunds excess PC,
301
+ // so overshooting within balance is safe.
285
302
  const currentBalance = ueaBalance;
286
- // Balance-aware adjustment — same logic whether UEA is deployed or not.
287
- // The old !isUEADeployed branch blindly used 200 UPC assuming fee-locking
288
- // would deposit enough, but the actual deposit depends on origin-chain ETH
289
- // locked — not a guaranteed 200 UPC. This caused executeUniversalTx to
290
- // revert when UEA balance < 200 UPC. The contract's swapAndBurnGas does
291
- // exactOutputSingle and refunds excess PC, so overshooting within balance
292
- // is safe.
293
303
  let adjustedValue;
294
304
  if (currentBalance > EVM_NATIVE_VALUE_TARGET + EVM_GAS_RESERVE) {
295
- // Enough balance: use 200 UPC target
296
- adjustedValue = EVM_NATIVE_VALUE_TARGET;
305
+ // Enough balance: cap at 200 UPC (pool estimate may be lower)
306
+ adjustedValue = nativeValueForGas < EVM_NATIVE_VALUE_TARGET
307
+ ? nativeValueForGas
308
+ : EVM_NATIVE_VALUE_TARGET;
297
309
  }
298
310
  else if (currentBalance > EVM_GAS_RESERVE) {
299
311
  // Low balance: use what's available minus reserve
300
- adjustedValue = currentBalance - EVM_GAS_RESERVE;
312
+ const available = currentBalance - EVM_GAS_RESERVE;
313
+ adjustedValue = nativeValueForGas < available ? nativeValueForGas : available;
314
+ }
315
+ else if (!isUEADeployed) {
316
+ // Fresh wallet: UEA not deployed, fee-locking WILL run and deposit funds.
317
+ // Use EVM_NATIVE_VALUE_TARGET (200 UPC) — fee-locking auto-covers this
318
+ // because requiredFunds includes nativeValueForGas, and lockFee converts
319
+ // the UPC amount to USD and deposits the equivalent ETH.
320
+ // The contract's swapAndBurnGas refunds any excess.
321
+ // If pool estimate produced a usable value, prefer it (capped at 200 UPC).
322
+ adjustedValue = nativeValueForGas > EVM_NATIVE_VALUE_TARGET
323
+ ? EVM_NATIVE_VALUE_TARGET
324
+ : nativeValueForGas > BigInt(0) ? nativeValueForGas : EVM_NATIVE_VALUE_TARGET;
325
+ (0, context_1.printLog)(ctx, `executeUoaToCea — fresh wallet (UEA not deployed): using ${adjustedValue.toString()} for nativeValueForGas`);
301
326
  }
302
327
  else {
303
- // Very low balance: use original query value as-is
328
+ // Deployed UEA with near-zero balance: best-effort with pool/1M estimate.
329
+ // User may need to add funds to UEA manually.
304
330
  adjustedValue = nativeValueForGas;
305
331
  }
306
332
  if (adjustedValue !== nativeValueForGas) {