Skip to content

Commit

Permalink
fix: Support apiKey from orderbook module when provided (#1668)
Browse files Browse the repository at this point in the history
  • Loading branch information
Sam-Jeston authored Apr 16, 2024
1 parent 4d34ebe commit 0933d64
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 25 deletions.
6 changes: 6 additions & 0 deletions packages/orderbook/src/api-client/api-client-factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,16 @@ export class ImmutableApiClientFactory {
apiEndpoint: string,
private readonly chainName: string,
private readonly seaportAddress: string,
rateLimitingKey?: string,
) {
this.orderbookClient = new OrderBookClient({
// eslint-disable-next-line @typescript-eslint/naming-convention
BASE: apiEndpoint,
// eslint-disable-next-line @typescript-eslint/naming-convention
HEADERS: rateLimitingKey ? {
// eslint-disable-next-line @typescript-eslint/naming-convention
'x-api-key': rateLimitingKey!,
} : undefined,
});
}

Expand Down
27 changes: 18 additions & 9 deletions packages/orderbook/src/config/config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Environment } from '@imtbl/config';
import { Environment, ModuleConfiguration } from '@imtbl/config';
import { providers } from 'ethers';

export const TESTNET_CHAIN_NAME = 'imtbl-zkevm-testnet';
Expand All @@ -20,19 +20,30 @@ export interface OrderbookModuleConfiguration {
provider: providers.JsonRpcProvider;
}

export function getConfiguredProvider(
url: string,
rateLimitingKey?: string,
): providers.JsonRpcProvider {
return new providers.JsonRpcProvider({
url,
headers: rateLimitingKey ? {
// eslint-disable-next-line @typescript-eslint/naming-convention
'x-api-key': rateLimitingKey!,
} : undefined,
});
}

export function getOrderbookConfig(
environment: Environment,
config: ModuleConfiguration<OrderbookOverrides>,
): OrderbookModuleConfiguration | null {
switch (environment) {
switch (config.baseConfig.environment) {
case Environment.SANDBOX:
return {
seaportContractAddress: '0x7d117aA8BD6D31c4fa91722f246388f38ab1942c',
zoneContractAddress: '0x8831867E347AB87FA30199C5B695F0A31604Bb52',
apiEndpoint: 'https://api.sandbox.immutable.com',
chainName: TESTNET_CHAIN_NAME,
provider: new providers.JsonRpcProvider(
'https://rpc.testnet.immutable.com',
),
provider: getConfiguredProvider('https://rpc.testnet.immutable.com', config.baseConfig.rateLimitingKey),
};
// not yet deployed
case Environment.PRODUCTION:
Expand All @@ -41,9 +52,7 @@ export function getOrderbookConfig(
zoneContractAddress: '0x00338b92Bec262078B3e49BF12bbEA058916BF91',
apiEndpoint: 'https://api.immutable.com',
chainName: MAINNET_CHAIN_NAME,
provider: new providers.JsonRpcProvider(
'https://rpc.immutable.com',
),
provider: getConfiguredProvider('https://rpc.immutable.com', config.baseConfig.rateLimitingKey),
};
default:
return null;
Expand Down
11 changes: 7 additions & 4 deletions packages/orderbook/src/orderbook.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ModuleConfiguration } from '@imtbl/config';
import { providers } from 'ethers';
import { ImmutableApiClient, ImmutableApiClientFactory } from './api-client';
import {
getConfiguredProvider,
getOrderbookConfig,
OrderbookModuleConfiguration,
OrderbookOverrides,
Expand Down Expand Up @@ -49,16 +49,17 @@ export class Orderbook {
private orderbookConfig: OrderbookModuleConfiguration;

constructor(config: ModuleConfiguration<OrderbookOverrides>) {
const obConfig = getOrderbookConfig(config.baseConfig.environment);
const obConfig = getOrderbookConfig(config);

const finalConfig: OrderbookModuleConfiguration = {
...obConfig,
...config.overrides,
} as OrderbookModuleConfiguration;

if (config.overrides?.jsonRpcProviderUrl) {
finalConfig.provider = new providers.JsonRpcProvider(
config.overrides.jsonRpcProviderUrl,
finalConfig.provider = getConfiguredProvider(
config.overrides?.jsonRpcProviderUrl!,
config.baseConfig.rateLimitingKey,
);
}

Expand All @@ -79,6 +80,7 @@ export class Orderbook {
apiEndpoint,
chainName,
this.orderbookConfig.seaportContractAddress,
config.baseConfig.rateLimitingKey,
).create();

const seaportLibFactory = new SeaportLibFactory(
Expand All @@ -90,6 +92,7 @@ export class Orderbook {
this.orderbookConfig.provider,
this.orderbookConfig.seaportContractAddress,
this.orderbookConfig.zoneContractAddress,
config.baseConfig.rateLimitingKey,
);
}

Expand Down
14 changes: 10 additions & 4 deletions packages/orderbook/src/seaport/seaport-lib-factory.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Seaport as SeaportLib } from '@opensea/seaport-js';
import { JsonRpcProvider, JsonRpcSigner } from 'ethers-v6';
import { FetchRequest, JsonRpcProvider, JsonRpcSigner } from 'ethers-v6';
import { providers } from 'ethers';
import { SEAPORT_CONTRACT_VERSION_V1_5 } from './constants';

Expand All @@ -11,8 +11,14 @@ export type SeaportVersion =
// safely instantiate a V6 provider for the V5 provider URL.
function convertToV6Provider(
provider: providers.JsonRpcProvider,
rateLimitingKey?: string,
): JsonRpcProvider {
const overwrittenProvider = new JsonRpcProvider(provider.connection.url);
const fetch = new FetchRequest(provider.connection.url);
if (rateLimitingKey) {
fetch.setHeader('x-api-key', rateLimitingKey);
}

const overwrittenProvider = new JsonRpcProvider(fetch);

// Need to override the getSigner method to mimic V5 behaviour
overwrittenProvider.getSigner = async function getSigner(
Expand Down Expand Up @@ -47,10 +53,10 @@ export class SeaportLibFactory {
private readonly provider: providers.JsonRpcProvider,
) { }

create(orderSeaportVersion?: SeaportVersion, orderSeaportAddress?: string): SeaportLib {
create(orderSeaportAddress?: string, rateLimitingKey?: string): SeaportLib {
const seaportContractAddress = orderSeaportAddress ?? this.defaultSeaportContractAddress;

return new SeaportLib(convertToV6Provider(this.provider), {
return new SeaportLib(convertToV6Provider(this.provider, rateLimitingKey), {
balanceAndApprovalChecksOnOrderCreation: true,
overrides: {
contractAddress: seaportContractAddress,
Expand Down
11 changes: 3 additions & 8 deletions packages/orderbook/src/seaport/seaport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import {
SEAPORT_CONTRACT_VERSION_V1_5,
} from './constants';
import { getOrderComponentsFromMessage } from './components';
import { SeaportLibFactory, SeaportVersion } from './seaport-lib-factory';
import { SeaportLibFactory } from './seaport-lib-factory';
import { prepareTransaction } from './transaction';
import { mapImmutableOrderToSeaportOrderComponents } from './map-to-seaport-order';

Expand All @@ -40,6 +40,7 @@ export class Seaport {
private provider: providers.JsonRpcProvider,
private seaportContractAddress: string,
private zoneContractAddress: string,
private rateLimitingKey?: string,
) {}

async prepareSeaportOrder(
Expand Down Expand Up @@ -317,13 +318,7 @@ export class Seaport {

private getSeaportLib(order?: Order): SeaportLib {
const seaportAddress = order?.protocol_data?.seaport_address ?? this.seaportContractAddress;

const seaportVersion: SeaportVersion = SEAPORT_CONTRACT_VERSION_V1_5;
// if (order?.protocol_data?.seaport_version === SEAPORT_CONTRACT_VERSION_V1_5) {
// seaportVersion = SEAPORT_CONTRACT_VERSION_V1_5;
// }

return this.seaportLibFactory.create(seaportVersion, seaportAddress);
return this.seaportLibFactory.create(seaportAddress, this.rateLimitingKey);
}

private static getExpirationISOTimeFromExtraData(extraData: string): string {
Expand Down

0 comments on commit 0933d64

Please sign in to comment.