@opensea/cli 1.0.0 → 1.0.1

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/dist/cli.js CHANGED
@@ -6,7 +6,7 @@ import { Command as Command13 } from "commander";
6
6
  // src/client.ts
7
7
  var DEFAULT_BASE_URL = "https://api.opensea.io";
8
8
  var DEFAULT_TIMEOUT_MS = 3e4;
9
- var USER_AGENT = `opensea-cli/${"1.0.0"}`;
9
+ var USER_AGENT = `opensea-cli/${"1.0.1"}`;
10
10
  var DEFAULT_MAX_RETRIES = 0;
11
11
  var DEFAULT_RETRY_BASE_DELAY_MS = 1e3;
12
12
  function isRetryableStatus(status, method) {
@@ -1992,6 +1992,30 @@ function createAdapter(provider) {
1992
1992
  }
1993
1993
 
1994
1994
  // src/sdk.ts
1995
+ function convertToSmallestUnit(amount, decimals) {
1996
+ const [whole = "0", frac = ""] = amount.split(".");
1997
+ if (frac.length > decimals) {
1998
+ throw new Error(
1999
+ `Too many decimal places (${frac.length}) for token with ${decimals} decimals`
2000
+ );
2001
+ }
2002
+ const paddedFrac = frac.padEnd(decimals, "0");
2003
+ return (BigInt(whole) * BigInt(10) ** BigInt(decimals) + BigInt(paddedFrac)).toString();
2004
+ }
2005
+ async function resolveQuantity(client, chain, tokenAddress, quantity) {
2006
+ if (/^\d+$/.test(quantity)) {
2007
+ return quantity;
2008
+ }
2009
+ if (!/^\d+\.\d+$/.test(quantity)) {
2010
+ throw new Error(
2011
+ `Invalid quantity "${quantity}": must be an integer or decimal number`
2012
+ );
2013
+ }
2014
+ const token = await client.get(
2015
+ `/api/v2/chain/${chain}/token/${tokenAddress}`
2016
+ );
2017
+ return convertToSmallestUnit(quantity, token.decimals);
2018
+ }
1995
2019
  var SwapsAPI = class {
1996
2020
  constructor(client) {
1997
2021
  this.client = client;
@@ -2067,7 +2091,10 @@ function swapsCommand(getClient2, getFormat2) {
2067
2091
  ).requiredOption("--to-chain <chain>", "Chain of the token to swap to").requiredOption(
2068
2092
  "--to-address <address>",
2069
2093
  "Contract address of the token to swap to"
2070
- ).requiredOption("--quantity <quantity>", "Amount to swap (in token units)").requiredOption("--address <address>", "Wallet address executing the swap").option(
2094
+ ).requiredOption(
2095
+ "--quantity <quantity>",
2096
+ "Amount to swap (decimals like 0.1 are auto-converted to smallest units)"
2097
+ ).requiredOption("--address <address>", "Wallet address executing the swap").option(
2071
2098
  "--slippage <slippage>",
2072
2099
  "Slippage tolerance (0.0 to 0.5, default: 0.01)"
2073
2100
  ).option(
@@ -2076,6 +2103,12 @@ function swapsCommand(getClient2, getFormat2) {
2076
2103
  ).action(
2077
2104
  async (options) => {
2078
2105
  const client = getClient2();
2106
+ const quantity = await resolveQuantity(
2107
+ client,
2108
+ options.fromChain,
2109
+ options.fromAddress,
2110
+ options.quantity
2111
+ );
2079
2112
  const result = await client.get(
2080
2113
  "/api/v2/swap/quote",
2081
2114
  {
@@ -2083,7 +2116,7 @@ function swapsCommand(getClient2, getFormat2) {
2083
2116
  from_address: options.fromAddress,
2084
2117
  to_chain: options.toChain,
2085
2118
  to_address: options.toAddress,
2086
- quantity: options.quantity,
2119
+ quantity,
2087
2120
  address: options.address,
2088
2121
  slippage: options.slippage ? parseFloatOption(options.slippage, "--slippage") : void 0,
2089
2122
  recipient: options.recipient
@@ -2100,7 +2133,10 @@ function swapsCommand(getClient2, getFormat2) {
2100
2133
  ).requiredOption("--to-chain <chain>", "Chain of the token to swap to").requiredOption(
2101
2134
  "--to-address <address>",
2102
2135
  "Contract address of the token to swap to"
2103
- ).requiredOption("--quantity <quantity>", "Amount to swap (in token units)").option(
2136
+ ).requiredOption(
2137
+ "--quantity <quantity>",
2138
+ "Amount to swap (decimals like 0.1 are auto-converted to smallest units)"
2139
+ ).option(
2104
2140
  "--slippage <slippage>",
2105
2141
  "Slippage tolerance (0.0 to 0.5, default: 0.01)"
2106
2142
  ).option(
@@ -2116,12 +2152,20 @@ function swapsCommand(getClient2, getFormat2) {
2116
2152
  );
2117
2153
  const address = await wallet.getAddress();
2118
2154
  console.error(`Using ${wallet.name} wallet: ${address}`);
2119
- const swaps = new SwapsAPI(getClient2());
2155
+ const client = getClient2();
2156
+ const quantity = await resolveQuantity(
2157
+ client,
2158
+ options.fromChain,
2159
+ options.fromAddress,
2160
+ options.quantity
2161
+ );
2162
+ const swaps = new SwapsAPI(client);
2120
2163
  const format = getFormat2();
2121
2164
  const slippage = options.slippage ? parseFloatOption(options.slippage, "--slippage") : void 0;
2122
2165
  if (options.dryRun) {
2123
2166
  const quote = await swaps.quote({
2124
2167
  ...options,
2168
+ quantity,
2125
2169
  address,
2126
2170
  slippage
2127
2171
  });
@@ -2131,6 +2175,7 @@ function swapsCommand(getClient2, getFormat2) {
2131
2175
  const results = await swaps.execute(
2132
2176
  {
2133
2177
  ...options,
2178
+ quantity,
2134
2179
  address,
2135
2180
  slippage
2136
2181
  },
@@ -2216,7 +2261,7 @@ var BANNER = `
2216
2261
  |_|
2217
2262
  `;
2218
2263
  var program = new Command13();
2219
- program.name("opensea").description("OpenSea CLI - Query the OpenSea API from the command line").version(process.env.npm_package_version ?? "0.0.0").addHelpText("before", BANNER).option("--api-key <key>", "OpenSea API key (or set OPENSEA_API_KEY env var)").option("--chain <chain>", "Default chain", "ethereum").option("--format <format>", "Output format (json, table, or toon)", "json").option("--base-url <url>", "API base URL").option("--timeout <ms>", "Request timeout in milliseconds", "30000").option("--verbose", "Log request and response info to stderr").option(
2264
+ program.name("opensea").description("OpenSea CLI - Query the OpenSea API from the command line").version("1.0.1").addHelpText("before", BANNER).option("--api-key <key>", "OpenSea API key (or set OPENSEA_API_KEY env var)").option("--chain <chain>", "Default chain", "ethereum").option("--format <format>", "Output format (json, table, or toon)", "json").option("--base-url <url>", "API base URL").option("--timeout <ms>", "Request timeout in milliseconds", "30000").option("--verbose", "Log request and response info to stderr").option(
2220
2265
  "--fields <fields>",
2221
2266
  "Comma-separated list of fields to include in output"
2222
2267
  ).option("--max-lines <lines>", "Truncate output after N lines").option("--max-retries <n>", "Max retries on 429/5xx (0 to disable)", "3").option("--no-retry", "Disable request retries");