@clonegod/ttd-sol-common 2.0.13 → 2.0.15

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.
@@ -3,3 +3,4 @@ export declare enum COMMITMENT_LEVEL {
3
3
  CONFIRMED = "confirmed",
4
4
  _FINALIZED = "finalized"
5
5
  }
6
+ export declare const LAMPORTS_PER_MICRO_LAMPORTS = 1000000;
@@ -1,9 +1,10 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.COMMITMENT_LEVEL = void 0;
3
+ exports.LAMPORTS_PER_MICRO_LAMPORTS = exports.COMMITMENT_LEVEL = void 0;
4
4
  var COMMITMENT_LEVEL;
5
5
  (function (COMMITMENT_LEVEL) {
6
6
  COMMITMENT_LEVEL["PROCESSED"] = "processed";
7
7
  COMMITMENT_LEVEL["CONFIRMED"] = "confirmed";
8
8
  COMMITMENT_LEVEL["_FINALIZED"] = "finalized";
9
9
  })(COMMITMENT_LEVEL || (exports.COMMITMENT_LEVEL = COMMITMENT_LEVEL = {}));
10
+ exports.LAMPORTS_PER_MICRO_LAMPORTS = 1000000;
@@ -1,2 +1,3 @@
1
1
  export * from './constants';
2
2
  export * from './get_wallet_token_account';
3
+ export * from './subscribe_account_update';
@@ -16,3 +16,4 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
17
  __exportStar(require("./constants"), exports);
18
18
  __exportStar(require("./get_wallet_token_account"), exports);
19
+ __exportStar(require("./subscribe_account_update"), exports);
@@ -1,4 +1,4 @@
1
1
  import { StandardPoolInfoType } from "@clonegod/ttd-core";
2
2
  import { DEX_ID } from "@clonegod/ttd-core/dist";
3
3
  import { SolanaPoolAccountUpdateEventData } from "../types";
4
- export declare function subscribe_account_update(dex_id: DEX_ID, pool_list: StandardPoolInfoType[], callback: (eventData: SolanaPoolAccountUpdateEventData) => void): void;
4
+ export declare function subscribe_pool_account_update(dex_id: DEX_ID, pool_list: StandardPoolInfoType[], callback: (eventData: SolanaPoolAccountUpdateEventData) => void): void;
@@ -1,8 +1,8 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.subscribe_account_update = subscribe_account_update;
3
+ exports.subscribe_pool_account_update = subscribe_pool_account_update;
4
4
  const dist_1 = require("@clonegod/ttd-core/dist");
5
- function subscribe_account_update(dex_id, pool_list, callback) {
5
+ function subscribe_pool_account_update(dex_id, pool_list, callback) {
6
6
  const ws_url = process.env.SUBSCIBE_MULTIPLE_PROVIDERS_WS_URL || 'ws://127.0.0.1:10000';
7
7
  const ws_client = new dist_1.WebSocketClient(ws_url);
8
8
  ws_client.onOpen(() => {
@@ -0,0 +1,3 @@
1
+ import { Transaction } from "@solana/web3.js";
2
+ export declare const HELIUS_JITO_TIP_ACCOUNTS: string[];
3
+ export declare const sendTxWithHelius: (signedTransaction: Transaction, send_jito?: boolean) => Promise<string>;
@@ -0,0 +1,41 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.sendTxWithHelius = exports.HELIUS_JITO_TIP_ACCOUNTS = void 0;
7
+ const axios_1 = __importDefault(require("axios"));
8
+ exports.HELIUS_JITO_TIP_ACCOUNTS = [
9
+ "4ACfpUFoaSD9bfPdeu6DBt89gB6ENTeHBXCAi87NhDEE",
10
+ "D2L6yPZ2FmmmTKPgzaMKdhu6EWZcTpLy1Vhx8uvZe7NZ",
11
+ "9bnz4RShgq1hAnLnZbP8kbgBg1kEmcJBYQq3gQbmnSta",
12
+ "5VY91ws6B2hMmBFRsXkoAAdsPHBJwRfBht4DXox3xkwn",
13
+ "2nyhqdwKcJZR2vcqCyrYsaPVdAnFoJjiksCXJ7hfEYgD",
14
+ "2q5pghRs6arqVjRvT5gfgWfWcHWmw1ZuCzphgd5KfWGJ",
15
+ "wyvPkWjVZz1M8fHQnMMCDTQDbkManefNNhweYk5WkcF",
16
+ "3KCKozbAaF75qEU33jtzozcJ29yJuaLJTy2jFdzUY8bT",
17
+ "4vieeGHPYPG2MmyPRcYjdiDmmhN3ww7hsFNap8pVN3Ey",
18
+ "4TQLFNWK8AovT1gFvda5jfw2oJeRMKEmw7aH6MGBJ3or"
19
+ ];
20
+ const sendTxWithHelius = async (signedTransaction, send_jito = true) => {
21
+ let url = 'http://tyo-sender.helius-rpc.com/fast';
22
+ if (!send_jito) {
23
+ url = url + "?swqos_only=true";
24
+ }
25
+ const response = await axios_1.default.post(url, {
26
+ jsonrpc: '2.0',
27
+ id: Date.now().toString(),
28
+ method: 'sendTransaction',
29
+ params: [
30
+ Buffer.from(signedTransaction.serialize()).toString('base64'),
31
+ {
32
+ encoding: 'base64',
33
+ skipPreflight: true,
34
+ maxRetries: 0
35
+ }
36
+ ]
37
+ });
38
+ console.dir(response.data, { depth: null });
39
+ return response.data.result;
40
+ };
41
+ exports.sendTxWithHelius = sendTxWithHelius;
@@ -0,0 +1 @@
1
+ export * from './send_tx';
@@ -0,0 +1,17 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./send_tx"), exports);
File without changes
File without changes
@@ -0,0 +1,6 @@
1
+ import { Transaction } from '@solana/web3.js';
2
+ export declare class TransactionSender {
3
+ constructor();
4
+ sendTransaction(singedTransactions: Transaction[]): Promise<string>;
5
+ sendBundle(signedTransactions: Transaction[]): Promise<string>;
6
+ }
@@ -0,0 +1,20 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.TransactionSender = void 0;
4
+ const helius_1 = require("./helius");
5
+ class TransactionSender {
6
+ constructor() {
7
+ }
8
+ async sendTransaction(singedTransactions) {
9
+ if (singedTransactions.length === 1) {
10
+ return await (0, helius_1.sendTxWithHelius)(singedTransactions[0]);
11
+ }
12
+ else {
13
+ return await this.sendBundle(singedTransactions);
14
+ }
15
+ }
16
+ async sendBundle(signedTransactions) {
17
+ throw new Error('not implemented');
18
+ }
19
+ }
20
+ exports.TransactionSender = TransactionSender;
@@ -13,6 +13,7 @@ export declare class SolTransactionBuilder {
13
13
  execute(context: TradeContext): Promise<string>;
14
14
  getPreInstructions(context: TradeContext): TransactionInstruction[];
15
15
  getPostInstructions(context: TradeContext): TransactionInstruction[];
16
- buildTransactionForSwap(context: TradeContext, swapInstructions: TransactionInstruction[]): Transaction;
17
- buildTransactionForTip(context: TradeContext, tipAccount: string, tipAmount: number, tipPayer: Keypair): Transaction;
16
+ buildTransactionForSwap(context: TradeContext, swapInstructions: TransactionInstruction[], send_jito?: boolean): Transaction;
17
+ getHeliusJitoTipInstruction(context: TradeContext): TransactionInstruction;
18
+ buildTransactionForTip(context: TradeContext, tipAccount: string, tipAmount: number, tipPayer?: Keypair): Transaction;
18
19
  }
@@ -3,7 +3,8 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.SolTransactionBuilder = void 0;
4
4
  const dist_1 = require("@clonegod/ttd-core/dist");
5
5
  const web3_js_1 = require("@solana/web3.js");
6
- const LAMPORTS_PER_MICRO_LAMPORTS = 1000000;
6
+ const common_1 = require("../common");
7
+ const helius_1 = require("./send/helius");
7
8
  class SolTransactionBuilder {
8
9
  constructor(appConfig) {
9
10
  this.appConfig = appConfig;
@@ -26,7 +27,7 @@ class SolTransactionBuilder {
26
27
  getPreInstructions(context) {
27
28
  const cu_limit = context.pool_info.cu_limit || 600000;
28
29
  const sol_priority_fee_lamports = context.trade_runtime.settings.strategy.sol_priority_fee;
29
- const priorityFeeMicroLamports = Math.floor((sol_priority_fee_lamports * LAMPORTS_PER_MICRO_LAMPORTS) / cu_limit);
30
+ const priorityFeeMicroLamports = Math.floor((sol_priority_fee_lamports * common_1.LAMPORTS_PER_MICRO_LAMPORTS) / cu_limit);
30
31
  const computeBudgetInstruction = web3_js_1.ComputeBudgetProgram.setComputeUnitLimit({
31
32
  units: cu_limit,
32
33
  });
@@ -38,7 +39,7 @@ class SolTransactionBuilder {
38
39
  getPostInstructions(context) {
39
40
  return [];
40
41
  }
41
- buildTransactionForSwap(context, swapInstructions) {
42
+ buildTransactionForSwap(context, swapInstructions, send_jito = true) {
42
43
  const max_block_offset = context.trade_runtime.settings.strategy.max_block_offset;
43
44
  const swapTx = new web3_js_1.Transaction();
44
45
  const preInstructions = this.getPreInstructions(context);
@@ -47,10 +48,22 @@ class SolTransactionBuilder {
47
48
  swapTx.recentBlockhash = this.recentBlockhash;
48
49
  swapTx.lastValidBlockHeight = this.recentBlockheight + max_block_offset;
49
50
  swapTx.feePayer = this.keypair.publicKey;
51
+ if (send_jito) {
52
+ let jitoTipInstruction = this.getHeliusJitoTipInstruction(context);
53
+ swapTx.instructions.push(jitoTipInstruction);
54
+ }
50
55
  swapTx.sign(this.keypair);
51
56
  return swapTx;
52
57
  }
53
- buildTransactionForTip(context, tipAccount, tipAmount, tipPayer) {
58
+ getHeliusJitoTipInstruction(context) {
59
+ let tip_lamports = Math.max(context.trade_runtime.settings.strategy.sol_tip_fee, 0.0002 * web3_js_1.LAMPORTS_PER_SOL);
60
+ return web3_js_1.SystemProgram.transfer({
61
+ fromPubkey: this.keypair.publicKey,
62
+ toPubkey: new web3_js_1.PublicKey(helius_1.HELIUS_JITO_TIP_ACCOUNTS[Math.floor(Math.random() * helius_1.HELIUS_JITO_TIP_ACCOUNTS.length)]),
63
+ lamports: tip_lamports,
64
+ });
65
+ }
66
+ buildTransactionForTip(context, tipAccount, tipAmount, tipPayer = undefined) {
54
67
  const max_block_offset = context.trade_runtime.settings.strategy.max_block_offset;
55
68
  if (!tipPayer) {
56
69
  tipPayer = this.keypair;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@clonegod/ttd-sol-common",
3
- "version": "2.0.13",
3
+ "version": "2.0.15",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "types": "types/index.d.ts",
@@ -12,3 +12,6 @@ export enum COMMITMENT_LEVEL {
12
12
  _FINALIZED = "finalized"
13
13
  }
14
14
 
15
+ // There are 10^6 micro-lamports in one lamport
16
+ export const LAMPORTS_PER_MICRO_LAMPORTS = 1000000;
17
+
@@ -1,3 +1,3 @@
1
1
  export * from './constants'
2
2
  export * from './get_wallet_token_account'
3
-
3
+ export * from './subscribe_account_update'
@@ -7,7 +7,7 @@ import { SolanaPoolAccountUpdateEventData } from "../types";
7
7
  * - 询价:基于池子数据计算价格
8
8
  * - 交易使用:交易中需要池子的最新状态
9
9
  */
10
- export function subscribe_account_update(dex_id: DEX_ID, pool_list: StandardPoolInfoType[], callback: (eventData: SolanaPoolAccountUpdateEventData) => void) {
10
+ export function subscribe_pool_account_update(dex_id: DEX_ID, pool_list: StandardPoolInfoType[], callback: (eventData: SolanaPoolAccountUpdateEventData) => void) {
11
11
  const ws_url = process.env.SUBSCIBE_MULTIPLE_PROVIDERS_WS_URL || 'ws://127.0.0.1:10000';
12
12
  const ws_client = new WebSocketClient(ws_url)
13
13
  ws_client.onOpen(() => {
@@ -0,0 +1,38 @@
1
+ import { Transaction } from "@solana/web3.js"
2
+ import axios from "axios"
3
+
4
+ export const HELIUS_JITO_TIP_ACCOUNTS = [
5
+ "4ACfpUFoaSD9bfPdeu6DBt89gB6ENTeHBXCAi87NhDEE",
6
+ "D2L6yPZ2FmmmTKPgzaMKdhu6EWZcTpLy1Vhx8uvZe7NZ",
7
+ "9bnz4RShgq1hAnLnZbP8kbgBg1kEmcJBYQq3gQbmnSta",
8
+ "5VY91ws6B2hMmBFRsXkoAAdsPHBJwRfBht4DXox3xkwn",
9
+ "2nyhqdwKcJZR2vcqCyrYsaPVdAnFoJjiksCXJ7hfEYgD",
10
+ "2q5pghRs6arqVjRvT5gfgWfWcHWmw1ZuCzphgd5KfWGJ",
11
+ "wyvPkWjVZz1M8fHQnMMCDTQDbkManefNNhweYk5WkcF",
12
+ "3KCKozbAaF75qEU33jtzozcJ29yJuaLJTy2jFdzUY8bT",
13
+ "4vieeGHPYPG2MmyPRcYjdiDmmhN3ww7hsFNap8pVN3Ey",
14
+ "4TQLFNWK8AovT1gFvda5jfw2oJeRMKEmw7aH6MGBJ3or"
15
+ ]
16
+
17
+ export const sendTxWithHelius = async (signedTransaction: Transaction, send_jito:boolean=true): Promise<string> => {
18
+ let url = 'http://tyo-sender.helius-rpc.com/fast'
19
+ if(!send_jito) {
20
+ url = url + "?swqos_only=true"
21
+ }
22
+ const response = await axios.post<{ result: string }>(url, {
23
+ jsonrpc: '2.0',
24
+ id: Date.now().toString(),
25
+ method: 'sendTransaction',
26
+ params: [
27
+ Buffer.from(signedTransaction.serialize()).toString('base64'),
28
+ {
29
+ encoding: 'base64',
30
+ skipPreflight: true, // Required for Sender
31
+ maxRetries: 0
32
+ }
33
+ ]
34
+ })
35
+ console.dir(response.data, { depth: null })
36
+
37
+ return response.data.result;
38
+ }
@@ -0,0 +1,2 @@
1
+ export * from './send_tx';
2
+
@@ -0,0 +1,35 @@
1
+ // import { TradeContext } from "@clonegod/ttd-core/dist";
2
+ // import { ComputeBudgetProgram, Keypair, LAMPORTS_PER_SOL, PublicKey, SystemProgram, Transaction, TransactionInstruction } from "@solana/web3.js";
3
+ // import axios from "axios";
4
+
5
+ // export class JitoUtils {
6
+ // public jitoTipAccounts: string[] = []
7
+
8
+ // constructor() {
9
+ // this.init();
10
+ // }
11
+
12
+ // async init(): Promise<void> {
13
+ // return this.fetchJitoTipAccounts().then((accounts) => {
14
+ // this.jitoTipAccounts = accounts;
15
+ // console.log(`fetchJitoTipAccounts success: ${accounts.length} accounts`, accounts)
16
+ // });
17
+ // }
18
+
19
+ // async fetchJitoTipAccounts(): Promise<string[]> {
20
+ // const response = await axios.post<{ result: string[] }>('https://mainnet.block-engine.jito.wtf/api/v1/getTipAccounts', {
21
+ // jsonrpc: '2.0',
22
+ // id: 1,
23
+ // method: 'getTipAccounts',
24
+ // params: []
25
+ // })
26
+ // return response.data.result;
27
+ // }
28
+
29
+ // getRandomJitoTipAccount(): string {
30
+ // if(this.jitoTipAccounts.length === 0) {
31
+ // throw new Error('jitoTipAccounts is empty');
32
+ // }
33
+ // return this.jitoTipAccounts[Math.floor(Math.random() * this.jitoTipAccounts.length)];
34
+ // }
35
+ // }
@@ -0,0 +1,34 @@
1
+ import { AppConfig, log_error, log_info, TradeContext } from '@clonegod/ttd-core/dist';
2
+ import { Connection, Keypair, Transaction, TransactionInstruction } from '@solana/web3.js';
3
+ import { sendTxWithHelius } from './helius';
4
+
5
+
6
+ /**
7
+ * Solana 交易发送器(参考 BSC 的多渠道并行发送设计)
8
+ *
9
+ * 设计思路对齐 BSC:
10
+ * - 交易构造由具体 DEX Trade 类完成,只返回 swap 指令
11
+ * - buildMainTx 负责构建主交易(所有渠道共用,只构建一次)
12
+ * - buildTipTx 负责构建 tip 交易(渠道特定,动态构建)
13
+ * - TransactionSender 负责多渠道并行发送(RPC / Helius / Jito)
14
+ */
15
+ export class TransactionSender {
16
+
17
+ constructor() {
18
+ }
19
+
20
+ async sendTransaction(singedTransactions: Transaction[]): Promise<string> {
21
+ if(singedTransactions.length === 1) {
22
+ return await sendTxWithHelius(singedTransactions[0])
23
+ } else {
24
+ return await this.sendBundle(singedTransactions)
25
+ }
26
+ }
27
+
28
+ async sendBundle(signedTransactions: Transaction[]): Promise<string> {
29
+ throw new Error('not implemented')
30
+ }
31
+
32
+
33
+ }
34
+
@@ -1,12 +1,11 @@
1
1
  import { CHAIN_ID, TradeContext } from "@clonegod/ttd-core/dist";
2
- import { SolanaBlockMetaUpdateEvent } from "../types";
3
2
  import { ComputeBudgetProgram, Connection, Keypair, LAMPORTS_PER_SOL, PublicKey, SystemProgram, Transaction, TransactionInstruction } from "@solana/web3.js";
3
+ import { LAMPORTS_PER_MICRO_LAMPORTS } from "../common";
4
+ import { SolanaBlockMetaUpdateEvent } from "../types";
4
5
  import { SolanaTradeAppConfig } from "./SolanaTradeAppConfig";
6
+ import { HELIUS_JITO_TIP_ACCOUNTS } from "./send/helius";
5
7
 
6
8
 
7
- // There are 10^6 micro-lamports in one lamport
8
- const LAMPORTS_PER_MICRO_LAMPORTS = 1000000;
9
-
10
9
  export class SolTransactionBuilder {
11
10
  appConfig: SolanaTradeAppConfig
12
11
  protected connection: Connection
@@ -67,7 +66,7 @@ export class SolTransactionBuilder {
67
66
  }
68
67
 
69
68
 
70
- buildTransactionForSwap(context: TradeContext, swapInstructions: TransactionInstruction[]): Transaction {
69
+ buildTransactionForSwap(context: TradeContext, swapInstructions: TransactionInstruction[], send_jito: boolean=true): Transaction {
71
70
  const max_block_offset = context.trade_runtime.settings.strategy.max_block_offset
72
71
  // console.log(`buildTransactionForSwap, max_block_offset: ${max_block_offset}`)
73
72
 
@@ -81,12 +80,26 @@ export class SolTransactionBuilder {
81
80
  swapTx.lastValidBlockHeight = this.recentBlockheight + max_block_offset
82
81
  swapTx.feePayer = this.keypair.publicKey
83
82
 
83
+ if(send_jito) {
84
+ let jitoTipInstruction = this.getHeliusJitoTipInstruction(context)
85
+ swapTx.instructions.push(jitoTipInstruction)
86
+ }
87
+
84
88
  swapTx.sign(this.keypair)
85
89
 
86
90
  return swapTx
87
91
  }
88
92
 
89
- buildTransactionForTip(context: TradeContext, tipAccount: string, tipAmount: number, tipPayer: Keypair): Transaction {
93
+ getHeliusJitoTipInstruction(context: TradeContext): TransactionInstruction {
94
+ let tip_lamports = Math.max(context.trade_runtime.settings.strategy.sol_tip_fee, 0.0002 * LAMPORTS_PER_SOL)
95
+ return SystemProgram.transfer({
96
+ fromPubkey: this.keypair.publicKey,
97
+ toPubkey: new PublicKey(HELIUS_JITO_TIP_ACCOUNTS[Math.floor(Math.random() * HELIUS_JITO_TIP_ACCOUNTS.length)]),
98
+ lamports: tip_lamports,
99
+ })
100
+ }
101
+
102
+ buildTransactionForTip(context: TradeContext, tipAccount: string, tipAmount: number, tipPayer: Keypair=undefined): Transaction {
90
103
  const max_block_offset = context.trade_runtime.settings.strategy.max_block_offset
91
104
 
92
105
  if(!tipPayer) {