@sun-protocol/sun-cli 1.2.2 → 1.2.3-beta.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 (66) hide show
  1. package/dist/commands/contract.d.ts.map +1 -1
  2. package/dist/commands/contract.js +18 -8
  3. package/dist/commands/contract.js.map +1 -1
  4. package/dist/commands/liquidity.d.ts +1 -0
  5. package/dist/commands/liquidity.d.ts.map +1 -1
  6. package/dist/commands/liquidity.js +197 -118
  7. package/dist/commands/liquidity.js.map +1 -1
  8. package/dist/commands/pool.js +3 -3
  9. package/dist/commands/pool.js.map +1 -1
  10. package/dist/commands/sunpump.d.ts.map +1 -1
  11. package/dist/commands/sunpump.js +29 -35
  12. package/dist/commands/sunpump.js.map +1 -1
  13. package/dist/commands/swap.d.ts.map +1 -1
  14. package/dist/commands/swap.js +34 -11
  15. package/dist/commands/swap.js.map +1 -1
  16. package/dist/commands/wallet.d.ts.map +1 -1
  17. package/dist/commands/wallet.js +17 -12
  18. package/dist/commands/wallet.js.map +1 -1
  19. package/dist/lib/command.d.ts +8 -7
  20. package/dist/lib/command.d.ts.map +1 -1
  21. package/dist/lib/command.js.map +1 -1
  22. package/dist/lib/context.d.ts +7 -4
  23. package/dist/lib/context.d.ts.map +1 -1
  24. package/dist/lib/context.js +13 -8
  25. package/dist/lib/context.js.map +1 -1
  26. package/dist/lib/output.js +1 -1
  27. package/dist/lib/output.js.map +1 -1
  28. package/dist/lib/sdk/compat.d.ts +2 -0
  29. package/dist/lib/sdk/compat.d.ts.map +1 -0
  30. package/dist/lib/sdk/compat.js +30 -0
  31. package/dist/lib/sdk/compat.js.map +1 -0
  32. package/dist/lib/sdk/constants.d.ts +4 -0
  33. package/dist/lib/sdk/constants.d.ts.map +1 -0
  34. package/dist/lib/sdk/constants.js +9 -0
  35. package/dist/lib/sdk/constants.js.map +1 -0
  36. package/dist/lib/sdk/errors.d.ts +3 -0
  37. package/dist/lib/sdk/errors.d.ts.map +1 -0
  38. package/dist/lib/sdk/errors.js +19 -0
  39. package/dist/lib/sdk/errors.js.map +1 -0
  40. package/dist/lib/sdk/factory.d.ts +17 -0
  41. package/dist/lib/sdk/factory.d.ts.map +1 -0
  42. package/dist/lib/sdk/factory.js +68 -0
  43. package/dist/lib/sdk/factory.js.map +1 -0
  44. package/dist/lib/sdk/index.d.ts +7 -0
  45. package/dist/lib/sdk/index.d.ts.map +1 -0
  46. package/dist/lib/sdk/index.js +23 -0
  47. package/dist/lib/sdk/index.js.map +1 -0
  48. package/dist/lib/sdk/types.d.ts +16 -0
  49. package/dist/lib/sdk/types.d.ts.map +1 -0
  50. package/dist/lib/sdk/types.js +3 -0
  51. package/dist/lib/sdk/types.js.map +1 -0
  52. package/dist/lib/sdk/wallet.d.ts +18 -0
  53. package/dist/lib/sdk/wallet.d.ts.map +1 -0
  54. package/dist/lib/sdk/wallet.js +31 -0
  55. package/dist/lib/sdk/wallet.js.map +1 -0
  56. package/dist/lib/tokens.js +5 -5
  57. package/dist/lib/tokens.js.map +1 -1
  58. package/dist/lib/wallet.d.ts +2 -2
  59. package/dist/lib/wallet.d.ts.map +1 -1
  60. package/dist/lib/wallet.js +2 -62
  61. package/dist/lib/wallet.js.map +1 -1
  62. package/package.json +12 -5
  63. package/dist/lib/sunpump.d.ts +0 -143
  64. package/dist/lib/sunpump.d.ts.map +0 -1
  65. package/dist/lib/sunpump.js +0 -155
  66. package/dist/lib/sunpump.js.map +0 -1
@@ -1 +1 @@
1
- {"version":3,"file":"contract.d.ts","sourceRoot":"","sources":["../../src/commands/contract.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAA;AAInC,wBAAgB,wBAAwB,CAAC,OAAO,EAAE,OAAO,QA0DxD"}
1
+ {"version":3,"file":"contract.d.ts","sourceRoot":"","sources":["../../src/commands/contract.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAA;AAcnC,wBAAgB,wBAAwB,CAAC,OAAO,EAAE,OAAO,QAwDxD"}
@@ -3,6 +3,15 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.registerContractCommands = registerContractCommands;
4
4
  const context_1 = require("../lib/context");
5
5
  const command_1 = require("../lib/command");
6
+ const compat_1 = require("../lib/sdk/compat");
7
+ function parseAbi(value) {
8
+ if (!value)
9
+ throw new Error('--abi is required for SDK contract calls');
10
+ return JSON.parse(value);
11
+ }
12
+ function parseValue(value) {
13
+ return value === undefined ? undefined : BigInt(value);
14
+ }
6
15
  function registerContractCommands(program) {
7
16
  const contract = program
8
17
  .command('contract')
@@ -16,12 +25,12 @@ function registerContractCommands(program) {
16
25
  await (0, command_1.readAction)({
17
26
  spinnerLabel: `Reading ${functionName}...`,
18
27
  errorLabel: 'Contract read failed',
19
- execute: (kit) => kit.readContract({
28
+ execute: (sdk) => sdk.runtime.contract.read({
20
29
  address,
21
30
  functionName,
22
31
  args: JSON.parse(opts.args),
23
- abi: opts.abi ? JSON.parse(opts.abi) : undefined,
24
- }, (0, context_1.getNetwork)()),
32
+ abi: parseAbi(opts.abi),
33
+ }),
25
34
  transform: (result) => ({ result }),
26
35
  });
27
36
  });
@@ -44,14 +53,15 @@ function registerContractCommands(program) {
44
53
  confirmMsg: 'Send this transaction?',
45
54
  spinnerLabel: `Sending ${functionName}...`,
46
55
  errorLabel: 'Contract send failed',
47
- execute: (kit) => kit.sendContractTx({
56
+ execute: (kit) => kit.runtime.contract
57
+ .write({
48
58
  address,
49
59
  functionName,
50
60
  args: JSON.parse(opts.args),
51
- value: opts.value,
52
- abi: opts.abi ? JSON.parse(opts.abi) : undefined,
53
- network: (0, context_1.getNetwork)(),
54
- }),
61
+ callValue: parseValue(opts.value),
62
+ abi: parseAbi(opts.abi),
63
+ })
64
+ .then(compat_1.toCliTxResult),
55
65
  });
56
66
  });
57
67
  }
@@ -1 +1 @@
1
- {"version":3,"file":"contract.js","sourceRoot":"","sources":["../../src/commands/contract.ts"],"names":[],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W,EAAC;YAChB,KAAK,EAAE,sBAAsB;YAC7B,OAAO,EAAE;gBACP,QAAQ,EAAE,OAAO;gBACjB,QAAQ,EAAE,YAAY;gBACtB,IAAI,EAAE,IAAI,CAAC,IAAI;gBACf,KAAK,EAAE,IAAI,CAAC,KAAK,IAAI,GAAG;gBACxB,OAAO,EAAE,IAAA,oBAAU,GAAE;aACtB;YACD,UAAU,EAAE,wBAAwB;YACpC,YAAY,EAAE,WAAW,YAAY,KAAK;YAC1C,UAAU,EAAE,sBAAsB;YAClC,OAAO,EAAE,CAAC,GAAG,EAAE,EAAE,CACf,GAAG,CAAC,OAAO,CAAC,QAAQ;iBACjB,KAAK,CAAC;gBACL,OAAO;gBACP,YAAY;gBACZ,IAAI,EAAE,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC;gBAC3B,SAAS,EAAE,UAAU,CAAC,IAAI,CAAC,KAAK,CAAC;gBACjC,GAAG,EAAE,QAAQ,CAAC,IAAI,CAAC,GAAG,CAAC;aACxB,CAAC;iBACD,IAAI,CAAC,sBAAa,CAAC;SACzB,CAAC,CAAA;IACJ,CAAC,CAAC,CAAA;AACN,CAAC"}
@@ -1,3 +1,4 @@
1
1
  import { Command } from 'commander';
2
+ export declare function unwrapContractAddressResult(result: unknown, label: string): string;
2
3
  export declare function registerLiquidityCommands(program: Command): void;
3
4
  //# sourceMappingURL=liquidity.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"liquidity.d.ts","sourceRoot":"","sources":["../../src/commands/liquidity.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAA;AAiInC,wBAAgB,yBAAyB,CAAC,OAAO,EAAE,OAAO,QA+rBzD"}
1
+ {"version":3,"file":"liquidity.d.ts","sourceRoot":"","sources":["../../src/commands/liquidity.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAA;AAsHnC,wBAAgB,2BAA2B,CAAC,MAAM,EAAE,OAAO,EAAE,KAAK,EAAE,MAAM,GAAG,MAAM,CAQlF;AA8DD,wBAAgB,yBAAyB,CAAC,OAAO,EAAE,OAAO,QAmtBzD"}
@@ -1,72 +1,134 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.unwrapContractAddressResult = unwrapContractAddressResult;
3
4
  exports.registerLiquidityCommands = registerLiquidityCommands;
4
5
  const context_1 = require("../lib/context");
5
6
  const command_1 = require("../lib/command");
6
7
  const output_1 = require("../lib/output");
7
8
  const tokens_1 = require("../lib/tokens");
8
- const sun_kit_1 = require("@sun-protocol/sun-kit");
9
- const V2_ROUTERS = {
10
- mainnet: sun_kit_1.SUNSWAP_V2_MAINNET_ROUTER,
11
- nile: sun_kit_1.SUNSWAP_V2_NILE_ROUTER,
12
- };
13
- const V2_FACTORIES = {
14
- mainnet: sun_kit_1.SUNSWAP_V2_MAINNET_FACTORY,
15
- nile: sun_kit_1.SUNSWAP_V2_NILE_FACTORY,
16
- };
17
- const V3_POSITION_MANAGERS = {
18
- mainnet: sun_kit_1.SUNSWAP_V3_MAINNET_POSITION_MANAGER,
19
- nile: sun_kit_1.SUNSWAP_V3_NILE_POSITION_MANAGER,
9
+ const sun_sdk_chains_1 = require("@sun-protocol/sun-sdk-chains");
10
+ const tronweb_1 = require("tronweb");
11
+ const constants_1 = require("../lib/sdk/constants");
12
+ const factory_1 = require("../lib/sdk/factory");
13
+ const compat_1 = require("../lib/sdk/compat");
14
+ const SUNSWAP_V2_FACTORY_MIN_ABI = [
15
+ {
16
+ constant: true,
17
+ inputs: [
18
+ { name: 'tokenA', type: 'address' },
19
+ { name: 'tokenB', type: 'address' },
20
+ ],
21
+ name: 'getPair',
22
+ outputs: [{ name: 'pair', type: 'address' }],
23
+ type: 'function',
24
+ },
25
+ ];
26
+ const SUNSWAP_V2_PAIR_MIN_ABI = [
27
+ {
28
+ constant: true,
29
+ inputs: [],
30
+ name: 'getReserves',
31
+ outputs: [
32
+ { name: '_reserve0', type: 'uint112' },
33
+ { name: '_reserve1', type: 'uint112' },
34
+ { name: '_blockTimestampLast', type: 'uint32' },
35
+ ],
36
+ type: 'function',
37
+ },
38
+ {
39
+ constant: true,
40
+ inputs: [],
41
+ name: 'token0',
42
+ outputs: [{ name: '', type: 'address' }],
43
+ type: 'function',
44
+ },
45
+ ];
46
+ const TICK_SPACING_BY_FEE = {
47
+ 100: 1,
48
+ 500: 10,
49
+ 3000: 60,
50
+ 10000: 200,
20
51
  };
52
+ function assertSdkNetwork(network) {
53
+ if (network === 'mainnet' || network === 'nile')
54
+ return network;
55
+ throw new Error(`Unsupported SDK network: ${network}`);
56
+ }
21
57
  function getV2Router(network, override) {
22
58
  if (override)
23
59
  return override;
24
- const router = V2_ROUTERS[network];
25
- if (!router) {
26
- throw new Error(`V2 router not configured for network: ${network}. Use --router to specify.`);
27
- }
28
- return router;
60
+ return (0, sun_sdk_chains_1.getContractAddress)(assertSdkNetwork(network), 'sunswapV2Router');
29
61
  }
30
62
  function getV3PositionManager(network, override) {
31
63
  if (override)
32
64
  return override;
33
- const pm = V3_POSITION_MANAGERS[network];
34
- if (!pm) {
35
- throw new Error(`V3 position manager not configured for network: ${network}. Use --pm to specify.`);
36
- }
37
- return pm;
65
+ return (0, sun_sdk_chains_1.getContractAddress)(assertSdkNetwork(network), 'sunswapV3PositionManager');
66
+ }
67
+ function getV2Factory(network) {
68
+ return (0, sun_sdk_chains_1.getContractAddress)(assertSdkNetwork(network), 'sunswapV2Factory');
69
+ }
70
+ function percentFromDecimal(value) {
71
+ return value === undefined ? undefined : `${parseFloat(value) * 100}%`;
72
+ }
73
+ function tickSpacingForFee(fee) {
74
+ return TICK_SPACING_BY_FEE[fee] ?? 60;
75
+ }
76
+ function toV3PoolOrder(token0, token1, value0, value1) {
77
+ const token0Hex = tronweb_1.TronWeb.address.toHex(token0).toLowerCase();
78
+ const token1Hex = tronweb_1.TronWeb.address.toHex(token1).toLowerCase();
79
+ return token0Hex < token1Hex ? [value0, value1] : [value1, value0];
80
+ }
81
+ function withContracts(overrides) {
82
+ const entries = Object.entries(overrides).filter((entry) => !!entry[1]);
83
+ return entries.length ? Object.fromEntries(entries) : undefined;
84
+ }
85
+ function mapTx(result) {
86
+ return (0, compat_1.toCliTxResult)(result);
38
87
  }
39
88
  function toWTRXIfNative(tokenAddress, network) {
40
- if (tokenAddress === sun_kit_1.TRX_ADDRESS) {
41
- return network === 'nile' ? sun_kit_1.WTRX_NILE : sun_kit_1.WTRX_MAINNET;
89
+ if (tokenAddress === constants_1.TRX_ADDRESS) {
90
+ return network === 'nile' ? constants_1.WTRX_NILE : constants_1.WTRX_MAINNET;
42
91
  }
43
92
  return tokenAddress;
44
93
  }
45
94
  function getWTRX(network) {
46
- return network === 'nile' ? sun_kit_1.WTRX_NILE : sun_kit_1.WTRX_MAINNET;
95
+ return network === 'nile' ? constants_1.WTRX_NILE : constants_1.WTRX_MAINNET;
47
96
  }
48
97
  function getLookupToken(token, network) {
49
- return token === sun_kit_1.TRX_ADDRESS ? getWTRX(network) : token;
98
+ return token === constants_1.TRX_ADDRESS ? getWTRX(network) : token;
99
+ }
100
+ function unwrapContractAddressResult(result, label) {
101
+ const value = Array.isArray(result)
102
+ ? result[0]
103
+ : result && typeof result === 'object'
104
+ ? Object.values(result)[0]
105
+ : result;
106
+ if (typeof value !== 'string')
107
+ throw new Error(`${label} did not return an address`);
108
+ return value;
109
+ }
110
+ function toBase58ContractAddress(tronWeb, result, label) {
111
+ const value = unwrapContractAddressResult(result, label);
112
+ if (value.startsWith('T'))
113
+ return value;
114
+ return tronWeb.address.fromHex(value.startsWith('0x') ? `41${value.slice(2)}` : value);
50
115
  }
51
116
  async function getV2PairReserves(network, tokenA, tokenB) {
52
- const tronWeb = await (0, sun_kit_1.createReadonlyTronWeb)(network);
53
- const factoryAddress = V2_FACTORIES[network];
54
- if (!factoryAddress) {
55
- throw new Error(`V2 factory not configured for network: ${network}`);
56
- }
117
+ const tronWeb = (0, factory_1.createReadonlyTronWeb)({ network });
118
+ const factoryAddress = getV2Factory(network);
57
119
  const lookupA = getLookupToken(tokenA, network);
58
120
  const lookupB = getLookupToken(tokenB, network);
59
- const factory = await tronWeb.contract(sun_kit_1.SUNSWAP_V2_FACTORY_MIN_ABI, factoryAddress);
60
- const pairHex = await factory.getPair(lookupA, lookupB).call();
61
- const pairBase58 = tronWeb.address.fromHex(pairHex);
121
+ const factory = await tronWeb.contract(SUNSWAP_V2_FACTORY_MIN_ABI, factoryAddress);
122
+ const pairResult = await factory.getPair(lookupA, lookupB).call();
123
+ const pairBase58 = toBase58ContractAddress(tronWeb, pairResult, 'V2 factory getPair');
62
124
  const zeroBase58 = tronWeb.address.fromHex('410000000000000000000000000000000000000000');
63
125
  if (!pairBase58 || pairBase58 === zeroBase58) {
64
126
  return { reserveA: '0', reserveB: '0', pairExists: false };
65
127
  }
66
- const pair = await tronWeb.contract(sun_kit_1.SUNSWAP_V2_PAIR_MIN_ABI, pairBase58);
128
+ const pair = await tronWeb.contract(SUNSWAP_V2_PAIR_MIN_ABI, pairBase58);
67
129
  const reserves = await pair.getReserves().call();
68
- const token0Hex = await pair.token0().call();
69
- const token0 = tronWeb.address.fromHex(token0Hex);
130
+ const token0Result = await pair.token0().call();
131
+ const token0 = toBase58ContractAddress(tronWeb, token0Result, 'V2 pair token0');
70
132
  const reserve0 = (reserves._reserve0 ?? reserves[0]).toString();
71
133
  const reserve1 = (reserves._reserve1 ?? reserves[1]).toString();
72
134
  const reserveA = token0 === lookupA ? reserve0 : reserve1;
@@ -156,19 +218,19 @@ function registerLiquidityCommands(program) {
156
218
  confirmMsg: 'Add V2 liquidity?',
157
219
  spinnerLabel: 'Adding V2 liquidity...',
158
220
  errorLabel: 'V2 add liquidity failed',
159
- execute: (kit) => kit.addLiquidityV2({
160
- network,
161
- routerAddress: router,
162
- abi: opts.abi ? JSON.parse(opts.abi) : undefined,
221
+ execute: (sdk) => sdk.liquidity.v2.add
222
+ .execute({
223
+ contracts: withContracts({ sunswapV2Router: router }),
163
224
  tokenA,
164
225
  tokenB,
165
226
  amountADesired: amountA,
166
227
  amountBDesired: amountB,
167
228
  amountAMin: opts.minA,
168
229
  amountBMin: opts.minB,
169
- to: opts.to,
230
+ recipient: opts.to,
170
231
  deadline: opts.deadline,
171
- }),
232
+ })
233
+ .then(mapTx),
172
234
  });
173
235
  });
174
236
  liq
@@ -210,18 +272,18 @@ function registerLiquidityCommands(program) {
210
272
  confirmMsg: 'Remove V2 liquidity?',
211
273
  spinnerLabel: 'Removing V2 liquidity...',
212
274
  errorLabel: 'V2 remove liquidity failed',
213
- execute: (kit) => kit.removeLiquidityV2({
214
- network,
215
- routerAddress: router,
216
- abi: opts.abi ? JSON.parse(opts.abi) : undefined,
275
+ execute: (sdk) => sdk.liquidity.v2.remove
276
+ .execute({
277
+ contracts: withContracts({ sunswapV2Router: router }),
217
278
  tokenA,
218
279
  tokenB,
219
280
  liquidity: opts.liquidity,
220
281
  amountAMin: opts.minA,
221
282
  amountBMin: opts.minB,
222
- to: opts.to,
283
+ recipient: opts.to,
223
284
  deadline: opts.deadline,
224
- }),
285
+ })
286
+ .then(mapTx),
225
287
  });
226
288
  });
227
289
  // ---------------------------------------------------------------------------
@@ -268,6 +330,8 @@ function registerLiquidityCommands(program) {
268
330
  const token0Note = token0 !== token0Input ? ` → WTRX` : '';
269
331
  const token1Note = token1 !== token1Input ? ` → WTRX` : '';
270
332
  const tickRange = opts.tickLower && opts.tickUpper ? `[${opts.tickLower}, ${opts.tickUpper}]` : '(auto)';
333
+ const [amount0, amount1] = toV3PoolOrder(token0, token1, opts.amount0, opts.amount1);
334
+ const [amount0Min, amount1Min] = toV3PoolOrder(token0, token1, opts.min0, opts.min1);
271
335
  await (0, command_1.writeAction)({
272
336
  title: 'V3 Mint Position',
273
337
  summary: {
@@ -283,22 +347,23 @@ function registerLiquidityCommands(program) {
283
347
  confirmMsg: 'Mint V3 position?',
284
348
  spinnerLabel: 'Minting V3 position...',
285
349
  errorLabel: 'V3 mint failed',
286
- execute: (kit) => kit.mintPositionV3({
287
- network,
288
- positionManagerAddress: pm,
289
- abi: opts.abi ? JSON.parse(opts.abi) : undefined,
290
- token0,
291
- token1,
292
- fee: opts.fee ? parseInt(opts.fee) : undefined,
350
+ execute: (sdk) => sdk.liquidity.v3.add
351
+ .execute({
352
+ contracts: withContracts({ sunswapV3PositionManager: pm }),
353
+ tokenA: token0,
354
+ tokenB: token1,
355
+ fee: opts.fee ? parseInt(opts.fee) : 3000,
293
356
  tickLower: opts.tickLower ? parseInt(opts.tickLower) : undefined,
294
357
  tickUpper: opts.tickUpper ? parseInt(opts.tickUpper) : undefined,
295
- amount0Desired: opts.amount0,
296
- amount1Desired: opts.amount1,
297
- amount0Min: opts.min0,
298
- amount1Min: opts.min1,
358
+ tickRangeWidth: opts.tickLower === undefined && opts.tickUpper === undefined ? 100 : undefined,
359
+ amount0,
360
+ amount1,
361
+ amount0Min,
362
+ amount1Min,
299
363
  recipient: opts.recipient,
300
364
  deadline: opts.deadline,
301
- }),
365
+ })
366
+ .then(mapTx),
302
367
  summarizeResult: (result) => {
303
368
  const out = {};
304
369
  if (result?.computedTicks) {
@@ -346,17 +411,17 @@ function registerLiquidityCommands(program) {
346
411
  confirmMsg: 'Increase V3 liquidity?',
347
412
  spinnerLabel: 'Increasing V3 liquidity...',
348
413
  errorLabel: 'V3 increase liquidity failed',
349
- execute: (kit) => kit.increaseLiquidityV3({
350
- network,
351
- positionManagerAddress: pm,
352
- abi: opts.abi ? JSON.parse(opts.abi) : undefined,
414
+ execute: (sdk) => sdk.liquidity.v3.increase
415
+ .execute({
416
+ contracts: withContracts({ sunswapV3PositionManager: pm }),
353
417
  tokenId: opts.tokenId,
354
- amount0Desired: opts.amount0,
355
- amount1Desired: opts.amount1,
418
+ amount0: opts.amount0,
419
+ amount1: opts.amount1,
356
420
  amount0Min: opts.min0,
357
421
  amount1Min: opts.min1,
358
422
  deadline: opts.deadline,
359
- }),
423
+ })
424
+ .then(mapTx),
360
425
  summarizeResult: (result) => {
361
426
  const out = {};
362
427
  if (result?.computedAmounts) {
@@ -393,16 +458,17 @@ function registerLiquidityCommands(program) {
393
458
  confirmMsg: 'Decrease V3 liquidity?',
394
459
  spinnerLabel: 'Decreasing V3 liquidity...',
395
460
  errorLabel: 'V3 decrease liquidity failed',
396
- execute: (kit) => kit.decreaseLiquidityV3({
397
- network,
398
- positionManagerAddress: pm,
399
- abi: opts.abi ? JSON.parse(opts.abi) : undefined,
461
+ execute: (sdk) => sdk.positions.v3.remove
462
+ .execute({
463
+ contracts: withContracts({ sunswapV3PositionManager: pm }),
400
464
  tokenId: opts.tokenId,
401
465
  liquidity: opts.liquidity,
402
466
  amount0Min: opts.min0,
403
467
  amount1Min: opts.min1,
468
+ collectFees: false,
404
469
  deadline: opts.deadline,
405
- }),
470
+ })
471
+ .then(mapTx),
406
472
  });
407
473
  });
408
474
  liq
@@ -426,13 +492,13 @@ function registerLiquidityCommands(program) {
426
492
  confirmMsg: 'Collect V3 fees?',
427
493
  spinnerLabel: 'Collecting V3 fees...',
428
494
  errorLabel: 'V3 collect failed',
429
- execute: (kit) => kit.collectPositionV3({
430
- network,
431
- positionManagerAddress: pm,
432
- abi: opts.abi ? JSON.parse(opts.abi) : undefined,
495
+ execute: (sdk) => sdk.positions.v3.collect
496
+ .execute({
497
+ contracts: withContracts({ sunswapV3PositionManager: pm }),
433
498
  tokenId: opts.tokenId,
434
499
  recipient: opts.recipient,
435
- }),
500
+ })
501
+ .then(mapTx),
436
502
  summarizeResult: (result) => {
437
503
  const out = {};
438
504
  if (result?.estimatedFees) {
@@ -481,13 +547,22 @@ function registerLiquidityCommands(program) {
481
547
  const token1Display = (0, tokens_1.getSymbolOrAddress)(token1Input, network);
482
548
  const token0Note = token0 !== token0Input ? ` → WTRX` : '';
483
549
  const token1Note = token1 !== token1Input ? ` → WTRX` : '';
550
+ const fee = opts.fee ? parseInt(opts.fee) : 3000;
551
+ const tickSpacing = tickSpacingForFee(fee);
552
+ const tickLower = opts.tickLower ? parseInt(opts.tickLower) : -2 * tickSpacing;
553
+ const tickUpper = opts.tickUpper ? parseInt(opts.tickUpper) : 2 * tickSpacing;
554
+ if (!opts.amount0 && !opts.amount1) {
555
+ console.error('Error: At least one of --amount0 or --amount1 is required');
556
+ process.exitCode = 1;
557
+ return;
558
+ }
484
559
  await (0, command_1.writeAction)({
485
560
  title: 'V4 Mint Position',
486
561
  summary: {
487
562
  Token0: `${token0Display}${token0Note} (${token0})`,
488
563
  Token1: `${token1Display}${token1Note} (${token1})`,
489
- Fee: opts.fee || '(auto)',
490
- 'Tick Range': opts.tickLower && opts.tickUpper ? `[${opts.tickLower}, ${opts.tickUpper}]` : '(auto)',
564
+ Fee: fee,
565
+ 'Tick Range': `[${tickLower}, ${tickUpper}]`,
491
566
  Amount0: opts.amount0 || '(auto)',
492
567
  Amount1: opts.amount1 || '(auto)',
493
568
  Slippage: opts.slippage
@@ -499,21 +574,23 @@ function registerLiquidityCommands(program) {
499
574
  confirmMsg: 'Mint V4 position?',
500
575
  spinnerLabel: 'Minting V4 position...',
501
576
  errorLabel: 'V4 mint failed',
502
- execute: (kit) => kit.mintPositionV4({
503
- network,
577
+ execute: (sdk) => sdk.liquidity.v4.add
578
+ .execute({
504
579
  token0,
505
580
  token1,
506
- fee: opts.fee ? parseInt(opts.fee) : undefined,
507
- tickLower: opts.tickLower ? parseInt(opts.tickLower) : undefined,
508
- tickUpper: opts.tickUpper ? parseInt(opts.tickUpper) : undefined,
509
- amount0Desired: opts.amount0,
510
- amount1Desired: opts.amount1,
511
- slippage: opts.slippage ? parseFloat(opts.slippage) : undefined,
512
- recipient: opts.recipient,
581
+ fee,
582
+ tickSpacing,
583
+ tickLower,
584
+ tickUpper,
585
+ amount0: opts.amount0,
586
+ amount1: opts.amount1,
587
+ slippage: percentFromDecimal(opts.slippage),
588
+ owner: opts.recipient,
513
589
  deadline: opts.deadline,
514
590
  sqrtPriceX96: opts.sqrtPrice,
515
- createPoolIfNeeded: opts.createPool,
516
- }),
591
+ createPool: opts.createPool,
592
+ })
593
+ .then(mapTx),
517
594
  summarizeResult: (result) => {
518
595
  const out = {};
519
596
  if (result?.poolCreated !== undefined)
@@ -559,6 +636,11 @@ function registerLiquidityCommands(program) {
559
636
  const token1 = toWTRXIfNative(token1Input, network);
560
637
  const token0Display = (0, tokens_1.getSymbolOrAddress)(token0Input, network);
561
638
  const token1Display = (0, tokens_1.getSymbolOrAddress)(token1Input, network);
639
+ if (!opts.amount0 && !opts.amount1) {
640
+ console.error('Error: At least one of --amount0 or --amount1 is required');
641
+ process.exitCode = 1;
642
+ return;
643
+ }
562
644
  await (0, command_1.writeAction)({
563
645
  title: 'V4 Increase Liquidity',
564
646
  summary: {
@@ -572,17 +654,15 @@ function registerLiquidityCommands(program) {
572
654
  confirmMsg: 'Increase V4 liquidity?',
573
655
  spinnerLabel: 'Increasing V4 liquidity...',
574
656
  errorLabel: 'V4 increase liquidity failed',
575
- execute: (kit) => kit.increaseLiquidityV4({
576
- network,
657
+ execute: (sdk) => sdk.liquidity.v4.increase
658
+ .execute({
577
659
  tokenId: opts.tokenId,
578
- token0,
579
- token1,
580
- fee: opts.fee ? parseInt(opts.fee) : undefined,
581
- amount0Desired: opts.amount0,
582
- amount1Desired: opts.amount1,
583
- slippage: opts.slippage ? parseFloat(opts.slippage) : undefined,
660
+ amount0: opts.amount0,
661
+ amount1: opts.amount1,
662
+ slippage: percentFromDecimal(opts.slippage),
584
663
  deadline: opts.deadline,
585
- }),
664
+ })
665
+ .then(mapTx),
586
666
  summarizeResult: (result) => {
587
667
  const out = {};
588
668
  if (result?.computedAmounts) {
@@ -635,18 +715,16 @@ function registerLiquidityCommands(program) {
635
715
  confirmMsg: 'Decrease V4 liquidity?',
636
716
  spinnerLabel: 'Decreasing V4 liquidity...',
637
717
  errorLabel: 'V4 decrease liquidity failed',
638
- execute: (kit) => kit.decreaseLiquidityV4({
639
- network,
718
+ execute: (sdk) => sdk.positions.v4.remove
719
+ .execute({
640
720
  tokenId: opts.tokenId,
641
721
  liquidity: opts.liquidity,
642
- token0,
643
- token1,
644
- fee: opts.fee ? parseInt(opts.fee) : undefined,
645
722
  amount0Min: opts.min0,
646
723
  amount1Min: opts.min1,
647
- slippage: opts.slippage ? parseFloat(opts.slippage) : undefined,
724
+ slippage: percentFromDecimal(opts.slippage),
648
725
  deadline: opts.deadline,
649
- }),
726
+ })
727
+ .then(mapTx),
650
728
  summarizeResult: (result) => {
651
729
  const out = {};
652
730
  if (result?.computedAmountMin) {
@@ -703,14 +781,12 @@ function registerLiquidityCommands(program) {
703
781
  confirmMsg: 'Collect V4 fees?',
704
782
  spinnerLabel: 'Collecting V4 fees...',
705
783
  errorLabel: 'V4 collect failed',
706
- execute: (kit) => kit.collectPositionV4({
707
- network,
784
+ execute: (sdk) => sdk.positions.v4.collect
785
+ .execute({
708
786
  tokenId: opts.tokenId,
709
- token0,
710
- token1,
711
- fee: opts.fee ? parseInt(opts.fee) : undefined,
712
787
  deadline: opts.deadline,
713
- }),
788
+ })
789
+ .then(mapTx),
714
790
  });
715
791
  });
716
792
  liq
@@ -722,15 +798,18 @@ function registerLiquidityCommands(program) {
722
798
  await (0, command_1.readAction)({
723
799
  spinnerLabel: 'Fetching V4 position info...',
724
800
  errorLabel: 'V4 position info failed',
725
- execute: (kit) => kit.getV4PositionInfo(opts.pm, opts.tokenId, (0, context_1.getNetwork)()),
801
+ execute: (sdk) => sdk.positions.v4.read({
802
+ tokenId: opts.tokenId,
803
+ contracts: withContracts({ sunswapV4PositionManager: opts.pm }),
804
+ }),
726
805
  transform: (result) => {
727
806
  if (!result)
728
807
  return { error: 'Position not found' };
729
808
  return {
730
- currency0: result.poolKey.currency0,
731
- currency1: result.poolKey.currency1,
732
- fee: result.poolKey.fee,
733
- tickSpacing: result.poolKey.tickSpacing,
809
+ currency0: result.poolKey?.currency0,
810
+ currency1: result.poolKey?.currency1,
811
+ fee: result.poolKey?.fee,
812
+ tickSpacing: result.poolKey?.tickSpacing,
734
813
  tickLower: result.tickLower,
735
814
  tickUpper: result.tickUpper,
736
815
  liquidity: result.liquidity,