|
1 |
| -# How to Integrate Express Relay as a Protocol |
| 1 | +import { Steps } from "nextra/components"; |
2 | 2 |
|
3 |
| -This section covers how to integrate Express Relay for your use case. Please see the relevant section below for the use case of interest. |
| 3 | +# How to Integrate Express Relay Swaps |
4 | 4 |
|
5 |
| -- [Swaps](integrate-as-protocol/swaps) |
| 5 | +This guide will explain how frontends can integrate Express Relay to empower swapping. |
| 6 | + |
| 7 | +<Steps> |
| 8 | + ### Install the Express Relay SDK |
| 9 | + |
| 10 | + Pyth provides a [Typescript SDK](https://www.npmjs.com/package/@pythnetwork/express-relay-js) to help developers integrate Express Relay into their frontends. |
| 11 | + |
| 12 | + You can install the SDK via npm or yarn. You can invoke the SDK client as below: |
| 13 | + |
| 14 | + ```typescript |
| 15 | + import { Client } from "@pythnetwork/express-relay-js"; |
| 16 | + |
| 17 | + const client = new Client( |
| 18 | + { baseUrl: "https://per-mainnet.dourolabs.app" }, |
| 19 | + undefined // Default WebSocket options |
| 20 | + ); |
| 21 | + ``` |
| 22 | + |
| 23 | + ### Request a Quote |
| 24 | + |
| 25 | + You can request a quote by calling the [`getQuote`](https://github.com/pyth-network/per/blob/281de989db887aaf568fed39315a76acc16548fa/sdk/js/src/index.ts#L501-L506) SDK method. |
| 26 | + |
| 27 | + The example below shows how you can construct a quote request for a USDC -> WSOL swap, with 100 USDC provided as input by the user: |
| 28 | + |
| 29 | + ```typescript |
| 30 | + const userWallet = new PublicKey("<INPUT USER PUBKEY>"); |
| 31 | + |
| 32 | + const quoteRequest = { |
| 33 | + chainId: "solana", |
| 34 | + inputTokenMint: new PublicKey("EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v"), // USDC mint |
| 35 | + outputTokenMint: new PublicKey("So11111111111111111111111111111111111111112"), // WSOL mint |
| 36 | + specifiedTokenAmount: { |
| 37 | + side: "input", |
| 38 | + amount: 100_000_000, |
| 39 | + }, |
| 40 | + userWallet, |
| 41 | + }; |
| 42 | + |
| 43 | + const quote = await client.getQuote(quoteRequest); |
| 44 | + ``` |
| 45 | + |
| 46 | + `quote` contains the full details, including the amount the searcher is quoting and the transaction that the user needs to sign. It also contains an `expirationTime`; after this time, the transaction will no longer succeed on chain, so you should request a new quote a few seconds before the `expirationTime`. |
| 47 | + |
| 48 | + ### Submit User Signature to the Express Relay Server |
| 49 | + |
| 50 | + Once you show the quote to the user, the user should sign the transaction if they wish to engage in the swap. The frontend can pass this signature along to the Express Relay server, which will handle aggregating all the required signatures for the transaction and submitting it to the RPC node. |
| 51 | + |
| 52 | + Below is an example showing how the frontend can submit the signed quote transaction to the server using the [`submitQuote`](https://github.com/pyth-network/per/blob/358eedc1f9072cdfc3418fba309697580f2474f9/sdk/js/src/index.ts#L537-L542) method. The response from the `getQuote` method includes a field called `referenceId`, which the frontend should use in its submission of the user signature. |
| 53 | + |
| 54 | + ```typescript |
| 55 | + const submitQuote = { |
| 56 | + chainId: "solana", |
| 57 | + referenceId: quote.referenceId, |
| 58 | + userSignature: signature, |
| 59 | + }; |
| 60 | + |
| 61 | + const txSubmitted = await client.submitQuote(submitQuote); |
| 62 | + ``` |
| 63 | + |
| 64 | + `submitQuote` returns the fully signed transaction that the server submitted to the RPC node. |
| 65 | + |
| 66 | +</Steps> |
| 67 | + |
| 68 | +## Additional Resources |
| 69 | + |
| 70 | +You may find these additional resources helpful for integrating Express Relay as a frontend. |
| 71 | + |
| 72 | +### Contract Addresses |
| 73 | + |
| 74 | +The [SVM](./contract-addresses.mdx) Contract Addresses page lists the relevant addresses for Express Relay integration. |
| 75 | + |
| 76 | +### Error Codes |
| 77 | + |
| 78 | +The [SVM](./errors.mdx) Error Codes page lists the error codes returned by Express Relay. |
| 79 | + |
| 80 | +### API Reference |
| 81 | + |
| 82 | +The [API Reference](https://per-mainnet.dourolabs.app/docs) provides detailed information on Express Relay APIs. |
0 commit comments