@cryptorubic/web3 0.8.17-alpha.solana.15 → 0.8.17-alpha.solana.17

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.15",
3
+ "version": "0.8.17-alpha.solana.17",
4
4
  "dependencies": {
5
5
  "@ethersproject/bignumber": "^5.8.0",
6
6
  "@mysten/sui": "^1.24.0",
@@ -19,8 +19,6 @@ export declare class SolanaAdapter extends AbstractAdapter<Connection, Connectio
19
19
  multicallByContract<T>(_contracts: MulticallParameters, _allowErrors?: boolean): Promise<MulticallResponse<T>[]>;
20
20
  multicallByAddress<T>(_address: string, _abi: Abi, _method: string, _methodArgs?: unknown[][], _allowErrors?: boolean): Promise<MulticallResponse<T>[]>;
21
21
  simulateTransaction(config: SolanaTxConfig, timeout?: number): Promise<string>;
22
- private updatePriorityFee;
23
- private encodeNumberToArrayLE;
24
22
  checkEnoughBalance(token: TokenAmount | PriceTokenAmount, walletAddress: string): Promise<boolean>;
25
23
  getBalance(userAddress: string, tokenAddress: string): Promise<BigNumber>;
26
24
  /**
@@ -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,54 +50,14 @@ 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
- if (config.estimateConsumedUnits) {
59
- const txWithBlockHash = Buffer.from(tx.serialize()).toString('base64');
60
- const [computedUnitsPrice, computedUnitsLimit] = await Promise.all([
61
- this.gasService.getConsumedUnitsPrice(txWithBlockHash),
62
- this.gasService.getConsumedUnitsLimit(txWithBlockHash)
63
- ]);
64
- this.updatePriorityFee(tx, computedUnitsPrice.toNumber(), computedUnitsLimit.toNumber());
65
- }
66
53
  const simulation = this.public.simulateTransaction(tx, { replaceRecentBlockhash: true });
67
- const resp = await Promise.race([
68
- simulation,
69
- new Promise((_, reject) => setTimeout(() => reject('Timeout'), timeout))
70
- ]);
54
+ const resp = await (0, timeout_1.withTimeout)(simulation, timeout, 'Solana Simulation Timeout!');
71
55
  return new bignumber_js_1.default(resp.value.unitsConsumed || exports.DEFAULT_CU_LIMIT).toFixed(0);
72
56
  }
73
57
  catch (err) {
74
58
  throw err;
75
59
  }
76
60
  }
77
- updatePriorityFee(tx, computeUnitPrice, computeUnitLimit) {
78
- const computeBudgetOfset = 1;
79
- const computeUnitPriceData = tx.message.compiledInstructions[1].data;
80
- const encodedPrice = this.encodeNumberToArrayLE(computeUnitPrice, 8);
81
- for (let i = 0; i < encodedPrice.length; i++) {
82
- computeUnitPriceData[i + computeBudgetOfset] = encodedPrice[i];
83
- }
84
- if (computeUnitLimit) {
85
- const computeUnitLimitData = tx.message.compiledInstructions[0].data;
86
- const encodedLimit = this.encodeNumberToArrayLE(computeUnitLimit, 4);
87
- for (let i = 0; i < encodedLimit.length; i++) {
88
- computeUnitLimitData[i + computeBudgetOfset] = encodedLimit[i];
89
- }
90
- }
91
- }
92
- encodeNumberToArrayLE(num, arraySize) {
93
- const result = new Uint8Array(arraySize);
94
- for (let i = 0; i < arraySize; i++) {
95
- result[i] = Number(num & 0xff);
96
- num >>= 8;
97
- }
98
- return result;
99
- }
100
61
  async checkEnoughBalance(token, walletAddress) {
101
62
  const balance = await this.getBalance(walletAddress, token.address);
102
63
  return balance.gte(token.tokenAmount);
@@ -1,13 +1,14 @@
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
  */
@@ -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, {
@@ -63,10 +68,11 @@ class SolanaGasService {
63
68
  }
64
69
  ]
65
70
  });
71
+ this.logger?.customLog('HELIUS PRIORITY_FEE SUCCESS', { priorityFeeEstimate: resp.result.priorityFeeEstimate });
66
72
  return new bignumber_js_1.default(resp.result.priorityFeeEstimate);
67
73
  }
68
74
  catch (err) {
69
- console.error('[SolanaGasService_getConsumedUnitsPrice] err ==> ', err);
75
+ this.logger?.customLog('HELIUS PRIORITY_FEE ERROR', err);
70
76
  return new bignumber_js_1.default(0);
71
77
  }
72
78
  }
@@ -74,14 +80,39 @@ class SolanaGasService {
74
80
  * @returns consumedUnitsPrice in micro-lamports(lamport * 10^-6)
75
81
  */
76
82
  async calculateCUPriceSolWeb3() {
77
- const BASE_FEE = 5_000;
78
- const resp = await this.connection.getRecentPrioritizationFees();
79
- const avgCUPrice = resp
80
- .reduce((acc, tx) => acc.plus(tx.prioritizationFee), new bignumber_js_1.default(0))
81
- .div(resp.length)
82
- .dp(0, bignumber_js_1.default.ROUND_CEIL);
83
- const cuPrice = new bignumber_js_1.default(Math.max(BASE_FEE, avgCUPrice.toNumber()));
84
- 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 lastTenTxFees = recentFees.slice(recentFees.length - 10);
99
+ const lastBlocksSlots = await this.connection.getBlocks(lastTenTxFees[0].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(lastTenTxFees[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;
85
116
  }
86
117
  }
87
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 {};