@octaflowlabs/onchain-sdk 1.0.0-test10 → 1.0.0-test6

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,2 +1,2 @@
1
1
  import { BroadcastTransactionOptions } from '../types/common';
2
- export declare const broadcastTransaction: ({ signedTx, rpcUrl, chainId, waitConfirmations, }: BroadcastTransactionOptions) => Promise<string>;
2
+ export declare const broadcastTransaction: ({ signedTx, rpcUrl, waitConfirmations, }: BroadcastTransactionOptions) => Promise<string>;
@@ -2,10 +2,8 @@
2
2
  import { Transaction } from 'ethers';
3
3
  /** local imports */
4
4
  import { getProvider } from './getProvider';
5
- export const broadcastTransaction = async ({ signedTx, rpcUrl, chainId, waitConfirmations = 0, }) => {
6
- const provider = getProvider(rpcUrl, chainId);
7
- if (!provider)
8
- throw new Error('Could not create provider with given rpcUrl');
5
+ export const broadcastTransaction = async ({ signedTx, rpcUrl, waitConfirmations = 0, }) => {
6
+ const provider = getProvider(rpcUrl);
9
7
  try {
10
8
  Transaction.from(signedTx);
11
9
  }
@@ -5,91 +5,82 @@ import { getProvider } from './getProvider';
5
5
  import { estimateGasLimitFromProvider } from './estimateGasLimitFromProvider';
6
6
  export const buildUnsignedTransferTx = async (options) => {
7
7
  const provider = getProvider(options.rpcUrl, options.chainId);
8
- if (!provider)
9
- throw new Error('Could not create provider with given rpcUrl and chainId');
8
+ let unsignedTx = { to: options.tokenAddress };
9
+ const isNativeToken = !options.tokenAddress ||
10
+ options.tokenAddress?.toLowerCase() === '0x0000000000000000000000000000000000000000' ||
11
+ options.tokenAddress?.toLowerCase() === '0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee';
12
+ if (!isNativeToken) {
13
+ if (!options.tokenAddress)
14
+ throw new Error('Token address is required for token transfer');
15
+ const code = await provider.getCode(options.tokenAddress);
16
+ if (code === '0x' || code === '0x0')
17
+ throw new Error('Invalid token address');
18
+ const erc20Interface = new Interface(['function transfer(address to, uint256 amount)']);
19
+ const decimals = options.tokenDecimals ?? 18;
20
+ if (!options.value)
21
+ throw new Error('Value is required for token transfer');
22
+ const amountParsed = parseUnits(options.value, decimals);
23
+ unsignedTx = {
24
+ ...unsignedTx,
25
+ data: erc20Interface.encodeFunctionData('transfer', [options.toAddress, amountParsed]),
26
+ value: 0n,
27
+ };
28
+ }
29
+ else {
30
+ if (!options.value)
31
+ throw new Error('Native transfer requires value');
32
+ unsignedTx = {
33
+ to: options.toAddress,
34
+ data: '0x',
35
+ value: parseUnits(options.value, 18),
36
+ };
37
+ }
38
+ if (options.chainId)
39
+ unsignedTx.chainId = options.chainId;
40
+ const nonce = await provider.getTransactionCount(options.fromAddress, 'pending');
41
+ const estimateGas = await estimateGasLimitFromProvider({
42
+ provider: provider,
43
+ unsignedTx,
44
+ walletAddress: options.fromAddress,
45
+ defaultGasLimit: options.defaultGasLimit,
46
+ });
47
+ const unsignedTxToReturn = {
48
+ from: options.fromAddress,
49
+ to: unsignedTx.to,
50
+ data: unsignedTx.data,
51
+ value: unsignedTx.value?.toString(),
52
+ gasLimit: estimateGas.gasLimit.toString(),
53
+ chainId: unsignedTx.chainId,
54
+ nonce,
55
+ maxFeePerGas: estimateGas.feeData.maxFeePerGas?.toString(),
56
+ maxPriorityFeePerGas: estimateGas.feeData.maxPriorityFeePerGas?.toString(),
57
+ };
58
+ if (estimateGas.feeData.gasPrice)
59
+ unsignedTxToReturn.gasPrice = estimateGas.feeData.gasPrice.toString();
10
60
  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,
48
- defaultGasLimit: options.defaultGasLimit,
49
- });
50
- const unsignedTxToReturn = {
61
+ await provider.call({
51
62
  from: options.fromAddress,
52
63
  to: unsignedTx.to,
53
64
  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
- try {
64
- await provider.call({
65
- from: options.fromAddress,
66
- to: unsignedTx.to,
67
- data: unsignedTx.data,
68
- value: unsignedTx.value,
69
- gasLimit: estimateGas.gasLimit,
70
- });
71
- }
72
- catch (error) {
73
- throw new Error('Transaction would revert, provider call unsuccessful: ' + error.message || error);
74
- }
75
- console.log('Built unsigned transfer transaction:', unsignedTxToReturn);
76
- return {
77
- unsignedTx: unsignedTxToReturn,
78
- nonce,
79
- gasEstimated: estimateGas.gasEstimated.toString(),
80
- gasLimit: estimateGas.gasLimit.toString(),
81
- bufferPercentage: estimateGas.bufferPercentage,
82
- feeData: {
83
- maxFeePerGas: estimateGas.feeData.maxFeePerGas?.toString(),
84
- maxPriorityFeePerGas: estimateGas.feeData.maxPriorityFeePerGas?.toString(),
85
- gasPrice: estimateGas.feeData.gasPrice?.toString(),
86
- },
87
- // suggestedGasFees: estimateGas.suggestedGasFees,
88
- // humanReadableFees: humanReadableGasEstimation,
89
- };
65
+ value: unsignedTx.value,
66
+ gasLimit: estimateGas.gasLimit,
67
+ });
90
68
  }
91
69
  catch (error) {
92
- console.error('Error building unsigned transfer transaction:', error);
93
- throw error;
70
+ throw new Error('Transaction would revert, provider call unsuccessful: ' + error.message || error);
94
71
  }
72
+ return {
73
+ unsignedTx: unsignedTxToReturn,
74
+ nonce,
75
+ gasEstimated: estimateGas.gasEstimated.toString(),
76
+ gasLimit: estimateGas.gasLimit.toString(),
77
+ bufferPercentage: estimateGas.bufferPercentage,
78
+ feeData: {
79
+ maxFeePerGas: estimateGas.feeData.maxFeePerGas?.toString(),
80
+ maxPriorityFeePerGas: estimateGas.feeData.maxPriorityFeePerGas?.toString(),
81
+ gasPrice: estimateGas.feeData.gasPrice?.toString(),
82
+ },
83
+ // suggestedGasFees: estimateGas.suggestedGasFees,
84
+ // humanReadableFees: humanReadableGasEstimation,
85
+ };
95
86
  };
@@ -1,3 +1,3 @@
1
1
  /** npm imports */
2
2
  import { JsonRpcProvider } from 'ethers';
3
- export declare const getProvider: (rpcUrl: string, chainId?: number) => JsonRpcProvider | undefined;
3
+ export declare const getProvider: (rpcUrl: string, chainId?: number) => JsonRpcProvider;
@@ -1,16 +1,7 @@
1
1
  /** npm imports */
2
- import { JsonRpcProvider, Network } from 'ethers';
2
+ import { JsonRpcProvider } from 'ethers';
3
3
  export const getProvider = (rpcUrl, chainId) => {
4
- try {
5
- if (chainId) {
6
- const network = Network.from(chainId);
7
- return new JsonRpcProvider(rpcUrl, network, { staticNetwork: network });
8
- }
9
- else {
10
- return new JsonRpcProvider(rpcUrl);
11
- }
12
- }
13
- catch (error) {
14
- console.warn('Could not create provider with chainId, falling back to rpcUrl only:', error);
15
- }
4
+ const provider = new JsonRpcProvider(rpcUrl, { chainId });
5
+ console.log({ provider });
6
+ return provider;
16
7
  };
@@ -2,8 +2,6 @@
2
2
  import { getProvider } from './getProvider';
3
3
  export const txStatus = async ({ rpcUrl, txHash, chainId, }) => {
4
4
  const provider = getProvider(rpcUrl, chainId);
5
- if (!provider)
6
- throw new Error('Could not create provider with given rpcUrl and chainId');
7
5
  try {
8
6
  const receipt = await provider.getTransactionReceipt(txHash);
9
7
  if (!receipt)
@@ -1,2 +1,2 @@
1
1
  import { BroadcastTransactionOptions } from '../types/common';
2
- export declare const broadcastTransaction: ({ signedTx, rpcUrl, chainId, waitConfirmations, }: BroadcastTransactionOptions) => Promise<string>;
2
+ export declare const broadcastTransaction: ({ signedTx, rpcUrl, waitConfirmations, }: BroadcastTransactionOptions) => Promise<string>;
@@ -5,10 +5,8 @@ exports.broadcastTransaction = void 0;
5
5
  const ethers_1 = require("ethers");
6
6
  /** local imports */
7
7
  const getProvider_1 = require("./getProvider");
8
- const broadcastTransaction = async ({ signedTx, rpcUrl, chainId, waitConfirmations = 0, }) => {
9
- const provider = (0, getProvider_1.getProvider)(rpcUrl, chainId);
10
- if (!provider)
11
- throw new Error('Could not create provider with given rpcUrl');
8
+ const broadcastTransaction = async ({ signedTx, rpcUrl, waitConfirmations = 0, }) => {
9
+ const provider = (0, getProvider_1.getProvider)(rpcUrl);
12
10
  try {
13
11
  ethers_1.Transaction.from(signedTx);
14
12
  }
@@ -8,92 +8,83 @@ const getProvider_1 = require("./getProvider");
8
8
  const estimateGasLimitFromProvider_1 = require("./estimateGasLimitFromProvider");
9
9
  const buildUnsignedTransferTx = async (options) => {
10
10
  const provider = (0, getProvider_1.getProvider)(options.rpcUrl, options.chainId);
11
- if (!provider)
12
- throw new Error('Could not create provider with given rpcUrl and chainId');
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 ethers_1.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 = (0, ethers_1.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: (0, ethers_1.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 (0, estimateGasLimitFromProvider_1.estimateGasLimitFromProvider)({
45
+ provider: provider,
46
+ unsignedTx,
47
+ walletAddress: options.fromAddress,
48
+ defaultGasLimit: options.defaultGasLimit,
49
+ });
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();
13
63
  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,
51
- defaultGasLimit: options.defaultGasLimit,
52
- });
53
- const unsignedTxToReturn = {
64
+ await provider.call({
54
65
  from: options.fromAddress,
55
66
  to: unsignedTx.to,
56
67
  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
- try {
67
- await provider.call({
68
- from: options.fromAddress,
69
- to: unsignedTx.to,
70
- data: unsignedTx.data,
71
- value: unsignedTx.value,
72
- gasLimit: estimateGas.gasLimit,
73
- });
74
- }
75
- catch (error) {
76
- throw new Error('Transaction would revert, provider call unsuccessful: ' + error.message || error);
77
- }
78
- console.log('Built unsigned transfer transaction:', unsignedTxToReturn);
79
- return {
80
- unsignedTx: unsignedTxToReturn,
81
- nonce,
82
- gasEstimated: estimateGas.gasEstimated.toString(),
83
- gasLimit: estimateGas.gasLimit.toString(),
84
- bufferPercentage: estimateGas.bufferPercentage,
85
- feeData: {
86
- maxFeePerGas: estimateGas.feeData.maxFeePerGas?.toString(),
87
- maxPriorityFeePerGas: estimateGas.feeData.maxPriorityFeePerGas?.toString(),
88
- gasPrice: estimateGas.feeData.gasPrice?.toString(),
89
- },
90
- // suggestedGasFees: estimateGas.suggestedGasFees,
91
- // humanReadableFees: humanReadableGasEstimation,
92
- };
68
+ value: unsignedTx.value,
69
+ gasLimit: estimateGas.gasLimit,
70
+ });
93
71
  }
94
72
  catch (error) {
95
- console.error('Error building unsigned transfer transaction:', error);
96
- throw error;
73
+ throw new Error('Transaction would revert, provider call unsuccessful: ' + error.message || error);
97
74
  }
75
+ return {
76
+ unsignedTx: unsignedTxToReturn,
77
+ nonce,
78
+ gasEstimated: estimateGas.gasEstimated.toString(),
79
+ gasLimit: estimateGas.gasLimit.toString(),
80
+ bufferPercentage: estimateGas.bufferPercentage,
81
+ feeData: {
82
+ maxFeePerGas: estimateGas.feeData.maxFeePerGas?.toString(),
83
+ maxPriorityFeePerGas: estimateGas.feeData.maxPriorityFeePerGas?.toString(),
84
+ gasPrice: estimateGas.feeData.gasPrice?.toString(),
85
+ },
86
+ // suggestedGasFees: estimateGas.suggestedGasFees,
87
+ // humanReadableFees: humanReadableGasEstimation,
88
+ };
98
89
  };
99
90
  exports.buildUnsignedTransferTx = buildUnsignedTransferTx;
@@ -1,3 +1,3 @@
1
1
  /** npm imports */
2
2
  import { JsonRpcProvider } from 'ethers';
3
- export declare const getProvider: (rpcUrl: string, chainId?: number) => JsonRpcProvider | undefined;
3
+ export declare const getProvider: (rpcUrl: string, chainId?: number) => JsonRpcProvider;
@@ -4,17 +4,8 @@ exports.getProvider = void 0;
4
4
  /** npm imports */
5
5
  const ethers_1 = require("ethers");
6
6
  const getProvider = (rpcUrl, chainId) => {
7
- try {
8
- if (chainId) {
9
- const network = ethers_1.Network.from(chainId);
10
- return new ethers_1.JsonRpcProvider(rpcUrl, network, { staticNetwork: network });
11
- }
12
- else {
13
- return new ethers_1.JsonRpcProvider(rpcUrl);
14
- }
15
- }
16
- catch (error) {
17
- console.warn('Could not create provider with chainId, falling back to rpcUrl only:', error);
18
- }
7
+ const provider = new ethers_1.JsonRpcProvider(rpcUrl, { chainId });
8
+ console.log({ provider });
9
+ return provider;
19
10
  };
20
11
  exports.getProvider = getProvider;
@@ -5,8 +5,6 @@ exports.txStatus = void 0;
5
5
  const getProvider_1 = require("./getProvider");
6
6
  const txStatus = async ({ rpcUrl, txHash, chainId, }) => {
7
7
  const provider = (0, getProvider_1.getProvider)(rpcUrl, chainId);
8
- if (!provider)
9
- throw new Error('Could not create provider with given rpcUrl and chainId');
10
8
  try {
11
9
  const receipt = await provider.getTransactionReceipt(txHash);
12
10
  if (!receipt)
@@ -43,7 +43,6 @@ export interface UnsignedTransferTxResponse {
43
43
  export interface BroadcastTransactionOptions {
44
44
  signedTx: string;
45
45
  rpcUrl: string;
46
- chainId?: number;
47
46
  waitConfirmations?: number;
48
47
  }
49
48
  export interface TxStatusOptions {
@@ -43,7 +43,6 @@ export interface UnsignedTransferTxResponse {
43
43
  export interface BroadcastTransactionOptions {
44
44
  signedTx: string;
45
45
  rpcUrl: string;
46
- chainId?: number;
47
46
  waitConfirmations?: number;
48
47
  }
49
48
  export interface TxStatusOptions {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@octaflowlabs/onchain-sdk",
3
- "version": "1.0.0-test10",
3
+ "version": "1.0.0-test6",
4
4
  "description": "onchain methods for web3",
5
5
  "repository": "https://github.com/crisramb665/onchain-sdk.git",
6
6
  "license": "MIT",