Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Added dry run feature for default tx executor and logs file #96

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ export class Bot {
`Send buy transaction attempt: ${i + 1}/${this.config.maxBuyRetries}`,
);
const tokenOut = new Token(TOKEN_PROGRAM_ID, poolKeys.baseMint, poolKeys.baseDecimals);
console.log("detected at ", new Date().toISOString());
const result = await this.swap(
poolKeys,
this.config.quoteAta,
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"bs58": "^5.0.0",
"dotenv": "^16.4.1",
"ed25519-hd-key": "^1.3.0",
"ffi-rs": "^1.0.69",
"i": "^0.3.7",
"npm": "^10.5.2",
"pino": "^8.18.0",
Expand Down
56 changes: 56 additions & 0 deletions transactions/tpu-transaction-executor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import {
BlockhashWithExpiryBlockHeight,
Keypair,
VersionedTransaction
} from '@solana/web3.js';
import { AxiosError } from 'axios';

import { TransactionExecutor } from './transaction-executor.interface';
import { load, DataType, open, close } from 'ffi-rs';
import bs58 from 'bs58';
import { logger } from '../helpers';

export class TpuTransactionExecutor implements TransactionExecutor {

constructor() { }

public async executeAndConfirm(
transaction: VersionedTransaction,
payer: Keypair,
latestBlockhash: BlockhashWithExpiryBlockHeight,
simulate: boolean
): Promise<{ confirmed: boolean; signature?: string }> {
const serializedTransaction = transaction.serialize();
const signatureBase58 = bs58.encode(transaction.signatures[0]);
let result = false
try {
open({
library: 'tpu_client', // key
path: "/Users/kasiopea/dev/rust/tpu-sol-test/target/aarch64-apple-darwin/release/libtpu_client.dylib" // path

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

???

Copy link
Author

@nikola43 nikola43 May 3, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've been researching how to call the TPU as people say transactions are faster, I've tried using
https://github.com/lmvdz/tpu-client but it doesn't work because solana migrated to quic, so the only way I've had to use it from TS is creating a mini library in rust and doing binding to ts using ffi-rs, this is the code
https://github.com/nikola43/tpu_client

So i serialize tx and send it to tpu_client for release it

I forgot add compiled library to project and leave my local path, my idea is build for mac, windows and linux for bot be compatible with any OS.

Refereces:
https://solana.stackexchange.com/questions/5971/tpu-client-sometimes-the-transaction-doesnt-go-through-0-5-sol-bounty

https://solana.stackexchange.com/questions/9502/optimal-transaction-execution-speed

https://solana.stackexchange.com/questions/10003/solana-tpu-client-and-sending-versioned-transactions

})
const RPC_ENDPOINT="http://127.0.0.1:8899"
const RPC_WEBSOCKET_ENDPOINT="ws://127.0.0.1:8900"


result = load({
library: "tpu_client", // path to the dynamic library file
funcName: 'send_tpu_tx', // the name of the function to call
retType: DataType.Boolean, // the return value type
paramsType: [DataType.String, DataType.String, DataType.U8Array, DataType.I32], // the parameter types
paramsValue: [RPC_ENDPOINT, RPC_WEBSOCKET_ENDPOINT, serializedTransaction, serializedTransaction.length] // the actual parameter values
})
console.log({
result
})
} catch (error) {
logger.error(error, "executeAndConfirm");
if (error instanceof AxiosError) {
logger.trace({ error: error.response?.data }, 'Failed to execute warp transaction');
}
} finally {
close('tpu_client')
}

return { confirmed: result, signature: signatureBase58 };
}
}