@parallel-protocol/cli 0.2.1 → 0.2.3

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 (2) hide show
  1. package/dist/index.js +67 -49
  2. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -8068,6 +8068,15 @@ var PREVIEW_WITHDRAW_ABI = [
8068
8068
  outputs: [{ type: "uint256" }]
8069
8069
  }
8070
8070
  ];
8071
+ var PREVIEW_MINT_ABI = [
8072
+ {
8073
+ name: "previewMint",
8074
+ type: "function",
8075
+ stateMutability: "view",
8076
+ inputs: [{ type: "uint256", name: "shares" }],
8077
+ outputs: [{ type: "uint256" }]
8078
+ }
8079
+ ];
8071
8080
  function buildTiming() {
8072
8081
  const now = BigInt(Math.floor(Date.now() / 1e3));
8073
8082
  return { validAfter: now - 60n, validBefore: now + 300n };
@@ -8075,8 +8084,21 @@ function buildTiming() {
8075
8084
  function slippageUp(amount) {
8076
8085
  return amount + amount * SLIPPAGE_BPS / 10000n;
8077
8086
  }
8078
- function slippageDown(amount) {
8079
- return amount - amount * SLIPPAGE_BPS / 10000n;
8087
+ async function previewMintAssets(chain, susdp, shares) {
8088
+ return await getClient(chain).readContract({
8089
+ address: susdp,
8090
+ abi: PREVIEW_MINT_ABI,
8091
+ functionName: "previewMint",
8092
+ args: [shares]
8093
+ });
8094
+ }
8095
+ async function previewWithdrawShares(chain, susdp, assets) {
8096
+ return await getClient(chain).readContract({
8097
+ address: susdp,
8098
+ abi: PREVIEW_WITHDRAW_ABI,
8099
+ functionName: "previewWithdraw",
8100
+ args: [assets]
8101
+ });
8080
8102
  }
8081
8103
  async function readTokenDomain(chain, token) {
8082
8104
  return readTokenDomainFull(chain, token);
@@ -8216,15 +8238,6 @@ async function quoteSwapOut(chain, amountOut, tokenIn, tokenOut) {
8216
8238
  args: [amountOut, tokenIn, tokenOut]
8217
8239
  });
8218
8240
  }
8219
- async function quoteSwapIn(chain, amountIn, tokenIn, tokenOut) {
8220
- const parallelizer = getParallelizerAddress(chain);
8221
- return await getClient(chain).readContract({
8222
- address: parallelizer,
8223
- abi: PARALLELIZER_ABI,
8224
- functionName: "quoteIn",
8225
- args: [amountIn, tokenIn, tokenOut]
8226
- });
8227
- }
8228
8241
  async function prepareRouteA(chain, senderAddress, amount, recipient) {
8229
8242
  const usdpAddress = getUSDpAddress(chain);
8230
8243
  if (!usdpAddress) throw new Error(`USDp not deployed on ${chain}`);
@@ -8375,13 +8388,15 @@ async function prepareRouteC(chain, senderAddress, amount, recipient) {
8375
8388
  const susdpAddress = getSUSDpAddress(chain);
8376
8389
  if (!usdpAddress) throw new Error(`USDp not deployed on ${chain}`);
8377
8390
  if (!susdpAddress) throw new Error(`sUSDp not deployed on ${chain}`);
8378
- const amountWei = parseUnits(amount, 18);
8391
+ const sharesWei = parseUnits(amount, 18);
8379
8392
  const { validAfter, validBefore } = buildTiming();
8380
8393
  const sharedNonce = generateNonce();
8381
- const [susdpDomain, usdpDomain] = await Promise.all([
8394
+ const [assetsNeeded, susdpDomain, usdpDomain] = await Promise.all([
8395
+ previewMintAssets(chain, susdpAddress, sharesWei),
8382
8396
  readTokenDomain(chain, susdpAddress),
8383
8397
  readTokenDomain(chain, usdpAddress)
8384
8398
  ]);
8399
+ const amountWei = slippageUp(assetsNeeded);
8385
8400
  const depositTd = buildDepositTypedData(susdpDomain, {
8386
8401
  vault: susdpAddress,
8387
8402
  owner: senderAddress,
@@ -8410,7 +8425,9 @@ async function prepareRouteC(chain, senderAddress, amount, recipient) {
8410
8425
  route: "C",
8411
8426
  steps: [
8412
8427
  {
8413
- label: "depositSignature",
8428
+ // Steps are keyed by label: "signature" is the payload's required
8429
+ // signature ("depositSignature" is Route G's extra deposit sig).
8430
+ label: "signature",
8414
8431
  description: "DepositWithAuthorization on sUSDp domain",
8415
8432
  messageHash: sig1MessageHash,
8416
8433
  typedData: depositTd
@@ -8451,14 +8468,15 @@ async function prepareRouteE(chain, senderAddress, amount, recipient) {
8451
8468
  const usdcAddress = getUSDCAddress(chain);
8452
8469
  if (!usdcAddress) throw new Error(`USDC not configured on ${chain}`);
8453
8470
  const parallelizer = getParallelizerAddress(chain);
8454
- const amountInWei = parseUnits(amount, 6);
8455
- const expectedUSDpOut = await quoteSwapIn(
8471
+ const amountOutWei = parseUnits(amount, 18);
8472
+ const usdcNeeded = await quoteSwapOut(
8456
8473
  chain,
8457
- amountInWei,
8474
+ amountOutWei,
8458
8475
  usdcAddress,
8459
8476
  usdpAddress
8460
8477
  );
8461
- const amountOutMin = slippageDown(expectedUSDpOut);
8478
+ const amountInWei = slippageUp(usdcNeeded);
8479
+ const amountOutMin = amountOutWei;
8462
8480
  const { validAfter, validBefore } = buildTiming();
8463
8481
  const userSalt = generateNonce();
8464
8482
  const derived = derivedNonceExactInput(
@@ -8518,8 +8536,8 @@ async function prepareRouteE(chain, senderAddress, amount, recipient) {
8518
8536
  }
8519
8537
  },
8520
8538
  quote: {
8521
- amountIn: amount,
8522
- amountOut: formatUnits(expectedUSDpOut, 18),
8539
+ amountIn: formatUnits(amountInWei, 6),
8540
+ amountOut: amount,
8523
8541
  slippageBps: Number(SLIPPAGE_BPS)
8524
8542
  },
8525
8543
  typedData,
@@ -8588,14 +8606,19 @@ async function prepareRouteG(chain, senderAddress, amount, recipient) {
8588
8606
  if (!usdcAddress) throw new Error(`USDC not configured on ${chain}`);
8589
8607
  if (!susdpAddress) throw new Error(`sUSDp not deployed on ${chain}`);
8590
8608
  const parallelizer = getParallelizerAddress(chain);
8591
- const amountInWei = parseUnits(amount, 6);
8592
- const expectedUSDpOut = await quoteSwapIn(
8593
- chain,
8594
- amountInWei,
8595
- usdcAddress,
8596
- usdpAddress
8609
+ const sharesWei = parseUnits(amount, 18);
8610
+ const domainsPromise = Promise.all([
8611
+ readTokenDomain(chain, usdcAddress),
8612
+ readTokenDomain(chain, susdpAddress),
8613
+ readTokenDomain(chain, usdpAddress)
8614
+ ]);
8615
+ domainsPromise.catch(() => {
8616
+ });
8617
+ const usdpNeeded = await previewMintAssets(chain, susdpAddress, sharesWei);
8618
+ const amountOutMin = slippageUp(usdpNeeded);
8619
+ const amountInWei = slippageUp(
8620
+ await quoteSwapOut(chain, amountOutMin, usdcAddress, usdpAddress)
8597
8621
  );
8598
- const amountOutMin = slippageDown(expectedUSDpOut);
8599
8622
  const { validAfter, validBefore } = buildTiming();
8600
8623
  const swapUserSalt = generateNonce();
8601
8624
  const depositNonce = generateNonce();
@@ -8610,11 +8633,7 @@ async function prepareRouteG(chain, senderAddress, amount, recipient) {
8610
8633
  // swap recipient = payer, not merchant
8611
8634
  validBefore
8612
8635
  );
8613
- const [usdcDomain, susdpDomain, usdpDomain] = await Promise.all([
8614
- readTokenDomain(chain, usdcAddress),
8615
- readTokenDomain(chain, susdpAddress),
8616
- readTokenDomain(chain, usdpAddress)
8617
- ]);
8636
+ const [usdcDomain, susdpDomain, usdpDomain] = await domainsPromise;
8618
8637
  const swapTd = buildReceiveAuthorizationTypedData(usdcDomain, {
8619
8638
  from: senderAddress,
8620
8639
  to: parallelizer,
@@ -8703,8 +8722,8 @@ async function prepareRouteG(chain, senderAddress, amount, recipient) {
8703
8722
  }
8704
8723
  },
8705
8724
  quote: {
8706
- amountIn: amount,
8707
- amountOut: formatUnits(expectedUSDpOut, 18),
8725
+ amountIn: formatUnits(amountInWei, 6),
8726
+ amountOut: amount,
8708
8727
  slippageBps: Number(SLIPPAGE_BPS)
8709
8728
  },
8710
8729
  gasEstimate: 2e5
@@ -8713,10 +8732,13 @@ async function prepareRouteG(chain, senderAddress, amount, recipient) {
8713
8732
  async function prepareRouteH(chain, senderAddress, amount, recipient) {
8714
8733
  const susdpAddress = getSUSDpAddress(chain);
8715
8734
  if (!susdpAddress) throw new Error(`sUSDp not deployed on ${chain}`);
8716
- const sharesWei = parseUnits(amount, 18);
8735
+ const assetsWei = parseUnits(amount, 18);
8717
8736
  const { validAfter, validBefore } = buildTiming();
8718
8737
  const nonce = generateNonce();
8719
- const susdpDomain = await readTokenDomain(chain, susdpAddress);
8738
+ const [sharesWei, susdpDomain] = await Promise.all([
8739
+ previewWithdrawShares(chain, susdpAddress, assetsWei),
8740
+ readTokenDomain(chain, susdpAddress)
8741
+ ]);
8720
8742
  const redeemTd = buildRedeemTypedData(susdpDomain, {
8721
8743
  vault: susdpAddress,
8722
8744
  owner: senderAddress,
@@ -9181,10 +9203,12 @@ async function preparePayment(params) {
9181
9203
  const { chain, amount } = params;
9182
9204
  const sender = params.senderAddress;
9183
9205
  const recv = params.recipient;
9184
- const target = params.targetToken;
9185
9206
  const tokenIn = params.tokenIn ?? "usdp";
9207
+ const usdpAddress = getUSDpAddress(chain);
9208
+ const target = params.targetToken ?? usdpAddress;
9209
+ const wantsUSDp = !target || !!usdpAddress && target.toLowerCase() === usdpAddress.toLowerCase();
9186
9210
  if (tokenIn === "usdp") {
9187
- if (!target) return prepareRouteA(chain, sender, amount, recv);
9211
+ if (!target || wantsUSDp) return prepareRouteA(chain, sender, amount, recv);
9188
9212
  const susdpAddress = getSUSDpAddress(chain);
9189
9213
  if (susdpAddress && target.toLowerCase() === susdpAddress.toLowerCase()) {
9190
9214
  return prepareRouteC(chain, sender, amount, recv);
@@ -9198,13 +9222,10 @@ async function preparePayment(params) {
9198
9222
  if (tokenIn === "usdc") {
9199
9223
  const usdcAddress = getUSDCAddress(chain);
9200
9224
  if (!usdcAddress) throw new Error(`USDC not configured on ${chain}`);
9201
- if (!target || target.toLowerCase() === usdcAddress.toLowerCase()) {
9225
+ if (!target || wantsUSDp) return prepareRouteE(chain, sender, amount, recv);
9226
+ if (target.toLowerCase() === usdcAddress.toLowerCase()) {
9202
9227
  return prepareRouteF(chain, sender, amount, recv);
9203
9228
  }
9204
- const usdpAddress = getUSDpAddress(chain);
9205
- if (usdpAddress && target.toLowerCase() === usdpAddress.toLowerCase()) {
9206
- return prepareRouteE(chain, sender, amount, recv);
9207
- }
9208
9229
  const susdpAddress = getSUSDpAddress(chain);
9209
9230
  if (susdpAddress && target.toLowerCase() === susdpAddress.toLowerCase()) {
9210
9231
  return prepareRouteG(chain, sender, amount, recv);
@@ -9216,13 +9237,10 @@ async function preparePayment(params) {
9216
9237
  if (tokenIn === "susdp") {
9217
9238
  const susdpAddress = getSUSDpAddress(chain);
9218
9239
  if (!susdpAddress) throw new Error(`sUSDp not deployed on ${chain}`);
9219
- if (!target || target.toLowerCase() === susdpAddress.toLowerCase()) {
9240
+ if (!target || wantsUSDp) return prepareRouteH(chain, sender, amount, recv);
9241
+ if (target.toLowerCase() === susdpAddress.toLowerCase()) {
9220
9242
  return prepareRouteJ(chain, sender, amount, recv);
9221
9243
  }
9222
- const usdpAddress = getUSDpAddress(chain);
9223
- if (usdpAddress && target.toLowerCase() === usdpAddress.toLowerCase()) {
9224
- return prepareRouteH(chain, sender, amount, recv);
9225
- }
9226
9244
  const usdcAddressForI = getUSDCAddress(chain);
9227
9245
  if (usdcAddressForI && target.toLowerCase() === usdcAddressForI.toLowerCase()) {
9228
9246
  return prepareRouteI(chain, sender, amount, recv, target);
@@ -14560,7 +14578,7 @@ var package_default = {
14560
14578
 
14561
14579
  // package.json
14562
14580
  var package_default2 = {
14563
- version: "0.2.1"};
14581
+ version: "0.2.3"};
14564
14582
 
14565
14583
  // src/commands/version.ts
14566
14584
  function versionCommand() {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@parallel-protocol/cli",
3
- "version": "0.2.1",
3
+ "version": "0.2.3",
4
4
  "private": false,
5
5
  "type": "module",
6
6
  "files": [