Skip to content

Commit

Permalink
feat: create quote response submit method, typescript (#312)
Browse files Browse the repository at this point in the history
* create quote response submit method, typescript

* ignore generated files in linting

* bump package version

* convert to date

* add todo
  • Loading branch information
anihamde authored Jan 8, 2025
1 parent eaea95d commit dbd90a1
Show file tree
Hide file tree
Showing 6 changed files with 215 additions and 58 deletions.
3 changes: 3 additions & 0 deletions auction-server/api-types/src/opportunity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,12 @@ pub struct OpportunityBidResult {
}

#[derive(Serialize, Deserialize, ToSchema, Clone, PartialEq, Debug, Display)]
#[serde(rename_all = "lowercase")]
pub enum ProgramSvm {
#[serde(rename = "swap_kamino")]
#[strum(serialize = "swap_kamino")]
SwapKamino,
#[serde(rename = "limo")]
#[strum(serialize = "limo")]
Limo,
}
Expand Down
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions sdk/js/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@pythnetwork/express-relay-js",
"version": "0.17.1",
"version": "0.17.2",
"description": "Utilities for interacting with the express relay protocol",
"homepage": "https://github.com/pyth-network/per/tree/main/sdk/js",
"author": "Douro Labs",
Expand All @@ -22,7 +22,7 @@
"generate-api-types": "openapi-typescript http://127.0.0.1:9000/docs/openapi.json --output src/serverTypes.d.ts",
"generate-anchor-types": "anchor idl type src/idl/idlExpressRelay.json --out src/expressRelayTypes.d.ts && anchor idl type src/examples/idl/idlDummy.json --out src/examples/dummyTypes.d.ts",
"format": "prettier --write \"src/**/*.ts\"",
"lint": "eslint src",
"lint": "eslint 'src/**/*.ts' --ignore-pattern '**/*.d.ts'",
"prepublishOnly": "pnpm build && pnpm test && pnpm lint",
"preversion": "pnpm lint",
"version": "pnpm format && git add -A src"
Expand All @@ -41,6 +41,7 @@
"@coral-xyz/anchor": "catalog:",
"@kamino-finance/limo-sdk": "catalog:",
"@solana/web3.js": "catalog:",
"bs58": "^6.0.0",
"decimal.js": "^10.4.3",
"isomorphic-ws": "^5.0.0",
"openapi-client-axios": "^7.5.5",
Expand Down
55 changes: 55 additions & 0 deletions sdk/js/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import createClient, {
} from "openapi-fetch";
import { Address, Hex, isAddress, isHex } from "viem";
import WebSocket from "isomorphic-ws";
import base58 from "bs58";
import {
Bid,
BidId,
Expand All @@ -20,6 +21,8 @@ import {
SvmChainUpdate,
OpportunityDelete,
ChainType,
QuoteRequest,
QuoteResponse,
} from "./types";
import {
Connection,
Expand Down Expand Up @@ -473,6 +476,35 @@ export class Client {
}
}

/**
* Gets the best quote for a given quote request
* @param quoteRequest Quote request to submit
* @returns Quote response representing the best quote for the request
*/
async getQuote(quoteRequest: QuoteRequest): Promise<QuoteResponse> {
const client = createClient<paths>(this.clientOptions);
const body = {
chain_id: quoteRequest.chainId,
input_token_mint: quoteRequest.inputTokenMint.toBase58(),
output_token_mint: quoteRequest.outputTokenMint.toBase58(),
router: quoteRequest.router.toBase58(),
specified_token_amount: quoteRequest.specifiedTokenAmount,
user_wallet_address: quoteRequest.userWallet.toBase58(),
version: "v1" as const,
};
// TODO: we may want to wrap all the GET/POST calls in a try/catch block to handle errors
const response = await client.POST("/v1/opportunities/quote", {
body: body,
});
if (response.error) {
throw ClientError.newHttpError(
JSON.stringify(response.error),
response.response.status
);
}
return this.convertQuoteResponse(response.data);
}

/**
* Submits a raw bid for a permission key
* @param bid
Expand Down Expand Up @@ -601,6 +633,29 @@ export class Client {
}
}

/**
* Converts a quote response from the server to the client format
* @param quoteResponse
* @returns Quote response in the converted client format
*/
public convertQuoteResponse(
quoteResponse: components["schemas"]["QuoteSvm"]
): QuoteResponse {
return {
chainId: quoteResponse.chain_id,
expirationTime: new Date(quoteResponse.expiration_time * 1000),
inputToken: {
token: new PublicKey(quoteResponse.input_token.token),
amount: BigInt(quoteResponse.input_token.amount),
},
outputToken: {
token: new PublicKey(quoteResponse.output_token.token),
amount: BigInt(quoteResponse.output_token.amount),
},
transaction: Transaction.from(base58.decode(quoteResponse.transaction)),
};
}

// EVM specific functions

/**
Expand Down
Loading

0 comments on commit dbd90a1

Please sign in to comment.