@octaflowlabs/onchain-sdk 1.2.2 → 1.3.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.
@@ -1,3 +1,8 @@
1
- import { BuildMaxNativeTransferTxOptions, BuildMaxNativeTransferTxResponse, BuildUnsignedTransferTxOptions, UnsignedTransferTxResponse } from '../types/common';
2
- export declare const buildUnsignedTransferTx: (options: BuildUnsignedTransferTxOptions) => Promise<UnsignedTransferTxResponse>;
3
- export declare const buildMaxNativeTransferTx: (options: BuildMaxNativeTransferTxOptions) => Promise<BuildMaxNativeTransferTxResponse>;
1
+ import { BuildBaseUnsignedTransferTxParams, BuildMaxNativeTransferTxOptions, BuildUnsignedTransferTxOptions, PrepareTransactionResult } from '../types/common';
2
+ export declare const buildBaseUnsignedTransferTx: ({ recipientAddress, value, tokenAddress, tokenDecimals, }: BuildBaseUnsignedTransferTxParams) => {
3
+ to: string;
4
+ data: string;
5
+ value: bigint;
6
+ };
7
+ export declare const buildUnsignedTransferTx: (options: BuildUnsignedTransferTxOptions) => Promise<PrepareTransactionResult>;
8
+ export declare const buildMaxNativeTransferTx: (options: BuildMaxNativeTransferTxOptions) => Promise<string>;
@@ -1,97 +1,46 @@
1
1
  /** npm imports */
2
- import { Interface, formatUnits, parseUnits } from 'ethers';
2
+ import { Interface, formatUnits, parseEther, parseUnits } from 'ethers';
3
3
  /** local imports */
4
4
  import { getProvider } from './getProvider';
5
- import { estimateGasLimitFromProvider } from './estimateGasLimitFromProvider';
5
+ import { prepareTransaction } from './prepareTransaction';
6
+ export const buildBaseUnsignedTransferTx = ({ recipientAddress, value, tokenAddress, tokenDecimals, }) => {
7
+ const isNative = !tokenAddress ||
8
+ tokenAddress.toLowerCase() === '0x0000000000000000000000000000000000000000' ||
9
+ tokenAddress.toLowerCase() === '0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee';
10
+ if (!isNative) {
11
+ const erc20Interface = new Interface(['function transfer(address to, uint256 amount)']);
12
+ const decimals = tokenDecimals ?? 18;
13
+ const amountParsed = parseUnits(value, decimals);
14
+ return {
15
+ to: tokenAddress,
16
+ data: erc20Interface.encodeFunctionData('transfer', [recipientAddress, amountParsed]),
17
+ value: 0n,
18
+ };
19
+ }
20
+ return {
21
+ to: recipientAddress,
22
+ data: '0x',
23
+ value: parseEther(value),
24
+ };
25
+ };
6
26
  export const buildUnsignedTransferTx = async (options) => {
7
27
  const provider = getProvider(options.rpcUrl, options.chainId);
8
28
  if (!provider)
9
29
  throw new Error('Could not create provider with given rpcUrl and chainId');
10
30
  try {
11
- let unsignedTx = { to: options.tokenAddress };
12
- const isNativeToken = !options.tokenAddress ||
13
- options.tokenAddress?.toLowerCase() === '0x0000000000000000000000000000000000000000' ||
14
- options.tokenAddress?.toLowerCase() === '0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee';
15
- if (!isNativeToken) {
16
- if (!options.tokenAddress)
17
- throw new Error('Token address is required for token transfer');
18
- const code = await provider.getCode(options.tokenAddress);
19
- if (code === '0x' || code === '0x0')
20
- throw new Error('Invalid token address');
21
- const erc20Interface = new Interface(['function transfer(address to, uint256 amount)']);
22
- const decimals = options.tokenDecimals ?? 18;
23
- if (!options.value)
24
- throw new Error('Value is required for token transfer');
25
- const amountParsed = parseUnits(options.value, decimals);
26
- unsignedTx = {
27
- ...unsignedTx,
28
- data: erc20Interface.encodeFunctionData('transfer', [options.toAddress, amountParsed]),
29
- value: 0n,
30
- };
31
- }
32
- else {
33
- if (!options.value)
34
- throw new Error('Native transfer requires value');
35
- unsignedTx = {
36
- to: options.toAddress,
37
- data: '0x',
38
- value: parseUnits(options.value, 18),
39
- };
40
- }
41
- if (options.chainId)
42
- unsignedTx.chainId = options.chainId;
43
- const nonce = await provider.getTransactionCount(options.fromAddress, 'pending');
44
- const estimateGas = await estimateGasLimitFromProvider({
45
- provider: provider,
46
- unsignedTx,
47
- walletAddress: options.fromAddress,
31
+ const baseUnsignedTx = buildBaseUnsignedTransferTx({
32
+ recipientAddress: options.toAddress,
33
+ value: options.value ?? '0',
34
+ tokenAddress: options.tokenAddress,
35
+ tokenDecimals: options.tokenDecimals,
36
+ });
37
+ return prepareTransaction({
38
+ rpcUrl: options.rpcUrl,
39
+ chainId: options.chainId ?? Number((await provider.getNetwork()).chainId),
40
+ tx: baseUnsignedTx,
41
+ fromAddress: options.fromAddress,
48
42
  defaultGasLimit: options.defaultGasLimit,
49
43
  });
50
- const unsignedTxToReturn = {
51
- from: options.fromAddress,
52
- to: unsignedTx.to,
53
- data: unsignedTx.data,
54
- value: unsignedTx.value?.toString(),
55
- gasLimit: estimateGas.gasLimit.toString(),
56
- chainId: unsignedTx.chainId,
57
- nonce,
58
- maxFeePerGas: estimateGas.feeData.maxFeePerGas?.toString(),
59
- maxPriorityFeePerGas: estimateGas.feeData.maxPriorityFeePerGas?.toString(),
60
- };
61
- if (estimateGas.feeData.gasPrice)
62
- unsignedTxToReturn.gasPrice = estimateGas.feeData.gasPrice.toString();
63
- const gasReserve = estimateGas.feeData.maxFeePerGas
64
- ? estimateGas.gasLimit * estimateGas.feeData.maxFeePerGas
65
- : estimateGas.feeData.gasPrice
66
- ? estimateGas.gasLimit * estimateGas.feeData.gasPrice
67
- : undefined;
68
- try {
69
- await provider.call({
70
- from: options.fromAddress,
71
- to: unsignedTx.to,
72
- data: unsignedTx.data,
73
- value: unsignedTx.value,
74
- gasLimit: estimateGas.gasLimit,
75
- });
76
- }
77
- catch (error) {
78
- throw new Error('Transaction would revert, provider call unsuccessful: ' + error.message || error);
79
- }
80
- return {
81
- unsignedTx: unsignedTxToReturn,
82
- nonce,
83
- gasEstimated: estimateGas.gasEstimated.toString(),
84
- gasLimit: estimateGas.gasLimit.toString(),
85
- gasReserve: gasReserve?.toString(),
86
- bufferPercentage: estimateGas.bufferPercentage,
87
- feeData: {
88
- maxFeePerGas: estimateGas.feeData.maxFeePerGas?.toString(),
89
- maxPriorityFeePerGas: estimateGas.feeData.maxPriorityFeePerGas?.toString(),
90
- gasPrice: estimateGas.feeData.gasPrice?.toString(),
91
- },
92
- // suggestedGasFees: estimateGas.suggestedGasFees,
93
- // humanReadableFees: humanReadableGasEstimation,
94
- };
95
44
  }
96
45
  catch (error) {
97
46
  console.error('Error building unsigned transfer transaction:', error);
@@ -99,31 +48,29 @@ export const buildUnsignedTransferTx = async (options) => {
99
48
  }
100
49
  };
101
50
  export const buildMaxNativeTransferTx = async (options) => {
102
- const isNativeToken = !options.tokenAddress ||
103
- options.tokenAddress?.toLowerCase() === '0x0000000000000000000000000000000000000000' ||
104
- options.tokenAddress?.toLowerCase() === '0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee';
105
- if (!isNativeToken)
106
- throw new Error('Max native transfer requires a native token address');
107
- const provisionalTx = await buildUnsignedTransferTx({
108
- ...options,
109
- value: '0',
110
- tokenAddress: options.tokenAddress,
51
+ const provider = getProvider(options.rpcUrl, options.chainId);
52
+ if (!provider)
53
+ throw new Error('Could not create provider with given rpcUrl and chainId');
54
+ const network = await provider.getNetwork();
55
+ const chainId = options.chainId ?? Number(network.chainId);
56
+ const provisionalTx = await prepareTransaction({
57
+ rpcUrl: options.rpcUrl,
58
+ chainId,
59
+ tx: {
60
+ to: options.toAddress,
61
+ data: '0x',
62
+ value: 0n,
63
+ },
64
+ fromAddress: options.fromAddress,
65
+ defaultGasLimit: options.defaultGasLimit,
111
66
  });
112
67
  if (!provisionalTx.gasReserve)
113
68
  throw new Error('Could not determine gas reserve for max send');
114
- const balanceWei = parseUnits(options.balance, 18);
69
+ const balanceWei = parseEther(options.balance);
115
70
  const gasReserveWei = BigInt(provisionalTx.gasReserve);
116
71
  const sendableWei = balanceWei - gasReserveWei;
117
72
  if (sendableWei <= 0n)
118
73
  throw new Error('Insufficient balance to cover gas reserve');
119
74
  const sendableValue = formatUnits(sendableWei, 18);
120
- const finalTx = await buildUnsignedTransferTx({
121
- ...options,
122
- value: sendableValue,
123
- tokenAddress: options.tokenAddress,
124
- });
125
- return {
126
- ...finalTx,
127
- sendableValue,
128
- };
75
+ return sendableValue;
129
76
  };
@@ -0,0 +1,2 @@
1
+ import { EstimateTransactionOptions, EstimateTransactionResult } from '../types/common';
2
+ export declare const estimateTransaction: ({ rpcUrl, chainId, tx, fromAddress, defaultGasLimit, }: EstimateTransactionOptions) => Promise<EstimateTransactionResult>;
@@ -0,0 +1,29 @@
1
+ /** local imports */
2
+ import { estimateGasLimitFromProvider } from './estimateGasLimitFromProvider';
3
+ import { getProvider } from './getProvider';
4
+ import { formatUnits } from 'ethers';
5
+ export const estimateTransaction = async ({ rpcUrl, chainId, tx, fromAddress, defaultGasLimit, }) => {
6
+ const provider = getProvider(rpcUrl, chainId);
7
+ if (!provider)
8
+ throw new Error('Could not get provider');
9
+ const estimateGas = await estimateGasLimitFromProvider({
10
+ provider,
11
+ unsignedTx: tx,
12
+ walletAddress: fromAddress,
13
+ defaultGasLimit: defaultGasLimit || 21000n,
14
+ });
15
+ const gasReserve = estimateGas.feeData.maxFeePerGas
16
+ ? estimateGas.gasLimit * estimateGas.feeData.maxFeePerGas
17
+ : estimateGas.feeData.gasPrice
18
+ ? estimateGas.gasLimit * estimateGas.feeData.gasPrice
19
+ : undefined;
20
+ const humanReadableGasReserve = gasReserve ? `${formatUnits(gasReserve, 18)}` : 'N/A';
21
+ return {
22
+ gasLimit: estimateGas.gasLimit,
23
+ gasEstimated: estimateGas.gasEstimated,
24
+ feeData: estimateGas.feeData,
25
+ gasReserve,
26
+ humanReadableGasReserve,
27
+ bufferPercentage: estimateGas.bufferPercentage,
28
+ };
29
+ };
@@ -0,0 +1,2 @@
1
+ import { PrepareTransactionParams, PrepareTransactionResult } from '../types/common';
2
+ export declare const prepareTransaction: ({ rpcUrl, chainId, tx, fromAddress, defaultGasLimit, }: PrepareTransactionParams) => Promise<PrepareTransactionResult>;
@@ -0,0 +1,43 @@
1
+ /** local imports */
2
+ import { estimateTransaction } from './estimateTransaction';
3
+ import { getProvider } from './getProvider';
4
+ export const prepareTransaction = async ({ rpcUrl, chainId, tx, fromAddress, defaultGasLimit, }) => {
5
+ const provider = getProvider(rpcUrl, chainId);
6
+ if (!provider)
7
+ throw new Error('Could not create provider');
8
+ const estimation = await estimateTransaction({
9
+ rpcUrl,
10
+ chainId,
11
+ tx,
12
+ fromAddress,
13
+ defaultGasLimit,
14
+ });
15
+ const nonce = await provider.getTransactionCount(fromAddress, 'pending');
16
+ const preparedTx = {
17
+ from: fromAddress,
18
+ to: tx.to,
19
+ data: tx.data,
20
+ value: tx.value,
21
+ gasLimit: estimation.gasLimit,
22
+ chainId,
23
+ nonce,
24
+ maxFeePerGas: estimation.feeData.maxFeePerGas,
25
+ maxPriorityFeePerGas: estimation.feeData.maxPriorityFeePerGas,
26
+ };
27
+ if (estimation.feeData.gasPrice)
28
+ preparedTx.gasPrice = estimation.feeData.gasPrice;
29
+ return {
30
+ unsignedTx: preparedTx,
31
+ nonce,
32
+ gasEstimated: estimation.gasEstimated,
33
+ gasLimit: estimation.gasLimit,
34
+ gasReserve: estimation.gasReserve,
35
+ bufferPercentage: estimation.bufferPercentage,
36
+ feeData: {
37
+ maxFeePerGas: estimation.feeData.maxFeePerGas,
38
+ maxPriorityFeePerGas: estimation.feeData.maxPriorityFeePerGas,
39
+ gasPrice: estimation.feeData.gasPrice,
40
+ },
41
+ humanReadableGasReserve: estimation.humanReadableGasReserve,
42
+ };
43
+ };
@@ -1,3 +1,8 @@
1
- import { BuildMaxNativeTransferTxOptions, BuildMaxNativeTransferTxResponse, BuildUnsignedTransferTxOptions, UnsignedTransferTxResponse } from '../types/common';
2
- export declare const buildUnsignedTransferTx: (options: BuildUnsignedTransferTxOptions) => Promise<UnsignedTransferTxResponse>;
3
- export declare const buildMaxNativeTransferTx: (options: BuildMaxNativeTransferTxOptions) => Promise<BuildMaxNativeTransferTxResponse>;
1
+ import { BuildBaseUnsignedTransferTxParams, BuildMaxNativeTransferTxOptions, BuildUnsignedTransferTxOptions, PrepareTransactionResult } from '../types/common';
2
+ export declare const buildBaseUnsignedTransferTx: ({ recipientAddress, value, tokenAddress, tokenDecimals, }: BuildBaseUnsignedTransferTxParams) => {
3
+ to: string;
4
+ data: string;
5
+ value: bigint;
6
+ };
7
+ export declare const buildUnsignedTransferTx: (options: BuildUnsignedTransferTxOptions) => Promise<PrepareTransactionResult>;
8
+ export declare const buildMaxNativeTransferTx: (options: BuildMaxNativeTransferTxOptions) => Promise<string>;
@@ -1,100 +1,50 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.buildMaxNativeTransferTx = exports.buildUnsignedTransferTx = void 0;
3
+ exports.buildMaxNativeTransferTx = exports.buildUnsignedTransferTx = exports.buildBaseUnsignedTransferTx = void 0;
4
4
  /** npm imports */
5
5
  const ethers_1 = require("ethers");
6
6
  /** local imports */
7
7
  const getProvider_1 = require("./getProvider");
8
- const estimateGasLimitFromProvider_1 = require("./estimateGasLimitFromProvider");
8
+ const prepareTransaction_1 = require("./prepareTransaction");
9
+ const buildBaseUnsignedTransferTx = ({ recipientAddress, value, tokenAddress, tokenDecimals, }) => {
10
+ const isNative = !tokenAddress ||
11
+ tokenAddress.toLowerCase() === '0x0000000000000000000000000000000000000000' ||
12
+ tokenAddress.toLowerCase() === '0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee';
13
+ if (!isNative) {
14
+ const erc20Interface = new ethers_1.Interface(['function transfer(address to, uint256 amount)']);
15
+ const decimals = tokenDecimals ?? 18;
16
+ const amountParsed = (0, ethers_1.parseUnits)(value, decimals);
17
+ return {
18
+ to: tokenAddress,
19
+ data: erc20Interface.encodeFunctionData('transfer', [recipientAddress, amountParsed]),
20
+ value: 0n,
21
+ };
22
+ }
23
+ return {
24
+ to: recipientAddress,
25
+ data: '0x',
26
+ value: (0, ethers_1.parseEther)(value),
27
+ };
28
+ };
29
+ exports.buildBaseUnsignedTransferTx = buildBaseUnsignedTransferTx;
9
30
  const buildUnsignedTransferTx = async (options) => {
10
31
  const provider = (0, getProvider_1.getProvider)(options.rpcUrl, options.chainId);
11
32
  if (!provider)
12
33
  throw new Error('Could not create provider with given rpcUrl and chainId');
13
34
  try {
14
- let unsignedTx = { to: options.tokenAddress };
15
- const isNativeToken = !options.tokenAddress ||
16
- options.tokenAddress?.toLowerCase() === '0x0000000000000000000000000000000000000000' ||
17
- options.tokenAddress?.toLowerCase() === '0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee';
18
- if (!isNativeToken) {
19
- if (!options.tokenAddress)
20
- throw new Error('Token address is required for token transfer');
21
- const code = await provider.getCode(options.tokenAddress);
22
- if (code === '0x' || code === '0x0')
23
- throw new Error('Invalid token address');
24
- const erc20Interface = new ethers_1.Interface(['function transfer(address to, uint256 amount)']);
25
- const decimals = options.tokenDecimals ?? 18;
26
- if (!options.value)
27
- throw new Error('Value is required for token transfer');
28
- const amountParsed = (0, ethers_1.parseUnits)(options.value, decimals);
29
- unsignedTx = {
30
- ...unsignedTx,
31
- data: erc20Interface.encodeFunctionData('transfer', [options.toAddress, amountParsed]),
32
- value: 0n,
33
- };
34
- }
35
- else {
36
- if (!options.value)
37
- throw new Error('Native transfer requires value');
38
- unsignedTx = {
39
- to: options.toAddress,
40
- data: '0x',
41
- value: (0, ethers_1.parseUnits)(options.value, 18),
42
- };
43
- }
44
- if (options.chainId)
45
- unsignedTx.chainId = options.chainId;
46
- const nonce = await provider.getTransactionCount(options.fromAddress, 'pending');
47
- const estimateGas = await (0, estimateGasLimitFromProvider_1.estimateGasLimitFromProvider)({
48
- provider: provider,
49
- unsignedTx,
50
- walletAddress: options.fromAddress,
35
+ const baseUnsignedTx = (0, exports.buildBaseUnsignedTransferTx)({
36
+ recipientAddress: options.toAddress,
37
+ value: options.value ?? '0',
38
+ tokenAddress: options.tokenAddress,
39
+ tokenDecimals: options.tokenDecimals,
40
+ });
41
+ return (0, prepareTransaction_1.prepareTransaction)({
42
+ rpcUrl: options.rpcUrl,
43
+ chainId: options.chainId ?? Number((await provider.getNetwork()).chainId),
44
+ tx: baseUnsignedTx,
45
+ fromAddress: options.fromAddress,
51
46
  defaultGasLimit: options.defaultGasLimit,
52
47
  });
53
- const unsignedTxToReturn = {
54
- from: options.fromAddress,
55
- to: unsignedTx.to,
56
- data: unsignedTx.data,
57
- value: unsignedTx.value?.toString(),
58
- gasLimit: estimateGas.gasLimit.toString(),
59
- chainId: unsignedTx.chainId,
60
- nonce,
61
- maxFeePerGas: estimateGas.feeData.maxFeePerGas?.toString(),
62
- maxPriorityFeePerGas: estimateGas.feeData.maxPriorityFeePerGas?.toString(),
63
- };
64
- if (estimateGas.feeData.gasPrice)
65
- unsignedTxToReturn.gasPrice = estimateGas.feeData.gasPrice.toString();
66
- const gasReserve = estimateGas.feeData.maxFeePerGas
67
- ? estimateGas.gasLimit * estimateGas.feeData.maxFeePerGas
68
- : estimateGas.feeData.gasPrice
69
- ? estimateGas.gasLimit * estimateGas.feeData.gasPrice
70
- : undefined;
71
- try {
72
- await provider.call({
73
- from: options.fromAddress,
74
- to: unsignedTx.to,
75
- data: unsignedTx.data,
76
- value: unsignedTx.value,
77
- gasLimit: estimateGas.gasLimit,
78
- });
79
- }
80
- catch (error) {
81
- throw new Error('Transaction would revert, provider call unsuccessful: ' + error.message || error);
82
- }
83
- return {
84
- unsignedTx: unsignedTxToReturn,
85
- nonce,
86
- gasEstimated: estimateGas.gasEstimated.toString(),
87
- gasLimit: estimateGas.gasLimit.toString(),
88
- gasReserve: gasReserve?.toString(),
89
- bufferPercentage: estimateGas.bufferPercentage,
90
- feeData: {
91
- maxFeePerGas: estimateGas.feeData.maxFeePerGas?.toString(),
92
- maxPriorityFeePerGas: estimateGas.feeData.maxPriorityFeePerGas?.toString(),
93
- gasPrice: estimateGas.feeData.gasPrice?.toString(),
94
- },
95
- // suggestedGasFees: estimateGas.suggestedGasFees,
96
- // humanReadableFees: humanReadableGasEstimation,
97
- };
98
48
  }
99
49
  catch (error) {
100
50
  console.error('Error building unsigned transfer transaction:', error);
@@ -103,32 +53,30 @@ const buildUnsignedTransferTx = async (options) => {
103
53
  };
104
54
  exports.buildUnsignedTransferTx = buildUnsignedTransferTx;
105
55
  const buildMaxNativeTransferTx = async (options) => {
106
- const isNativeToken = !options.tokenAddress ||
107
- options.tokenAddress?.toLowerCase() === '0x0000000000000000000000000000000000000000' ||
108
- options.tokenAddress?.toLowerCase() === '0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee';
109
- if (!isNativeToken)
110
- throw new Error('Max native transfer requires a native token address');
111
- const provisionalTx = await (0, exports.buildUnsignedTransferTx)({
112
- ...options,
113
- value: '0',
114
- tokenAddress: options.tokenAddress,
56
+ const provider = (0, getProvider_1.getProvider)(options.rpcUrl, options.chainId);
57
+ if (!provider)
58
+ throw new Error('Could not create provider with given rpcUrl and chainId');
59
+ const network = await provider.getNetwork();
60
+ const chainId = options.chainId ?? Number(network.chainId);
61
+ const provisionalTx = await (0, prepareTransaction_1.prepareTransaction)({
62
+ rpcUrl: options.rpcUrl,
63
+ chainId,
64
+ tx: {
65
+ to: options.toAddress,
66
+ data: '0x',
67
+ value: 0n,
68
+ },
69
+ fromAddress: options.fromAddress,
70
+ defaultGasLimit: options.defaultGasLimit,
115
71
  });
116
72
  if (!provisionalTx.gasReserve)
117
73
  throw new Error('Could not determine gas reserve for max send');
118
- const balanceWei = (0, ethers_1.parseUnits)(options.balance, 18);
74
+ const balanceWei = (0, ethers_1.parseEther)(options.balance);
119
75
  const gasReserveWei = BigInt(provisionalTx.gasReserve);
120
76
  const sendableWei = balanceWei - gasReserveWei;
121
77
  if (sendableWei <= 0n)
122
78
  throw new Error('Insufficient balance to cover gas reserve');
123
79
  const sendableValue = (0, ethers_1.formatUnits)(sendableWei, 18);
124
- const finalTx = await (0, exports.buildUnsignedTransferTx)({
125
- ...options,
126
- value: sendableValue,
127
- tokenAddress: options.tokenAddress,
128
- });
129
- return {
130
- ...finalTx,
131
- sendableValue,
132
- };
80
+ return sendableValue;
133
81
  };
134
82
  exports.buildMaxNativeTransferTx = buildMaxNativeTransferTx;
@@ -0,0 +1,2 @@
1
+ import { EstimateTransactionOptions, EstimateTransactionResult } from '../types/common';
2
+ export declare const estimateTransaction: ({ rpcUrl, chainId, tx, fromAddress, defaultGasLimit, }: EstimateTransactionOptions) => Promise<EstimateTransactionResult>;
@@ -0,0 +1,33 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.estimateTransaction = void 0;
4
+ /** local imports */
5
+ const estimateGasLimitFromProvider_1 = require("./estimateGasLimitFromProvider");
6
+ const getProvider_1 = require("./getProvider");
7
+ const ethers_1 = require("ethers");
8
+ const estimateTransaction = async ({ rpcUrl, chainId, tx, fromAddress, defaultGasLimit, }) => {
9
+ const provider = (0, getProvider_1.getProvider)(rpcUrl, chainId);
10
+ if (!provider)
11
+ throw new Error('Could not get provider');
12
+ const estimateGas = await (0, estimateGasLimitFromProvider_1.estimateGasLimitFromProvider)({
13
+ provider,
14
+ unsignedTx: tx,
15
+ walletAddress: fromAddress,
16
+ defaultGasLimit: defaultGasLimit || 21000n,
17
+ });
18
+ const gasReserve = estimateGas.feeData.maxFeePerGas
19
+ ? estimateGas.gasLimit * estimateGas.feeData.maxFeePerGas
20
+ : estimateGas.feeData.gasPrice
21
+ ? estimateGas.gasLimit * estimateGas.feeData.gasPrice
22
+ : undefined;
23
+ const humanReadableGasReserve = gasReserve ? `${(0, ethers_1.formatUnits)(gasReserve, 18)}` : 'N/A';
24
+ return {
25
+ gasLimit: estimateGas.gasLimit,
26
+ gasEstimated: estimateGas.gasEstimated,
27
+ feeData: estimateGas.feeData,
28
+ gasReserve,
29
+ humanReadableGasReserve,
30
+ bufferPercentage: estimateGas.bufferPercentage,
31
+ };
32
+ };
33
+ exports.estimateTransaction = estimateTransaction;
@@ -0,0 +1,2 @@
1
+ import { PrepareTransactionParams, PrepareTransactionResult } from '../types/common';
2
+ export declare const prepareTransaction: ({ rpcUrl, chainId, tx, fromAddress, defaultGasLimit, }: PrepareTransactionParams) => Promise<PrepareTransactionResult>;
@@ -0,0 +1,47 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.prepareTransaction = void 0;
4
+ /** local imports */
5
+ const estimateTransaction_1 = require("./estimateTransaction");
6
+ const getProvider_1 = require("./getProvider");
7
+ const prepareTransaction = async ({ rpcUrl, chainId, tx, fromAddress, defaultGasLimit, }) => {
8
+ const provider = (0, getProvider_1.getProvider)(rpcUrl, chainId);
9
+ if (!provider)
10
+ throw new Error('Could not create provider');
11
+ const estimation = await (0, estimateTransaction_1.estimateTransaction)({
12
+ rpcUrl,
13
+ chainId,
14
+ tx,
15
+ fromAddress,
16
+ defaultGasLimit,
17
+ });
18
+ const nonce = await provider.getTransactionCount(fromAddress, 'pending');
19
+ const preparedTx = {
20
+ from: fromAddress,
21
+ to: tx.to,
22
+ data: tx.data,
23
+ value: tx.value,
24
+ gasLimit: estimation.gasLimit,
25
+ chainId,
26
+ nonce,
27
+ maxFeePerGas: estimation.feeData.maxFeePerGas,
28
+ maxPriorityFeePerGas: estimation.feeData.maxPriorityFeePerGas,
29
+ };
30
+ if (estimation.feeData.gasPrice)
31
+ preparedTx.gasPrice = estimation.feeData.gasPrice;
32
+ return {
33
+ unsignedTx: preparedTx,
34
+ nonce,
35
+ gasEstimated: estimation.gasEstimated,
36
+ gasLimit: estimation.gasLimit,
37
+ gasReserve: estimation.gasReserve,
38
+ bufferPercentage: estimation.bufferPercentage,
39
+ feeData: {
40
+ maxFeePerGas: estimation.feeData.maxFeePerGas,
41
+ maxPriorityFeePerGas: estimation.feeData.maxPriorityFeePerGas,
42
+ gasPrice: estimation.feeData.gasPrice,
43
+ },
44
+ humanReadableGasReserve: estimation.humanReadableGasReserve,
45
+ };
46
+ };
47
+ exports.prepareTransaction = prepareTransaction;
@@ -5,12 +5,14 @@ export { ERC20_TOKEN_CONTRACT_ABI };
5
5
  export { GAS_LIMIT_PER_TX_TYPE, MULTICALL3_ADDRESS } from './constants/constants';
6
6
  export { NETWORKS_REGISTRY, NetworkField, NetworkRegistry } from './constants/NETWORKS_REGISTRY';
7
7
  /** basic blockchain exports */
8
- export { buildMaxNativeTransferTx, buildUnsignedTransferTx, } from './blockchain/buildUnsignedTransferTx';
8
+ export { buildBaseUnsignedTransferTx, buildMaxNativeTransferTx, buildUnsignedTransferTx, } from './blockchain/buildUnsignedTransferTx';
9
9
  export { broadcastTransaction } from './blockchain/broadcastTransaction';
10
10
  export { estimateGasLimitFromProvider } from './blockchain/estimateGasLimitFromProvider';
11
11
  export { getProvider } from './blockchain/getProvider';
12
12
  export { txStatus } from './blockchain/txStatus';
13
13
  export { getBalance, getBalances } from './blockchain/getBalances';
14
+ export { estimateTransaction } from './blockchain/estimateTransaction';
15
+ export { prepareTransaction } from './blockchain/prepareTransaction';
14
16
  /** services exports */
15
17
  export { EvmWalletService, EvmGeneratedWallet, EvmDerivedWallet, } from './services/evm-wallet-core/evmWalletService';
16
18
  export { EntropySource } from './services/evm-wallet-core/entropy';
@@ -24,4 +26,4 @@ export { formattedAmountForDisplay, parsedAmount } from './utils/formatAmount';
24
26
  export { handleErrorMessages, errorMessagesForBroadcast, errorMessagesForGasLimitEstimation, } from './utils/handleErrorMessages';
25
27
  export { normalizeAddress } from './utils/normalizeAddress';
26
28
  /** types exports */
27
- export { BroadcastTransactionOptions, BuildMaxNativeTransferTxOptions, BuildMaxNativeTransferTxResponse, BuildUnsignedTransferTxOptions, EstimateGasLimitFromProviderProps, GasEstimateResult, TxStatusOptions, TxStatusResponse, UnsignedTransferTxResponse, FormatAmountOptions, TransactionRequest, GetBalanceParams, GetBalancesParams, GetBalancesChainRequest, GetBalanceResult, ChainBalances, TokenBalance, ChainGroup, } from './types/common';
29
+ export { BroadcastTransactionOptions, BuildMaxNativeTransferTxOptions, BuildUnsignedTransferTxOptions, EstimateGasLimitFromProviderProps, GasEstimateResult, TxStatusOptions, BuildBaseUnsignedTransferTxParams, TxStatusResponse, EstimateTransactionOptions, EstimateTransactionResult, PrepareTransactionParams, PrepareTransactionResult, FormatAmountOptions, TransactionRequest, GetBalanceParams, GetBalancesParams, GetBalancesChainRequest, GetBalanceResult, ChainBalances, TokenBalance, ChainGroup, } from './types/common';
package/dist/cjs/index.js CHANGED
@@ -3,7 +3,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.normalizeAddress = exports.errorMessagesForGasLimitEstimation = exports.errorMessagesForBroadcast = exports.handleErrorMessages = exports.parsedAmount = exports.formattedAmountForDisplay = exports.NATIVE_TOKENS = exports.transformBigInt = exports.getShortenData = exports.getShortenTransactionHashOrAddress = exports.signTransaction = exports.signMessage = exports.createWallet = exports.EvmWalletService = exports.getBalances = exports.getBalance = exports.txStatus = exports.getProvider = exports.estimateGasLimitFromProvider = exports.broadcastTransaction = exports.buildUnsignedTransferTx = exports.buildMaxNativeTransferTx = exports.NETWORKS_REGISTRY = exports.MULTICALL3_ADDRESS = exports.GAS_LIMIT_PER_TX_TYPE = exports.ERC20_TOKEN_CONTRACT_ABI = void 0;
6
+ exports.normalizeAddress = exports.errorMessagesForGasLimitEstimation = exports.errorMessagesForBroadcast = exports.handleErrorMessages = exports.parsedAmount = exports.formattedAmountForDisplay = exports.NATIVE_TOKENS = exports.transformBigInt = exports.getShortenData = exports.getShortenTransactionHashOrAddress = exports.signTransaction = exports.signMessage = exports.createWallet = exports.EvmWalletService = exports.prepareTransaction = exports.estimateTransaction = exports.getBalances = exports.getBalance = exports.txStatus = exports.getProvider = exports.estimateGasLimitFromProvider = exports.broadcastTransaction = exports.buildUnsignedTransferTx = exports.buildMaxNativeTransferTx = exports.buildBaseUnsignedTransferTx = exports.NETWORKS_REGISTRY = exports.MULTICALL3_ADDRESS = exports.GAS_LIMIT_PER_TX_TYPE = exports.ERC20_TOKEN_CONTRACT_ABI = void 0;
7
7
  /** ABIs exports */
8
8
  const ERC20_TOKEN_CONTRACT_ABI_1 = __importDefault(require("./ABIs/ERC20_TOKEN_CONTRACT_ABI"));
9
9
  exports.ERC20_TOKEN_CONTRACT_ABI = ERC20_TOKEN_CONTRACT_ABI_1.default;
@@ -15,6 +15,7 @@ var NETWORKS_REGISTRY_1 = require("./constants/NETWORKS_REGISTRY");
15
15
  Object.defineProperty(exports, "NETWORKS_REGISTRY", { enumerable: true, get: function () { return NETWORKS_REGISTRY_1.NETWORKS_REGISTRY; } });
16
16
  /** basic blockchain exports */
17
17
  var buildUnsignedTransferTx_1 = require("./blockchain/buildUnsignedTransferTx");
18
+ Object.defineProperty(exports, "buildBaseUnsignedTransferTx", { enumerable: true, get: function () { return buildUnsignedTransferTx_1.buildBaseUnsignedTransferTx; } });
18
19
  Object.defineProperty(exports, "buildMaxNativeTransferTx", { enumerable: true, get: function () { return buildUnsignedTransferTx_1.buildMaxNativeTransferTx; } });
19
20
  Object.defineProperty(exports, "buildUnsignedTransferTx", { enumerable: true, get: function () { return buildUnsignedTransferTx_1.buildUnsignedTransferTx; } });
20
21
  var broadcastTransaction_1 = require("./blockchain/broadcastTransaction");
@@ -28,6 +29,10 @@ Object.defineProperty(exports, "txStatus", { enumerable: true, get: function ()
28
29
  var getBalances_1 = require("./blockchain/getBalances");
29
30
  Object.defineProperty(exports, "getBalance", { enumerable: true, get: function () { return getBalances_1.getBalance; } });
30
31
  Object.defineProperty(exports, "getBalances", { enumerable: true, get: function () { return getBalances_1.getBalances; } });
32
+ var estimateTransaction_1 = require("./blockchain/estimateTransaction");
33
+ Object.defineProperty(exports, "estimateTransaction", { enumerable: true, get: function () { return estimateTransaction_1.estimateTransaction; } });
34
+ var prepareTransaction_1 = require("./blockchain/prepareTransaction");
35
+ Object.defineProperty(exports, "prepareTransaction", { enumerable: true, get: function () { return prepareTransaction_1.prepareTransaction; } });
31
36
  /** services exports */
32
37
  var evmWalletService_1 = require("./services/evm-wallet-core/evmWalletService");
33
38
  Object.defineProperty(exports, "EvmWalletService", { enumerable: true, get: function () { return evmWalletService_1.EvmWalletService; } });
@@ -20,6 +20,10 @@ export declare class EvmWalletService {
20
20
  generateMultipleWallets(mnemonic: string, count: number, startIndex?: number): EvmDerivedWallet[];
21
21
  deriveWalletFromMnemonic(mnemonic: string, accountIndex?: number, customPath?: string): EvmDerivedWallet;
22
22
  deriveFromPrivateKey(privateKey: string): Omit<EvmDerivedWallet, 'path'>;
23
+ getFirstAvailableIndexForMnemonicImport(mnemonic: string, existingAddresses: string[], maxIndicesToCheck?: number): {
24
+ index: number;
25
+ wallet: EvmDerivedWallet;
26
+ } | null;
23
27
  signMessage(privateKey: string, message: string): Promise<string>;
24
28
  isValidMnemonic(mnemonic: string): boolean;
25
29
  private validateMnemonic;
@@ -58,6 +58,18 @@ class EvmWalletService {
58
58
  publicKey: wallet.signingKey.publicKey,
59
59
  };
60
60
  }
61
+ getFirstAvailableIndexForMnemonicImport(mnemonic, existingAddresses, maxIndicesToCheck = 20) {
62
+ this.validateMnemonic(mnemonic);
63
+ const existingSet = new Set(existingAddresses.map((addr) => (0, ethers_1.getAddress)(addr)));
64
+ for (let i = 0; i < maxIndicesToCheck; i++) {
65
+ const wallet = this.deriveWalletFromMnemonic(mnemonic, i);
66
+ const normalizedAddress = (0, ethers_1.getAddress)(wallet.address);
67
+ if (!existingSet.has(normalizedAddress)) {
68
+ return { index: i, wallet };
69
+ }
70
+ }
71
+ return null;
72
+ }
61
73
  async signMessage(privateKey, message) {
62
74
  const wallet = new ethers_1.Wallet(privateKey);
63
75
  return await wallet.signMessage(message);
@@ -72,9 +84,8 @@ class EvmWalletService {
72
84
  }
73
85
  }
74
86
  validateMnemonic(mnemonic) {
75
- if (!this.isValidMnemonic(mnemonic)) {
87
+ if (!this.isValidMnemonic(mnemonic))
76
88
  throw new Error('Invalid mnemonic phrase');
77
- }
78
89
  }
79
90
  getEntropyForWordCount(wordCount) {
80
91
  const entropyMap = {
@@ -17,36 +17,59 @@ export interface GasEstimateResult {
17
17
  gasPrice?: bigint;
18
18
  };
19
19
  }
20
+ export interface EstimateTransactionOptions {
21
+ rpcUrl: string;
22
+ chainId: number;
23
+ tx: TransactionRequest;
24
+ fromAddress: string;
25
+ defaultGasLimit?: bigint;
26
+ }
27
+ export interface PrepareTransactionParams extends EstimateTransactionOptions {
28
+ }
29
+ export interface EstimateTransactionResult {
30
+ gasLimit: bigint;
31
+ gasEstimated: bigint;
32
+ feeData: {
33
+ maxFeePerGas?: bigint;
34
+ maxPriorityFeePerGas?: bigint;
35
+ gasPrice?: bigint;
36
+ };
37
+ gasReserve?: bigint;
38
+ humanReadableGasReserve: string;
39
+ bufferPercentage: number;
40
+ }
41
+ export interface PrepareTransactionResult {
42
+ unsignedTx: TransactionRequest;
43
+ nonce: number;
44
+ gasEstimated: bigint;
45
+ gasLimit: bigint;
46
+ gasReserve?: bigint;
47
+ bufferPercentage: number;
48
+ feeData: {
49
+ maxFeePerGas?: bigint;
50
+ maxPriorityFeePerGas?: bigint;
51
+ gasPrice?: bigint;
52
+ };
53
+ humanReadableGasReserve: string;
54
+ }
55
+ export interface BuildBaseUnsignedTransferTxParams {
56
+ recipientAddress: string;
57
+ value: string;
58
+ tokenAddress?: string;
59
+ tokenDecimals?: number;
60
+ }
20
61
  export interface BuildUnsignedTransferTxOptions {
21
62
  fromAddress: string;
22
63
  toAddress: string;
23
64
  value?: string;
24
- data?: string;
25
65
  tokenAddress?: string;
26
66
  tokenDecimals?: number;
27
67
  rpcUrl: string;
28
68
  chainId?: number;
29
69
  defaultGasLimit: bigint;
30
70
  }
31
- export interface UnsignedTransferTxResponse {
32
- unsignedTx: TransactionRequest;
33
- nonce: number;
34
- gasEstimated: string;
35
- gasLimit: string;
36
- gasReserve?: string;
37
- bufferPercentage: number;
38
- feeData: {
39
- maxFeePerGas?: string;
40
- maxPriorityFeePerGas?: string;
41
- gasPrice?: string;
42
- };
43
- }
44
- export interface BuildMaxNativeTransferTxOptions extends Omit<BuildUnsignedTransferTxOptions, 'value' | 'tokenAddress' | 'tokenDecimals'> {
71
+ export interface BuildMaxNativeTransferTxOptions extends Omit<BuildUnsignedTransferTxOptions, 'value' | 'tokenDecimals'> {
45
72
  balance: string;
46
- tokenAddress?: string;
47
- }
48
- export interface BuildMaxNativeTransferTxResponse extends UnsignedTransferTxResponse {
49
- sendableValue: string;
50
73
  }
51
74
  export interface BroadcastTransactionOptions {
52
75
  signedTx: string;
@@ -1,2 +1,2 @@
1
- export declare const getShortenTransactionHashOrAddress: (txHashOrAddress: string) => string;
2
- export declare const getShortenData: (data: string) => string;
1
+ export declare const getShortenTransactionHashOrAddress: (txHashOrAddress: string, firstPartLength?: number, lastPartLength?: number) => string;
2
+ export declare const getShortenData: (data: string, firstPartLength?: number, lastPartLength?: number) => string;
@@ -1,15 +1,15 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.getShortenData = exports.getShortenTransactionHashOrAddress = void 0;
4
- const getShortenTransactionHashOrAddress = (txHashOrAddress) => {
5
- const firstCharacters = txHashOrAddress.substring(0, 6);
6
- const lastCharacters = txHashOrAddress.slice(-4);
4
+ const getShortenTransactionHashOrAddress = (txHashOrAddress, firstPartLength = 6, lastPartLength = 4) => {
5
+ const firstCharacters = txHashOrAddress.substring(0, firstPartLength);
6
+ const lastCharacters = txHashOrAddress.slice(-lastPartLength);
7
7
  return `${firstCharacters}...${lastCharacters}`;
8
8
  };
9
9
  exports.getShortenTransactionHashOrAddress = getShortenTransactionHashOrAddress;
10
- const getShortenData = (data) => {
11
- const firstCharacters = data.substring(0, 12);
12
- const lastCharacters = data.slice(-12);
10
+ const getShortenData = (data, firstPartLength = 12, lastPartLength = 12) => {
11
+ const firstCharacters = data.substring(0, firstPartLength);
12
+ const lastCharacters = data.slice(-lastPartLength);
13
13
  return `${firstCharacters}...${lastCharacters}`;
14
14
  };
15
15
  exports.getShortenData = getShortenData;
package/dist/index.d.ts CHANGED
@@ -5,12 +5,14 @@ export { ERC20_TOKEN_CONTRACT_ABI };
5
5
  export { GAS_LIMIT_PER_TX_TYPE, MULTICALL3_ADDRESS } from './constants/constants';
6
6
  export { NETWORKS_REGISTRY, NetworkField, NetworkRegistry } from './constants/NETWORKS_REGISTRY';
7
7
  /** basic blockchain exports */
8
- export { buildMaxNativeTransferTx, buildUnsignedTransferTx, } from './blockchain/buildUnsignedTransferTx';
8
+ export { buildBaseUnsignedTransferTx, buildMaxNativeTransferTx, buildUnsignedTransferTx, } from './blockchain/buildUnsignedTransferTx';
9
9
  export { broadcastTransaction } from './blockchain/broadcastTransaction';
10
10
  export { estimateGasLimitFromProvider } from './blockchain/estimateGasLimitFromProvider';
11
11
  export { getProvider } from './blockchain/getProvider';
12
12
  export { txStatus } from './blockchain/txStatus';
13
13
  export { getBalance, getBalances } from './blockchain/getBalances';
14
+ export { estimateTransaction } from './blockchain/estimateTransaction';
15
+ export { prepareTransaction } from './blockchain/prepareTransaction';
14
16
  /** services exports */
15
17
  export { EvmWalletService, EvmGeneratedWallet, EvmDerivedWallet, } from './services/evm-wallet-core/evmWalletService';
16
18
  export { EntropySource } from './services/evm-wallet-core/entropy';
@@ -24,4 +26,4 @@ export { formattedAmountForDisplay, parsedAmount } from './utils/formatAmount';
24
26
  export { handleErrorMessages, errorMessagesForBroadcast, errorMessagesForGasLimitEstimation, } from './utils/handleErrorMessages';
25
27
  export { normalizeAddress } from './utils/normalizeAddress';
26
28
  /** types exports */
27
- export { BroadcastTransactionOptions, BuildMaxNativeTransferTxOptions, BuildMaxNativeTransferTxResponse, BuildUnsignedTransferTxOptions, EstimateGasLimitFromProviderProps, GasEstimateResult, TxStatusOptions, TxStatusResponse, UnsignedTransferTxResponse, FormatAmountOptions, TransactionRequest, GetBalanceParams, GetBalancesParams, GetBalancesChainRequest, GetBalanceResult, ChainBalances, TokenBalance, ChainGroup, } from './types/common';
29
+ export { BroadcastTransactionOptions, BuildMaxNativeTransferTxOptions, BuildUnsignedTransferTxOptions, EstimateGasLimitFromProviderProps, GasEstimateResult, TxStatusOptions, BuildBaseUnsignedTransferTxParams, TxStatusResponse, EstimateTransactionOptions, EstimateTransactionResult, PrepareTransactionParams, PrepareTransactionResult, FormatAmountOptions, TransactionRequest, GetBalanceParams, GetBalancesParams, GetBalancesChainRequest, GetBalanceResult, ChainBalances, TokenBalance, ChainGroup, } from './types/common';
package/dist/index.js CHANGED
@@ -5,12 +5,14 @@ export { ERC20_TOKEN_CONTRACT_ABI };
5
5
  export { GAS_LIMIT_PER_TX_TYPE, MULTICALL3_ADDRESS } from './constants/constants';
6
6
  export { NETWORKS_REGISTRY } from './constants/NETWORKS_REGISTRY';
7
7
  /** basic blockchain exports */
8
- export { buildMaxNativeTransferTx, buildUnsignedTransferTx, } from './blockchain/buildUnsignedTransferTx';
8
+ export { buildBaseUnsignedTransferTx, buildMaxNativeTransferTx, buildUnsignedTransferTx, } from './blockchain/buildUnsignedTransferTx';
9
9
  export { broadcastTransaction } from './blockchain/broadcastTransaction';
10
10
  export { estimateGasLimitFromProvider } from './blockchain/estimateGasLimitFromProvider';
11
11
  export { getProvider } from './blockchain/getProvider';
12
12
  export { txStatus } from './blockchain/txStatus';
13
13
  export { getBalance, getBalances } from './blockchain/getBalances';
14
+ export { estimateTransaction } from './blockchain/estimateTransaction';
15
+ export { prepareTransaction } from './blockchain/prepareTransaction';
14
16
  /** services exports */
15
17
  export { EvmWalletService, } from './services/evm-wallet-core/evmWalletService';
16
18
  export { createWallet, signMessage, signTransaction } from './services/evm-wallet-core/signer';
@@ -20,6 +20,10 @@ export declare class EvmWalletService {
20
20
  generateMultipleWallets(mnemonic: string, count: number, startIndex?: number): EvmDerivedWallet[];
21
21
  deriveWalletFromMnemonic(mnemonic: string, accountIndex?: number, customPath?: string): EvmDerivedWallet;
22
22
  deriveFromPrivateKey(privateKey: string): Omit<EvmDerivedWallet, 'path'>;
23
+ getFirstAvailableIndexForMnemonicImport(mnemonic: string, existingAddresses: string[], maxIndicesToCheck?: number): {
24
+ index: number;
25
+ wallet: EvmDerivedWallet;
26
+ } | null;
23
27
  signMessage(privateKey: string, message: string): Promise<string>;
24
28
  isValidMnemonic(mnemonic: string): boolean;
25
29
  private validateMnemonic;
@@ -1,5 +1,5 @@
1
1
  /** npm imports */
2
- import { Wallet, HDNodeWallet, Mnemonic } from 'ethers';
2
+ import { Wallet, HDNodeWallet, Mnemonic, getAddress } from 'ethers';
3
3
  export class EvmWalletService {
4
4
  constructor(entropy) {
5
5
  this.entropy = entropy;
@@ -55,6 +55,18 @@ export class EvmWalletService {
55
55
  publicKey: wallet.signingKey.publicKey,
56
56
  };
57
57
  }
58
+ getFirstAvailableIndexForMnemonicImport(mnemonic, existingAddresses, maxIndicesToCheck = 20) {
59
+ this.validateMnemonic(mnemonic);
60
+ const existingSet = new Set(existingAddresses.map((addr) => getAddress(addr)));
61
+ for (let i = 0; i < maxIndicesToCheck; i++) {
62
+ const wallet = this.deriveWalletFromMnemonic(mnemonic, i);
63
+ const normalizedAddress = getAddress(wallet.address);
64
+ if (!existingSet.has(normalizedAddress)) {
65
+ return { index: i, wallet };
66
+ }
67
+ }
68
+ return null;
69
+ }
58
70
  async signMessage(privateKey, message) {
59
71
  const wallet = new Wallet(privateKey);
60
72
  return await wallet.signMessage(message);
@@ -69,9 +81,8 @@ export class EvmWalletService {
69
81
  }
70
82
  }
71
83
  validateMnemonic(mnemonic) {
72
- if (!this.isValidMnemonic(mnemonic)) {
84
+ if (!this.isValidMnemonic(mnemonic))
73
85
  throw new Error('Invalid mnemonic phrase');
74
- }
75
86
  }
76
87
  getEntropyForWordCount(wordCount) {
77
88
  const entropyMap = {
@@ -17,36 +17,59 @@ export interface GasEstimateResult {
17
17
  gasPrice?: bigint;
18
18
  };
19
19
  }
20
+ export interface EstimateTransactionOptions {
21
+ rpcUrl: string;
22
+ chainId: number;
23
+ tx: TransactionRequest;
24
+ fromAddress: string;
25
+ defaultGasLimit?: bigint;
26
+ }
27
+ export interface PrepareTransactionParams extends EstimateTransactionOptions {
28
+ }
29
+ export interface EstimateTransactionResult {
30
+ gasLimit: bigint;
31
+ gasEstimated: bigint;
32
+ feeData: {
33
+ maxFeePerGas?: bigint;
34
+ maxPriorityFeePerGas?: bigint;
35
+ gasPrice?: bigint;
36
+ };
37
+ gasReserve?: bigint;
38
+ humanReadableGasReserve: string;
39
+ bufferPercentage: number;
40
+ }
41
+ export interface PrepareTransactionResult {
42
+ unsignedTx: TransactionRequest;
43
+ nonce: number;
44
+ gasEstimated: bigint;
45
+ gasLimit: bigint;
46
+ gasReserve?: bigint;
47
+ bufferPercentage: number;
48
+ feeData: {
49
+ maxFeePerGas?: bigint;
50
+ maxPriorityFeePerGas?: bigint;
51
+ gasPrice?: bigint;
52
+ };
53
+ humanReadableGasReserve: string;
54
+ }
55
+ export interface BuildBaseUnsignedTransferTxParams {
56
+ recipientAddress: string;
57
+ value: string;
58
+ tokenAddress?: string;
59
+ tokenDecimals?: number;
60
+ }
20
61
  export interface BuildUnsignedTransferTxOptions {
21
62
  fromAddress: string;
22
63
  toAddress: string;
23
64
  value?: string;
24
- data?: string;
25
65
  tokenAddress?: string;
26
66
  tokenDecimals?: number;
27
67
  rpcUrl: string;
28
68
  chainId?: number;
29
69
  defaultGasLimit: bigint;
30
70
  }
31
- export interface UnsignedTransferTxResponse {
32
- unsignedTx: TransactionRequest;
33
- nonce: number;
34
- gasEstimated: string;
35
- gasLimit: string;
36
- gasReserve?: string;
37
- bufferPercentage: number;
38
- feeData: {
39
- maxFeePerGas?: string;
40
- maxPriorityFeePerGas?: string;
41
- gasPrice?: string;
42
- };
43
- }
44
- export interface BuildMaxNativeTransferTxOptions extends Omit<BuildUnsignedTransferTxOptions, 'value' | 'tokenAddress' | 'tokenDecimals'> {
71
+ export interface BuildMaxNativeTransferTxOptions extends Omit<BuildUnsignedTransferTxOptions, 'value' | 'tokenDecimals'> {
45
72
  balance: string;
46
- tokenAddress?: string;
47
- }
48
- export interface BuildMaxNativeTransferTxResponse extends UnsignedTransferTxResponse {
49
- sendableValue: string;
50
73
  }
51
74
  export interface BroadcastTransactionOptions {
52
75
  signedTx: string;
@@ -1,2 +1,2 @@
1
- export declare const getShortenTransactionHashOrAddress: (txHashOrAddress: string) => string;
2
- export declare const getShortenData: (data: string) => string;
1
+ export declare const getShortenTransactionHashOrAddress: (txHashOrAddress: string, firstPartLength?: number, lastPartLength?: number) => string;
2
+ export declare const getShortenData: (data: string, firstPartLength?: number, lastPartLength?: number) => string;
@@ -1,10 +1,10 @@
1
- export const getShortenTransactionHashOrAddress = (txHashOrAddress) => {
2
- const firstCharacters = txHashOrAddress.substring(0, 6);
3
- const lastCharacters = txHashOrAddress.slice(-4);
1
+ export const getShortenTransactionHashOrAddress = (txHashOrAddress, firstPartLength = 6, lastPartLength = 4) => {
2
+ const firstCharacters = txHashOrAddress.substring(0, firstPartLength);
3
+ const lastCharacters = txHashOrAddress.slice(-lastPartLength);
4
4
  return `${firstCharacters}...${lastCharacters}`;
5
5
  };
6
- export const getShortenData = (data) => {
7
- const firstCharacters = data.substring(0, 12);
8
- const lastCharacters = data.slice(-12);
6
+ export const getShortenData = (data, firstPartLength = 12, lastPartLength = 12) => {
7
+ const firstCharacters = data.substring(0, firstPartLength);
8
+ const lastCharacters = data.slice(-lastPartLength);
9
9
  return `${firstCharacters}...${lastCharacters}`;
10
10
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@octaflowlabs/onchain-sdk",
3
- "version": "1.2.2",
3
+ "version": "1.3.1",
4
4
  "description": "onchain methods for web3",
5
5
  "repository": "https://github.com/crisramb665/onchain-sdk.git",
6
6
  "license": "MIT",