@clonegod/ttd-sol-common 1.0.84 → 1.0.85

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.
@@ -44,7 +44,7 @@ class TradeContext {
44
44
  }
45
45
  this.slippage_bps = Math.min(100, Math.max(30, this.trade_runtime.settings.slippage_bps));
46
46
  this.aToB = this.pool_info.is_reverse_token ? !this.order_msg.aToB : this.order_msg.aToB;
47
- this.solana_trade_runtime = (0, send_tx_1.create_solana_trade_runtime)(this.connection, this.trade_runtime, this.pool_info);
47
+ this.solana_trade_runtime = yield (0, send_tx_1.create_solana_trade_runtime)(this.connection, this.trade_runtime, this.pool_info);
48
48
  });
49
49
  }
50
50
  }
@@ -43,11 +43,20 @@ const subscribe_pool_change_by_geyser = (geyser_ws_url, pool_list, eventEmitter)
43
43
  };
44
44
  ws_client.send(JSON.stringify(request));
45
45
  }
46
+ function startPing(ws) {
47
+ setInterval(() => {
48
+ if (ws.readyState === ws_1.default.OPEN) {
49
+ ws.ping();
50
+ console.log('Ping helius ws server');
51
+ }
52
+ }, 30000);
53
+ }
46
54
  ws_client.on('open', function open() {
47
55
  (0, dist_1.log_info)(`WebSocket is open`);
48
56
  pool_list.forEach((pool) => {
49
57
  subscribe_account_change(ws_client, pool);
50
58
  });
59
+ startPing(ws_client);
51
60
  });
52
61
  ws_client.on('message', function incoming(data) {
53
62
  return __awaiter(this, void 0, void 0, function* () {
@@ -48,9 +48,18 @@ const subscribe_wallet_transaction = (geyser_ws_url, wallet_token_account_list,
48
48
  };
49
49
  ws_client.send(JSON.stringify(request));
50
50
  }
51
+ function startPing(ws) {
52
+ setInterval(() => {
53
+ if (ws.readyState === ws_1.default.OPEN) {
54
+ ws.ping();
55
+ console.log('Ping helius ws server');
56
+ }
57
+ }, 30000);
58
+ }
51
59
  ws_client.on('open', function open() {
52
60
  (0, dist_1.log_info)(`WebSocket is open`);
53
61
  subscribe_account_transaction();
62
+ startPing(ws_client);
54
63
  });
55
64
  ws_client.on('message', function incoming(data) {
56
65
  return __awaiter(this, void 0, void 0, function* () {
@@ -574,9 +574,6 @@ class RpcClient {
574
574
  setTimeout(() => __awaiter(this, void 0, void 0, function* () {
575
575
  const serializedTransaction = bs58_1.default.encode(transaction.serialize());
576
576
  let jitoApiUrl = `${types_1.JITO_API_URLS[region]}/api/v1/bundles`;
577
- if (solana_trade_runtime.jito_plugin_url) {
578
- jitoApiUrl = solana_trade_runtime.jito_plugin_url;
579
- }
580
577
  this.sendJitoTransaction(serializedTransaction, `${types_1.JITO_API_URLS['Default']}/api/v1/transactions?bundleOnly=true`);
581
578
  const bundleId = yield this.sendJitoBundle([serializedTransaction], jitoApiUrl);
582
579
  (0, dist_1.log_info)(`sendJitoBundle finish`, {
@@ -702,9 +699,6 @@ class RpcClient {
702
699
  setTimeout(() => __awaiter(this, void 0, void 0, function* () {
703
700
  const serializedTransaction = bs58_1.default.encode(transaction.serialize());
704
701
  let jitoApiUrl = `${types_1.JITO_API_URLS[region]}/api/v1/transactions?bundleOnly=true`;
705
- if (solana_trade_runtime.jito_plugin_url) {
706
- jitoApiUrl = solana_trade_runtime.jito_plugin_url;
707
- }
708
702
  (0, dist_1.log_info)('sendSmartTransactionWithTipMixed, start');
709
703
  this.sendTransaction(transaction);
710
704
  this.sendJitoTransaction(serializedTransaction, `${types_1.JITO_API_URLS['Default']}/api/v1/transactions?bundleOnly=true`);
@@ -0,0 +1 @@
1
+ export declare function get_current_jito_tip(speed: string): Promise<number>;
@@ -0,0 +1,55 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.get_current_jito_tip = get_current_jito_tip;
13
+ const dist_1 = require("@clonegod/ttd-common/dist");
14
+ const web3_js_1 = require("@solana/web3.js");
15
+ var jito_tip_pct;
16
+ const jito_tip_stats_url = process.env.JITO_TIP_FEE_STATS_URL || 'https://bundles.jito.wtf/api/v1/bundles/tip_floor';
17
+ const JITO_TIP_DEFAULT = 0.00008;
18
+ const JITO_TIP_MIN = 0.00004;
19
+ const JITO_TIP_MAX = 0.0005;
20
+ function get_jito_tip_floor() {
21
+ return __awaiter(this, void 0, void 0, function* () {
22
+ jito_tip_pct = (yield (yield fetch(jito_tip_stats_url)).json());
23
+ (0, dist_1.log_info)(`get_jito_tip_floor`, jito_tip_pct);
24
+ });
25
+ }
26
+ function refreshJitoTipsPct() {
27
+ setInterval(() => __awaiter(this, void 0, void 0, function* () {
28
+ get_jito_tip_floor();
29
+ }), 1 * 60 * 1000);
30
+ }
31
+ function get_current_jito_tip(speed) {
32
+ return __awaiter(this, void 0, void 0, function* () {
33
+ let tip_sol = JITO_TIP_DEFAULT;
34
+ if (!jito_tip_pct) {
35
+ jito_tip_pct = yield get_jito_tip_floor();
36
+ }
37
+ if (speed === 'fast') {
38
+ tip_sol = jito_tip_pct['landed_tips_50th_percentile'];
39
+ }
40
+ if (speed === 'turbo') {
41
+ tip_sol = jito_tip_pct['landed_tips_75th_percentile'];
42
+ }
43
+ if (speed === 'ultra') {
44
+ tip_sol = jito_tip_pct['landed_tips_95th_percentile'];
45
+ }
46
+ if (tip_sol < JITO_TIP_MIN) {
47
+ tip_sol = JITO_TIP_MIN;
48
+ }
49
+ if (tip_sol > JITO_TIP_MAX) {
50
+ tip_sol = JITO_TIP_MAX;
51
+ }
52
+ let tip_lamports = Math.ceil((tip_sol * 1.02) * web3_js_1.LAMPORTS_PER_SOL);
53
+ return tip_lamports;
54
+ });
55
+ }
@@ -2,6 +2,6 @@ import { StandardPoolInfoType, TradeRuntimeType, TradeStrategyType } from "@clon
2
2
  import { JitoRegion } from "../helius_sdk_v1.4.0";
3
3
  import { Connection } from "@solana/web3.js";
4
4
  import { SolanaTradeRuntimeType } from "../../types";
5
- export declare function create_solana_trade_runtime(connection: Connection, trade_runtime: TradeRuntimeType, pool_info: StandardPoolInfoType): SolanaTradeRuntimeType;
6
- export declare function calculate_gas_fee(trade_strategy: TradeStrategyType): number;
5
+ export declare function create_solana_trade_runtime(connection: Connection, trade_runtime: TradeRuntimeType, pool_info: StandardPoolInfoType): Promise<SolanaTradeRuntimeType>;
6
+ export declare function calculate_gas_fee(trade_strategy: TradeStrategyType): Promise<number>;
7
7
  export declare function get_jito_region(): JitoRegion;
@@ -1,83 +1,96 @@
1
1
  "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
2
11
  Object.defineProperty(exports, "__esModule", { value: true });
3
12
  exports.create_solana_trade_runtime = create_solana_trade_runtime;
4
13
  exports.calculate_gas_fee = calculate_gas_fee;
5
14
  exports.get_jito_region = get_jito_region;
6
15
  const dist_1 = require("@clonegod/ttd-common/dist");
7
16
  const constants_1 = require("../common/constants");
17
+ const get_jito_tip_floor_1 = require("./get_jito_tip_floor");
8
18
  const DEFAULT_TIP_RATIO = 0.3;
9
19
  function create_solana_trade_runtime(connection, trade_runtime, pool_info) {
10
- let trade_strategy = trade_runtime.settings.strategy;
11
- let { total_gas, priority_fee, tip_amount } = get_priority_fee_and_tip(trade_strategy);
12
- let { cu_limit, use_jito_plugin } = pool_info;
13
- if (isNaN(cu_limit) || cu_limit < 35000) {
14
- cu_limit = 35000;
15
- }
16
- let cu_price = Math.ceil(priority_fee * constants_1.ONE_MILLION / cu_limit);
17
- let jito_plugin_url = '';
18
- if (use_jito_plugin) {
19
- jito_plugin_url = process.env.JITO_PLUGIN_URL;
20
- }
21
- (0, dist_1.log_info)(`trade_strategy & gas`, {
22
- trade_strategy,
23
- total_gas,
24
- priority_fee,
25
- cu_limit,
26
- cu_price,
27
- use_jito_plugin,
28
- jito_plugin_url,
29
- tip_amount
20
+ return __awaiter(this, void 0, void 0, function* () {
21
+ let trade_strategy = trade_runtime.settings.strategy;
22
+ let { total_gas, priority_fee, tip_amount } = yield get_priority_fee_and_tip(trade_strategy);
23
+ let { cu_limit } = pool_info;
24
+ if (isNaN(cu_limit) || cu_limit < 35000) {
25
+ cu_limit = 35000;
26
+ }
27
+ let cu_price = Math.ceil(priority_fee * constants_1.ONE_MILLION / cu_limit);
28
+ (0, dist_1.log_info)(`trade_strategy & gas`, {
29
+ trade_strategy,
30
+ total_gas,
31
+ priority_fee,
32
+ cu_limit,
33
+ cu_price,
34
+ tip_amount
35
+ });
36
+ let solana_trade_runtime = Object.assign(Object.assign({}, trade_runtime), { connection,
37
+ total_gas,
38
+ priority_fee,
39
+ cu_limit,
40
+ cu_price,
41
+ tip_amount });
42
+ return solana_trade_runtime;
30
43
  });
31
- let solana_trade_runtime = Object.assign(Object.assign({}, trade_runtime), { connection,
32
- total_gas,
33
- priority_fee,
34
- cu_limit,
35
- cu_price,
36
- use_jito_plugin,
37
- jito_plugin_url,
38
- tip_amount });
39
- return solana_trade_runtime;
40
44
  }
41
45
  function get_priority_fee_and_tip(trade_strategy) {
42
- let priority_fee = 0;
43
- let tip_amount = 0;
44
- let tip_ratio = trade_strategy.tip_ratio || DEFAULT_TIP_RATIO;
45
- let total_gas = calculate_gas_fee(trade_strategy);
46
- if (trade_strategy.broadcast_type === 'rpc') {
47
- priority_fee = total_gas;
48
- tip_amount = 0;
49
- }
50
- else if (trade_strategy.broadcast_type === 'jito') {
51
- priority_fee = 0;
52
- tip_amount = total_gas;
53
- }
54
- else if (trade_strategy.broadcast_type === 'mixed') {
55
- if (tip_ratio < 0 || tip_ratio > 1) {
56
- tip_ratio = DEFAULT_TIP_RATIO;
46
+ return __awaiter(this, void 0, void 0, function* () {
47
+ let priority_fee = 0;
48
+ let tip_amount = 0;
49
+ let tip_ratio = trade_strategy.tip_ratio || DEFAULT_TIP_RATIO;
50
+ let total_gas = yield calculate_gas_fee(trade_strategy);
51
+ if (trade_strategy.broadcast_type === 'rpc') {
52
+ priority_fee = total_gas;
53
+ tip_amount = 0;
57
54
  }
58
- tip_amount = Math.ceil(total_gas * tip_ratio);
59
- priority_fee = total_gas - tip_amount;
60
- }
61
- return {
62
- total_gas,
63
- priority_fee,
64
- tip_amount
65
- };
55
+ else if (trade_strategy.broadcast_type === 'jito') {
56
+ priority_fee = 0;
57
+ tip_amount = total_gas;
58
+ }
59
+ else if (trade_strategy.broadcast_type === 'mixed') {
60
+ if (tip_ratio < 0 || tip_ratio > 1) {
61
+ tip_ratio = DEFAULT_TIP_RATIO;
62
+ }
63
+ tip_amount = Math.ceil(total_gas * tip_ratio);
64
+ priority_fee = total_gas - tip_amount;
65
+ }
66
+ return {
67
+ total_gas,
68
+ priority_fee,
69
+ tip_amount
70
+ };
71
+ });
66
72
  }
67
73
  function calculate_gas_fee(trade_strategy) {
68
- let { speed, fee_mode, fee_exact, fee_max_cap, } = trade_strategy;
69
- let gas_fee = 1000;
70
- switch (fee_mode) {
71
- case "exact":
72
- gas_fee = get_excat_fee_by_speed(speed, fee_exact);
73
- break;
74
- case "max_cap":
75
- gas_fee = get_dynamic_fee_by_speed(speed, 50000, fee_max_cap);
76
- break;
77
- default:
78
- throw new Error(`Not support fee_mode: ${fee_mode}`);
79
- }
80
- return gas_fee;
74
+ return __awaiter(this, void 0, void 0, function* () {
75
+ let { speed, fee_mode, fee_exact, fee_max_cap, broadcast_type } = trade_strategy;
76
+ let gas_fee = 1000;
77
+ switch (fee_mode) {
78
+ case "exact":
79
+ gas_fee = get_excat_fee_by_speed(speed, fee_exact);
80
+ break;
81
+ case "max_cap":
82
+ if (broadcast_type === 'jito') {
83
+ gas_fee = yield (0, get_jito_tip_floor_1.get_current_jito_tip)(speed);
84
+ }
85
+ else {
86
+ gas_fee = get_dynamic_fee_by_speed(speed, 50000, fee_max_cap);
87
+ }
88
+ break;
89
+ default:
90
+ throw new Error(`Not support fee_mode: ${fee_mode}`);
91
+ }
92
+ return gas_fee;
93
+ });
81
94
  }
82
95
  function get_excat_fee_by_speed(speed, fee_exact) {
83
96
  let factor = 1;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@clonegod/ttd-sol-common",
3
- "version": "1.0.84",
3
+ "version": "1.0.85",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "types": "types/index.d.ts",
@@ -13,7 +13,7 @@
13
13
  "push": "npm run build && npm publish"
14
14
  },
15
15
  "dependencies": {
16
- "@clonegod/ttd-common": "^1.0.159",
16
+ "@clonegod/ttd-common": "^1.0.160",
17
17
  "@irys/sdk": "^0.2.10",
18
18
  "@metaplex-foundation/mpl-token-metadata": "^2.5.2",
19
19
  "@solana/web3.js": "1.91.6",
package/types/index.d.ts CHANGED
@@ -52,8 +52,6 @@ export interface SolanaTradeRuntimeType extends TradeRuntimeType {
52
52
  // Compute Budget: SetComputeUnitLimit
53
53
  cu_limit: number
54
54
 
55
- jito_plugin_url: string
56
-
57
55
  tip_amount: number
58
56
  }
59
57