Skip to content

Commit cb030d4

Browse files
authored
feat/er/add-limo-examples (#496)
* add code examples to construct svm bids * update permanent links
1 parent 01cb3b1 commit cb030d4

File tree

1 file changed

+106
-7
lines changed
  • pages/express-relay/integrate-as-searcher

1 file changed

+106
-7
lines changed

pages/express-relay/integrate-as-searcher/svm.mdx

Lines changed: 106 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ SVM Express Relay searchers fulfill opportunities representing limit orders on t
88

99
### Subscribe to New Opportunities
1010

11-
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.
11+
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.
1212
Searchers can also directly fetch available opportunities via HTTP or subscribe to them via WebSocket.
1313

1414
<Tabs items={['Typescript', 'Python', 'HTTP', 'Websocket']}>
@@ -100,20 +100,119 @@ The server responds with opportunities in the following format:
100100
### Construct the Bid
101101

102102
Searchers should construct a bid by evaluating the fetched opportunity.
103-
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).
104103

105104
<Callout type="warning" emoji="⚠️">
106-
Before constructing the bid, make sure your wallet has the required assets.
105+
Before constructing the bid, make sure your wallet has the required assets to
106+
fulfill the limit order and SOL to pay the bid amount.
107107
</Callout>
108108

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

111+
<Tabs items={['Typescript', 'Python']}>
112+
<Tabs.Tab>
113+
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).
114+
115+
```typescript copy
116+
import { OpportunitySvm } from "../index";
117+
import { BidSvm } from "../types";
118+
119+
import * as anchor from "@coral-xyz/anchor";
120+
import * as limo from "@kamino-finance/limo-sdk";
121+
122+
/**
123+
* Generates a bid for a given opportunity. The transaction in this bid transfers assets from the searcher's wallet to fulfill the limit order.
124+
* @param opportunity The SVM opportunity to bid on
125+
* @returns The generated bid object
126+
*/
127+
async generateBid(opportunity: OpportunitySvm): Promise<BidSvm> {
128+
const order = opportunity.order;
129+
const limoClient = new limo.LimoClient(
130+
this.connectionSvm,
131+
order.state.globalConfig
132+
);
133+
134+
const ixsTakeOrder = await this.generateTakeOrderIxs(limoClient, order);
135+
const txRaw = new anchor.web3.Transaction().add(...ixsTakeOrder);
136+
137+
const bidData = await this.getBidData(limoClient, order);
138+
139+
const bid = await this.client.constructSvmBid(
140+
txRaw,
141+
this.searcher.publicKey,
142+
bidData.router,
143+
order.address,
144+
bidData.bidAmount,
145+
new anchor.BN(Math.round(Date.now() / 1000 + DAY_IN_SECONDS)),
146+
this.chainId,
147+
bidData.relayerSigner,
148+
bidData.relayerFeeReceiver
149+
);
150+
151+
bid.transaction.recentBlockhash = this.recentBlockhash[this.chainId];
152+
bid.transaction.sign(this.searcher);
153+
return bid;
154+
}
111155
```
156+
157+
</Tabs.Tab>
158+
<Tabs.Tab>
159+
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).
160+
161+
```python copy
162+
import logging
163+
164+
from solders.transaction import Transaction
165+
166+
from express_relay.models.svm import BidSvm
167+
from express_relay.svm.limo_client import OrderStateAndAddress
168+
169+
DEADLINE = 2**62
170+
logger = logging.getLogger(__name__)
171+
172+
async def assess_opportunity(self, opp: OpportunitySvm) -> BidSvm | None:
173+
"""
174+
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.
175+
176+
Args:
177+
opp: An object representing a single opportunity.
178+
Returns:
179+
A bid object if the opportunity is worth taking to be submitted to the Express Relay server, otherwise None.
180+
"""
181+
order: OrderStateAndAddress = {"address": opp.order_address, "state": opp.order}
182+
ixs_take_order = await self.generate_take_order_ixs(order)
183+
bid_data = await self.get_bid_data(order)
184+
185+
submit_bid_ix = self.client.get_svm_submit_bid_instruction(
186+
searcher=self.private_key.pubkey(),
187+
router=bid_data.router,
188+
permission_key=order["address"],
189+
bid_amount=bid_data.bid_amount,
190+
deadline=DEADLINE,
191+
chain_id=self.chain_id,
192+
fee_receiver_relayer=bid_data.relayer_fee_receiver,
193+
relayer_signer=bid_data.relayer_signer,
194+
)
195+
transaction = Transaction.new_with_payer(
196+
[submit_bid_ix] + ixs_take_order, self.private_key.pubkey()
197+
)
198+
transaction.partial_sign(
199+
[self.private_key], recent_blockhash=self.recent_blockhash[self.chain_id]
200+
)
201+
bid = BidSvm(transaction=transaction, chain_id=self.chain_id)
202+
return bid
203+
```
204+
205+
</Tabs.Tab>
206+
</Tabs>
207+
208+
The bid you construct will look like
209+
210+
```json
112211
{
113212
// serialized transaction object, in base-64 encoding
114-
transaction: "SGVsbG8sIFdvcmxkIQ==",
115-
chain_id: "solana",
116-
env: "svm"
213+
"transaction": "SGVsbG8sIFdvcmxkIQ==",
214+
"chain_id": "solana",
215+
"env": "svm"
117216
}
118217
```
119218

0 commit comments

Comments
 (0)