@clonegod/ttd-sol-common 2.0.41 → 2.0.43
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/dist/config/SolanaTradeAppConfig.d.ts +1 -0
- package/dist/config/SolanaTradeAppConfig.js +19 -3
- package/dist/trade/tx_builder.js +15 -12
- package/dist/trade/tx_result_check.js +1 -1
- package/package.json +1 -1
- package/src/config/SolanaTradeAppConfig.ts +25 -3
- package/src/trade/tx_builder.ts +23 -17
- package/src/trade/tx_result_check.ts +2 -2
- package/src/types/index.ts +1 -0
|
@@ -3,6 +3,7 @@ import { Connection, Keypair } from "@solana/web3.js";
|
|
|
3
3
|
export declare class SolanaTradeAppConfig extends AbstractTradeAppConfig {
|
|
4
4
|
connection: Connection;
|
|
5
5
|
keypair: Keypair;
|
|
6
|
+
private ws_client;
|
|
6
7
|
constructor();
|
|
7
8
|
init(): Promise<void>;
|
|
8
9
|
subscribe_wallet_raw_txn_event(): void;
|
|
@@ -5,6 +5,7 @@ const dist_1 = require("@clonegod/ttd-core/dist");
|
|
|
5
5
|
class SolanaTradeAppConfig extends dist_1.AbstractTradeAppConfig {
|
|
6
6
|
constructor() {
|
|
7
7
|
super();
|
|
8
|
+
this.ws_client = null;
|
|
8
9
|
}
|
|
9
10
|
async init() {
|
|
10
11
|
await super.init();
|
|
@@ -12,6 +13,19 @@ class SolanaTradeAppConfig extends dist_1.AbstractTradeAppConfig {
|
|
|
12
13
|
(0, dist_1.log_info)('SolanaTradeAppConfig init ...');
|
|
13
14
|
}
|
|
14
15
|
subscribe_wallet_raw_txn_event() {
|
|
16
|
+
if (this.is_already_subscribe_wallet_raw_txn) {
|
|
17
|
+
if (this.ws_client && this.ws_client.isConnected()) {
|
|
18
|
+
return;
|
|
19
|
+
}
|
|
20
|
+
else {
|
|
21
|
+
(0, dist_1.log_info)('subscribe_wallet_raw_txn_event was subscribed but connection lost, reconnecting...');
|
|
22
|
+
if (this.ws_client) {
|
|
23
|
+
this.ws_client.disconnect();
|
|
24
|
+
this.ws_client = null;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
this.is_already_subscribe_wallet_raw_txn = true;
|
|
15
29
|
const transactionHandler = async (messageStr) => {
|
|
16
30
|
let messageObj = messageStr;
|
|
17
31
|
if (typeof messageStr === 'string') {
|
|
@@ -22,9 +36,11 @@ class SolanaTradeAppConfig extends dist_1.AbstractTradeAppConfig {
|
|
|
22
36
|
this.emit(dist_1.LOCAL_EVENT_NAME.EVENT_WALLET_TRANSACTION + '#' + tx_id, messageObj);
|
|
23
37
|
};
|
|
24
38
|
const ws_port = process.env.STREAM_WS_TRADE_PORT || 10002;
|
|
25
|
-
const
|
|
26
|
-
|
|
27
|
-
|
|
39
|
+
const ws_url = `ws://127.0.0.1:${ws_port}`;
|
|
40
|
+
this.ws_client = new dist_1.WebSocketClient(ws_url);
|
|
41
|
+
this.ws_client.onMessage(transactionHandler);
|
|
42
|
+
this.ws_client.connect();
|
|
43
|
+
(0, dist_1.log_info)(`subscribe_wallet_raw_txn_event, WebSocket connecting to ${ws_url}`);
|
|
28
44
|
}
|
|
29
45
|
}
|
|
30
46
|
exports.SolanaTradeAppConfig = SolanaTradeAppConfig;
|
package/dist/trade/tx_builder.js
CHANGED
|
@@ -48,19 +48,22 @@ class SolTransactionBuilder {
|
|
|
48
48
|
swapTx.recentBlockhash = this.recentBlockhash;
|
|
49
49
|
swapTx.lastValidBlockHeight = this.recentBlockheight + max_block_offset;
|
|
50
50
|
swapTx.feePayer = this.keypair.publicKey;
|
|
51
|
-
|
|
52
|
-
if (
|
|
53
|
-
tip_lamports =
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
51
|
+
const { sol_bundle_only, sol_tip_fee } = context.trade_runtime.settings.strategy;
|
|
52
|
+
if (!sol_bundle_only) {
|
|
53
|
+
let tip_lamports = sol_tip_fee;
|
|
54
|
+
if (tip_lamports < 5000) {
|
|
55
|
+
tip_lamports = 5000;
|
|
56
|
+
}
|
|
57
|
+
if (tip_lamports > 300000) {
|
|
58
|
+
tip_lamports = 300000;
|
|
59
|
+
}
|
|
60
|
+
let tip_instruction = web3_js_1.SystemProgram.transfer({
|
|
61
|
+
fromPubkey: this.keypair.publicKey,
|
|
62
|
+
toPubkey: new web3_js_1.PublicKey(helius_1.HELIUS_TIP_ACCOUNTS[Math.floor(Math.random() * helius_1.HELIUS_TIP_ACCOUNTS.length)]),
|
|
63
|
+
lamports: tip_lamports,
|
|
64
|
+
});
|
|
65
|
+
swapTx.instructions.push(tip_instruction);
|
|
57
66
|
}
|
|
58
|
-
let tip_instruction = web3_js_1.SystemProgram.transfer({
|
|
59
|
-
fromPubkey: this.keypair.publicKey,
|
|
60
|
-
toPubkey: new web3_js_1.PublicKey(helius_1.HELIUS_TIP_ACCOUNTS[Math.floor(Math.random() * helius_1.HELIUS_TIP_ACCOUNTS.length)]),
|
|
61
|
-
lamports: tip_lamports,
|
|
62
|
-
});
|
|
63
|
-
swapTx.instructions.push(tip_instruction);
|
|
64
67
|
swapTx.sign(this.keypair);
|
|
65
68
|
return swapTx;
|
|
66
69
|
}
|
|
@@ -3,8 +3,8 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.TransactionResultChecker = void 0;
|
|
4
4
|
const dist_1 = require("@clonegod/ttd-core/dist");
|
|
5
5
|
const web3_js_1 = require("@solana/web3.js");
|
|
6
|
-
const tx_result_parse_1 = require("./tx_result_parse");
|
|
7
6
|
const common_1 = require("../common");
|
|
7
|
+
const tx_result_parse_1 = require("./tx_result_parse");
|
|
8
8
|
class TransactionResultChecker extends dist_1.AbstractTransactionResultCheck {
|
|
9
9
|
constructor(env_args, event_emitter) {
|
|
10
10
|
super(env_args, event_emitter);
|
package/package.json
CHANGED
|
@@ -7,6 +7,9 @@ export class SolanaTradeAppConfig extends AbstractTradeAppConfig {
|
|
|
7
7
|
public connection: Connection;
|
|
8
8
|
public keypair: Keypair;
|
|
9
9
|
|
|
10
|
+
// WebSocket 客户端实例,用于订阅交易结果
|
|
11
|
+
private ws_client: WebSocketClient | null = null;
|
|
12
|
+
|
|
10
13
|
constructor() {
|
|
11
14
|
super();
|
|
12
15
|
}
|
|
@@ -19,6 +22,22 @@ export class SolanaTradeAppConfig extends AbstractTradeAppConfig {
|
|
|
19
22
|
|
|
20
23
|
// 订阅 Wallet 在链上发生的tx data
|
|
21
24
|
subscribe_wallet_raw_txn_event(): void {
|
|
25
|
+
// 检查是否已经订阅
|
|
26
|
+
if(this.is_already_subscribe_wallet_raw_txn) {
|
|
27
|
+
// 如果已订阅,检查连接状态
|
|
28
|
+
if(this.ws_client && this.ws_client.isConnected()) {
|
|
29
|
+
return;
|
|
30
|
+
} else {
|
|
31
|
+
// 连接已断开,需要重新连接
|
|
32
|
+
log_info('subscribe_wallet_raw_txn_event was subscribed but connection lost, reconnecting...');
|
|
33
|
+
if(this.ws_client) {
|
|
34
|
+
this.ws_client.disconnect();
|
|
35
|
+
this.ws_client = null;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
this.is_already_subscribe_wallet_raw_txn = true;
|
|
40
|
+
|
|
22
41
|
// 定义处理函数,便于后续移除
|
|
23
42
|
const transactionHandler = async (messageStr: any) => {
|
|
24
43
|
let messageObj = messageStr
|
|
@@ -34,9 +53,12 @@ export class SolanaTradeAppConfig extends AbstractTradeAppConfig {
|
|
|
34
53
|
};
|
|
35
54
|
|
|
36
55
|
const ws_port = process.env.STREAM_WS_TRADE_PORT || 10002;
|
|
37
|
-
const
|
|
38
|
-
|
|
39
|
-
|
|
56
|
+
const ws_url = `ws://127.0.0.1:${ws_port}`;
|
|
57
|
+
this.ws_client = new WebSocketClient(ws_url)
|
|
58
|
+
this.ws_client.onMessage(transactionHandler);
|
|
59
|
+
this.ws_client.connect();
|
|
60
|
+
|
|
61
|
+
log_info(`subscribe_wallet_raw_txn_event, WebSocket connecting to ${ws_url}`);
|
|
40
62
|
|
|
41
63
|
// 不断开连接
|
|
42
64
|
// setTimeout(() => { ws.disconnect(); }, 6000);
|
package/src/trade/tx_builder.ts
CHANGED
|
@@ -87,24 +87,30 @@ export class SolTransactionBuilder {
|
|
|
87
87
|
swapTx.lastValidBlockHeight = this.recentBlockheight + max_block_offset
|
|
88
88
|
swapTx.feePayer = this.keypair.publicKey
|
|
89
89
|
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
//
|
|
93
|
-
//
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
tip_lamports =
|
|
90
|
+
const {sol_bundle_only, sol_tip_fee} = context.trade_runtime.settings.strategy
|
|
91
|
+
|
|
92
|
+
// sol_bundle_only = false, 使用 Helius Sender
|
|
93
|
+
// sol_bundle_only = true, 使用 Jito Bundle - 不需要给Helius小费
|
|
94
|
+
if(!sol_bundle_only) {
|
|
95
|
+
// 使用 Helius Sender:
|
|
96
|
+
// 1、swqos_only: 至少 5000 lamports 小费
|
|
97
|
+
// -> transaction must send a tip of at least 5000 lamports to one of the following Helius wallets: [xxx, ...]
|
|
98
|
+
// 2、swqos + jito: 至少 0.0002 SOL 小费 -> lamports = 0.0002 * LAMPORTS_PER_SOL
|
|
99
|
+
// -> Requires minimum 0.0002 SOL tip.
|
|
100
|
+
let tip_lamports = sol_tip_fee
|
|
101
|
+
if(tip_lamports < 5000) {
|
|
102
|
+
tip_lamports = 5000
|
|
103
|
+
}
|
|
104
|
+
if(tip_lamports > 300000) {
|
|
105
|
+
tip_lamports = 300000
|
|
106
|
+
}
|
|
107
|
+
let tip_instruction = SystemProgram.transfer({
|
|
108
|
+
fromPubkey: this.keypair.publicKey,
|
|
109
|
+
toPubkey: new PublicKey(HELIUS_TIP_ACCOUNTS[Math.floor(Math.random() * HELIUS_TIP_ACCOUNTS.length)]),
|
|
110
|
+
lamports: tip_lamports,
|
|
111
|
+
})
|
|
112
|
+
swapTx.instructions.push(tip_instruction)
|
|
101
113
|
}
|
|
102
|
-
let tip_instruction = SystemProgram.transfer({
|
|
103
|
-
fromPubkey: this.keypair.publicKey,
|
|
104
|
-
toPubkey: new PublicKey(HELIUS_TIP_ACCOUNTS[Math.floor(Math.random() * HELIUS_TIP_ACCOUNTS.length)]),
|
|
105
|
-
lamports: tip_lamports,
|
|
106
|
-
})
|
|
107
|
-
swapTx.instructions.push(tip_instruction)
|
|
108
114
|
|
|
109
115
|
swapTx.sign(this.keypair)
|
|
110
116
|
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import { OrderMessageType, PriceMessageType, StandardPoolInfoType, StandardSwapDetailType, TradeBroadcastType, TradeResultBalanceChangeType, TradeResultType, TradeTimeFlowType } from "@clonegod/ttd-core"
|
|
2
|
-
import { AbstractTransactionResultCheck, getServerInfo, isEmpty, LOCAL_EVENT_NAME,
|
|
2
|
+
import { AbstractTransactionResultCheck, getServerInfo, isEmpty, LOCAL_EVENT_NAME, log_error, log_info, log_warn, TradeContext, TRANSACTION_STATE_FAILED, TRANSACTION_STATE_PROCESSING, TRANSACTION_STATE_SUCCESS } from "@clonegod/ttd-core/dist"
|
|
3
3
|
import { Connection, ParsedTransactionWithMeta, PublicKey } from "@solana/web3.js"
|
|
4
4
|
import { EventEmitter } from 'events'
|
|
5
|
-
import { TransactionResultParser } from "./tx_result_parse"
|
|
6
5
|
import { COMMITMENT_LEVEL } from "../common"
|
|
6
|
+
import { TransactionResultParser } from "./tx_result_parse"
|
|
7
7
|
|
|
8
8
|
|
|
9
9
|
|