@cryptorubic/web3 0.8.17-alpha.solana.4 → 0.8.17-alpha.solana.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,17 +1,17 @@
1
1
  {
2
2
  "name": "@cryptorubic/web3",
3
- "version": "0.8.17-alpha.solana.4",
3
+ "version": "0.8.17-alpha.solana.6",
4
4
  "dependencies": {
5
5
  "tslib": "^2.3.0",
6
6
  "bignumber.js": "9.1.2",
7
- "@cryptorubic/core": "0.8.17-alpha.solana.4",
7
+ "@cryptorubic/core": "0.8.17-alpha.solana.6",
8
8
  "viem": "^2.19.1",
9
9
  "web3-utils": "^4.3.1",
10
10
  "@ton/ton": "^15.1.0",
11
11
  "@solana/web3.js": "1.95.3",
12
12
  "@solflare-wallet/utl-sdk": "^1.4.0",
13
13
  "@ethersproject/bignumber": "^5.7.0",
14
- "@cryptorubic/tron-types": "0.8.17-alpha.solana.4",
14
+ "@cryptorubic/tron-types": "0.8.17-alpha.solana.6",
15
15
  "bitcoin-address-validation": "^2.2.3",
16
16
  "axios": "0.27.2",
17
17
  "crc-32": "^1.2.2",
@@ -34,13 +34,13 @@ class EvmAdapter extends abstract_adapter_1.AbstractAdapter {
34
34
  return null;
35
35
  }
36
36
  const transports = rpcList.map((rpc) => (0, viem_1.http)(rpc, {
37
- batch: { batchSize: 2000, wait: 16 },
37
+ batch: { batchSize: 2_000, wait: 16 },
38
38
  onFetchResponse: (response) => {
39
39
  this.handlePossibleError(response);
40
40
  },
41
41
  retryCount: 2,
42
42
  retryDelay: 100,
43
- timeout: 5000
43
+ timeout: 5_000
44
44
  }));
45
45
  const chain = viem_blockchain_mapping_1.viemBlockchainMapping[this.blockchain];
46
46
  return (0, viem_1.createPublicClient)({
@@ -48,7 +48,7 @@ class EvmAdapter extends abstract_adapter_1.AbstractAdapter {
48
48
  chain,
49
49
  transport: (0, viem_1.fallback)(transports, {
50
50
  rank: {
51
- interval: 1800000
51
+ interval: 1_800_000
52
52
  }
53
53
  })
54
54
  });
@@ -135,7 +135,7 @@ class EvmAdapter extends abstract_adapter_1.AbstractAdapter {
135
135
  throw err;
136
136
  }
137
137
  }
138
- async simulateTransaction(config, from, timeout = 15000) {
138
+ async simulateTransaction(config, from, timeout = 15_000) {
139
139
  try {
140
140
  const callParams = {
141
141
  account: from,
@@ -278,7 +278,7 @@ class EvmAdapter extends abstract_adapter_1.AbstractAdapter {
278
278
  to: tx.to
279
279
  };
280
280
  }
281
- async approveOnPermit2(fromAddress, tokenAddress, permit2Address, spenderAddress, deadline = new bignumber_js_1.default(1000000), amount) {
281
+ async approveOnPermit2(fromAddress, tokenAddress, permit2Address, spenderAddress, deadline = new bignumber_js_1.default(1_000_000), amount) {
282
282
  const approveAmount = amount ? amount : new bignumber_js_1.default(2).pow(256).minus(1);
283
283
  const expiration = new bignumber_js_1.default(Date.now()).plus(deadline).toFixed();
284
284
  const tx = this.encodePermit2Approve(tokenAddress, spenderAddress, permit2Address, expiration, approveAmount.toFixed());
@@ -6,12 +6,12 @@ import BigNumber from 'bignumber.js';
6
6
  import { SolanaRawInstruction } from './models/solana-web3-types';
7
7
  import { SolanaTxConfig } from '../../utils/models/solana-transaction-config';
8
8
  import { SolanaAdapterConfig } from './models/solana-adapter-config';
9
- import { SolanaApiService } from './utils/solana-utils/solana-api-service';
9
+ import { SolanaGasService } from './utils/solana-utils/solana-gas-service';
10
10
  export declare const NATIVE_SOLANA_MINT_ADDRESS = "So11111111111111111111111111111111111111111";
11
11
  export declare const DEFAULT_CU_LIMIT = 600000;
12
12
  export declare class SolanaAdapter extends AbstractAdapter<Connection, Connection, SolanaBlockchainName> {
13
13
  private readonly httpClient;
14
- readonly apiService: SolanaApiService;
14
+ readonly apiService: SolanaGasService;
15
15
  constructor(rpcList: string[], httpClient: HttpClient, solanaConfig: SolanaAdapterConfig, logger?: ICustomLogger);
16
16
  private createPublicClient;
17
17
  read<T>(_address: string, _abi: Abi, _method: string, _methodArgs?: unknown[]): Promise<T>;
@@ -9,16 +9,16 @@ const web3_pure_1 = require("../../utils/web3-pure");
9
9
  const bignumber_js_1 = require("bignumber.js");
10
10
  const bs58_1 = require("bs58");
11
11
  const utils_1 = require("ethers/lib/utils");
12
- const solana_api_service_1 = require("./utils/solana-utils/solana-api-service");
12
+ const solana_gas_service_1 = require("./utils/solana-utils/solana-gas-service");
13
13
  const solana_tokens_service_1 = require("./utils/solana-utils/solana-tokens-service");
14
14
  exports.NATIVE_SOLANA_MINT_ADDRESS = 'So11111111111111111111111111111111111111111';
15
- exports.DEFAULT_CU_LIMIT = 600000;
15
+ exports.DEFAULT_CU_LIMIT = 600_000;
16
16
  class SolanaAdapter extends abstract_adapter_1.AbstractAdapter {
17
17
  constructor(rpcList, httpClient, solanaConfig, logger) {
18
18
  super(core_1.BLOCKCHAIN_NAME.SOLANA, logger);
19
19
  this.httpClient = httpClient;
20
20
  this.public = this.createPublicClient(rpcList);
21
- this.apiService = new solana_api_service_1.SolanaApiService(httpClient, this.public, solanaConfig);
21
+ this.apiService = new solana_gas_service_1.SolanaGasService(httpClient, this.public, solanaConfig);
22
22
  }
23
23
  createPublicClient(rpcList) {
24
24
  if (!rpcList?.[0]) {
@@ -41,7 +41,7 @@ class SolanaAdapter extends abstract_adapter_1.AbstractAdapter {
41
41
  async multicallByAddress(_address, _abi, _method, _methodArgs = [], _allowErrors = true) {
42
42
  throw new Error('Method multicall is not supported');
43
43
  }
44
- async simulateTransaction(config, timeout = 15000) {
44
+ async simulateTransaction(config, timeout = 15_000) {
45
45
  try {
46
46
  const bufferData = config.data.startsWith('0x') ? Buffer.from(config.data.slice(2), 'hex') : Buffer.from(config.data, 'base64');
47
47
  const { blockhash } = await this.public.getLatestBlockhash();
@@ -0,0 +1,22 @@
1
+ import { HttpClient } from '@cryptorubic/core';
2
+ import { Connection } from '@solana/web3.js';
3
+ import { SolanaAdapterConfig } from '../../models/solana-adapter-config';
4
+ import BigNumber from 'bignumber.js';
5
+ export declare class SolanaGasService {
6
+ private httpClient;
7
+ private readonly connection;
8
+ private readonly HELIUS_API_URL;
9
+ private readonly HELIUS_API_KEY;
10
+ constructor(httpClient: HttpClient, connection: Connection, solanaConfig: SolanaAdapterConfig);
11
+ /**
12
+ * @returns wei ComputedUnitsLimit - like gasLimit in evm
13
+ */
14
+ getConsumedUnitsLimit(txData: string): Promise<BigNumber>;
15
+ /**
16
+ * @param txData base64 or hex string
17
+ * @returns wei ComputedUnitsPrice - like gasPrice in evm
18
+ */
19
+ getConsumedUnitsPrice(txData: string): Promise<BigNumber>;
20
+ private calculateCUPriceHelius;
21
+ private calculateCUPriceSolWeb3;
22
+ }
@@ -1,9 +1,10 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.SolanaApiService = void 0;
3
+ exports.SolanaGasService = void 0;
4
4
  const utils_1 = require("ethers/lib/utils");
5
5
  const utility_funcs_1 = require("./utility-funcs");
6
- class SolanaApiService {
6
+ const bignumber_js_1 = require("bignumber.js");
7
+ class SolanaGasService {
7
8
  constructor(httpClient, connection, solanaConfig) {
8
9
  this.httpClient = httpClient;
9
10
  this.connection = connection;
@@ -11,29 +12,34 @@ class SolanaApiService {
11
12
  this.HELIUS_API_KEY = solanaConfig.heliusApiKey;
12
13
  }
13
14
  /**
14
- * @returns ComputedUnitsLimit - like gasLimit in evm
15
+ * @returns wei ComputedUnitsLimit - like gasLimit in evm
15
16
  */
16
17
  async getConsumedUnitsLimit(txData) {
17
- const DEFAULT_CU_LIMIT = 600000;
18
+ const DEFAULT_CU_LIMIT = 600_000;
18
19
  try {
19
20
  const tx = (0, utility_funcs_1.convertB64DataToTx)(txData);
20
21
  const resp = await this.connection.simulateTransaction(tx, {
21
22
  replaceRecentBlockhash: true
22
23
  });
23
- return resp.value.unitsConsumed ? resp.value.unitsConsumed * 1.2 : DEFAULT_CU_LIMIT;
24
+ return resp.value.unitsConsumed ? new bignumber_js_1.default(resp.value.unitsConsumed * 1.2) : new bignumber_js_1.default(DEFAULT_CU_LIMIT);
24
25
  }
25
26
  catch (err) {
26
27
  console.error('Solana_simulateTransaction_Error ==> ', err);
27
- return DEFAULT_CU_LIMIT;
28
+ return new bignumber_js_1.default(DEFAULT_CU_LIMIT);
28
29
  }
29
30
  }
30
31
  /**
31
- * @returns ComputedUnitsPrice - like gasPrice in evm
32
+ * @param txData base64 or hex string
33
+ * @returns wei ComputedUnitsPrice - like gasPrice in evm
32
34
  */
33
35
  async getConsumedUnitsPrice(txData) {
34
36
  if (!this.HELIUS_API_KEY) {
35
- throw new Error('[SolanaApiService_getConsumedUnitsPrice] params.solanaParams.heliusApiKey is not provided in "createFactory" method to use this method');
37
+ console.warn('[SolanaApiService_getConsumedUnitsPrice] heliusApiKey is not provided in "createFactory"');
36
38
  }
39
+ const cuPrice = await Promise.any([this.calculateCUPriceHelius(txData), this.calculateCUPriceSolWeb3()]);
40
+ return cuPrice;
41
+ }
42
+ async calculateCUPriceHelius(txData) {
37
43
  const tx = (0, utility_funcs_1.convertB64DataToTx)(txData);
38
44
  const resp = await this.httpClient.post(`${this.HELIUS_API_URL}/?api-key=${this.HELIUS_API_KEY}`, {
39
45
  jsonrpc: '2.0',
@@ -46,7 +52,15 @@ class SolanaApiService {
46
52
  }
47
53
  ]
48
54
  });
49
- return resp.result.priorityFeeEstimate;
55
+ return new bignumber_js_1.default(resp.result.priorityFeeEstimate);
56
+ }
57
+ async calculateCUPriceSolWeb3() {
58
+ const resp = await this.connection.getRecentPrioritizationFees();
59
+ const cuPriceSum = resp
60
+ .reduce((acc, tx) => acc.plus(tx.prioritizationFee), new bignumber_js_1.default(0))
61
+ .div(resp.length)
62
+ .dp(0, bignumber_js_1.default.ROUND_CEIL);
63
+ return cuPriceSum;
50
64
  }
51
65
  }
52
- exports.SolanaApiService = SolanaApiService;
66
+ exports.SolanaGasService = SolanaGasService;
@@ -57,7 +57,7 @@ class SolanaTokensService {
57
57
  }
58
58
  async fetchTokensFromOldBackend(mints, prevFetchedTokens) {
59
59
  const tokensAddresses = mints.map((mint) => mint.toString());
60
- const { content: tokensFromOlbBackend } = await (0, timeout_1.withTimeout)(this.getTokensListOld(tokensAddresses), 3000, 'Api Timeout!').catch(() => ({ content: [] }));
60
+ const { content: tokensFromOlbBackend } = await (0, timeout_1.withTimeout)(this.getTokensListOld(tokensAddresses), 3_000, 'Api Timeout!').catch(() => ({ content: [] }));
61
61
  const notSortedTokensList = [...prevFetchedTokens, ...tokensFromOlbBackend];
62
62
  const notFetchedMints = this.getNotFetchedTokensList(notSortedTokensList);
63
63
  return {
@@ -1,2 +1,2 @@
1
1
  import { VersionedTransaction } from '@solana/web3.js';
2
- export declare function convertB64DataToTx(base64Data: string): VersionedTransaction;
2
+ export declare function convertB64DataToTx(txData: string): VersionedTransaction;
@@ -2,8 +2,8 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.convertB64DataToTx = convertB64DataToTx;
4
4
  const web3_js_1 = require("@solana/web3.js");
5
- function convertB64DataToTx(base64Data) {
6
- const bufferData = base64Data.startsWith('0x') ? Buffer.from(base64Data.slice(2), 'hex') : Buffer.from(base64Data, 'base64');
5
+ function convertB64DataToTx(txData) {
6
+ const bufferData = txData.startsWith('0x') ? Buffer.from(txData.slice(2), 'hex') : Buffer.from(txData, 'base64');
7
7
  const tx = web3_js_1.VersionedTransaction.deserialize(bufferData);
8
8
  return tx;
9
9
  }
@@ -1,18 +0,0 @@
1
- import { HttpClient } from '@cryptorubic/core';
2
- import { Connection } from '@solana/web3.js';
3
- import { SolanaAdapterConfig } from '../../models/solana-adapter-config';
4
- export declare class SolanaApiService {
5
- private httpClient;
6
- private readonly connection;
7
- private readonly HELIUS_API_URL;
8
- private readonly HELIUS_API_KEY;
9
- constructor(httpClient: HttpClient, connection: Connection, solanaConfig: SolanaAdapterConfig);
10
- /**
11
- * @returns ComputedUnitsLimit - like gasLimit in evm
12
- */
13
- getConsumedUnitsLimit(txData: string): Promise<number>;
14
- /**
15
- * @returns ComputedUnitsPrice - like gasPrice in evm
16
- */
17
- getConsumedUnitsPrice(txData: string): Promise<number>;
18
- }