Skip to content
This repository has been archived by the owner on Aug 30, 2022. It is now read-only.

Commit

Permalink
Merge branch 'main' into joaquim/use_isbrowser
Browse files Browse the repository at this point in the history
  • Loading branch information
joaquim-verges committed May 23, 2022
2 parents e58d3fc + 7a98040 commit 8f91166
Show file tree
Hide file tree
Showing 17 changed files with 366 additions and 139 deletions.
4 changes: 3 additions & 1 deletion .eslintignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# folders

dist/
.swc/
abis/

# files
8 changes: 3 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@ Here's how to provide your own private key to the SDK to perform transactions wi
// my_script.js
import { ThirdwebSDK } from "@thirdweb-dev/sdk";

/
// load your private key in a secure way (env variable, never commited to git)
const privateKey = process.env.PRIVATE_KEY;
// instantiate the SDK based on your private key, with the desired chain to connect to
Expand Down Expand Up @@ -118,28 +117,27 @@ Easiest way to get started on the frontend is using one of our templates in the

- [Step by step guides and recipes](https://portal.thirdweb.com)
- [Full Api Reference and code examples](https://docs.thirdweb.com/typescript)
-

## Build from source

To build the project:

```bash
```shell
yarn install
yarn build
```

After building, to run the tests (requires a local hardhat node running):

```bash
```shell
yarn test:all
```

OR

If you have make and docker installed you can simply run

```bash
```shell
make test
```

Expand Down
1 change: 1 addition & 0 deletions docs/sdk.claimeligibility.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export declare enum ClaimEligibility
| NoClaimConditionSet | <code>&quot;There is no claim condition set.&quot;</code> | |
| NotEnoughSupply | <code>&quot;There is not enough supply to claim.&quot;</code> | |
| NotEnoughTokens | <code>&quot;There are not enough tokens in the wallet to pay for the claim.&quot;</code> | |
| NoWallet | <code>&quot;No wallet connected.&quot;</code> | |
| Unknown | <code>&quot;No claim conditions found.&quot;</code> | |
| WaitBeforeNextClaimTransaction | <code>&quot;Not enough time since last claim transaction. Please wait.&quot;</code> | |

6 changes: 4 additions & 2 deletions etc/sdk.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ import { ContractInterface } from 'ethers';
import type { ContractTransaction } from 'ethers';
import { ethers } from 'ethers';
import { Event as Event_2 } from 'ethers';
import { EventEmitter2 } from 'eventemitter2';
import EventEmitter from 'eventemitter3';
import type { EventFilter } from 'ethers';
import type { EventFragment } from '@ethersproject/abi';
import { extendShape } from 'zod';
import type { FunctionFragment } from '@ethersproject/abi';
import type { Listener } from '@ethersproject/providers';
import { ListenerFn } from 'eventemitter2';
import { ListenerFn } from 'eventemitter3';
import type { Overrides } from 'ethers';
import type { PayableOverrides } from 'ethers';
import type { PopulatedTransaction } from 'ethers';
Expand Down Expand Up @@ -429,6 +429,8 @@ export enum ClaimEligibility {
// (undocumented)
NotEnoughTokens = "There are not enough tokens in the wallet to pay for the claim.",
// (undocumented)
NoWallet = "No wallet connected.",
// (undocumented)
Unknown = "No claim conditions found.",
// (undocumented)
WaitBeforeNextClaimTransaction = "Not enough time since last claim transaction. Please wait."
Expand Down
2 changes: 2 additions & 0 deletions injected-shims/iife-shims.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// eslint-disable-next-line @typescript-eslint/no-var-requires
window.Buffer = require("buffer/").Buffer;
20 changes: 16 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@thirdweb-dev/sdk",
"version": "2.2.15",
"version": "2.2.16-7",
"description": "The main thirdweb SDK.",
"repository": {
"type": "git",
Expand All @@ -11,15 +11,23 @@
"main": "dist/index.js",
"types": "dist/src/index.d.ts",
"module": "dist/index.mjs",
"browser": "dist/index.js",
"browser": "dist/index.global.js",
"unpkg": "dist/index.global.js",
"exports": {
".": {
"require": "./dist/index.js",
"import": "./dist/index.mjs",
"types": "./dist/src/index.d.ts"
}
},
"files": [
"dist/**/*"
],
"sideEffects": false,
"scripts": {
"clean": "rm -rf dist/",
"dev": "tsdx watch",
"build:lib": "tsup && yarn generate-types",
"build:lib": "patch-package & rm -rf ./dist && tsup && yarn generate-types",
"format": "prettier src/ --write",
"prepublishOnly": "yarn lint && yarn build",
"lint": "eslint src/",
Expand Down Expand Up @@ -56,6 +64,7 @@
"@typescript-eslint/parser": "^5.2.0",
"chai": "^4.3.6",
"deep-equal-in-any-order": "^1.1.18",
"esbuild-plugin-alias": "^0.2.1",
"eslint": "^8",
"eslint-config-prettier": "^8.3.0",
"eslint-plugin-import": "^2.24.2",
Expand All @@ -64,8 +73,10 @@
"eslint-plugin-tsdoc": "^0.2.14",
"esm": "^3.2.25",
"ethers": "^5.6.2",
"events": "^3.3.0",
"hardhat": "2.9.3",
"mocha": "^10.0.0",
"patch-package": "^6.4.7",
"prettier": "^2.4.1",
"ts-enum-util": "^4.0.2",
"ts-mocha": "^10.0.0",
Expand All @@ -80,9 +91,10 @@
"dependencies": {
"@thirdweb-dev/contracts": "2.3.8",
"@web-std/file": "^3.0.0",
"buffer": "^6.0.3",
"cross-fetch": "^3.1.5",
"deep-equal": "^2.0.5",
"eventemitter2": "^6.4.5",
"eventemitter3": "^4.0.7",
"form-data": "^4.0.0",
"keccak256": "^1.0.6",
"merkletreejs": "^0.2.24",
Expand Down
24 changes: 24 additions & 0 deletions patches/util+0.12.4.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
diff --git a/node_modules/util/util.js b/node_modules/util/util.js
index 6eea657..eaa0226 100644
--- a/node_modules/util/util.js
+++ b/node_modules/util/util.js
@@ -614,14 +614,14 @@ exports.promisify = function promisify(original) {
throw new TypeError('The "original" argument must be of type Function');

if (kCustomPromisifiedSymbol && original[kCustomPromisifiedSymbol]) {
- var fn = original[kCustomPromisifiedSymbol];
- if (typeof fn !== 'function') {
+ var _fn = original[kCustomPromisifiedSymbol];
+ if (typeof _fn !== 'function') {
throw new TypeError('The "util.promisify.custom" argument must be of type Function');
}
- Object.defineProperty(fn, kCustomPromisifiedSymbol, {
- value: fn, enumerable: false, writable: false, configurable: true
+ Object.defineProperty(_fn, kCustomPromisifiedSymbol, {
+ value: _fn, enumerable: false, writable: false, configurable: true
});
- return fn;
+ return _fn;
}

function fn() {
2 changes: 1 addition & 1 deletion src/core/classes/contract-events.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ContractWrapper } from "./contract-wrapper";
import { BaseContract, providers } from "ethers";
import { EventType } from "../../constants";
import { ListenerFn } from "eventemitter2";
import { ListenerFn } from "eventemitter3";

/**
* Listen to Contract events in real time
Expand Down
14 changes: 10 additions & 4 deletions src/core/classes/drop-claim-conditions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,6 @@ export class DropClaimConditions<TContract extends DropERC721 | DropERC20> {
quantity: Amount,
addressToCheck?: string,
): Promise<boolean> {
if (addressToCheck === undefined) {
addressToCheck = await this.contractWrapper.getSignerAddress();
}
// TODO switch to use verifyClaim
return (
(await this.getClaimIneligibilityReasons(quantity, addressToCheck))
Expand Down Expand Up @@ -146,7 +143,16 @@ export class DropClaimConditions<TContract extends DropERC721 | DropERC20> {
);

if (addressToCheck === undefined) {
addressToCheck = await this.contractWrapper.getSignerAddress();
try {
addressToCheck = await this.contractWrapper.getSignerAddress();
} catch (err) {
console.warn("failed to get signer address", err);
}
}

// if we have been unable to get a signer address, we can't check eligibility, so return a NoWallet error reason
if (!addressToCheck) {
return [ClaimEligibility.NoWallet];
}

try {
Expand Down
14 changes: 10 additions & 4 deletions src/core/classes/drop-erc1155-claim-conditions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,6 @@ export class DropErc1155ClaimConditions {
quantity: BigNumberish,
addressToCheck?: string,
): Promise<boolean> {
if (addressToCheck === undefined) {
addressToCheck = await this.contractWrapper.getSignerAddress();
}
// TODO switch to use verifyClaim
return (
(
Expand Down Expand Up @@ -150,7 +147,16 @@ export class DropErc1155ClaimConditions {
let claimCondition: ClaimCondition;

if (addressToCheck === undefined) {
addressToCheck = await this.contractWrapper.getSignerAddress();
try {
addressToCheck = await this.contractWrapper.getSignerAddress();
} catch (err) {
console.warn("failed to get signer address", err);
}
}

// if we have been unable to get a signer address, we can't check eligibility, so return a NoWallet error reason
if (!addressToCheck) {
return [ClaimEligibility.NoWallet];
}

try {
Expand Down
5 changes: 2 additions & 3 deletions src/core/classes/erc-1155-enumerable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,8 @@ export class Erc1155Enumerable implements DetectableFeature {
* @returns The NFT metadata for all NFTs in the contract.
*/
public async owned(walletAddress?: string): Promise<EditionMetadataOwner[]> {
const address = walletAddress
? walletAddress
: await this.contractWrapper.getSignerAddress();
const address =
walletAddress || (await this.contractWrapper.getSignerAddress());
const maxId = await this.contractWrapper.readContract.nextTokenIdToMint();
const balances = await this.contractWrapper.readContract.balanceOfBatch(
Array(maxId.toNumber()).fill(address),
Expand Down
6 changes: 3 additions & 3 deletions src/core/classes/erc-721-enumerable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@ export class Erc721Enumerable implements DetectableFeature {
* @param walletAddress - the wallet address to query, defaults to the connected wallet
*/
public async tokenIds(walletAddress?: string): Promise<BigNumber[]> {
const address = walletAddress
? walletAddress
: await this.contractWrapper.getSignerAddress();
const address =
walletAddress || (await this.contractWrapper.getSignerAddress());

const balance = await this.contractWrapper.readContract.balanceOf(address);
const indices = Array.from(Array(balance.toNumber()).keys());
return await Promise.all(
Expand Down
13 changes: 13 additions & 0 deletions src/core/classes/marketplace-auction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,19 @@ export class MarketplaceAuction {
* ```
*/
public async getWinner(listingId: BigNumberish): Promise<string> {
const listing = await this.validateListing(BigNumber.from(listingId));
const offers = await this.contractWrapper.readContract.winningBid(
listingId,
);
const now = BigNumber.from(Math.floor(Date.now() / 1000));
const endTime = BigNumber.from(listing.endTimeInEpochSeconds);

// if we have a winner in the map and the current time is past the endtime of the auction return the address of the winner
if (now.gt(endTime) && offers.offeror !== constants.AddressZero) {
return offers.offeror;
}
// otherwise fall back to query filter things

// TODO this should be via indexer or direct contract call
const closedAuctions = await this.contractWrapper.readContract.queryFilter(
this.contractWrapper.readContract.filters.AuctionClosed(),
Expand Down
4 changes: 2 additions & 2 deletions src/core/classes/rpc-connection-handler.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ethers, Signer, providers } from "ethers";
import { EventEmitter2 } from "eventemitter2";
import EventEmitter from "eventemitter3";
import { getReadOnlyProvider } from "../../constants/urls";
import {
SDKOptions,
Expand All @@ -11,7 +11,7 @@ import { NetworkOrSignerOrProvider } from "../types";
/**
* @internal
*/
export class RPCConnectionHandler extends EventEmitter2 {
export class RPCConnectionHandler extends EventEmitter {
private provider: providers.Provider;
private signer: Signer | undefined;
protected readonly options: SDKOptionsOutput;
Expand Down
2 changes: 2 additions & 0 deletions src/enums/ClaimEligibility.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,7 @@ export enum ClaimEligibility {

NoClaimConditionSet = "There is no claim condition set.",

NoWallet = "No wallet connected.",

Unknown = "No claim conditions found.",
}
54 changes: 47 additions & 7 deletions tsup.config.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,49 @@
import alias from "esbuild-plugin-alias";
import path from "path";
import { defineConfig } from "tsup";

export default defineConfig({
entry: ["src/index.ts"],
sourcemap: true,
clean: true,
minify: true,
format: ["cjs", "esm"],
});
export default defineConfig([
// build normal build
{
name: "package",
entry: ["src/index.ts"],
sourcemap: true,
// we'll just manually "clean" the dist dir before running this (to avoid potential race conditions)
clean: false,
minify: true,
platform: "node",
replaceNodeEnv: true,
globalName: "ThirdwebSDK",
format: ["cjs", "esm"],
},
// also build iife (UMD build for <script> tag use)
{
name: "script",
entry: ["src/index.ts"],
sourcemap: true,
// we'll just manually "clean" the dist dir before running this (to avoid potential race conditions
clean: false,
minify: true,
platform: "browser",
replaceNodeEnv: true,
shims: true,
globalName: "_thirdweb",
format: ["iife"],
keepNames: true,
inject: ["./injected-shims/iife-shims.js"],
// inject globals onto window
banner: {
js: 'window.global=window;window.globalThis=window;window.process={env:{NODE_ENV:"production"}};',
},
// inject ThirdwebSDK into window
footer: { js: "window.ThirdwebSDK = window._thirdweb.ThirdwebSDK;" },
esbuildPlugins: [
alias({
stream: path.resolve(
__dirname,
`node_modules/stream-browserify/index.js`,
),
}),
],
},
]);
Loading

0 comments on commit 8f91166

Please sign in to comment.