Skip to content

Commit 5f6403f

Browse files
committed
getOrderByHash handle os2 order response
1 parent 48a1b5d commit 5f6403f

File tree

4 files changed

+47
-32
lines changed

4 files changed

+47
-32
lines changed

src/api/api.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import {
3434
GetBestListingResponse,
3535
GetOffersResponse,
3636
GetListingsResponse,
37+
GetOrderByHashResponse,
3738
CollectionOffer,
3839
CollectionOrderByOption,
3940
CancelOrderResponse,
@@ -126,14 +127,14 @@ export class OpenSeaAPI {
126127
* @param orderHash The hash of the order to fetch
127128
* @param protocolAddress The address of the seaport contract
128129
* @param chain The chain where the order is located. Defaults to the chain set in the constructor.
129-
* @returns The {@link OrderV2} returned by the API
130+
* @returns The {@link GetOrderByHashResponse} returned by the API (can be Offer or Listing)
130131
* @throws An error if the order is not found
131132
*/
132133
public async getOrderByHash(
133134
orderHash: string,
134135
protocolAddress: string,
135136
chain: Chain = this.chain,
136-
): Promise<OrderV2> {
137+
): Promise<GetOrderByHashResponse> {
137138
return this.ordersAPI.getOrderByHash(orderHash, protocolAddress, chain);
138139
}
139140

src/api/orders.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@ import {
33
getOrderByHashPath,
44
getCancelOrderPath,
55
} from "./apiPaths";
6-
import { GetOrdersResponse, CancelOrderResponse } from "./types";
6+
import {
7+
GetOrdersResponse,
8+
CancelOrderResponse,
9+
GetOrderByHashResponse,
10+
} from "./types";
711
import {
812
FulfillmentDataResponse,
913
OrderAPIOptions,
@@ -72,16 +76,17 @@ export class OrdersAPI {
7276

7377
/**
7478
* Gets a single order by its order hash.
79+
* Returns the raw API response which can be either an Offer or Listing.
7580
*/
7681
async getOrderByHash(
7782
orderHash: string,
7883
protocolAddress: string,
7984
chain: Chain = this.chain,
80-
): Promise<OrderV2> {
85+
): Promise<GetOrderByHashResponse> {
8186
const response = await this.fetcher.get<{
82-
order: OrdersQueryResponse["orders"][0];
87+
order: GetOrderByHashResponse;
8388
}>(getOrderByHashPath(chain, protocolAddress, orderHash));
84-
return deserializeOrder(response.order);
89+
return response.order;
8590
}
8691

8792
/**

src/api/types.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,13 @@ export type GetBestOfferResponse = Offer | CollectionOffer;
243243
*/
244244
export type GetBestListingResponse = Listing;
245245

246+
/**
247+
* Response from OpenSea API for fetching an order by hash.
248+
* Can be either an Offer or a Listing.
249+
* @category API Response Types
250+
*/
251+
export type GetOrderByHashResponse = Offer | Listing;
252+
246253
/**
247254
* Response from OpenSea API for offchain canceling an order.
248255
* @category API Response Types

src/sdk/cancellation.ts

Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { OrderComponents } from "@opensea/seaport-js/lib/types";
22
import { Overrides, Signer } from "ethers";
3+
import { GetOrderByHashResponse } from "../api/types";
34
import { OrderV2 } from "../orders/types";
45
import { DEFAULT_SEAPORT_CONTRACT_ADDRESS } from "../orders/utils";
56
import { Chain, EventType } from "../types";
@@ -55,37 +56,41 @@ export class CancellationManager {
5556
// Check account availability after parameter validation
5657
await this.context.requireAccountIsAvailable(accountAddress);
5758

58-
let orderToCancel: OrderV2;
59+
let orderComponents: OrderComponents;
60+
let effectiveProtocolAddress: string;
5961

6062
if (order) {
6163
// Using OrderV2 object directly
6264
requireValidProtocol(order.protocolAddress);
63-
orderToCancel = order;
65+
effectiveProtocolAddress = order.protocolAddress;
66+
orderComponents = order.protocolData.parameters;
67+
68+
this.context.dispatch(EventType.CancelOrder, {
69+
orderV2: order,
70+
accountAddress,
71+
});
6472
} else if (orderHash) {
6573
// Fetch order from API using order hash
6674
requireValidProtocol(protocolAddress);
67-
orderToCancel = await this.context.api.getOrderByHash(
75+
const fetchedOrder = await this.context.api.getOrderByHash(
6876
orderHash,
6977
protocolAddress,
7078
this.context.chain,
7179
);
72-
requireValidProtocol(orderToCancel.protocolAddress);
80+
requireValidProtocol(fetchedOrder.protocol_address);
81+
effectiveProtocolAddress = fetchedOrder.protocol_address;
82+
orderComponents = fetchedOrder.protocol_data.parameters;
7383
} else {
7484
// Should never reach here due to earlier validation
7585
throw new Error("Invalid input");
7686
}
7787

78-
this.context.dispatch(EventType.CancelOrder, {
79-
orderV2: orderToCancel,
80-
accountAddress,
81-
});
82-
8388
// Transact and get the transaction hash
8489
const transactionHash = await this.cancelSeaportOrders({
85-
orders: [orderToCancel.protocolData.parameters],
90+
orders: [orderComponents],
8691
accountAddress,
8792
domain,
88-
protocolAddress: orderToCancel.protocolAddress,
93+
protocolAddress: effectiveProtocolAddress,
8994
});
9095

9196
// Await transaction confirmation
@@ -176,9 +181,17 @@ export class CancellationManager {
176181
return order as OrderComponents;
177182
}
178183
});
184+
185+
// Dispatch event for the first order if available (for backwards compatibility with cancelOrder)
186+
if (firstOrderV2) {
187+
this.context.dispatch(EventType.CancelOrder, {
188+
orderV2: firstOrderV2,
189+
accountAddress,
190+
});
191+
}
179192
} else if (orderHashes) {
180193
// Fetch orders from the API using order hashes
181-
const fetchedOrders: OrderV2[] = [];
194+
const fetchedOrders: GetOrderByHashResponse[] = [];
182195
for (const orderHash of orderHashes) {
183196
const order = await this.context.api.getOrderByHash(
184197
orderHash,
@@ -190,26 +203,15 @@ export class CancellationManager {
190203

191204
// Extract OrderComponents from the fetched orders
192205
orderComponents = fetchedOrders.map((order) => {
193-
requireValidProtocol(order.protocolAddress);
194-
effectiveProtocolAddress = order.protocolAddress;
195-
return order.protocolData.parameters;
206+
requireValidProtocol(order.protocol_address);
207+
effectiveProtocolAddress = order.protocol_address;
208+
return order.protocol_data.parameters;
196209
});
197-
198-
// Save the first order for event dispatching
199-
firstOrderV2 = fetchedOrders[0];
200210
} else {
201211
// Should never reach here due to earlier validation
202212
throw new Error("Invalid input");
203213
}
204214

205-
// Dispatch event for the first order if available (for backwards compatibility with cancelOrder)
206-
if (firstOrderV2) {
207-
this.context.dispatch(EventType.CancelOrder, {
208-
orderV2: firstOrderV2,
209-
accountAddress,
210-
});
211-
}
212-
213215
// Transact and get the transaction hash
214216
const transactionHash = await this.cancelSeaportOrders({
215217
orders: orderComponents,

0 commit comments

Comments
 (0)