Skip to content

Commit

Permalink
throw friendly error if account is not available through wallet or pr…
Browse files Browse the repository at this point in the history
…ovider (#1104)
  • Loading branch information
ryanio committed Jul 18, 2023
1 parent 930757e commit d753bf3
Show file tree
Hide file tree
Showing 2 changed files with 117 additions and 5 deletions.
42 changes: 38 additions & 4 deletions src/sdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,8 @@ export class OpenSeaSDK {
amountInEth: BigNumberish;
accountAddress: string;
}) {
await this._checkAccountIsAvailable(accountAddress);

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

this._dispatch(EventType.WrapEth, { accountAddress, amount: value });
Expand Down Expand Up @@ -193,6 +195,8 @@ export class OpenSeaSDK {
amountInEth: BigNumberish;
accountAddress: string;
}) {
await this._checkAccountIsAvailable(accountAddress);

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

this._dispatch(EventType.UnwrapWeth, { accountAddress, amount });
Expand Down Expand Up @@ -350,6 +354,8 @@ export class OpenSeaSDK {
expirationTime?: BigNumberish;
paymentTokenAddress?: string;
}): Promise<OrderV2> {
await this._checkAccountIsAvailable(accountAddress);

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

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

const collection = await this.api.getCollection(collectionSlug);
const buildOfferResult = await this.api.buildOffer(
accountAddress,
Expand Down Expand Up @@ -614,10 +624,6 @@ export class OpenSeaSDK {
accountAddress: string;
domain?: string;
}): Promise<string> {
if (!isValidProtocol(order.protocolAddress)) {
throw new Error("Unsupported protocol");
}

if (!order.taker?.address) {
throw new Error(
"Order is not a private listing must have a taker address",
Expand Down Expand Up @@ -669,6 +675,8 @@ export class OpenSeaSDK {
recipientAddress?: string;
domain?: string;
}): Promise<string> {
await this._checkAccountIsAvailable(accountAddress);

if (!isValidProtocol(order.protocolAddress)) {
throw new Error("Unsupported protocol");
}
Expand Down Expand Up @@ -752,6 +760,8 @@ export class OpenSeaSDK {
accountAddress: string;
domain?: string;
}) {
await this._checkAccountIsAvailable(accountAddress);

if (!isValidProtocol(order.protocolAddress)) {
throw new Error("Unsupported protocol");
}
Expand Down Expand Up @@ -915,6 +925,8 @@ export class OpenSeaSDK {
* @returns Transaction hash of the approval transaction
*/
public async approveOrder(order: OrderV2, domain?: string) {
await this._checkAccountIsAvailable(order.maker.address);

if (!isValidProtocol(order.protocolAddress)) {
throw new Error("Unsupported protocol");
}
Expand Down Expand Up @@ -1044,6 +1056,28 @@ export class OpenSeaSDK {
this._emitter.emit(event, data);
}

/**
* Throws an error if an account is not available through the provider.
*
* @param accountAddress The account address to check is available.
*/
private async _checkAccountIsAvailable(accountAddress: string) {
const accountAddressChecksummed = ethers.utils.getAddress(accountAddress);
if (
(this._signerOrProvider as Wallet).address !==
accountAddressChecksummed &&
!(
await (
this._signerOrProvider as providers.JsonRpcProvider
).listAccounts()
).includes(accountAddressChecksummed)
) {
throw new Error(
`Specified accountAddress is not available through wallet or provider: ${accountAddressChecksummed}`,
);
}
}

private async _confirmTransaction(
transactionHash: string,
event: EventType,
Expand Down
80 changes: 79 additions & 1 deletion test/sdk/misc.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { assert } from "chai";
import { assert, expect } from "chai";
import { ethers } from "ethers";
import { suite, test } from "mocha";
import {
SHARED_STOREFRONT_LAZY_MINT_ADAPTER_CROSS_CHAIN_ADDRESS,
Expand Down Expand Up @@ -72,4 +73,81 @@ suite("SDK: misc", () => {
SHARED_STOREFRONT_LAZY_MINT_ADAPTER_CROSS_CHAIN_ADDRESS,
);
});

test("Should throw an error when using methods that need a provider or wallet with the accountAddress", async () => {
const wallet = ethers.Wallet.createRandom();
const accountAddress = wallet.address;
const expectedErrorMessage = `Specified accountAddress is not available through wallet or provider: ${accountAddress}`;

/* eslint-disable @typescript-eslint/no-explicit-any */
try {
await client.wrapEth({ amountInEth: "0.1", accountAddress });
throw new Error("should have thrown");
} catch (e: any) {
expect(e.message).to.equal(expectedErrorMessage);
}

try {
await client.unwrapWeth({ amountInEth: "0.1", accountAddress });
throw new Error("should have thrown");
} catch (e: any) {
expect(e.message).to.equal(expectedErrorMessage);
}

const asset = {} as any;

try {
await client.createBuyOrder({ asset, startAmount: 1, accountAddress });
throw new Error("should have thrown");
} catch (e: any) {
expect(e.message).to.equal(expectedErrorMessage);
}

try {
await client.createSellOrder({ asset, startAmount: 1, accountAddress });
throw new Error("should have thrown");
} catch (e: any) {
expect(e.message).to.equal(expectedErrorMessage);
}

try {
await client.createCollectionOffer({
collectionSlug: "",
amount: 1,
quantity: 1,
paymentTokenAddress: "",
accountAddress,
});
throw new Error("should have thrown");
} catch (e: any) {
expect(e.message).to.equal(expectedErrorMessage);
}

const order = {} as any;

try {
await client.fulfillOrder({ order, accountAddress });
throw new Error("should have thrown");
} catch (e: any) {
expect(e.message).to.equal(expectedErrorMessage);
}

try {
await client.cancelOrder({ order, accountAddress });
throw new Error("should have thrown");
} catch (e: any) {
expect(e.message).to.equal(expectedErrorMessage);
}

try {
await client.approveOrder({
...order,
maker: { address: accountAddress },
});
throw new Error("should have thrown");
} catch (e: any) {
expect(e.message).to.equal(expectedErrorMessage);
}
/* eslint-enable @typescript-eslint/no-explicit-any */
});
});

0 comments on commit d753bf3

Please sign in to comment.