Skip to content

Commit

Permalink
upgrade to ethers v6 (#1311)
Browse files Browse the repository at this point in the history
* upgrade to ethers v6

* lint
  • Loading branch information
ryanio authored Dec 7, 2023
1 parent 0b01022 commit fc6864b
Show file tree
Hide file tree
Showing 19 changed files with 1,267 additions and 3,346 deletions.
2 changes: 1 addition & 1 deletion developerDocs/getting-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ const balance = await openseaSDK.getBalance({
asset,
});

const ownsKitty = balance.gt(0);
const ownsKitty = balance > 0n;
```

### Making Offers
Expand Down
6 changes: 2 additions & 4 deletions developerDocs/quick-start.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,9 @@ import { ethers } from "ethers";
import { OpenSeaSDK, Chain } from "opensea-js";

// This example provider won't let you make transactions, only read-only calls:
const provider = new ethers.providers.JsonRpcProvider(
"https://mainnet.infura.io",
);
const provider = new ethers.JsonRpcProvider("https://mainnet.infura.io");

const openseaSDK = new OpenSeaSDK(walletWithProvider, {
const openseaSDK = new OpenSeaSDK(provider, {
chain: Chain.Mainnet,
apiKey: YOUR_API_KEY,
});
Expand Down
4,326 changes: 1,130 additions & 3,196 deletions package-lock.json

Large diffs are not rendered by default.

14 changes: 6 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,8 @@
"src"
],
"scripts": {
"abi-type-gen": "typechain --target=ethers-v5 src/abi/*.json --out-dir=src/typechain/contracts",
"build": "npm run abi-type-gen && tsc -p tsconfig.build.json && npm run bundle",
"bundle": "browserify lib/index.js -o lib/bundle.js",
"abi-type-gen": "typechain --target=ethers-v6 src/abi/*.json --out-dir=src/typechain/contracts",
"build": "npm run abi-type-gen && tsc -p tsconfig.build.json",
"check-types": "tsc --noEmit --project tsconfig.json",
"coverage-report": "nyc report --reporter=lcov",
"docs-build": "typedoc",
Expand All @@ -39,18 +38,17 @@
},
"types": "lib/index.d.ts",
"dependencies": {
"@opensea/seaport-js": "^2.0.8",
"ethers": "^5.7.2"
"@opensea/seaport-js": "^3.0.0",
"ethers": "^6.9.0"
},
"devDependencies": {
"@typechain/ethers-v5": "^11.0.0",
"@typechain/ethers-v6": "^0.5.1",
"@types/chai": "4.3.11",
"@types/chai-as-promised": "^7.1.5",
"@types/mocha": "^10.0.0",
"@types/node": "^18.0.0",
"@typescript-eslint/eslint-plugin": "^6.0.0",
"@typescript-eslint/parser": "^6.0.0",
"browserify": "^17.0.0",
"chai": "4.3.10",
"chai-as-promised": "^7.1.1",
"concurrently": "^8.2.0",
Expand All @@ -60,7 +58,7 @@
"eslint-config-prettier": "^9.0.0",
"eslint-import-resolver-typescript": "^3.0.0",
"eslint-plugin-import": "^2.25.3",
"eslint-plugin-prettier": "^5.0.0-alpha.2",
"eslint-plugin-prettier": "^5.0.1",
"husky": "^8.0.3",
"lint-staged": "^15.0.0",
"mocha": "^10.0.0",
Expand Down
41 changes: 21 additions & 20 deletions src/api/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -559,7 +559,7 @@ export class OpenSeaAPI {
public async get<T>(apiPath: string, query: object = {}): Promise<T> {
const qs = this.objectToSearchParams(query);
const url = `${this.apiBaseUrl}${apiPath}?${qs}`;
return await this._fetch({ url });
return await this._fetch(url);
}

/**
Expand All @@ -572,14 +572,10 @@ export class OpenSeaAPI {
public async post<T>(
apiPath: string,
body?: object,
opts?: ethers.utils.ConnectionInfo,
opts?: object,
): Promise<T> {
const options = {
url: `${this.apiBaseUrl}${apiPath}`,
...opts,
};

return await this._fetch(options, body);
const url = `${this.apiBaseUrl}${apiPath}`;
return await this._fetch(url, opts, body);
}

private objectToSearchParams(params: object = {}) {
Expand All @@ -601,24 +597,29 @@ export class OpenSeaAPI {
* @param opts ethers ConnectionInfo, similar to Fetch API
* @param body Optional body to send. If set, will POST, otherwise GET
*/
private async _fetch(opts: ethers.utils.ConnectionInfo, body?: object) {
const headers = {
private async _fetch(url: string, headers?: object, body?: object) {
headers = {
"x-app-id": "opensea-js",
...(this.apiKey ? { "X-API-KEY": this.apiKey } : {}),
...opts.headers,
};
const req = {
...opts,
headers,
...headers,
};

const req = new ethers.FetchRequest(url);
for (const [key, value] of Object.entries(headers)) {
req.setHeader(key, value);
}
if (body) {
req.body = body;
}

this.logger(
`Sending request: ${opts.url} ${JSON.stringify(req).slice(0, 200)}...`,
`Sending request: ${url} ${JSON.stringify({
request: req,
headers: req.headers,
})}`,
);

return await ethers.utils.fetchJson(
req,
body ? JSON.stringify(body) : undefined,
);
const response = await req.send();
return response.bodyJson;
}
}
2 changes: 1 addition & 1 deletion src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export const MAX_EXPIRATION_MONTHS = 1;
export const API_BASE_MAINNET = "https://api.opensea.io";
export const API_BASE_TESTNET = "https://testnets-api.opensea.io";

export const DEFAULT_ZONE = ethers.constants.AddressZero;
export const DEFAULT_ZONE = ethers.ZeroAddress;
export const ENGLISH_AUCTION_ZONE_MAINNETS =
"0x110b2b128a9ed1be5ef3232d8e4e41640df5c2cd";
export const ENGLISH_AUCTION_ZONE_TESTNETS =
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { OpenSeaSDK } from "./sdk";
* ```ts
* import { ethers } from 'ethers'
* import { OpenSeaSDK, Chain } from 'opensea-js'
* const provider = new ethers.providers.JsonRpcProvider('https://mainnet.infura.io')
* const provider = new ethers.JsonRpcProvider('https://mainnet.infura.io')
* const client = new OpenSeaSDK(provider, {
* chain: Chain.Mainnet
* })
Expand Down
9 changes: 4 additions & 5 deletions src/orders/privateListings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import {
} from "@opensea/seaport-js/lib/types";
import { isCurrencyItem } from "@opensea/seaport-js/lib/utils/item";
import { generateRandomSalt } from "@opensea/seaport-js/lib/utils/order";
import { BigNumber } from "ethers";

export const getPrivateListingConsiderations = (
offer: CreateInputItem[],
Expand Down Expand Up @@ -42,12 +41,12 @@ export const constructPrivateListingCounterOrder = (

const { aggregatedStartAmount, aggregatedEndAmount } = paymentItems.reduce(
({ aggregatedStartAmount, aggregatedEndAmount }, item) => ({
aggregatedStartAmount: aggregatedStartAmount.add(item.startAmount),
aggregatedEndAmount: aggregatedEndAmount.add(item.endAmount),
aggregatedStartAmount: aggregatedStartAmount + BigInt(item.startAmount),
aggregatedEndAmount: aggregatedEndAmount + BigInt(item.endAmount),
}),
{
aggregatedStartAmount: BigNumber.from(0),
aggregatedEndAmount: BigNumber.from(0),
aggregatedStartAmount: 0n,
aggregatedEndAmount: 0n,
},
);

Expand Down
5 changes: 2 additions & 3 deletions src/orders/types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { BasicOrderParametersStruct } from "@opensea/seaport-js/lib/typechain-types/seaport_v1_4/contracts/Seaport";
import { BasicOrderParametersStruct } from "@opensea/seaport-js/lib/typechain-types/seaport_v1_5/contracts/Seaport";
import { AdvancedOrder, OrderWithCounter } from "@opensea/seaport-js/lib/types";
import { BigNumber } from "ethers";
import { OpenSeaAccount, OrderSide } from "../types";

// Protocol data
Expand Down Expand Up @@ -41,7 +40,7 @@ export type OrderV2 = {
/** The contract address of the protocol. */
protocolAddress: string;
/** The current price of the order. */
currentPrice: BigNumber;
currentPrice: bigint;
/** The maker fees for the order. */
makerFees: OrderFee[];
/** The taker fees for the order. */
Expand Down
3 changes: 1 addition & 2 deletions src/orders/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { CROSS_CHAIN_SEAPORT_V1_5_ADDRESS } from "@opensea/seaport-js/lib/constants";
import { BigNumber } from "ethers";
import {
OrdersQueryOptions,
OrderV2,
Expand Down Expand Up @@ -118,7 +117,7 @@ export const deserializeOrder = (order: SerializedOrderV2): OrderV2 => {
taker: order.taker ? accountFromJSON(order.taker) : null,
protocolData: order.protocol_data,
protocolAddress: order.protocol_address,
currentPrice: BigNumber.from(order.current_price),
currentPrice: BigInt(order.current_price),
makerFees: order.maker_fees.map(({ account, basis_points }) => ({
account: accountFromJSON(account),
basisPoints: basis_points,
Expand Down
Loading

0 comments on commit fc6864b

Please sign in to comment.