|
| 1 | +import yargs from "yargs"; |
| 2 | +import { hideBin } from "yargs/helpers"; |
| 3 | +import { Client } from "../index"; |
| 4 | +import { BidStatusUpdate } from "../types"; |
| 5 | +import { SVM_CONSTANTS } from "../const"; |
| 6 | + |
| 7 | +import * as anchor from "@coral-xyz/anchor"; |
| 8 | +import { Program, AnchorProvider } from "@coral-xyz/anchor"; |
| 9 | +import { Keypair, PublicKey, Connection } from "@solana/web3.js"; |
| 10 | +import dummyIdl from "./idl/idlDummy.json"; |
| 11 | +import { Dummy } from "./dummyTypes"; |
| 12 | + |
| 13 | +const DAY_IN_SECONDS = 60 * 60 * 24; |
| 14 | +const DUMMY_PIDS: Record<string, PublicKey> = { |
| 15 | + solana: new PublicKey("HYCgALnu6CM2gkQVopa1HGaNf8Vzbs9bomWRiKP267P3"), |
| 16 | +}; |
| 17 | + |
| 18 | +class SimpleSearcherSvm { |
| 19 | + private client: Client; |
| 20 | + private connectionSvm: Connection; |
| 21 | + constructor( |
| 22 | + public endpointExpressRelay: string, |
| 23 | + public chainId: string, |
| 24 | + public privateKey: string, |
| 25 | + public endpointSvm: string, |
| 26 | + public apiKey?: string |
| 27 | + ) { |
| 28 | + this.client = new Client( |
| 29 | + { |
| 30 | + baseUrl: endpointExpressRelay, |
| 31 | + apiKey, |
| 32 | + }, |
| 33 | + undefined, |
| 34 | + () => { |
| 35 | + return Promise.resolve(); |
| 36 | + }, |
| 37 | + this.bidStatusHandler.bind(this) |
| 38 | + ); |
| 39 | + this.connectionSvm = new Connection(endpointSvm, "confirmed"); |
| 40 | + } |
| 41 | + |
| 42 | + async bidStatusHandler(bidStatus: BidStatusUpdate) { |
| 43 | + let resultDetails = ""; |
| 44 | + if (bidStatus.type == "submitted" || bidStatus.type == "won") { |
| 45 | + resultDetails = `, transaction ${bidStatus.result}`; |
| 46 | + } else if (bidStatus.type == "lost") { |
| 47 | + if (bidStatus.result) { |
| 48 | + resultDetails = `, transaction ${bidStatus.result}`; |
| 49 | + } |
| 50 | + } |
| 51 | + console.log( |
| 52 | + `Bid status for bid ${bidStatus.id}: ${bidStatus.type.replaceAll( |
| 53 | + "_", |
| 54 | + " " |
| 55 | + )}${resultDetails}` |
| 56 | + ); |
| 57 | + } |
| 58 | + |
| 59 | + async dummyBid() { |
| 60 | + const secretKey = anchor.utils.bytes.bs58.decode(this.privateKey); |
| 61 | + const searcher = Keypair.fromSecretKey(secretKey); |
| 62 | + |
| 63 | + const provider = new AnchorProvider( |
| 64 | + this.connectionSvm, |
| 65 | + new anchor.Wallet(searcher), |
| 66 | + {} |
| 67 | + ); |
| 68 | + const dummy = new Program<Dummy>(dummyIdl as Dummy, provider); |
| 69 | + |
| 70 | + const permission = PublicKey.default; |
| 71 | + const router = Keypair.generate().publicKey; |
| 72 | + const bidAmount = new anchor.BN(argv.bid); |
| 73 | + |
| 74 | + const svmConstants = SVM_CONSTANTS[this.chainId]; |
| 75 | + if (!(this.chainId in DUMMY_PIDS)) { |
| 76 | + throw new Error(`Dummy program id not found for chain ${this.chainId}`); |
| 77 | + } |
| 78 | + const dummyPid = DUMMY_PIDS[this.chainId]; |
| 79 | + |
| 80 | + const ixDummy = await dummy.methods |
| 81 | + .doNothing() |
| 82 | + .accountsStrict({ |
| 83 | + payer: searcher.publicKey, |
| 84 | + expressRelay: svmConstants.expressRelayProgram, |
| 85 | + sysvarInstructions: anchor.web3.SYSVAR_INSTRUCTIONS_PUBKEY, |
| 86 | + permission, |
| 87 | + router, |
| 88 | + }) |
| 89 | + .instruction(); |
| 90 | + ixDummy.programId = dummyPid; |
| 91 | + |
| 92 | + const txRaw = new anchor.web3.Transaction().add(ixDummy); |
| 93 | + |
| 94 | + const bid = await this.client.constructSvmBid( |
| 95 | + txRaw, |
| 96 | + searcher.publicKey, |
| 97 | + router, |
| 98 | + permission, |
| 99 | + bidAmount, |
| 100 | + new anchor.BN(Math.round(Date.now() / 1000 + DAY_IN_SECONDS)), |
| 101 | + this.chainId |
| 102 | + ); |
| 103 | + |
| 104 | + try { |
| 105 | + const { blockhash } = await this.connectionSvm.getLatestBlockhash(); |
| 106 | + bid.transaction.recentBlockhash = blockhash; |
| 107 | + bid.transaction.sign(Keypair.fromSecretKey(secretKey)); |
| 108 | + const bidId = await this.client.submitBid(bid); |
| 109 | + console.log(`Successful bid. Bid id ${bidId}`); |
| 110 | + } catch (error) { |
| 111 | + console.error(`Failed to bid: ${error}`); |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + async start() { |
| 116 | + for (;;) { |
| 117 | + await this.dummyBid(); |
| 118 | + await new Promise((resolve) => setTimeout(resolve, 2000)); |
| 119 | + } |
| 120 | + } |
| 121 | +} |
| 122 | + |
| 123 | +const argv = yargs(hideBin(process.argv)) |
| 124 | + .option("endpoint-express-relay", { |
| 125 | + description: |
| 126 | + "Express relay endpoint. e.g: https://per-staging.dourolabs.app/", |
| 127 | + type: "string", |
| 128 | + demandOption: true, |
| 129 | + }) |
| 130 | + .option("chain-id", { |
| 131 | + description: "Chain id to fetch opportunities for. e.g: solana", |
| 132 | + type: "string", |
| 133 | + demandOption: true, |
| 134 | + }) |
| 135 | + .option("bid", { |
| 136 | + description: "Bid amount in lamports", |
| 137 | + type: "string", |
| 138 | + default: "100", |
| 139 | + }) |
| 140 | + .option("private-key", { |
| 141 | + description: "Private key to sign the bid with. In 64-byte base58 format", |
| 142 | + type: "string", |
| 143 | + demandOption: true, |
| 144 | + }) |
| 145 | + .option("api-key", { |
| 146 | + description: |
| 147 | + "The API key of the searcher to authenticate with the server for fetching and submitting bids", |
| 148 | + type: "string", |
| 149 | + demandOption: false, |
| 150 | + }) |
| 151 | + .option("endpoint-svm", { |
| 152 | + description: "SVM RPC endpoint", |
| 153 | + type: "string", |
| 154 | + demandOption: true, |
| 155 | + }) |
| 156 | + .help() |
| 157 | + .alias("help", "h") |
| 158 | + .parseSync(); |
| 159 | +async function run() { |
| 160 | + if (SVM_CONSTANTS[argv.chainId] === undefined) { |
| 161 | + throw new Error(`SVM constants not found for chain ${argv.chainId}`); |
| 162 | + } |
| 163 | + const searcherSvm = Keypair.fromSecretKey( |
| 164 | + anchor.utils.bytes.bs58.decode(argv.privateKey) |
| 165 | + ); |
| 166 | + console.log(`Using searcher pubkey: ${searcherSvm.publicKey.toBase58()}`); |
| 167 | + |
| 168 | + const simpleSearcher = new SimpleSearcherSvm( |
| 169 | + argv.endpointExpressRelay, |
| 170 | + argv.chainId, |
| 171 | + argv.privateKey, |
| 172 | + argv.endpointSvm, |
| 173 | + argv.apiKey |
| 174 | + ); |
| 175 | + await simpleSearcher.start(); |
| 176 | +} |
| 177 | + |
| 178 | +run(); |
0 commit comments