@cryptorubic/web3 0.8.17-alpha.solana.14 → 0.8.17-alpha.solana.16

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,6 +1,6 @@
1
1
  {
2
2
  "name": "@cryptorubic/web3",
3
- "version": "0.8.17-alpha.solana.14",
3
+ "version": "0.8.17-alpha.solana.16",
4
4
  "dependencies": {
5
5
  "@ethersproject/bignumber": "^5.8.0",
6
6
  "@mysten/sui": "^1.24.0",
@@ -11,6 +11,7 @@ const bs58_1 = require("bs58");
11
11
  const js_base64_1 = require("js-base64");
12
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
+ const timeout_1 = require("./utils/timeout");
14
15
  exports.NATIVE_SOLANA_MINT_ADDRESS = 'So11111111111111111111111111111111111111111';
15
16
  exports.DEFAULT_CU_LIMIT = 600_000;
16
17
  class SolanaAdapter extends abstract_adapter_1.AbstractAdapter {
@@ -20,7 +21,7 @@ class SolanaAdapter extends abstract_adapter_1.AbstractAdapter {
20
21
  super(core_1.BLOCKCHAIN_NAME.SOLANA, logger);
21
22
  this.httpClient = httpClient;
22
23
  this.public = this.createPublicClient(rpcList);
23
- this.gasService = new solana_gas_service_1.SolanaGasService(httpClient, this.public, solanaConfig);
24
+ this.gasService = new solana_gas_service_1.SolanaGasService(httpClient, this.public, logger, solanaConfig);
24
25
  }
25
26
  createPublicClient(rpcList) {
26
27
  if (!rpcList?.[0]) {
@@ -49,16 +50,8 @@ class SolanaAdapter extends abstract_adapter_1.AbstractAdapter {
49
50
  const { blockhash } = await this.public.getLatestBlockhash();
50
51
  const tx = web3_js_1.VersionedTransaction.deserialize(bufferData);
51
52
  tx.message.recentBlockhash = blockhash;
52
- const simulation = this.public.simulateTransaction(tx, {
53
- commitment: 'finalized',
54
- replaceRecentBlockhash: false,
55
- sigVerify: true,
56
- accounts: { encoding: 'base64', addresses: [...Object.values(config.accounts)] }
57
- });
58
- const resp = await Promise.race([
59
- simulation,
60
- new Promise((_, reject) => setTimeout(() => reject('Timeout'), timeout))
61
- ]);
53
+ const simulation = this.public.simulateTransaction(tx, { replaceRecentBlockhash: true });
54
+ const resp = await (0, timeout_1.withTimeout)(simulation, timeout, 'Solana Simulation Timeout!');
62
55
  return new bignumber_js_1.default(resp.value.unitsConsumed || exports.DEFAULT_CU_LIMIT).toFixed(0);
63
56
  }
64
57
  catch (err) {
@@ -1,22 +1,29 @@
1
- import { HttpClient } from '@cryptorubic/core';
1
+ import { HttpClient, ICustomLogger } from '@cryptorubic/core';
2
2
  import { Connection } from '@solana/web3.js';
3
3
  import { SolanaAdapterConfig } from '../../models/solana-adapter-config';
4
4
  import BigNumber from 'bignumber.js';
5
5
  export declare class SolanaGasService {
6
6
  private httpClient;
7
7
  private readonly connection;
8
+ private readonly logger;
8
9
  private readonly HELIUS_API_URL;
9
10
  private readonly HELIUS_API_KEY;
10
- constructor(httpClient: HttpClient, connection: Connection, solanaConfig: SolanaAdapterConfig);
11
+ constructor(httpClient: HttpClient, connection: Connection, logger: ICustomLogger | undefined, solanaConfig: SolanaAdapterConfig);
11
12
  /**
12
13
  * @returns wei ComputedUnitsLimit - like gasLimit in evm
13
14
  */
14
15
  getConsumedUnitsLimit(txData: string): Promise<BigNumber>;
15
16
  /**
16
17
  * @param txData base64 or hex string
17
- * @returns wei ComputedUnitsPrice - like gasPrice in evm
18
+ * @returns consumedUnitsPrice in micro-lamports(lamport * 10^-6)
18
19
  */
19
20
  getConsumedUnitsPrice(txData: string): Promise<BigNumber>;
21
+ /**
22
+ * @returns consumedUnitsPrice in micro-lamports(lamport * 10^-6)
23
+ */
20
24
  private calculateCUPriceHelius;
25
+ /**
26
+ * @returns consumedUnitsPrice in micro-lamports(lamport * 10^-6)
27
+ */
21
28
  private calculateCUPriceSolWeb3;
22
29
  }
@@ -1,24 +1,29 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.SolanaGasService = void 0;
4
+ const web3_js_1 = require("@solana/web3.js");
4
5
  const utils_1 = require("ethers/lib/utils");
5
6
  const utility_funcs_1 = require("./utility-funcs");
6
7
  const bignumber_js_1 = require("bignumber.js");
8
+ const timeout_1 = require("../timeout");
9
+ const DEFAULT_CU_LIMIT = 200_000; // lamports
10
+ const DEFAULT_CU_PRICE = 1_000_000; // micro-lamports
7
11
  class SolanaGasService {
8
12
  httpClient;
9
13
  connection;
14
+ logger;
10
15
  HELIUS_API_URL = 'https://mainnet.helius-rpc.com';
11
16
  HELIUS_API_KEY;
12
- constructor(httpClient, connection, solanaConfig) {
17
+ constructor(httpClient, connection, logger, solanaConfig) {
13
18
  this.httpClient = httpClient;
14
19
  this.connection = connection;
20
+ this.logger = logger;
15
21
  this.HELIUS_API_KEY = solanaConfig.heliusApiKey;
16
22
  }
17
23
  /**
18
24
  * @returns wei ComputedUnitsLimit - like gasLimit in evm
19
25
  */
20
26
  async getConsumedUnitsLimit(txData) {
21
- const DEFAULT_CU_LIMIT = 600_000;
22
27
  try {
23
28
  const tx = (0, utility_funcs_1.convertB64DataToTx)(txData);
24
29
  const resp = await this.connection.simulateTransaction(tx, {
@@ -33,7 +38,7 @@ class SolanaGasService {
33
38
  }
34
39
  /**
35
40
  * @param txData base64 or hex string
36
- * @returns wei ComputedUnitsPrice - like gasPrice in evm
41
+ * @returns consumedUnitsPrice in micro-lamports(lamport * 10^-6)
37
42
  */
38
43
  async getConsumedUnitsPrice(txData) {
39
44
  if (!this.HELIUS_API_KEY) {
@@ -46,6 +51,9 @@ class SolanaGasService {
46
51
  .reduce((acc, price) => (acc.gt(price) ? acc : price), new bignumber_js_1.default(0));
47
52
  return cuPrice;
48
53
  }
54
+ /**
55
+ * @returns consumedUnitsPrice in micro-lamports(lamport * 10^-6)
56
+ */
49
57
  async calculateCUPriceHelius(txData) {
50
58
  try {
51
59
  const tx = (0, utility_funcs_1.convertB64DataToTx)(txData);
@@ -60,22 +68,51 @@ class SolanaGasService {
60
68
  }
61
69
  ]
62
70
  });
71
+ this.logger?.customLog('HELIUS PRIORITY_FEE SUCCESS', { priorityFeeEstimate: resp.result.priorityFeeEstimate });
63
72
  return new bignumber_js_1.default(resp.result.priorityFeeEstimate);
64
73
  }
65
74
  catch (err) {
66
- console.error('[SolanaGasService_getConsumedUnitsPrice] err ==> ', err);
75
+ this.logger?.customLog('HELIUS PRIORITY_FEE ERROR', err);
67
76
  return new bignumber_js_1.default(0);
68
77
  }
69
78
  }
79
+ /**
80
+ * @returns consumedUnitsPrice in micro-lamports(lamport * 10^-6)
81
+ */
70
82
  async calculateCUPriceSolWeb3() {
71
- const BASE_FEE = 5_000;
72
- const resp = await this.connection.getRecentPrioritizationFees();
73
- const avgCUPrice = resp
74
- .reduce((acc, tx) => acc.plus(tx.prioritizationFee), new bignumber_js_1.default(0))
75
- .div(resp.length)
76
- .dp(0, bignumber_js_1.default.ROUND_CEIL);
77
- const cuPrice = new bignumber_js_1.default(Math.max(BASE_FEE, avgCUPrice.toNumber()));
78
- return cuPrice;
83
+ const estimateCULimitForTx = async (tx, idx) => {
84
+ if (!tx || !tx.message)
85
+ return { success: false, cuLimit: new bignumber_js_1.default(0), idx };
86
+ const transaction = web3_js_1.VersionedTransaction.deserialize(Buffer.from(tx.serialize()));
87
+ const simulationResp = await (0, timeout_1.withTimeout)(this.connection.simulateTransaction(transaction, { replaceRecentBlockhash: true }), 3_000, '[SolanaGasService_calculateCUPriceSolWeb3] Timeout');
88
+ if (typeof simulationResp !== 'object' ||
89
+ !simulationResp.context ||
90
+ !simulationResp.value ||
91
+ !simulationResp.value.unitsConsumed ||
92
+ simulationResp.value.err) {
93
+ return { success: false, cuLimit: new bignumber_js_1.default(0), idx };
94
+ }
95
+ return { success: true, cuLimit: new bignumber_js_1.default(simulationResp.value.unitsConsumed), idx };
96
+ };
97
+ const recentFees = await this.connection.getRecentPrioritizationFees();
98
+ const lastFiveTxFees = recentFees.slice(0, 10);
99
+ const lastBlocksSlots = await this.connection.getBlocks(lastFiveTxFees[0].slot, lastFiveTxFees[lastFiveTxFees.length - 1].slot);
100
+ const lastBlocksSigs = await Promise.all(lastBlocksSlots.map((slot) => this.connection.getBlockSignatures(slot, 'confirmed')));
101
+ const flattenSigs = lastBlocksSigs.flatMap((b) => b.signatures);
102
+ const lastTxs = await this.connection.getTransactions(flattenSigs, { commitment: 'confirmed', maxSupportedTransactionVersion: 0 });
103
+ const serializedTxs = lastTxs
104
+ .filter(Boolean)
105
+ .map((tx) => web3_js_1.VersionedTransaction.deserialize(Buffer.from(tx.transaction.message.serialize())));
106
+ const lastCuLimits = await Promise.all(serializedTxs.map((tx, idx) => estimateCULimitForTx(tx, idx)));
107
+ const successCuLimits = lastCuLimits.filter((obj) => obj.success);
108
+ const avgCuPrice = successCuLimits.reduce((acc, cuLimitInfo) => {
109
+ const cuPrice = new bignumber_js_1.default(lastFiveTxFees[cuLimitInfo.idx].prioritizationFee)
110
+ .multipliedBy(1_000_000)
111
+ .div(cuLimitInfo.cuLimit);
112
+ return cuPrice.gt(acc) ? cuPrice : acc;
113
+ }, new bignumber_js_1.default(0));
114
+ this.logger?.customLog('SOLANA_WEB3 PRIORITY_FEE SUCCESS', { priorityFeeEstimate: avgCuPrice, lastCuLimits });
115
+ return avgCuPrice;
79
116
  }
80
117
  }
81
118
  exports.SolanaGasService = SolanaGasService;
@@ -1,11 +1,5 @@
1
1
  type Base64String = `AQA${string}`;
2
2
  export interface SolanaTxConfig {
3
3
  data: Base64String;
4
- accounts: {
5
- walletAddress?: string;
6
- fromATA?: string;
7
- toATA?: string;
8
- };
9
- estimateConsumedUnits?: boolean;
10
4
  }
11
5
  export {};