|
| 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 { Keypair, PublicKey, Connection } from "@solana/web3.js"; |
| 9 | + |
| 10 | +import * as limo from "@kamino-finance/limo-sdk"; |
| 11 | +import { Decimal } from "decimal.js"; |
| 12 | +import { |
| 13 | + getPdaAuthority, |
| 14 | + OrderStateAndAddress, |
| 15 | +} from "@kamino-finance/limo-sdk/dist/utils"; |
| 16 | + |
| 17 | +const DAY_IN_SECONDS = 60 * 60 * 24; |
| 18 | + |
| 19 | +class SimpleSearcherLimo { |
| 20 | + private client: Client; |
| 21 | + private connectionSvm: Connection; |
| 22 | + private clientLimo: limo.LimoClient; |
| 23 | + private searcher: Keypair; |
| 24 | + constructor( |
| 25 | + public endpointExpressRelay: string, |
| 26 | + public chainId: string, |
| 27 | + privateKey: string, |
| 28 | + public endpointSvm: string, |
| 29 | + public globalConfig: PublicKey, |
| 30 | + public apiKey?: string |
| 31 | + ) { |
| 32 | + this.client = new Client( |
| 33 | + { |
| 34 | + baseUrl: endpointExpressRelay, |
| 35 | + apiKey, |
| 36 | + }, |
| 37 | + undefined, |
| 38 | + () => { |
| 39 | + return Promise.resolve(); |
| 40 | + }, |
| 41 | + this.bidStatusHandler.bind(this) |
| 42 | + ); |
| 43 | + this.connectionSvm = new Connection(endpointSvm, "confirmed"); |
| 44 | + this.clientLimo = new limo.LimoClient(this.connectionSvm, globalConfig); |
| 45 | + const secretKey = anchor.utils.bytes.bs58.decode(privateKey); |
| 46 | + this.searcher = Keypair.fromSecretKey(secretKey); |
| 47 | + } |
| 48 | + |
| 49 | + async bidStatusHandler(bidStatus: BidStatusUpdate) { |
| 50 | + let resultDetails = ""; |
| 51 | + if (bidStatus.type == "submitted" || bidStatus.type == "won") { |
| 52 | + resultDetails = `, transaction ${bidStatus.result}`; |
| 53 | + } else if (bidStatus.type == "lost") { |
| 54 | + if (bidStatus.result) { |
| 55 | + resultDetails = `, transaction ${bidStatus.result}`; |
| 56 | + } |
| 57 | + } |
| 58 | + console.log( |
| 59 | + `Bid status for bid ${bidStatus.id}: ${bidStatus.type}${resultDetails}` |
| 60 | + ); |
| 61 | + } |
| 62 | + |
| 63 | + async evaluateOrder(order: OrderStateAndAddress) { |
| 64 | + const inputMintDecimals = await this.clientLimo.getOrderInputMintDecimals( |
| 65 | + order |
| 66 | + ); |
| 67 | + const outputMintDecimals = await this.clientLimo.getOrderOutputMintDecimals( |
| 68 | + order |
| 69 | + ); |
| 70 | + const inputAmount = new Decimal( |
| 71 | + order.state.remainingInputAmount.toNumber() |
| 72 | + ).div(new Decimal(10).pow(inputMintDecimals)); |
| 73 | + |
| 74 | + const outputAmount = new Decimal( |
| 75 | + order.state.expectedOutputAmount.toNumber() |
| 76 | + ).div(new Decimal(10).pow(outputMintDecimals)); |
| 77 | + |
| 78 | + console.log("Order address", order.address.toBase58()); |
| 79 | + console.log( |
| 80 | + "Sell token", |
| 81 | + order.state.inputMint.toBase58(), |
| 82 | + "amount:", |
| 83 | + inputAmount.toString() |
| 84 | + ); |
| 85 | + console.log( |
| 86 | + "Buy token", |
| 87 | + order.state.outputMint.toBase58(), |
| 88 | + "amount:", |
| 89 | + outputAmount.toString() |
| 90 | + ); |
| 91 | + |
| 92 | + const ixsTakeOrder = await this.clientLimo.takeOrderIx( |
| 93 | + this.searcher.publicKey, |
| 94 | + order, |
| 95 | + inputAmount, |
| 96 | + SVM_CONSTANTS[this.chainId].expressRelayProgram, |
| 97 | + inputMintDecimals, |
| 98 | + outputMintDecimals |
| 99 | + ); |
| 100 | + const txRaw = new anchor.web3.Transaction().add(...ixsTakeOrder); |
| 101 | + |
| 102 | + const router = getPdaAuthority( |
| 103 | + this.clientLimo.getProgramID(), |
| 104 | + this.globalConfig |
| 105 | + ); |
| 106 | + const bidAmount = new anchor.BN(argv.bid); |
| 107 | + |
| 108 | + const bid = await this.client.constructSvmBid( |
| 109 | + txRaw, |
| 110 | + this.searcher.publicKey, |
| 111 | + router, |
| 112 | + order.address, |
| 113 | + bidAmount, |
| 114 | + new anchor.BN(Math.round(Date.now() / 1000 + DAY_IN_SECONDS)), |
| 115 | + this.chainId |
| 116 | + ); |
| 117 | + |
| 118 | + try { |
| 119 | + const { blockhash } = await this.connectionSvm.getLatestBlockhash(); |
| 120 | + bid.transaction.recentBlockhash = blockhash; |
| 121 | + bid.transaction.sign(this.searcher); |
| 122 | + const bidId = await this.client.submitBid(bid); |
| 123 | + console.log(`Successful bid. Bid id ${bidId}`); |
| 124 | + } catch (error) { |
| 125 | + console.error(`Failed to bid: ${error}`); |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + async bidOnNewOrders() { |
| 130 | + let allOrders = |
| 131 | + await this.clientLimo.getAllOrdersStateAndAddressWithFilters([]); |
| 132 | + allOrders = allOrders.filter( |
| 133 | + (order) => !order.state.remainingInputAmount.isZero() |
| 134 | + ); |
| 135 | + if (allOrders.length === 0) { |
| 136 | + console.log("No orders to bid on"); |
| 137 | + return; |
| 138 | + } |
| 139 | + for (const order of allOrders) { |
| 140 | + await this.evaluateOrder(order); |
| 141 | + } |
| 142 | + // Note: You need to parallelize this in production with something like: |
| 143 | + // await Promise.all(allOrders.map((order) => this.evaluateOrder(order))); |
| 144 | + } |
| 145 | + |
| 146 | + async start() { |
| 147 | + for (;;) { |
| 148 | + await this.bidOnNewOrders(); |
| 149 | + await new Promise((resolve) => setTimeout(resolve, 2000)); |
| 150 | + } |
| 151 | + } |
| 152 | +} |
| 153 | + |
| 154 | +const argv = yargs(hideBin(process.argv)) |
| 155 | + .option("endpoint-express-relay", { |
| 156 | + description: |
| 157 | + "Express relay endpoint. e.g: https://per-staging.dourolabs.app/", |
| 158 | + type: "string", |
| 159 | + demandOption: true, |
| 160 | + }) |
| 161 | + .option("chain-id", { |
| 162 | + description: "Chain id to bid on Limo opportunities for. e.g: solana", |
| 163 | + type: "string", |
| 164 | + demandOption: true, |
| 165 | + }) |
| 166 | + .option("global-config", { |
| 167 | + description: "Global config address", |
| 168 | + type: "string", |
| 169 | + demandOption: true, |
| 170 | + }) |
| 171 | + .option("bid", { |
| 172 | + description: "Bid amount in lamports", |
| 173 | + type: "string", |
| 174 | + default: "100", |
| 175 | + }) |
| 176 | + .option("private-key", { |
| 177 | + description: "Private key to sign the bid with. In 64-byte base58 format", |
| 178 | + type: "string", |
| 179 | + demandOption: true, |
| 180 | + }) |
| 181 | + .option("api-key", { |
| 182 | + description: |
| 183 | + "The API key of the searcher to authenticate with the server for fetching and submitting bids", |
| 184 | + type: "string", |
| 185 | + demandOption: false, |
| 186 | + }) |
| 187 | + .option("endpoint-svm", { |
| 188 | + description: "SVM RPC endpoint", |
| 189 | + type: "string", |
| 190 | + demandOption: true, |
| 191 | + }) |
| 192 | + .help() |
| 193 | + .alias("help", "h") |
| 194 | + .parseSync(); |
| 195 | +async function run() { |
| 196 | + if (!SVM_CONSTANTS[argv.chainId]) { |
| 197 | + throw new Error(`SVM constants not found for chain ${argv.chainId}`); |
| 198 | + } |
| 199 | + const searcherSvm = Keypair.fromSecretKey( |
| 200 | + anchor.utils.bytes.bs58.decode(argv.privateKey) |
| 201 | + ); |
| 202 | + console.log(`Using searcher pubkey: ${searcherSvm.publicKey.toBase58()}`); |
| 203 | + |
| 204 | + const simpleSearcher = new SimpleSearcherLimo( |
| 205 | + argv.endpointExpressRelay, |
| 206 | + argv.chainId, |
| 207 | + argv.privateKey, |
| 208 | + argv.endpointSvm, |
| 209 | + new PublicKey(argv.globalConfig), |
| 210 | + argv.apiKey |
| 211 | + ); |
| 212 | + await simpleSearcher.start(); |
| 213 | +} |
| 214 | + |
| 215 | +run(); |
0 commit comments