Skip to content

Commit

Permalink
refactor: [TD-1668] Update orderbook OpenAPI SDK (#2152)
Browse files Browse the repository at this point in the history
  • Loading branch information
lfportal authored Sep 18, 2024
1 parent 83bb1fd commit f931703
Show file tree
Hide file tree
Showing 30 changed files with 1,432 additions and 484 deletions.
237 changes: 212 additions & 25 deletions packages/orderbook/src/openapi/mapper.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,57 @@
import { Order as OpenApiOrder } from './sdk/models/Order';
import { Trade as OpenApiTrade } from './sdk/models/Trade';
import { Page as OpenApiPage } from './sdk/models/Page';
import {
Bid,
CollectionBid,
ERC1155CollectionItem,
ERC1155Item,
ERC20Item,
NativeItem,
Order,
ERC721CollectionItem,
ERC721Item,
FeeType,
Listing,
NativeItem,
Order,
Page,
Trade,
ERC1155Item,
} from '../types';
import { exhaustiveSwitch } from '../utils';
import { Order as OpenApiOrder } from './sdk/models/Order';
import { Page as OpenApiPage } from './sdk/models/Page';
import { Trade as OpenApiTrade } from './sdk/models/Trade';

export function mapListingFromOpenApiOrder(order: OpenApiOrder): Listing {
if (order.type !== OpenApiOrder.type.LISTING) {
throw new Error('Order type must be LISTING');
}

const sellItems: (ERC721Item | ERC1155Item)[] = order.sell.map((item) => {
if (item.type === 'ERC721') {
return {
type: 'ERC721',
contractAddress: item.contract_address,
tokenId: item.token_id,
};
}

if (item.type === 'ERC1155') {
return {
type: 'ERC1155',
contractAddress: item.contract_address,
tokenId: item.token_id,
amount: item.amount,
};
}

throw new Error('Listing sell items must either ERC721 or ERC1155');
});

export function mapFromOpenApiOrder(order: OpenApiOrder): Order {
const buyItems: (ERC20Item | NativeItem)[] = order.buy.map((item) => {
if (item.type === 'NATIVE') {
return {
type: 'NATIVE',
amount: item.amount,
};
}

if (item.type === 'ERC20') {
return {
type: 'ERC20',
Expand All @@ -22,17 +60,58 @@ export function mapFromOpenApiOrder(order: OpenApiOrder): Order {
};
}

if (item.type === 'NATIVE') {
throw new Error('Listing buy items must be either NATIVE or ERC20');
});

return {
id: order.id,
type: order.type,
chain: order.chain,
accountAddress: order.account_address,
sell: sellItems,
buy: buyItems,
fees: order.fees.map((fee) => ({
amount: fee.amount,
recipientAddress: fee.recipient_address,
type: fee.type as unknown as FeeType,
})),
status: order.status,
fillStatus: order.fill_status,
startAt: order.start_at,
endAt: order.end_at,
salt: order.salt,
signature: order.signature,
orderHash: order.order_hash,
protocolData: {
orderType: order.protocol_data.order_type,
counter: order.protocol_data.counter,
seaportAddress: order.protocol_data.seaport_address,
seaportVersion: order.protocol_data.seaport_version,
zoneAddress: order.protocol_data.zone_address,
},
createdAt: order.created_at,
updatedAt: order.updated_at,
};
}

export function mapBidFromOpenApiOrder(order: OpenApiOrder): Bid {
if (order.type !== OpenApiOrder.type.BID) {
throw new Error('Order type must be BID');
}

const sellItems: ERC20Item[] = order.buy.map((item) => {
if (item.type === 'ERC20') {
return {
type: 'NATIVE',
type: 'ERC20',
contractAddress: item.contract_address,
amount: item.amount,
};
}

throw new Error('Buy items must be either ERC20 or NATIVE');
throw new Error('Bid sell items must be ERC20');
});

const sellItems: (ERC721Item | ERC1155Item)[] = order.sell.map((item) => {
const buyItems: (ERC721Item | ERC1155Item)[] = order.sell.map((item) => {
if (item.type === 'ERC721') {
return {
type: 'ERC721',
Expand All @@ -50,24 +129,27 @@ export function mapFromOpenApiOrder(order: OpenApiOrder): Order {
};
}

throw new Error('Sell items must ERC721 or ERC1155');
throw new Error('Bid buy items must either ERC721 or ERC1155');
});

return {
id: order.id,
type: order.type,
chain: order.chain,
accountAddress: order.account_address,
buy: buyItems,
sell: sellItems,
buy: buyItems,
fees: order.fees.map((fee) => ({
amount: fee.amount,
recipientAddress: fee.recipient_address,
type: fee.type as unknown as FeeType,
})),
status: order.status,
fillStatus: order.fill_status,
chain: order.chain,
createdAt: order.created_at,
startAt: order.start_at,
endAt: order.end_at,
salt: order.salt,
signature: order.signature,
orderHash: order.order_hash,
protocolData: {
orderType: order.protocol_data.order_type,
Expand All @@ -76,16 +158,17 @@ export function mapFromOpenApiOrder(order: OpenApiOrder): Order {
seaportVersion: order.protocol_data.seaport_version,
zoneAddress: order.protocol_data.zone_address,
},
salt: order.salt,
signature: order.signature,
startAt: order.start_at,
status: order.status,
createdAt: order.created_at,
updatedAt: order.updated_at,
};
}

export function mapFromOpenApiTrade(trade: OpenApiTrade): Trade {
const buyItems: (ERC20Item | NativeItem)[] = trade.buy.map((item) => {
export function mapCollectionBidFromOpenApiOrder(order: OpenApiOrder): CollectionBid {
if (order.type !== OpenApiOrder.type.COLLECTION_BID) {
throw new Error('Order type must be COLLECTION_BID');
}

const sellItems: ERC20Item[] = order.buy.map((item) => {
if (item.type === 'ERC20') {
return {
type: 'ERC20',
Expand All @@ -94,17 +177,90 @@ export function mapFromOpenApiTrade(trade: OpenApiTrade): Trade {
};
}

throw new Error('Collection bid sell items must be ERC20');
});

const buyItems: (ERC721CollectionItem | ERC1155CollectionItem)[] = order.sell.map((item) => {
if (item.type === 'ERC721_COLLECTION') {
return {
type: 'ERC721_COLLECTION',
contractAddress: item.contract_address,
amount: item.amount,
};
}

if (item.type === 'ERC1155_COLLECTION') {
return {
type: 'ERC1155_COLLECTION',
contractAddress: item.contract_address,
amount: item.amount,
};
}

throw new Error('Collection bid buy items must either ERC721_COLLECTION or ERC1155_COLLECTION');
});

return {
id: order.id,
type: order.type,
chain: order.chain,
accountAddress: order.account_address,
sell: sellItems,
buy: buyItems,
fees: order.fees.map((fee) => ({
amount: fee.amount,
recipientAddress: fee.recipient_address,
type: fee.type as unknown as FeeType,
})),
status: order.status,
fillStatus: order.fill_status,
startAt: order.start_at,
endAt: order.end_at,
salt: order.salt,
signature: order.signature,
orderHash: order.order_hash,
protocolData: {
orderType: order.protocol_data.order_type,
counter: order.protocol_data.counter,
seaportAddress: order.protocol_data.seaport_address,
seaportVersion: order.protocol_data.seaport_version,
zoneAddress: order.protocol_data.zone_address,
},
createdAt: order.created_at,
updatedAt: order.updated_at,
};
}

export function mapOrderFromOpenApiOrder(order: OpenApiOrder): Order {
switch (order.type) {
case OpenApiOrder.type.LISTING:
return mapListingFromOpenApiOrder(order);
case OpenApiOrder.type.BID:
return mapBidFromOpenApiOrder(order);
case OpenApiOrder.type.COLLECTION_BID:
return mapCollectionBidFromOpenApiOrder(order);
default:
return exhaustiveSwitch(order.type);
}
}

export function mapFromOpenApiTrade(trade: OpenApiTrade): Trade {
const buyItems: (ERC20Item | NativeItem | ERC721Item | ERC1155Item)[] = trade.buy.map((item) => {
if (item.type === 'NATIVE') {
return {
type: 'NATIVE',
amount: item.amount,
};
}

throw new Error('Buy items must be either ERC20 or NATIVE');
});
if (item.type === 'ERC20') {
return {
type: 'ERC20',
contractAddress: item.contract_address,
amount: item.amount,
};
}

const sellItems: (ERC721Item | ERC1155Item)[] = trade.sell.map((item) => {
if (item.type === 'ERC721') {
return {
type: 'ERC721',
Expand All @@ -122,9 +278,40 @@ export function mapFromOpenApiTrade(trade: OpenApiTrade): Trade {
};
}

throw new Error('Sell items must ERC721');
throw new Error('Buy items must be NATIVE, ERC20, ERC721 or ERC1155');
});

const sellItems: (ERC20Item | ERC721Item | ERC1155Item)[] = trade.sell.map(
(item) => {
if (item.type === 'ERC20') {
return {
type: 'ERC20',
contractAddress: item.contract_address,
amount: item.amount,
};
}

if (item.type === 'ERC721') {
return {
type: 'ERC721',
contractAddress: item.contract_address,
tokenId: item.token_id,
};
}

if (item.type === 'ERC1155') {
return {
type: 'ERC1155',
contractAddress: item.contract_address,
tokenId: item.token_id,
amount: item.amount,
};
}

throw new Error('Sell items must be ERC20, ERC721 or ERC1155');
},
);

return {
id: trade.id,
orderId: trade.order_id,
Expand Down
9 changes: 9 additions & 0 deletions packages/orderbook/src/openapi/sdk/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,22 @@ export { OpenAPI } from './core/OpenAPI';
export type { OpenAPIConfig } from './core/OpenAPI';

export type { ActiveOrderStatus } from './models/ActiveOrderStatus';
export type { AssetCollectionItem } from './models/AssetCollectionItem';
export type { BidResult } from './models/BidResult';
export { CancelledOrderStatus } from './models/CancelledOrderStatus';
export type { CancelOrdersRequestBody } from './models/CancelOrdersRequestBody';
export type { CancelOrdersResult } from './models/CancelOrdersResult';
export type { CancelOrdersResultData } from './models/CancelOrdersResultData';
export type { Chain } from './models/Chain';
export type { ChainName } from './models/ChainName';
export type { CollectionBidResult } from './models/CollectionBidResult';
export type { CreateBidRequestBody } from './models/CreateBidRequestBody';
export type { CreateCollectionBidRequestBody } from './models/CreateCollectionBidRequestBody';
export type { CreateListingRequestBody } from './models/CreateListingRequestBody';
export type { ERC1155CollectionItem } from './models/ERC1155CollectionItem';
export type { ERC1155Item } from './models/ERC1155Item';
export type { ERC20Item } from './models/ERC20Item';
export type { ERC721CollectionItem } from './models/ERC721CollectionItem';
export type { ERC721Item } from './models/ERC721Item';
export type { Error } from './models/Error';
export type { ExpiredOrderStatus } from './models/ExpiredOrderStatus';
Expand All @@ -30,6 +37,8 @@ export type { FulfillableOrder } from './models/FulfillableOrder';
export type { FulfillmentDataRequest } from './models/FulfillmentDataRequest';
export type { InactiveOrderStatus } from './models/InactiveOrderStatus';
export type { Item } from './models/Item';
export type { ListBidsResult } from './models/ListBidsResult';
export type { ListCollectionBidsResult } from './models/ListCollectionBidsResult';
export type { ListingResult } from './models/ListingResult';
export type { ListListingsResult } from './models/ListListingsResult';
export type { ListTradeResult } from './models/ListTradeResult';
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/* istanbul ignore file */
/* tslint:disable */
/* eslint-disable */

import type { ERC1155CollectionItem } from './ERC1155CollectionItem';
import type { ERC721CollectionItem } from './ERC721CollectionItem';

export type AssetCollectionItem = (ERC721CollectionItem | ERC1155CollectionItem);

10 changes: 10 additions & 0 deletions packages/orderbook/src/openapi/sdk/models/BidResult.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/* istanbul ignore file */
/* tslint:disable */
/* eslint-disable */

import type { Order } from './Order';

export type BidResult = {
result: Order;
};

10 changes: 10 additions & 0 deletions packages/orderbook/src/openapi/sdk/models/CollectionBidResult.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/* istanbul ignore file */
/* tslint:disable */
/* eslint-disable */

import type { Order } from './Order';

export type CollectionBidResult = {
result: Order;
};

Loading

0 comments on commit f931703

Please sign in to comment.