Skip to content

Commit

Permalink
Add remove opportunties to sdk (#200)
Browse files Browse the repository at this point in the history
  • Loading branch information
danimhr authored Nov 1, 2024
1 parent 67296fd commit 27278c4
Show file tree
Hide file tree
Showing 13 changed files with 235 additions and 54 deletions.
1 change: 1 addition & 0 deletions auction-server/src/opportunity/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ pub struct OpportunityDeleteV1Svm {
}

#[derive(Serialize, Deserialize, ToSchema, Clone, PartialEq, Debug)]
#[serde(tag = "version")]
pub enum OpportunityDeleteSvm {
#[serde(rename = "v1")]
#[schema(title = "v1")]
Expand Down
4 changes: 2 additions & 2 deletions sdk/js/package-lock.json

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

2 changes: 1 addition & 1 deletion 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.12.3",
"version": "0.13.0",
"description": "Utilities for interacting with the express relay protocol",
"homepage": "https://github.com/pyth-network/per/tree/main/sdk/js",
"author": "Douro Labs",
Expand Down
15 changes: 13 additions & 2 deletions sdk/js/src/examples/simpleSearcherLimo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@ import {
Opportunity,
OpportunitySvm,
} from "../index";
import { BidStatusUpdate, ChainId, SvmChainUpdate } from "../types";
import {
BidStatusUpdate,
ChainId,
OpportunityDelete,
SvmChainUpdate,
} from "../types";
import { SVM_CONSTANTS } from "../const";

import * as anchor from "@coral-xyz/anchor";
Expand Down Expand Up @@ -43,7 +48,8 @@ class SimpleSearcherLimo {
undefined,
this.opportunityHandler.bind(this),
this.bidStatusHandler.bind(this),
this.svmChainUpdateHandler.bind(this)
this.svmChainUpdateHandler.bind(this),
this.removeOpportunitiesHandler.bind(this)
);
this.connectionSvm = new Connection(endpointSvm, "confirmed");
}
Expand Down Expand Up @@ -186,6 +192,11 @@ class SimpleSearcherLimo {
this.recentBlockhash[update.chain_id] = update.blockhash;
}

// NOTE: Developers are responsible for implementing custom removal logic specific to their use case.
async removeOpportunitiesHandler(opportunityDelete: OpportunityDelete) {
console.log(`Opportunities ${opportunityDelete} don't exist anymore`);
}

async start() {
try {
await this.client.subscribeChains([argv.chainId]);
Expand Down
47 changes: 46 additions & 1 deletion sdk/js/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
OpportunityCreate,
TokenAmount,
SvmChainUpdate,
OpportunityDelete,
} from "./types";
import {
Connection,
Expand Down Expand Up @@ -97,6 +98,10 @@ export class Client {
update: SvmChainUpdate
) => Promise<void>;

private websocketRemoveOpportunitiesCallback?: (
opportunityDelete: OpportunityDelete
) => Promise<void>;

private getAuthorization() {
return this.clientOptions.apiKey
? {
Expand All @@ -110,7 +115,10 @@ export class Client {
wsOptions?: WsOptions,
opportunityCallback?: (opportunity: Opportunity) => Promise<void>,
bidStatusCallback?: (statusUpdate: BidStatusUpdate) => Promise<void>,
svmChainUpdateCallback?: (update: SvmChainUpdate) => Promise<void>
svmChainUpdateCallback?: (update: SvmChainUpdate) => Promise<void>,
removeOpportunitiesCallback?: (
opportunityDelete: OpportunityDelete
) => Promise<void>
) {
this.clientOptions = clientOptions;
this.clientOptions.headers = {
Expand All @@ -121,6 +129,7 @@ export class Client {
this.websocketOpportunityCallback = opportunityCallback;
this.websocketBidStatusCallback = bidStatusCallback;
this.websocketSvmChainUpdateCallback = svmChainUpdateCallback;
this.websocketRemoveOpportunitiesCallback = removeOpportunitiesCallback;
}

private connectWebsocket() {
Expand Down Expand Up @@ -158,6 +167,17 @@ export class Client {
if (typeof this.websocketSvmChainUpdateCallback === "function") {
await this.websocketSvmChainUpdateCallback(message.update);
}
} else if ("type" in message && message.type === "remove_opportunities") {
if (typeof this.websocketRemoveOpportunitiesCallback === "function") {
await this.websocketRemoveOpportunitiesCallback({
chainId: message.opportunity_delete.chain_id,
program: message.opportunity_delete.program,
permissionAccount: new PublicKey(
message.opportunity_delete.permission_account
),
router: new PublicKey(message.opportunity_delete.router),
});
}
} else if ("id" in message && message.id) {
// Response to a request sent earlier via the websocket with the same id
const callback = this.callbackRouter[message.id];
Expand Down Expand Up @@ -333,6 +353,31 @@ export class Client {
}
}

/**
* Remove an opportunity from the server and update the searchers
* @param opportunity Opportunity to be removed
*/
async removeOpportunity(opportunity: OpportunityDelete) {
if (opportunity.program !== "limo") {
throw new ClientError("Only limo opportunities can be removed");
}

const client = createClient<paths>(this.clientOptions);
const body = {
chain_id: opportunity.chainId,
version: "v1" as const,
program: opportunity.program,
permission_account: opportunity.permissionAccount.toBase58(),
router: opportunity.router.toBase58(),
};
const response = await client.DELETE("/v1/opportunities", {
body,
});
if (response.error) {
throw new ClientError(response.error.error);
}
}

/**
* Submits a raw bid for a permission key
* @param bid
Expand Down
Loading

0 comments on commit 27278c4

Please sign in to comment.