Skip to content

Commit

Permalink
Add ERC1155 support to orderbook package
Browse files Browse the repository at this point in the history
  • Loading branch information
Sam-Jeston committed May 3, 2024
1 parent 7bb037c commit b7322f6
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 16 deletions.
15 changes: 13 additions & 2 deletions packages/orderbook/src/openapi/mapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
FeeType,
Page,
Trade,
ERC1155Item,
} from '../types';

export function mapFromOpenApiOrder(order: OpenApiOrder): Order {
Expand All @@ -31,7 +32,7 @@ export function mapFromOpenApiOrder(order: OpenApiOrder): Order {
throw new Error('Buy items must be either ERC20 or NATIVE');
});

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

throw new Error('Sell items must ERC721');
if (item.type === 'ERC1155') {
return {
type: 'ERC1155',
contractAddress: item.contract_address,
tokenId: item.token_id,
amount: item.amount,
};
}

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

return {
Expand All @@ -54,6 +64,7 @@ export function mapFromOpenApiOrder(order: OpenApiOrder): Order {
recipientAddress: fee.recipient_address,
type: fee.type as unknown as FeeType,
})),
fillStatus: order.fill_status,
chain: order.chain,
createdAt: order.created_at,
endAt: order.end_at,
Expand Down
5 changes: 4 additions & 1 deletion packages/orderbook/src/orderbook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -209,12 +209,15 @@ export class Orderbook {
* @param {string} listingId - The listingId to fulfil.
* @param {string} takerAddress - The address of the account fulfilling the order.
* @param {FeeValue[]} takerFees - Taker ecosystem fees to be paid.
* @param {string} amountToFill - Amount of the order to fill, defaults to sell item amount.
* Only applies to ERC1155 orders
* @return {FulfillOrderResponse} Approval and fulfilment transactions.
*/
async fulfillOrder(
listingId: string,
takerAddress: string,
takerFees: FeeValue[],
amountToFill?: string,
): Promise<FulfillOrderResponse> {
const fulfillmentDataRes = await this.apiClient.fulfillmentData([
{
Expand Down Expand Up @@ -246,7 +249,7 @@ export class Orderbook {
);
}

return this.seaport.fulfillOrder(orderResult, takerAddress, extraData);
return this.seaport.fulfillOrder(orderResult, takerAddress, extraData, amountToFill);
}

async fulfillBulkOrders(
Expand Down
2 changes: 2 additions & 0 deletions packages/orderbook/src/seaport/seaport.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,7 @@ describe('Seaport', () => {
chain: { id: '1', name: 'imtbl-zkevm-local' },
created_at: new Date().toISOString(),
end_at: new Date().toISOString(),
fill_status: { numerator: '0', denominator: '0' },
id: '1',
order_hash: randomAddress(),
protocol_data: {
Expand Down Expand Up @@ -466,6 +467,7 @@ describe('Seaport', () => {
parameters: anything(),
signature: immutableOrder.signature,
},
unitsToFill: undefined,
extraData: fakeExtraData,
tips: [],
},
Expand Down
30 changes: 21 additions & 9 deletions packages/orderbook/src/seaport/seaport.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Seaport as SeaportLib } from '@opensea/seaport-js';
import {
ApprovalAction,
CreateInputItem,
CreateOrderAction,
ExchangeAction,
OrderComponents,
Expand All @@ -12,6 +13,7 @@ import { mapFromOpenApiOrder } from 'openapi/mapper';
import {
Action,
ActionType,
ERC1155Item,
ERC20Item,
ERC721Item,
FulfillOrderResponse,
Expand Down Expand Up @@ -45,7 +47,7 @@ export class Seaport {

async prepareSeaportOrder(
offerer: string,
listingItem: ERC721Item,
listingItem: ERC721Item | ERC1155Item,
considerationItem: ERC20Item | NativeItem,
orderStart: Date,
orderExpiry: Date,
Expand Down Expand Up @@ -104,6 +106,7 @@ export class Seaport {
order: Order,
account: string,
extraData: string,
unitsToFill?: string,
): Promise<FulfillOrderResponse> {
const { orderComponents, tips } = this.mapImmutableOrderToSeaportOrderComponents(order);
const seaportLib = this.getSeaportLib(order);
Expand All @@ -116,6 +119,7 @@ export class Seaport {
parameters: orderComponents,
signature: order.signature,
},
unitsToFill,
extraData,
tips,
},
Expand Down Expand Up @@ -264,22 +268,30 @@ export class Seaport {

private createSeaportOrder(
offerer: string,
listingItem: ERC721Item,
listingItem: ERC721Item | ERC1155Item,
considerationItem: ERC20Item | NativeItem,
orderStart: Date,
orderExpiry: Date,
): Promise<OrderUseCase<CreateOrderAction>> {
const seaportLib = this.getSeaportLib();

const offerItem: CreateInputItem = listingItem.type === 'ERC721'
? {
itemType: ItemType.ERC721,
token: listingItem.contractAddress,
identifier: listingItem.tokenId,
}
: {
itemType: ItemType.ERC1155,
token: listingItem.contractAddress,
identifier: listingItem.tokenId,
amount: listingItem.amount,
};

return seaportLib.createOrder(
{
allowPartialFills: false,
offer: [
{
itemType: ItemType.ERC721,
token: listingItem.contractAddress,
identifier: listingItem.tokenId,
},
],
offer: [offerItem],
consideration: [
{
token:
Expand Down
19 changes: 15 additions & 4 deletions packages/orderbook/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@ import { Fee as OpenapiFee, OrdersService, OrderStatus } from './openapi/sdk';
// Strictly re-export only the OrderStatusName enum from the openapi types
export { OrderStatusName } from './openapi/sdk';

export interface ERC1155Item {
type: 'ERC1155';
contractAddress: string;
tokenId: string;
amount: string;
}

export interface ERC721Item {
type: 'ERC721';
contractAddress: string;
Expand All @@ -29,7 +36,7 @@ export interface RoyaltyInfo {

export interface PrepareListingParams {
makerAddress: string;
sell: ERC721Item;
sell: ERC721Item | ERC1155Item;
buy: ERC20Item | NativeItem;
orderExpiry?: Date;
}
Expand Down Expand Up @@ -162,14 +169,18 @@ export interface Order {
type: 'LISTING';
accountAddress: string;
buy: (ERC20Item | NativeItem)[];
sell: ERC721Item[];
sell: (ERC721Item | ERC1155Item)[];
fees: Fee[];
chain: {
id: string;
name: string;
};
createdAt: string;
updatedAt: string;
fillStatus: {
numerator: string,
denominator: string,
};
/**
* Time after which the Order is considered active
*/
Expand All @@ -180,7 +191,7 @@ export interface Order {
endAt: string;
orderHash: string;
protocolData: {
orderType: 'FULL_RESTRICTED';
orderType: 'FULL_RESTRICTED' | 'PARTIAL_RESTRICTED';
zoneAddress: string;
counter: string;
seaportAddress: string;
Expand Down Expand Up @@ -219,7 +230,7 @@ export interface Trade {
name: string;
};
buy: (ERC20Item | NativeItem)[];
sell: ERC721Item[];
sell: (ERC721Item | ERC1155Item)[];
buyerFees: Fee[];
sellerAddress: string;
buyerAddress: string;
Expand Down

0 comments on commit b7322f6

Please sign in to comment.