Skip to content

Commit

Permalink
[NO-CHANGELOG] TD-805 feat: allow orderbook client to override provid…
Browse files Browse the repository at this point in the history
…er, move ethers to peer dependency (#569)
  • Loading branch information
reVrost committed Jul 19, 2023
1 parent 49ec313 commit b2a516b
Show file tree
Hide file tree
Showing 11 changed files with 101 additions and 73 deletions.
43 changes: 25 additions & 18 deletions packages/orderbook/src/config/config.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Environment } from '@imtbl/config';
import { providers } from 'ethers';

export const LOCAL_CHAIN_NAME = 'imtbl-zkevm-local';
Expand All @@ -6,39 +7,45 @@ export const TESTNET_CHAIN_NAME = 'imtbl-zkevm-testnet';
export const MAINNET_CHAIN_NAME = 'imtbl-zkevm-mainnet';

export interface OrderbookOverrides {
chainName: string;
provider?: providers.JsonRpcProvider | providers.Web3Provider;
seaportContractAddress?: string;
zoneContractAddress?: string;
chainName?: string;
apiEndpoint?: string;
}

export interface OrderbookModuleConfiguration {
seaportContractAddress: string
zoneContractAddress: string
seaportContractAddress: string;
zoneContractAddress: string;
apiEndpoint: string;
provider: providers.JsonRpcProvider | providers.Web3Provider
chainName: string;
provider: providers.JsonRpcProvider | providers.Web3Provider;
}

export function getOrderbookConfig(chainName?: string): OrderbookModuleConfiguration | null {
switch (chainName) {
case DEVNET_CHAIN_NAME:
return {
seaportContractAddress: '0x41388404Efb7a68Fd31d75CEf71dF91e2BDBa2fb',
zoneContractAddress: '0xCb5063b0c1dcF4f7fed8E7eaa79faf9859792767',
apiEndpoint: 'https://order-book-mr.dev.imtbl.com',
provider: new providers.JsonRpcProvider('https://zkevm-rpc.dev.x.immutable.com'),
};
case TESTNET_CHAIN_NAME:
export function getOrderbookConfig(
environment: Environment,
): OrderbookModuleConfiguration | null {
switch (environment) {
case Environment.SANDBOX:
return {
seaportContractAddress: '0x45E23dA18804F99Cf67408AeBE85F67c958381Ff',
zoneContractAddress: '0x25b00b7eb97eab6194798E9B8eF63Aa526D5bd7E',
apiEndpoint: 'https://order-book-mr.sandbox.imtbl.com',
provider: new providers.JsonRpcProvider('https://zkevm-rpc.sandbox.x.immutable.com'),
chainName: TESTNET_CHAIN_NAME,
provider: new providers.JsonRpcProvider(
'https://zkevm-rpc.sandbox.x.immutable.com',
),
};
case MAINNET_CHAIN_NAME:
// not yet deployed
// not yet deployed
case Environment.PRODUCTION:
return {
seaportContractAddress: '',
zoneContractAddress: '',
apiEndpoint: 'https://order-book-mr.imtbl.com',
provider: new providers.JsonRpcProvider('https://zkevm-rpc.x.immutable.com'),
chainName: MAINNET_CHAIN_NAME,
provider: new providers.JsonRpcProvider(
'https://zkevm-rpc.x.immutable.com',
),
};
default:
return null;
Expand Down
44 changes: 22 additions & 22 deletions packages/orderbook/src/orderbook.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { ModuleConfiguration } from '@imtbl/config';
import { ImmutableApiClient, ImmutableApiClientFactory } from 'api-client';
import { OrderbookModuleConfiguration, OrderbookOverrides, getOrderbookConfig } from 'config/config';
import {
getOrderbookConfig,
OrderbookModuleConfiguration,
OrderbookOverrides,
} from 'config/config';
import { ERC721Factory } from 'erc721';
import { ListingResult, ListListingsResult, OrderStatus } from 'openapi/sdk';
import { Seaport } from 'seaport';
Expand All @@ -26,26 +30,25 @@ export class Orderbook {

private orderbookConfig: OrderbookModuleConfiguration;

constructor(
config: ModuleConfiguration<OrderbookOverrides>,
localConfig?: OrderbookModuleConfiguration,
) {
// either use preconfigured config or local config passed in
const maybeConfig = getOrderbookConfig(config.overrides?.chainName) || localConfig;
if (!maybeConfig) {
throw new Error('Orderbook configuration not passed, either select one of the preseet chainNames or pass in localConfig');
}
this.orderbookConfig = maybeConfig;
constructor(config: ModuleConfiguration<OrderbookOverrides>) {
const obConfig = getOrderbookConfig(config.baseConfig.environment);

const { apiEndpoint } = this.orderbookConfig;
if (!apiEndpoint) {
throw new Error('API endpoint must be provided as an override');
const finalConfig: OrderbookModuleConfiguration = {
...obConfig,
...config.overrides,
} as OrderbookModuleConfiguration;

if (!finalConfig) {
throw new Error(
'Orderbook configuration not passed, please specify the environment under config.baseConfig.environment',
);
}

// TODO: Move chainId lookup to a map based on env. Just using override to get dev started
const chainName = config.overrides?.chainName;
if (!chainName) {
throw new Error('chainName must be provided as an override');
this.orderbookConfig = finalConfig;

const { apiEndpoint, chainName } = this.orderbookConfig;
if (!apiEndpoint) {
throw new Error('API endpoint must be provided');
}

this.apiClient = new ImmutableApiClientFactory(
Expand Down Expand Up @@ -105,10 +108,7 @@ export class Orderbook {
sell.contractAddress,
this.orderbookConfig.provider,
).create();
const royaltyInfo = await erc721.royaltyInfo(
sell.tokenId,
buy.amount,
);
const royaltyInfo = await erc721.royaltyInfo(sell.tokenId, buy.amount);

return this.seaport.prepareSeaportOrder(
makerAddress,
Expand Down
5 changes: 3 additions & 2 deletions packages/orderbook/src/test/cancel.e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,15 @@ describe('cancel order', () => {
const provider = getLocalhostProvider();
const offerer = getOffererWallet(provider);

const localConfigOverrides = getLocalConfigFromEnv();
const sdk = new Orderbook({
baseConfig: {
environment: Environment.SANDBOX,
},
overrides: {
chainName: 'imtbl-zkevm-local',
...localConfigOverrides,
},
}, getLocalConfigFromEnv());
});

const { contract } = await deployTestToken(offerer);
await contract.safeMint(offerer.address);
Expand Down
5 changes: 3 additions & 2 deletions packages/orderbook/src/test/create.e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,15 @@ describe('prepareListing and createOrder e2e', () => {
const provider = getLocalhostProvider();
const offerer = getOffererWallet(provider);

const localConfigOverrides = getLocalConfigFromEnv();
const sdk = new Orderbook({
baseConfig: {
environment: Environment.SANDBOX,
},
overrides: {
chainName: 'imtbl-zkevm-local',
...localConfigOverrides,
},
}, getLocalConfigFromEnv());
});

const { contract } = await deployTestToken(offerer);
await contract.safeMint(offerer.address);
Expand Down
34 changes: 23 additions & 11 deletions packages/orderbook/src/test/expiry.demo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import { log } from 'console';
import { OrderStatus } from '../openapi/sdk/index';
import { Orderbook } from '../orderbook';
import {
getLocalhostProvider,
deployTestToken,
getFulfillerWallet,
getLocalhostProvider,
getOffererWallet,
deployTestToken,
signAndSubmitTx,
signMessage,
TestToken,
Expand Down Expand Up @@ -48,12 +48,11 @@ describe('', () => {
baseConfig: {
environment: Environment.SANDBOX,
},
overrides: {
chainName: 'imtbl-zkevm-testnet',
},
});

log(`Preparing soon-to-expire listing for user ${offerer.address} for NFT collection ${nftContract.address}, TokenID 0`);
log(
`Preparing soon-to-expire listing for user ${offerer.address} for NFT collection ${nftContract.address}, TokenID 0`,
);

// Prepare the listing details
const soonToExpireListing = await sdk.prepareListing({
Expand All @@ -72,27 +71,40 @@ describe('', () => {

log('Signing and submitting approval transaction...');
// Sign and submit the approval transaction for the offerer
await signAndSubmitTx(soonToExpireListing.unsignedApprovalTransaction!, offerer, provider);
await signAndSubmitTx(
soonToExpireListing.unsignedApprovalTransaction!,
offerer,
provider,
);

// Sign the EIP712 order message for the offerer. This is the signature that the order book API
// stores and allows the fulfiller to fulfil the order, as long as they also have a valid
// operator signature
const signature = await signMessage(soonToExpireListing.typedOrderMessageForSigning, offerer);
const signature = await signMessage(
soonToExpireListing.typedOrderMessageForSigning,
offerer,
);

log('Submitting order to orderbook API...');
// Submit the order creation request to the order book API
const { result: { id: orderId } } = await sdk.createListing({
const {
result: { id: orderId },
} = await sdk.createListing({
orderComponents: soonToExpireListing.orderComponents,
orderHash: soonToExpireListing.orderHash,
orderSignature: signature,
});
log('Submitted order to orderbook API with expiry time set in the future');

await waitForOrderToBeOfStatus(sdk, orderId, OrderStatus.ACTIVE);
log(`Listing ${orderId} is now ACTIVE, it will soon transition to EXPIRED, waiting...`);
log(
`Listing ${orderId} is now ACTIVE, it will soon transition to EXPIRED, waiting...`,
);

await waitForOrderToBeOfStatus(sdk, orderId, OrderStatus.EXPIRED);
log(`Listing ${orderId} is now EXPIRED. Attempting to fulfill the expired listing...`);
log(
`Listing ${orderId} is now EXPIRED. Attempting to fulfill the expired listing...`,
);

try {
await sdk.fulfillOrder(orderId, fulfiller.address);
Expand Down
26 changes: 17 additions & 9 deletions packages/orderbook/src/test/fulfil.demo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import { log } from 'console';
import { OrderStatus } from '../openapi/sdk/index';
import { Orderbook } from '../orderbook';
import {
getLocalhostProvider,
deployTestToken,
getFulfillerWallet,
getLocalhostProvider,
getOffererWallet,
deployTestToken,
signAndSubmitTx,
signMessage,
TestToken,
Expand Down Expand Up @@ -48,9 +48,6 @@ describe('', () => {
baseConfig: {
environment: Environment.SANDBOX,
},
overrides: {
chainName: 'imtbl-zkevm-testnet',
},
});

log('Signing and submitting approval transaction...');
Expand All @@ -69,14 +66,23 @@ describe('', () => {
orderExpiry: new Date(Date.now() + 1000000 * 30),
});

await signAndSubmitTx(validListing.unsignedApprovalTransaction!, offerer, provider);
await signAndSubmitTx(
validListing.unsignedApprovalTransaction!,
offerer,
provider,
);

const signature2 = await signMessage(validListing.typedOrderMessageForSigning, offerer);
const signature2 = await signMessage(
validListing.typedOrderMessageForSigning,
offerer,
);

log('Cretaing new listing to be fulfilled...');

// Submit the order creation request to the order book API
const { result: { id: orderId2 } } = await sdk.createListing({
const {
result: { id: orderId2 },
} = await sdk.createListing({
orderComponents: validListing.orderComponents,
orderHash: validListing.orderHash,
orderSignature: signature2,
Expand All @@ -90,7 +96,9 @@ describe('', () => {
fulfiller.address,
);
await signAndSubmitTx(unsignedFulfillmentTransaction, fulfiller, provider);
log(`Fulfilment transaction sent, waiting for listing ${orderId2} to become FILLED`);
log(
`Fulfilment transaction sent, waiting for listing ${orderId2} to become FILLED`,
);

await waitForOrderToBeOfStatus(sdk, orderId2, OrderStatus.FILLED);
log(`Listing ${orderId2} is now FILLED`);
Expand Down
5 changes: 3 additions & 2 deletions packages/orderbook/src/test/fulfil.e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,15 @@ describe('fulfil order', () => {
const offerer = getOffererWallet(provider);
const fulfiller = getFulfillerWallet(provider);

const localConfigOverrides = getLocalConfigFromEnv();
const sdk = new Orderbook({
baseConfig: {
environment: Environment.SANDBOX,
},
overrides: {
chainName: 'imtbl-zkevm-local',
...localConfigOverrides,
},
}, getLocalConfigFromEnv());
});

const { contract } = await deployTestToken(offerer);
await contract.safeMint(offerer.address);
Expand Down
3 changes: 2 additions & 1 deletion packages/orderbook/src/test/helpers/config.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// eslint-disable-next-line import/no-extraneous-dependencies
import dotenv from 'dotenv';
import { OrderbookModuleConfiguration } from 'config';
import { LOCAL_CHAIN_NAME, OrderbookModuleConfiguration } from 'config';
import { getLocalhostProvider } from './provider';

dotenv.config();
Expand All @@ -16,6 +16,7 @@ export function getLocalConfigFromEnv(): OrderbookModuleConfiguration {

return {
apiEndpoint: process.env.ORDERBOOK_MR_API_URL,
chainName: LOCAL_CHAIN_NAME,
seaportContractAddress: process.env.SEAPORT_CONTRACT_ADDRESS,
zoneContractAddress: process.env.ZONE_CONTRACT_ADDRESS,
provider: getLocalhostProvider(),
Expand Down
7 changes: 3 additions & 4 deletions packages/orderbook/src/test/list.e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ import { TestToken } from './helpers/test-token';
import { waitForOrderToBeOfStatus } from './helpers/order';
import { getLocalConfigFromEnv } from './helpers';

const LOCAL_CHAIN_NAME = 'imtbl-zkevm-local';

async function createListing(
sdk: Orderbook,
token: TestToken,
Expand Down Expand Up @@ -61,14 +59,15 @@ describe('listListings e2e', () => {
const provider = getLocalhostProvider();
const offerer = getOffererWallet(provider);

const localConfigOverrides = getLocalConfigFromEnv();
const sdk = new Orderbook({
baseConfig: {
environment: Environment.SANDBOX,
},
overrides: {
chainName: LOCAL_CHAIN_NAME,
...localConfigOverrides,
},
}, getLocalConfigFromEnv());
});

let token1ContractAddress = '';
let token2ContractAddress = '';
Expand Down
1 change: 0 additions & 1 deletion sdk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
"@jest/globals": "^29.5.0",
"@magic-ext/oidc": "^1.0.1",
"@metamask/detect-provider": "^2.0.0",
"@opensea/seaport-js": "^1.3.0",
"@uniswap/router-sdk": "^1.4.0",
"@uniswap/sdk-core": "^3.0.1",
"@uniswap/v3-sdk": "^3.9.0",
Expand Down
1 change: 0 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4127,7 +4127,6 @@ __metadata:
"@jest/globals": ^29.5.0
"@magic-ext/oidc": ^1.0.1
"@metamask/detect-provider": ^2.0.0
"@opensea/seaport-js": ^1.3.0
"@rollup/plugin-commonjs": ^24.0.1
"@rollup/plugin-json": ^6.0.0
"@rollup/plugin-node-resolve": ^15.0.2
Expand Down

0 comments on commit b2a516b

Please sign in to comment.