Skip to content

feat/er/add-limo-examples #496

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

Merged
merged 2 commits into from
Nov 7, 2024
Merged
Changes from all commits
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
113 changes: 106 additions & 7 deletions pages/express-relay/integrate-as-searcher/svm.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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.

<Tabs items={['Typescript', 'Python', 'HTTP', 'Websocket']}>
Expand Down Expand Up @@ -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).

<Callout type="warning" emoji="⚠️">
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.
</Callout>

The bid you construct will look like
See the following examples of how to construct a bid object via the SDKs:

<Tabs items={['Typescript', 'Python']}>
<Tabs.Tab>
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<BidSvm> {
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;
}
```

</Tabs.Tab>
<Tabs.Tab>
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

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
```

</Tabs.Tab>
</Tabs>

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"
}
```

Expand Down
Loading