Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify isValidProtocol check #1140

Merged
merged 2 commits into from
Aug 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 15 additions & 26 deletions src/sdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ import {
getAssetItemType,
getAddressAfterRemappingSharedStorefrontAddressToLazyMintAdapterAddress,
feesToBasisPoints,
isValidProtocol,
requireValidProtocol,
getWETHAddress,
} from "./utils/utils";

Expand Down Expand Up @@ -158,7 +158,7 @@ export class OpenSeaSDK {
amountInEth: BigNumberish;
accountAddress: string;
}) {
await this._checkAccountIsAvailable(accountAddress);
await this._requireAccountIsAvailable(accountAddress);

const value = parseEther(FixedNumber.from(amountInEth).toString());

Expand Down Expand Up @@ -198,7 +198,7 @@ export class OpenSeaSDK {
amountInEth: BigNumberish;
accountAddress: string;
}) {
await this._checkAccountIsAvailable(accountAddress);
await this._requireAccountIsAvailable(accountAddress);

const amount = parseEther(FixedNumber.from(amountInEth).toString());

Expand Down Expand Up @@ -348,7 +348,7 @@ export class OpenSeaSDK {
expirationTime?: BigNumberish;
paymentTokenAddress?: string;
}): Promise<OrderV2> {
await this._checkAccountIsAvailable(accountAddress);
await this._requireAccountIsAvailable(accountAddress);

if (!asset.tokenId) {
throw new Error("Asset must have a tokenId");
Expand Down Expand Up @@ -460,7 +460,7 @@ export class OpenSeaSDK {
paymentTokenAddress?: string;
buyerAddress?: string;
}): Promise<OrderV2> {
await this._checkAccountIsAvailable(accountAddress);
await this._requireAccountIsAvailable(accountAddress);

if (!asset.tokenId) {
throw new Error("Asset must have a tokenId");
Expand Down Expand Up @@ -562,7 +562,7 @@ export class OpenSeaSDK {
expirationTime?: number | string;
paymentTokenAddress: string;
}): Promise<Offer | null> {
await this._checkAccountIsAvailable(accountAddress);
await this._requireAccountIsAvailable(accountAddress);

const collection = await this.api.getCollection(collectionSlug);
const buildOfferResult = await this.api.buildOffer(
Expand Down Expand Up @@ -666,7 +666,7 @@ export class OpenSeaSDK {
}

/**
* Fullfill or "take" an order for an asset. The order can be either a buy or sell order.
* Fulfill or "take" an order for an asset. The order can be either a buy or sell order.
* @param options
* @param options.order The order to fulfill, a.k.a. "take"
* @param options.accountAddress Address of the wallet taking the offer.
Expand All @@ -689,11 +689,8 @@ export class OpenSeaSDK {
recipientAddress?: string;
domain?: string;
}): Promise<string> {
await this._checkAccountIsAvailable(accountAddress);

if (!isValidProtocol(order.protocolAddress)) {
throw new Error("Unsupported protocol");
}
await this._requireAccountIsAvailable(accountAddress);
requireValidProtocol(order.protocolAddress);

if (order.orderHash) {
const result = await this.api.generateFulfillmentData(
Expand Down Expand Up @@ -778,11 +775,8 @@ export class OpenSeaSDK {
accountAddress: string;
domain?: string;
}) {
await this._checkAccountIsAvailable(accountAddress);

if (!isValidProtocol(order.protocolAddress)) {
throw new Error("Unsupported protocol");
}
await this._requireAccountIsAvailable(accountAddress);
requireValidProtocol(order.protocolAddress);

this._dispatch(EventType.CancelOrder, { orderV2: order, accountAddress });

Expand Down Expand Up @@ -821,9 +815,7 @@ export class OpenSeaSDK {
order: OrderV2;
accountAddress: string;
}): Promise<boolean> {
if (!isValidProtocol(order.protocolAddress)) {
throw new Error("Unsupported protocol");
}
requireValidProtocol(order.protocolAddress);

try {
const isValid = await this.seaport_v1_5
Expand Down Expand Up @@ -954,11 +946,8 @@ export class OpenSeaSDK {
* @throws Error if the order's protocol address is not supported by OpenSea. See {@link isValidProtocol}.
*/
public async approveOrder(order: OrderV2, domain?: string) {
await this._checkAccountIsAvailable(order.maker.address);

if (!isValidProtocol(order.protocolAddress)) {
throw new Error("Unsupported protocol");
}
await this._requireAccountIsAvailable(order.maker.address);
requireValidProtocol(order.protocolAddress);

this._dispatch(EventType.ApproveOrder, {
orderV2: order,
Expand Down Expand Up @@ -1090,7 +1079,7 @@ export class OpenSeaSDK {
*
* @param accountAddress The account address to check is available.
*/
private async _checkAccountIsAvailable(accountAddress: string) {
private async _requireAccountIsAvailable(accountAddress: string) {
const accountAddressChecksummed = ethers.utils.getAddress(accountAddress);
if (
(this._signerOrProvider instanceof Wallet &&
Expand Down
12 changes: 11 additions & 1 deletion src/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,7 @@ export const isTestChain = (chain: Chain): boolean => {
};

/**
* Checks if a protocol address is valid.
* Returns if a protocol address is valid.
* @param protocolAddress The protocol address
*/
export const isValidProtocol = (protocolAddress: string): boolean => {
Expand All @@ -425,6 +425,16 @@ export const isValidProtocol = (protocolAddress: string): boolean => {
return validProtocolAddresses.includes(checkSumAddress);
};

/**
* Throws an error if the protocol address is not valid.
* @param protocolAddress The protocol address
*/
export const requireValidProtocol = (protocolAddress: string) => {
if (!isValidProtocol(protocolAddress)) {
throw new Error("Unsupported protocol");
}
};

/**
* Decodes an encoded string of token IDs into an array of individual token IDs using BigNumber for precise calculations.
*
Expand Down
Loading