@clonegod/ttd-sol-common 2.0.44 → 2.0.46

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.
@@ -1,3 +1,2 @@
1
- import { Transaction } from "@solana/web3.js";
2
1
  export declare const HELIUS_TIP_ACCOUNTS: string[];
3
- export declare const sendTxWithHelius: (signedTransaction: Transaction, swqos_only: boolean) => Promise<string>;
2
+ export declare const sendTxWithHelius: (signedTransactionBase64: string, swqos_only: boolean) => Promise<string>;
@@ -14,7 +14,7 @@ exports.HELIUS_TIP_ACCOUNTS = [
14
14
  "4vieeGHPYPG2MmyPRcYjdiDmmhN3ww7hsFNap8pVN3Ey",
15
15
  "4TQLFNWK8AovT1gFvda5jfw2oJeRMKEmw7aH6MGBJ3or"
16
16
  ];
17
- const sendTxWithHelius = async (signedTransaction, swqos_only) => {
17
+ const sendTxWithHelius = async (signedTransactionBase64, swqos_only) => {
18
18
  let url = process.env.HELIUS_SEND_TX_URL || 'http://tyo-sender.helius-rpc.com/fast';
19
19
  if (swqos_only) {
20
20
  url = url + "?swqos_only=true";
@@ -25,7 +25,7 @@ const sendTxWithHelius = async (signedTransaction, swqos_only) => {
25
25
  id: Date.now().toString(),
26
26
  method: 'sendTransaction',
27
27
  params: [
28
- Buffer.from(signedTransaction.serialize()).toString('base64'),
28
+ signedTransactionBase64,
29
29
  {
30
30
  encoding: 'base64',
31
31
  skipPreflight: true,
@@ -1,4 +1,3 @@
1
- import { Transaction } from "@solana/web3.js";
2
1
  export declare const JITO_TIP_ACCOUNTS: string[];
3
2
  export declare class JitoUtils {
4
3
  static jitoTipAccounts: string[];
@@ -6,5 +5,5 @@ export declare class JitoUtils {
6
5
  static fetchJitoTipAccounts(): Promise<string[]>;
7
6
  }
8
7
  export declare const getJitoTipAccount: () => string;
9
- export declare const sendBundleWithJito: (mainTx: Transaction, tipTx: Transaction) => Promise<string>;
10
- export declare const sendBundleWithJitoMultiIps: (mainTx: Transaction, tipTx: Transaction) => Promise<string>;
8
+ export declare const sendBundleWithJito: (mainTxBase64: string, tipTxBase64: string) => Promise<string>;
9
+ export declare const sendBundleWithJitoMultiIps: (mainTxHash: string, mainTxBase64: string, tipTxBase64: string) => Promise<string>;
@@ -36,7 +36,6 @@ Object.defineProperty(exports, "__esModule", { value: true });
36
36
  exports.sendBundleWithJitoMultiIps = exports.sendBundleWithJito = exports.getJitoTipAccount = exports.JitoUtils = exports.JITO_TIP_ACCOUNTS = void 0;
37
37
  const dist_1 = require("@clonegod/ttd-core/dist");
38
38
  const axios_1 = __importStar(require("axios"));
39
- const common_1 = require("../../common");
40
39
  const http_client_1 = require("./http_client");
41
40
  exports.JITO_TIP_ACCOUNTS = [
42
41
  "DfXygSm4jCyNCybVYYK6DwvWqjKee8pbDmJGcLWNDXjh",
@@ -77,7 +76,7 @@ const getJitoTipAccount = () => {
77
76
  };
78
77
  exports.getJitoTipAccount = getJitoTipAccount;
79
78
  JitoUtils.init();
80
- const sendBundleWithJito = async (mainTx, tipTx) => {
79
+ const sendBundleWithJito = async (mainTxBase64, tipTxBase64) => {
81
80
  var _a, _b;
82
81
  let url = process.env.JITO_SEND_BUNDLE_URL || 'https://tokyo.mainnet.block-engine.jito.wtf/api/v1/bundles';
83
82
  const client = (0, http_client_1.getHttpClient)(url);
@@ -86,7 +85,7 @@ const sendBundleWithJito = async (mainTx, tipTx) => {
86
85
  id: 1,
87
86
  method: 'sendBundle',
88
87
  params: [
89
- [tipTx, mainTx].map(tx => Buffer.from(tx.serialize()).toString('base64')),
88
+ [tipTxBase64, mainTxBase64],
90
89
  {
91
90
  "encoding": "base64"
92
91
  }
@@ -110,14 +109,14 @@ const sendBundleWithJito = async (mainTx, tipTx) => {
110
109
  }
111
110
  };
112
111
  exports.sendBundleWithJito = sendBundleWithJito;
113
- const sendBundleWithJitoMultiIps = async (mainTx, tipTx) => {
112
+ const sendBundleWithJitoMultiIps = async (mainTxHash, mainTxBase64, tipTxBase64) => {
114
113
  let ips = (process.env.JITO_SEND_BUNDLE_SERVER_IPS || '127.0.0.1').split(',');
115
114
  let urls = ips.map(ip => `http://${ip}:10001/solana/send_tx`);
116
115
  const body = {
117
116
  trace_id: '',
118
- txid: (0, common_1.getSignature)(mainTx),
117
+ txid: mainTxHash,
119
118
  encoding: 'base64',
120
- encoded_tx: [tipTx, mainTx].map(tx => Buffer.from(tx.serialize()).toString('base64')).join(','),
119
+ encoded_tx: [tipTxBase64, mainTxBase64].join(','),
121
120
  max_retry: 3
122
121
  };
123
122
  Promise.all(urls.map(async (url) => {
@@ -1,6 +1,7 @@
1
- import { Transaction } from '@solana/web3.js';
1
+ import { Transaction, VersionedTransaction } from '@solana/web3.js';
2
2
  export declare class TransactionSender {
3
3
  constructor();
4
- sendTransaction(singedTransaction: Transaction, swqos_only: boolean): Promise<string>;
5
- sendBundle(mainTx: Transaction, tipTx: Transaction, use_multi_ips?: boolean): Promise<string>;
4
+ sendTransaction(singedTransaction: Transaction | VersionedTransaction, swqos_only?: boolean): Promise<string>;
5
+ sendBundle(mainTx: Transaction | VersionedTransaction, tipTx: Transaction | VersionedTransaction, use_multi_ips?: boolean): Promise<string>;
6
6
  }
7
+ export declare function serializeTransactionBase64(tx: Transaction | VersionedTransaction): string;
@@ -1,21 +1,38 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.TransactionSender = void 0;
4
+ exports.serializeTransactionBase64 = serializeTransactionBase64;
5
+ const web3_js_1 = require("@solana/web3.js");
4
6
  const helius_1 = require("./helius");
5
7
  const jito_1 = require("./jito");
8
+ const common_1 = require("../../common");
6
9
  class TransactionSender {
7
10
  constructor() {
8
11
  }
9
- async sendTransaction(singedTransaction, swqos_only) {
10
- return await (0, helius_1.sendTxWithHelius)(singedTransaction, swqos_only);
12
+ async sendTransaction(singedTransaction, swqos_only = true) {
13
+ const singedTxBase64 = serializeTransactionBase64(singedTransaction);
14
+ return await (0, helius_1.sendTxWithHelius)(singedTxBase64, swqos_only);
11
15
  }
12
16
  async sendBundle(mainTx, tipTx, use_multi_ips = false) {
17
+ const mainTxBase64 = serializeTransactionBase64(mainTx);
18
+ const tipTxBase64 = serializeTransactionBase64(tipTx);
13
19
  if (use_multi_ips) {
14
- return await (0, jito_1.sendBundleWithJitoMultiIps)(mainTx, tipTx);
20
+ const mainTxHash = (0, common_1.getSignature)(mainTx);
21
+ return await (0, jito_1.sendBundleWithJitoMultiIps)(mainTxHash, mainTxBase64, tipTxBase64);
15
22
  }
16
23
  else {
17
- return await (0, jito_1.sendBundleWithJito)(mainTx, tipTx);
24
+ return await (0, jito_1.sendBundleWithJito)(mainTxBase64, tipTxBase64);
18
25
  }
19
26
  }
20
27
  }
21
28
  exports.TransactionSender = TransactionSender;
29
+ function serializeTransactionBase64(tx) {
30
+ let buffer;
31
+ if (tx instanceof web3_js_1.VersionedTransaction) {
32
+ buffer = Buffer.from(tx.serialize());
33
+ }
34
+ else {
35
+ buffer = Buffer.from(tx.serialize({ verifySignatures: false }));
36
+ }
37
+ return buffer.toString('base64');
38
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@clonegod/ttd-sol-common",
3
- "version": "2.0.44",
3
+ "version": "2.0.46",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "types": "types/index.d.ts",
@@ -1,4 +1,3 @@
1
- import { Transaction } from "@solana/web3.js"
2
1
  import { getHttpClient } from "./http_client"
3
2
 
4
3
  export const HELIUS_TIP_ACCOUNTS = [
@@ -17,7 +16,7 @@ export const HELIUS_TIP_ACCOUNTS = [
17
16
  /**
18
17
  * Jito + SWQOS
19
18
  */
20
- export const sendTxWithHelius = async (signedTransaction: Transaction, swqos_only: boolean): Promise<string> => {
19
+ export const sendTxWithHelius = async (signedTransactionBase64: string, swqos_only: boolean): Promise<string> => {
21
20
  let url = process.env.HELIUS_SEND_TX_URL || 'http://tyo-sender.helius-rpc.com/fast'
22
21
 
23
22
  if (swqos_only) {
@@ -31,7 +30,7 @@ export const sendTxWithHelius = async (signedTransaction: Transaction, swqos_onl
31
30
  id: Date.now().toString(),
32
31
  method: 'sendTransaction',
33
32
  params: [
34
- Buffer.from(signedTransaction.serialize()).toString('base64'),
33
+ signedTransactionBase64,
35
34
  {
36
35
  encoding: 'base64',
37
36
  skipPreflight: true, // Required for Sender
@@ -1,7 +1,5 @@
1
1
  import { log_warn } from "@clonegod/ttd-core/dist";
2
- import { Transaction } from "@solana/web3.js";
3
2
  import axios, { AxiosError } from "axios";
4
- import { getSignature } from "../../common";
5
3
  import { getHttpClient } from "./http_client";
6
4
 
7
5
  export const JITO_TIP_ACCOUNTS = [
@@ -50,7 +48,7 @@ JitoUtils.init()
50
48
 
51
49
  // -----------------------------------------------------------
52
50
 
53
- export const sendBundleWithJito = async (mainTx: Transaction, tipTx: Transaction): Promise<string> => {
51
+ export const sendBundleWithJito = async (mainTxBase64: string, tipTxBase64: string): Promise<string> => {
54
52
  let url = process.env.JITO_SEND_BUNDLE_URL || 'https://tokyo.mainnet.block-engine.jito.wtf/api/v1/bundles'
55
53
 
56
54
  // 使用共享的 axios 实例,复用连接池
@@ -61,7 +59,7 @@ export const sendBundleWithJito = async (mainTx: Transaction, tipTx: Transaction
61
59
  id: 1,
62
60
  method: 'sendBundle',
63
61
  params: [
64
- [tipTx, mainTx].map(tx => Buffer.from(tx.serialize()).toString('base64')),
62
+ [tipTxBase64, mainTxBase64],
65
63
  {
66
64
  "encoding": "base64"
67
65
  }
@@ -94,15 +92,15 @@ export const sendBundleWithJito = async (mainTx: Transaction, tipTx: Transaction
94
92
  }
95
93
 
96
94
 
97
- export const sendBundleWithJitoMultiIps = async (mainTx: Transaction, tipTx: Transaction): Promise<string> => {
95
+ export const sendBundleWithJitoMultiIps = async (mainTxHash: string, mainTxBase64: string, tipTxBase64: string): Promise<string> => {
98
96
  let ips = (process.env.JITO_SEND_BUNDLE_SERVER_IPS || '127.0.0.1').split(',')
99
97
  let urls = ips.map(ip => `http://${ip}:10001/solana/send_tx`)
100
98
 
101
99
  const body = {
102
100
  trace_id: '',
103
- txid: getSignature(mainTx),
101
+ txid: mainTxHash,
104
102
  encoding: 'base64', // base64
105
- encoded_tx: [tipTx, mainTx].map(tx => Buffer.from(tx.serialize()).toString('base64')).join(','), // main tx
103
+ encoded_tx: [tipTxBase64, mainTxBase64].join(','), // main tx
106
104
  max_retry: 3
107
105
  }
108
106
 
@@ -1,6 +1,7 @@
1
- import { Transaction } from '@solana/web3.js';
1
+ import { Transaction, VersionedTransaction } from '@solana/web3.js';
2
2
  import { sendTxWithHelius } from './helius';
3
3
  import { sendBundleWithJito, sendBundleWithJitoMultiIps } from './jito';
4
+ import { getSignature } from '../../common';
4
5
 
5
6
 
6
7
  /**
@@ -11,17 +12,55 @@ export class TransactionSender {
11
12
  constructor() {
12
13
  }
13
14
 
14
- async sendTransaction(singedTransaction: Transaction, swqos_only:boolean): Promise<string> {
15
- return await sendTxWithHelius(singedTransaction, swqos_only)
15
+ /**
16
+ * 使用Helius Sender 发送交易
17
+ * - swqos_only=true, 需给 helius 发送至少5000lamports的通道使用费
18
+ * - swqos_only=false, 需给helius 发送至少0.0002SOL的小费,会同时向jito发送交易
19
+ *
20
+ * @param singedTransaction 签名后的交易
21
+ * @param swqos_only true:swqos ;false: swqos + jito
22
+ * @returns txhash
23
+ */
24
+ async sendTransaction(singedTransaction: Transaction | VersionedTransaction, swqos_only:boolean=true): Promise<string> {
25
+ const singedTxBase64 = serializeTransactionBase64(singedTransaction)
26
+ return await sendTxWithHelius(singedTxBase64, swqos_only)
16
27
  }
17
28
 
18
- async sendBundle(mainTx: Transaction, tipTx:Transaction, use_multi_ips: boolean=false): Promise<string> {
29
+ /**
30
+ * 使用Jito Bundle 发送交易
31
+ * - use_multi_ips=true, 使用多台服务器向jito提交交易(缓解jito的限流)
32
+ * - use_multi_ips=false, 本地直接向jito提交交易
33
+ *
34
+ * @param mainTx 主交易
35
+ * @param tipTx 小费交易
36
+ * @param use_multi_ips true:多节点;false: 单节点
37
+ * @returns bundleId
38
+ */
39
+ async sendBundle(mainTx: Transaction | VersionedTransaction, tipTx: Transaction | VersionedTransaction, use_multi_ips: boolean=false): Promise<string> {
40
+ const mainTxBase64 = serializeTransactionBase64(mainTx)
41
+ const tipTxBase64 = serializeTransactionBase64(tipTx)
19
42
  if(use_multi_ips) {
20
- return await sendBundleWithJitoMultiIps(mainTx, tipTx)
43
+ const mainTxHash = getSignature(mainTx)
44
+ return await sendBundleWithJitoMultiIps(mainTxHash, mainTxBase64, tipTxBase64)
21
45
  } else {
22
- return await sendBundleWithJito(mainTx, tipTx)
46
+ return await sendBundleWithJito(mainTxBase64, tipTxBase64)
23
47
  }
24
48
  }
25
49
 
26
50
  }
27
51
 
52
+
53
+ /**
54
+ * 序列化交易为 base64 字符串
55
+ */
56
+ export function serializeTransactionBase64(tx: Transaction | VersionedTransaction): string {
57
+ let buffer:Buffer
58
+ if (tx instanceof VersionedTransaction) {
59
+ buffer = Buffer.from(tx.serialize())
60
+ } else {
61
+ // Transaction.serialize() 参数是可选的,但默认会验证签名
62
+ // 传入 { verifySignatures: false } 可以跳过验证,因为我们已经在发送前签名了
63
+ buffer = Buffer.from(tx.serialize({ verifySignatures: false }))
64
+ }
65
+ return buffer.toString('base64')
66
+ }