@zeroxyz/cli 0.0.9 → 0.0.10

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 +63 -16
  2. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -6,7 +6,7 @@ import { Command as Command8 } from "commander";
6
6
  // package.json
7
7
  var package_default = {
8
8
  name: "@zeroxyz/cli",
9
- version: "0.0.9",
9
+ version: "0.0.10",
10
10
  type: "module",
11
11
  bin: {
12
12
  zero: "dist/index.js",
@@ -151,11 +151,13 @@ var fetchCommand = (appContext2) => new Command2("fetch").description("Fetch a c
151
151
  if (options.data && !headers["content-type"]) {
152
152
  headers["content-type"] = "application/json";
153
153
  }
154
+ const log = (msg) => console.error(` ${msg}`);
154
155
  const requestInit = {
155
156
  method: options.data ? "POST" : "GET",
156
157
  headers,
157
158
  body: options.data
158
159
  };
160
+ log(`Calling ${url}...`);
159
161
  const response = await fetch(url, requestInit);
160
162
  const paymentReq = detectPaymentRequirement(
161
163
  response.headers,
@@ -164,11 +166,15 @@ var fetchCommand = (appContext2) => new Command2("fetch").description("Fetch a c
164
166
  let finalResponse;
165
167
  let paymentMeta;
166
168
  if (paymentReq) {
169
+ log(
170
+ `Payment required (${paymentReq.protocol}) \u2014 preparing payment...`
171
+ );
167
172
  const result = await paymentService.handlePayment(
168
173
  url,
169
174
  requestInit,
170
175
  paymentReq,
171
- options.maxPay
176
+ options.maxPay,
177
+ log
172
178
  );
173
179
  finalResponse = result.response;
174
180
  paymentMeta = {
@@ -178,6 +184,9 @@ var fetchCommand = (appContext2) => new Command2("fetch").description("Fetch a c
178
184
  amount: result.amount,
179
185
  asset: result.asset
180
186
  };
187
+ log(
188
+ `Paid ${result.amount} ${result.asset} via ${result.protocol} on ${result.chain}`
189
+ );
181
190
  } else {
182
191
  finalResponse = response;
183
192
  }
@@ -953,6 +962,7 @@ var ApiService = class {
953
962
  // src/services/payment-service.ts
954
963
  import {
955
964
  adaptViemWallet,
965
+ convertViemChainToRelayChain,
956
966
  createClient as createRelayClient,
957
967
  getClient as getRelayClient,
958
968
  MAINNET_RELAY_API
@@ -978,6 +988,12 @@ var PATHUSD_TEMPO = "0x20c0000000000000000000000000000000000000";
978
988
  var BASE_CHAIN_ID = 8453;
979
989
  var TEMPO_CHAIN_ID = 4217;
980
990
  var TEMPO_TESTNET_CHAIN_ID = 42431;
991
+ var DEFAULT_MAX_DEPOSIT = "100";
992
+ var buildRelayClientOptions = () => ({
993
+ baseApiUrl: MAINNET_RELAY_API,
994
+ source: "zero-cli",
995
+ chains: [convertViemChainToRelayChain(base)]
996
+ });
981
997
  var calculateBuffer = (baseBalance) => {
982
998
  const twentyFivePercent = baseBalance / 4n;
983
999
  const twoDollars = 2000000n;
@@ -1016,14 +1032,11 @@ var PaymentService = class {
1016
1032
  relayInitialized = false;
1017
1033
  ensureRelayClient = () => {
1018
1034
  if (!this.relayInitialized) {
1019
- createRelayClient({
1020
- baseApiUrl: MAINNET_RELAY_API,
1021
- source: "zero-cli"
1022
- });
1035
+ createRelayClient(buildRelayClientOptions());
1023
1036
  this.relayInitialized = true;
1024
1037
  }
1025
1038
  };
1026
- bridgeToTempo = async (requiredAmount) => {
1039
+ bridgeToTempo = async (requiredAmount, onProgress) => {
1027
1040
  if (!this.account) throw new Error("No wallet configured");
1028
1041
  this.ensureRelayClient();
1029
1042
  const baseBalance = await this.getBalanceRaw("base");
@@ -1034,6 +1047,9 @@ var PaymentService = class {
1034
1047
  `Insufficient Base USDC to bridge: have ${formatUnits(baseBalance, 6)}, need ${formatUnits(bridgeAmount, 6)} (${formatUnits(requiredAmount, 6)} + ${formatUnits(buffer, 6)} buffer)`
1035
1048
  );
1036
1049
  }
1050
+ onProgress?.(
1051
+ `Bridging ${formatUnits(bridgeAmount, 6)} USDC from Base to Tempo...`
1052
+ );
1037
1053
  const walletClient = createWalletClient({
1038
1054
  account: this.account,
1039
1055
  chain: base,
@@ -1047,7 +1063,10 @@ var PaymentService = class {
1047
1063
  amount: bridgeAmount.toString(),
1048
1064
  tradeType: "EXACT_INPUT",
1049
1065
  user: this.account.address,
1050
- recipient: this.account.address
1066
+ recipient: this.account.address,
1067
+ options: {
1068
+ usePermit: true
1069
+ }
1051
1070
  });
1052
1071
  let bridgeTxHash = null;
1053
1072
  await getRelayClient().actions.execute({
@@ -1061,17 +1080,25 @@ var PaymentService = class {
1061
1080
  });
1062
1081
  return bridgeTxHash;
1063
1082
  };
1064
- handlePayment = async (url, request, paymentRequirement, maxPay) => {
1083
+ handlePayment = async (url, request, paymentRequirement, maxPay, onProgress) => {
1065
1084
  if (!this.account) {
1066
1085
  throw new Error(
1067
1086
  "No wallet configured \u2014 run `zero init` or set ZERO_PRIVATE_KEY"
1068
1087
  );
1069
1088
  }
1070
1089
  if (paymentRequirement.protocol === "x402") {
1090
+ onProgress?.("Paying via x402 on Base...");
1071
1091
  return this.payX402(url, request, paymentRequirement.raw, maxPay);
1072
1092
  }
1073
1093
  if (paymentRequirement.protocol === "mpp") {
1074
- return this.payMpp(url, request, paymentRequirement.raw, maxPay);
1094
+ onProgress?.("Paying via MPP on Tempo...");
1095
+ return this.payMpp(
1096
+ url,
1097
+ request,
1098
+ paymentRequirement.raw,
1099
+ maxPay,
1100
+ onProgress
1101
+ );
1075
1102
  }
1076
1103
  throw new Error("Unrecognized 402 payment protocol");
1077
1104
  };
@@ -1120,16 +1147,35 @@ var PaymentService = class {
1120
1147
  asset: "USDC"
1121
1148
  };
1122
1149
  };
1123
- payMpp = async (url, request, _raw, maxPay) => {
1150
+ payMpp = async (url, request, _raw, maxPay, onProgress) => {
1124
1151
  if (!this.account) throw new Error("No wallet configured");
1125
1152
  let capturedAmount = "0";
1126
1153
  const mppx = Mppx.create({
1127
1154
  polyfill: false,
1128
- methods: [tempo({ account: this.account })],
1155
+ methods: [
1156
+ tempo({
1157
+ account: this.account,
1158
+ maxDeposit: maxPay ?? DEFAULT_MAX_DEPOSIT
1159
+ })
1160
+ ],
1129
1161
  onChallenge: async (challenge) => {
1130
1162
  const challengeRequest = challenge.request;
1131
- const amountRaw = challengeRequest.amount;
1132
- capturedAmount = formatUnits(BigInt(amountRaw), 6);
1163
+ const intent = challenge.intent;
1164
+ let requiredRaw;
1165
+ if (intent === "session") {
1166
+ const suggestedDeposit = challengeRequest.suggestedDeposit;
1167
+ if (suggestedDeposit) {
1168
+ requiredRaw = BigInt(suggestedDeposit);
1169
+ } else {
1170
+ const depositStr = maxPay ?? DEFAULT_MAX_DEPOSIT;
1171
+ requiredRaw = BigInt(
1172
+ Math.floor(Number.parseFloat(depositStr) * 1e6)
1173
+ );
1174
+ }
1175
+ } else {
1176
+ requiredRaw = BigInt(challengeRequest.amount);
1177
+ }
1178
+ capturedAmount = formatUnits(requiredRaw, 6);
1133
1179
  if (maxPay && Number.parseFloat(capturedAmount) > Number.parseFloat(maxPay)) {
1134
1180
  throw new Error(
1135
1181
  `Payment of ${capturedAmount} USDC exceeds --max-pay ${maxPay}`
@@ -1139,14 +1185,15 @@ var PaymentService = class {
1139
1185
  const challengeChainId = challengeRequest.chainId ?? methodDetails?.chainId;
1140
1186
  const isTestnet = challengeChainId === TEMPO_TESTNET_CHAIN_ID;
1141
1187
  const balanceChain = isTestnet ? "tempo-testnet" : "tempo";
1188
+ onProgress?.(`Checking Tempo balance...`);
1142
1189
  const tempoBalance = await this.getBalanceRaw(balanceChain);
1143
- if (tempoBalance < BigInt(amountRaw)) {
1190
+ if (tempoBalance < requiredRaw) {
1144
1191
  if (isTestnet) {
1145
1192
  throw new Error(
1146
1193
  `Insufficient pathUSD on Tempo testnet: have ${formatUnits(tempoBalance, 6)}, need ${capturedAmount}. Fund your wallet with the Tempo testnet faucet: https://docs.tempo.xyz/quickstart/faucet`
1147
1194
  );
1148
1195
  }
1149
- await this.bridgeToTempo(BigInt(amountRaw));
1196
+ await this.bridgeToTempo(requiredRaw, onProgress);
1150
1197
  }
1151
1198
  return void 0;
1152
1199
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zeroxyz/cli",
3
- "version": "0.0.9",
3
+ "version": "0.0.10",
4
4
  "type": "module",
5
5
  "bin": {
6
6
  "zero": "dist/index.js",