@cryptorubic/web3 0.13.0-alpha.solana-gas.2 → 0.13.0-alpha.solana-gas.3
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 +1 -1
- package/src/lib/adapter/adapters/utils/solana-utils/solana-gas-service.d.ts +0 -26
- package/src/lib/adapter/adapters/utils/solana-utils/solana-gas-service.js +0 -97
- package/src/lib/adapter/adapters/utils/solana-utils/utility-funcs.d.ts +0 -2
- package/src/lib/adapter/adapters/utils/solana-utils/utility-funcs.js +0 -9
package/package.json
CHANGED
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
import { HttpClient, ICustomLogger } from '@cryptorubic/core';
|
|
2
|
-
import { Connection } from '@solana/web3.js';
|
|
3
|
-
import BigNumber from 'bignumber.js';
|
|
4
|
-
export declare class SolanaGasService {
|
|
5
|
-
private httpClient;
|
|
6
|
-
private readonly connection;
|
|
7
|
-
private readonly logger;
|
|
8
|
-
private readonly HELIUS_API_URL;
|
|
9
|
-
private readonly HELIUS_API_KEY;
|
|
10
|
-
readonly DEFAULT_CU_LIMIT = 200000;
|
|
11
|
-
constructor(httpClient: HttpClient, connection: Connection, logger: ICustomLogger | undefined);
|
|
12
|
-
/**
|
|
13
|
-
* @returns wei ComputedUnitsLimit - like gasLimit in evm
|
|
14
|
-
*/
|
|
15
|
-
getConsumedUnitsLimit(txData: string): Promise<BigNumber>;
|
|
16
|
-
/**
|
|
17
|
-
* @param txData base64 or hex string
|
|
18
|
-
* @returns prioritizationFee in micro-lamports (lamport = 1_000_000 microlamports)
|
|
19
|
-
*/
|
|
20
|
-
getPriorityFee(txData: string): Promise<BigNumber>;
|
|
21
|
-
/**
|
|
22
|
-
* @returns prioritizationFee in micro-lamports(lamport * 10^-6)
|
|
23
|
-
*/
|
|
24
|
-
private calculatePriorityFeeHelius;
|
|
25
|
-
private getRecentPriorityFeeSolWeb3;
|
|
26
|
-
}
|
|
@@ -1,97 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.SolanaGasService = void 0;
|
|
4
|
-
const utils_1 = require("ethers/lib/utils");
|
|
5
|
-
const utility_funcs_1 = require("./utility-funcs");
|
|
6
|
-
const bignumber_js_1 = require("bignumber.js");
|
|
7
|
-
// solana gas fee FORMULA: 200_000 CU * 0.02 Lamports/CU = 4000 Lamports = 0.000004 SOL
|
|
8
|
-
class SolanaGasService {
|
|
9
|
-
httpClient;
|
|
10
|
-
connection;
|
|
11
|
-
logger;
|
|
12
|
-
HELIUS_API_URL = 'https://mainnet.helius-rpc.com';
|
|
13
|
-
HELIUS_API_KEY;
|
|
14
|
-
DEFAULT_CU_LIMIT = 200_000; //in CU
|
|
15
|
-
constructor(httpClient, connection, logger) {
|
|
16
|
-
this.httpClient = httpClient;
|
|
17
|
-
this.connection = connection;
|
|
18
|
-
this.logger = logger;
|
|
19
|
-
this.HELIUS_API_KEY = '';
|
|
20
|
-
}
|
|
21
|
-
/**
|
|
22
|
-
* @returns wei ComputedUnitsLimit - like gasLimit in evm
|
|
23
|
-
*/
|
|
24
|
-
async getConsumedUnitsLimit(txData) {
|
|
25
|
-
try {
|
|
26
|
-
const tx = (0, utility_funcs_1.convertB64DataToTx)(txData);
|
|
27
|
-
const resp = await this.connection.simulateTransaction(tx, {
|
|
28
|
-
replaceRecentBlockhash: true
|
|
29
|
-
});
|
|
30
|
-
return resp.value.unitsConsumed ? new bignumber_js_1.default(resp.value.unitsConsumed * 1.2) : new bignumber_js_1.default(this.DEFAULT_CU_LIMIT);
|
|
31
|
-
}
|
|
32
|
-
catch (err) {
|
|
33
|
-
console.error('[SolanaApiService_getConsumedUnitsLimit] err ==> ', err);
|
|
34
|
-
return new bignumber_js_1.default(this.DEFAULT_CU_LIMIT);
|
|
35
|
-
}
|
|
36
|
-
}
|
|
37
|
-
/**
|
|
38
|
-
* @param txData base64 or hex string
|
|
39
|
-
* @returns prioritizationFee in micro-lamports (lamport = 1_000_000 microlamports)
|
|
40
|
-
*/
|
|
41
|
-
async getPriorityFee(txData) {
|
|
42
|
-
if (!this.HELIUS_API_KEY) {
|
|
43
|
-
console.warn('[SolanaApiService_getPriorityFee] heliusApiKey is not provided in "createFactory"');
|
|
44
|
-
}
|
|
45
|
-
const resp = await Promise.allSettled([this.calculatePriorityFeeHelius(txData), this.getRecentPriorityFeeSolWeb3()]);
|
|
46
|
-
const cuPrice = resp
|
|
47
|
-
.filter((r) => r.status === 'fulfilled')
|
|
48
|
-
.map((r) => r.value)
|
|
49
|
-
.reduce((acc, price) => (acc.gt(price) ? acc : price), new bignumber_js_1.default(0));
|
|
50
|
-
return cuPrice;
|
|
51
|
-
}
|
|
52
|
-
/**
|
|
53
|
-
* @returns prioritizationFee in micro-lamports(lamport * 10^-6)
|
|
54
|
-
*/
|
|
55
|
-
async calculatePriorityFeeHelius(txData) {
|
|
56
|
-
try {
|
|
57
|
-
const tx = (0, utility_funcs_1.convertB64DataToTx)(txData);
|
|
58
|
-
const resp = await this.httpClient.post(`${this.HELIUS_API_URL}/?api-key=${this.HELIUS_API_KEY}`, {
|
|
59
|
-
jsonrpc: '2.0',
|
|
60
|
-
id: '1',
|
|
61
|
-
method: 'getPriorityFeeEstimate',
|
|
62
|
-
params: [
|
|
63
|
-
{
|
|
64
|
-
transaction: utils_1.base58.encode(tx.serialize()), // Pass the serialized transaction in Base58
|
|
65
|
-
options: { priorityLevel: 'Medium' }
|
|
66
|
-
}
|
|
67
|
-
]
|
|
68
|
-
});
|
|
69
|
-
this.logger?.customLog('HELIUS PRIORITY_FEE SUCCESS', { priorityFeeEstimate: resp.result.priorityFeeEstimate });
|
|
70
|
-
return new bignumber_js_1.default(resp.result.priorityFeeEstimate);
|
|
71
|
-
}
|
|
72
|
-
catch (err) {
|
|
73
|
-
this.logger?.customLog('HELIUS PRIORITY_FEE ERROR', err);
|
|
74
|
-
return new bignumber_js_1.default(0);
|
|
75
|
-
}
|
|
76
|
-
}
|
|
77
|
-
async getRecentPriorityFeeSolWeb3() {
|
|
78
|
-
const lastSlot = await this.connection.getSlot();
|
|
79
|
-
// last slots array [1000, 1001, 1002 ...]
|
|
80
|
-
const last10Slots = Array.from({ length: 10 }, (_, idx) => lastSlot - idx).reverse();
|
|
81
|
-
const getPriorityFeesFromBlock = async (slotNum) => {
|
|
82
|
-
const block = await this.connection.getBlock(slotNum, { maxSupportedTransactionVersion: 0, commitment: 'confirmed' });
|
|
83
|
-
if (!block)
|
|
84
|
-
return [];
|
|
85
|
-
return block.transactions.map((tx) => (tx.meta && tx.meta.fee ? tx.meta.fee : 0));
|
|
86
|
-
};
|
|
87
|
-
const resp = await Promise.all(last10Slots.map((slotNum) => getPriorityFeesFromBlock(slotNum)));
|
|
88
|
-
const filteredPriorityFees = resp.flat().filter((fee) => fee > 0);
|
|
89
|
-
const avgProrityFee = filteredPriorityFees
|
|
90
|
-
.reduce((acc, fee) => acc.plus(fee), new bignumber_js_1.default(0))
|
|
91
|
-
.div(filteredPriorityFees.length)
|
|
92
|
-
.dp(0, bignumber_js_1.default.ROUND_CEIL);
|
|
93
|
-
this.logger?.customLog('SOLANA_WEB3 PRIORITY_FEE SUCCESS', { priorityFeeEstimate: avgProrityFee.toNumber() });
|
|
94
|
-
return avgProrityFee;
|
|
95
|
-
}
|
|
96
|
-
}
|
|
97
|
-
exports.SolanaGasService = SolanaGasService;
|
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.convertB64DataToTx = convertB64DataToTx;
|
|
4
|
-
const web3_js_1 = require("@solana/web3.js");
|
|
5
|
-
function convertB64DataToTx(txData) {
|
|
6
|
-
const bufferData = txData.startsWith('0x') ? Buffer.from(txData.slice(2), 'hex') : Buffer.from(txData, 'base64');
|
|
7
|
-
const tx = web3_js_1.VersionedTransaction.deserialize(bufferData);
|
|
8
|
-
return tx;
|
|
9
|
-
}
|