From eca5837a7a492fd98550b0500232f1b0e2660875 Mon Sep 17 00:00:00 2001 From: ani Date: Thu, 7 Nov 2024 22:43:59 +0000 Subject: [PATCH 1/2] add code examples to construct svm bids --- .../integrate-as-searcher/svm.mdx | 111 +++++++++++++++++- 1 file changed, 105 insertions(+), 6 deletions(-) diff --git a/pages/express-relay/integrate-as-searcher/svm.mdx b/pages/express-relay/integrate-as-searcher/svm.mdx index 6e3ea0b9..8ffbe091 100644 --- a/pages/express-relay/integrate-as-searcher/svm.mdx +++ b/pages/express-relay/integrate-as-searcher/svm.mdx @@ -100,20 +100,119 @@ The server responds with opportunities in the following format: ### Construct the Bid Searchers should construct a bid by evaluating the fetched opportunity. -The SDKs provide an easy way to construct a bid; refer to these basic examples in [Typescript](https://github.com/pyth-network/per/blob/0bd47e39d704c4b19b596512e4be05db4f715e58/sdk/js/src/examples/simpleSearcherLimo.ts#L75) and [Python](https://github.com/pyth-network/per/blob/0bd47e39d704c4b19b596512e4be05db4f715e58/sdk/python/express_relay/searcher/examples/simple_searcher_svm.py#L110). - Before constructing the bid, make sure your wallet has the required assets. + Before constructing the bid, make sure your wallet has the required assets to + fulfill the limit order and SOL to pay the bid amount. -The bid you construct will look like +See the following examples of how to construct a bid object via the SDKs: + + +Below is an excerpt of example code. See the full example in the [Typescript SDK](https://github.com/pyth-network/per/blob/4be711525948cf24c0ebd4ebab007dc7f51b7069/sdk/js/src/examples/simpleSearcherLimo.ts). + +```typescript copy +import { OpportunitySvm } from "../index"; +import { BidSvm } from "../types"; + +import * as anchor from "@coral-xyz/anchor"; +import * as limo from "@kamino-finance/limo-sdk"; + +/** + * Generates a bid for a given opportunity. The transaction in this bid transfers assets from the searcher's wallet to fulfill the limit order. + * @param opportunity The SVM opportunity to bid on + * @returns The generated bid object + */ +async generateBid(opportunity: OpportunitySvm): Promise { + const order = opportunity.order; + const limoClient = new limo.LimoClient( + this.connectionSvm, + order.state.globalConfig + ); + + const ixsTakeOrder = await this.generateTakeOrderIxs(limoClient, order); + const txRaw = new anchor.web3.Transaction().add(...ixsTakeOrder); + + const bidData = await this.getBidData(limoClient, order); + + const bid = await this.client.constructSvmBid( + txRaw, + this.searcher.publicKey, + bidData.router, + order.address, + bidData.bidAmount, + new anchor.BN(Math.round(Date.now() / 1000 + DAY_IN_SECONDS)), + this.chainId, + bidData.relayerSigner, + bidData.relayerFeeReceiver + ); + + bid.transaction.recentBlockhash = this.recentBlockhash[this.chainId]; + bid.transaction.sign(this.searcher); + return bid; +} ``` + + + +Below is an excerpt of example code. See the full example in the [Python SDK](https://github.com/pyth-network/per/blob/main/sdk/python/express_relay/searcher/examples/simple_searcher_svm.py). + +```python copy +import logging + +from solders.transaction import Transaction + +from express_relay.models.svm import BidSvm +from express_relay.svm.limo_client import OrderStateAndAddress + +DEADLINE = 2**62 +logger = logging.getLogger(__name__) + +async def assess_opportunity(self, opp: OpportunitySvm) -> BidSvm | None: + """ + Method to assess an opportunity and return a bid if the opportunity is worth taking. This method always returns a bid for any valid opportunity. The transaction in this bid transfers assets from the searcher's wallet to fulfill the limit order. + + Args: + opp: An object representing a single opportunity. + Returns: + A bid object if the opportunity is worth taking to be submitted to the Express Relay server, otherwise None. + """ + order: OrderStateAndAddress = {"address": opp.order_address, "state": opp.order} + ixs_take_order = await self.generate_take_order_ixs(order) + bid_data = await self.get_bid_data(order) + + submit_bid_ix = self.client.get_svm_submit_bid_instruction( + searcher=self.private_key.pubkey(), + router=bid_data.router, + permission_key=order["address"], + bid_amount=bid_data.bid_amount, + deadline=DEADLINE, + chain_id=self.chain_id, + fee_receiver_relayer=bid_data.relayer_fee_receiver, + relayer_signer=bid_data.relayer_signer, + ) + transaction = Transaction.new_with_payer( + [submit_bid_ix] + ixs_take_order, self.private_key.pubkey() + ) + transaction.partial_sign( + [self.private_key], recent_blockhash=self.recent_blockhash[self.chain_id] + ) + bid = BidSvm(transaction=transaction, chain_id=self.chain_id) + return bid +``` + + + + +The bid you construct will look like + +```json { // serialized transaction object, in base-64 encoding - transaction: "SGVsbG8sIFdvcmxkIQ==", - chain_id: "solana", - env: "svm" + "transaction": "SGVsbG8sIFdvcmxkIQ==", + "chain_id": "solana", + "env": "svm" } ``` From ddebaf1fc3d289daa7243f535ab75de9f83ed337 Mon Sep 17 00:00:00 2001 From: ani Date: Thu, 7 Nov 2024 23:05:46 +0000 Subject: [PATCH 2/2] update permanent links --- pages/express-relay/integrate-as-searcher/svm.mdx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pages/express-relay/integrate-as-searcher/svm.mdx b/pages/express-relay/integrate-as-searcher/svm.mdx index 8ffbe091..acd8eb48 100644 --- a/pages/express-relay/integrate-as-searcher/svm.mdx +++ b/pages/express-relay/integrate-as-searcher/svm.mdx @@ -8,7 +8,7 @@ SVM Express Relay searchers fulfill opportunities representing limit orders on t ### Subscribe to New Opportunities -Express Relay provides searchers with [Typescript](https://github.com/pyth-network/per/tree/8e311d3dce7a54865ff98b25e57c6af2dd984d1f/sdk/js) and [Python](https://github.com/pyth-network/per/tree/8e311d3dce7a54865ff98b25e57c6af2dd984d1f/sdk/python) SDKs to interact with Express Relay. +Express Relay provides searchers with [Typescript](https://github.com/pyth-network/per/tree/4be711525948cf24c0ebd4ebab007dc7f51b7069/sdk/js) and [Python](https://github.com/pyth-network/per/tree/4be711525948cf24c0ebd4ebab007dc7f51b7069/sdk/python) SDKs to interact with Express Relay. Searchers can also directly fetch available opportunities via HTTP or subscribe to them via WebSocket. @@ -156,7 +156,7 @@ async generateBid(opportunity: OpportunitySvm): Promise { -Below is an excerpt of example code. See the full example in the [Python SDK](https://github.com/pyth-network/per/blob/main/sdk/python/express_relay/searcher/examples/simple_searcher_svm.py). +Below is an excerpt of example code. See the full example in the [Python SDK](https://github.com/pyth-network/per/blob/4be711525948cf24c0ebd4ebab007dc7f51b7069/sdk/python/express_relay/searcher/examples/simple_searcher_svm.py). ```python copy import logging