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

more flexible API for setting batch claim conditions on edition drops #481

Merged
merged 2 commits into from
Jun 23, 2022
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
16 changes: 16 additions & 0 deletions docs/sdk.claimconditionsfortoken.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->

[Home](./index.md) &gt; [@thirdweb-dev/sdk](./sdk.md) &gt; [ClaimConditionsForToken](./sdk.claimconditionsfortoken.md)

## ClaimConditionsForToken type

<b>Signature:</b>

```typescript
export declare type ClaimConditionsForToken = {
tokenId: BigNumberish;
claimConditions: ClaimConditionInput[];
};
```
<b>References:</b> [ClaimConditionInput](./sdk.claimconditioninput.md)

2 changes: 1 addition & 1 deletion docs/sdk.droperc1155claimconditions.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,6 @@ export declare class DropErc1155ClaimConditions
| [getAll(tokenId)](./sdk.droperc1155claimconditions.getall.md) | | Get all the claim conditions |
| [getClaimIneligibilityReasons(tokenId, quantity, addressToCheck)](./sdk.droperc1155claimconditions.getclaimineligibilityreasons.md) | | For any claim conditions that a particular wallet is violating, this function returns human-readable information about the breaks in the condition that can be used to inform the user. |
| [set(tokenId, claimConditionInputs, resetClaimEligibilityForAll)](./sdk.droperc1155claimconditions.set.md) | | Set claim conditions on a single NFT |
| [setBatch(tokenIds, claimConditionInputs, resetClaimEligibilityForAll)](./sdk.droperc1155claimconditions.setbatch.md) | | Set claim conditions on multiple NFTs at once |
| [setBatch(claimConditionsForToken, resetClaimEligibilityForAll)](./sdk.droperc1155claimconditions.setbatch.md) | | Set claim conditions on multiple NFTs at once |
| [update(tokenId, index, claimConditionInput)](./sdk.droperc1155claimconditions.update.md) | | Update a single claim condition with new data. |

34 changes: 18 additions & 16 deletions docs/sdk.droperc1155claimconditions.setbatch.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Set claim conditions on multiple NFTs at once
<b>Signature:</b>

```typescript
setBatch(tokenIds: BigNumberish[], claimConditionInputs: ClaimConditionInput[], resetClaimEligibilityForAll?: boolean): Promise<{
setBatch(claimConditionsForToken: ClaimConditionsForToken[], resetClaimEligibilityForAll?: boolean): Promise<{
receipt: ethers.providers.TransactionReceipt;
}>;
```
Expand All @@ -18,8 +18,7 @@ setBatch(tokenIds: BigNumberish[], claimConditionInputs: ClaimConditionInput[],

| Parameter | Type | Description |
| --- | --- | --- |
| tokenIds | BigNumberish\[\] | the token ids to set the claim conditions on |
| claimConditionInputs | [ClaimConditionInput](./sdk.claimconditioninput.md)<!-- -->\[\] | The claim conditions |
| claimConditionsForToken | [ClaimConditionsForToken](./sdk.claimconditionsfortoken.md)<!-- -->\[\] | The claim conditions for each NFT |
| resetClaimEligibilityForAll | boolean | <i>(Optional)</i> Whether to reset the state of who already claimed NFTs previously |

<b>Returns:</b>
Expand All @@ -34,22 +33,25 @@ Sets the claim conditions that need to be fulfilled by users to claim the given


```javascript
const presaleStartTime = new Date();
const publicSaleStartTime = new Date(Date.now() + 60 * 60 * 24 * 1000);
const claimConditions = [
const claimConditionsForTokens = [
{
startTime: presaleStartTime, // start the presale now
maxQuantity: 2, // limit how many mints for this presale
price: 0.01, // presale price
snapshot: ['0x...', '0x...'], // limit minting to only certain addresses
tokenId: 0,
claimConditions: [{
startTime: new Date(), // start the claim phase now
maxQuantity: 2, // limit how many mints for this tokenId
price: 0.01, // price for this tokenId
snapshot: ['0x...', '0x...'], // limit minting to only certain addresses
}]
},
{
startTime: publicSaleStartTime, // 24h after presale, start public sale
price: 0.08, // public sale price
}
]);
tokenId: 1,
claimConditions: [{
startTime: new Date(),
price: 0.08, // different price for this tokenId
}]
},
];

const tokenIds = [0,1,2]; // the ids of the NFTs to set claim conditions on
await dropContract.claimConditions.setBatch(tokenIds, claimConditions);
await dropContract.claimConditions.setBatch(claimConditionsForTokens);
```

1 change: 1 addition & 0 deletions docs/sdk.md
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@
| [BufferOrStringWithName](./sdk.bufferorstringwithname.md) | |
| [ClaimCondition](./sdk.claimcondition.md) | Represents a claim condition fetched from the SDK |
| [ClaimConditionInput](./sdk.claimconditioninput.md) | Input model to create a claim condition with optional snapshot of wallets |
| [ClaimConditionsForToken](./sdk.claimconditionsfortoken.md) | |
| [ClaimVerification](./sdk.claimverification.md) | |
| [ContractEvent](./sdk.contractevent.md) | |
| [ContractForContractType](./sdk.contractforcontracttype.md) | |
Expand Down
8 changes: 7 additions & 1 deletion etc/sdk.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,12 @@ export const ClaimConditionOutputSchema: z.ZodObject<z.extendShape<{
waitInSeconds: string | number | bigint | BigNumber;
}>;

// @public (undocumented)
export type ClaimConditionsForToken = {
tokenId: BigNumberish;
claimConditions: ClaimConditionInput[];
};

// @public (undocumented)
export enum ClaimEligibility {
// (undocumented)
Expand Down Expand Up @@ -912,7 +918,7 @@ export class DropErc1155ClaimConditions {
getAll(tokenId: BigNumberish): Promise<ClaimCondition[]>;
getClaimIneligibilityReasons(tokenId: BigNumberish, quantity: BigNumberish, addressToCheck?: string): Promise<ClaimEligibility[]>;
set(tokenId: BigNumberish, claimConditionInputs: ClaimConditionInput[], resetClaimEligibilityForAll?: boolean): Promise<TransactionResult>;
setBatch(tokenIds: BigNumberish[], claimConditionInputs: ClaimConditionInput[], resetClaimEligibilityForAll?: boolean): Promise<{
setBatch(claimConditionsForToken: ClaimConditionsForToken[], resetClaimEligibilityForAll?: boolean): Promise<{
receipt: ethers.providers.TransactionReceipt;
}>;
update(tokenId: BigNumberish, index: number, claimConditionInput: ClaimConditionInput): Promise<TransactionResult>;
Expand Down
4 changes: 3 additions & 1 deletion src/common/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,9 @@ export async function convertToTWError(
// not sure what this is, just throw it back
return error;
}
const reason = parseMessageParts(/.*?"message[^a-zA-Z0-9]*([^"\\]*).*?/, raw);
const reason =
error.reason ||
parseMessageParts(/.*?"message[^a-zA-Z0-9]*([^"\\]*).*?/, raw);
const data = parseMessageParts(/.*?"data[^a-zA-Z0-9]*([^"\\]*).*?/, raw);
const rpcUrl = parseMessageParts(/.*?"url[^a-zA-Z0-9]*([^"\\]*).*?/, raw);
let from = parseMessageParts(/.*?"from[^a-zA-Z0-9]*([^"\\]*).*?/, raw);
Expand Down
2 changes: 1 addition & 1 deletion src/contracts/smart-contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ export class SmartContract<
// parse last arg as tx options if present
let txOptions: CallOverrides | undefined;
try {
if (args.length > 1 && typeof args[args.length - 1] === "object") {
if (args.length > 0 && typeof args[args.length - 1] === "object") {
const last = args[args.length - 1];
txOptions = CallOverrideSchema.parse(last);
// if call overrides found, remove it from args array
Expand Down
84 changes: 51 additions & 33 deletions src/core/classes/drop-erc1155-claim-conditions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ import { DropERC1155, IERC20, IERC20__factory } from "contracts";
import { BigNumber, BigNumberish, constants, ethers } from "ethers";
import { isNativeToken } from "../../common/currency";
import { ContractWrapper } from "./contract-wrapper";
import { ClaimCondition, ClaimConditionInput } from "../../types";
import {
ClaimCondition,
ClaimConditionInput,
ClaimConditionsForToken,
} from "../../types";
import deepEqual from "fast-deep-equal";
import { ClaimEligibility } from "../../enums";
import { TransactionResult } from "../index";
Expand Down Expand Up @@ -302,8 +306,12 @@ export class DropErc1155ClaimConditions {
resetClaimEligibilityForAll = false,
): Promise<TransactionResult> {
return this.setBatch(
[tokenId],
claimConditionInputs,
[
{
tokenId,
claimConditions: claimConditionInputs,
},
],
resetClaimEligibilityForAll,
);
}
Expand All @@ -315,47 +323,57 @@ export class DropErc1155ClaimConditions {
*
* @example
* ```javascript
* const presaleStartTime = new Date();
* const publicSaleStartTime = new Date(Date.now() + 60 * 60 * 24 * 1000);
* const claimConditions = [
* const claimConditionsForTokens = [
* {
* startTime: presaleStartTime, // start the presale now
* maxQuantity: 2, // limit how many mints for this presale
* price: 0.01, // presale price
* snapshot: ['0x...', '0x...'], // limit minting to only certain addresses
* tokenId: 0,
* claimConditions: [{
* startTime: new Date(), // start the claim phase now
* maxQuantity: 2, // limit how many mints for this tokenId
* price: 0.01, // price for this tokenId
* snapshot: ['0x...', '0x...'], // limit minting to only certain addresses
* }]
* },
* {
* startTime: publicSaleStartTime, // 24h after presale, start public sale
* price: 0.08, // public sale price
* }
* ]);
* tokenId: 1,
* claimConditions: [{
* startTime: new Date(),
* price: 0.08, // different price for this tokenId
* }]
* },
* ];
*
* const tokenIds = [0,1,2]; // the ids of the NFTs to set claim conditions on
* await dropContract.claimConditions.setBatch(tokenIds, claimConditions);
* await dropContract.claimConditions.setBatch(claimConditionsForTokens);
* ```
*
* @param tokenIds - the token ids to set the claim conditions on
* @param claimConditionInputs - The claim conditions
* @param claimConditionsForToken - The claim conditions for each NFT
* @param resetClaimEligibilityForAll - Whether to reset the state of who already claimed NFTs previously
*/
public async setBatch(
tokenIds: BigNumberish[],
claimConditionInputs: ClaimConditionInput[],
claimConditionsForToken: ClaimConditionsForToken[],
resetClaimEligibilityForAll = false,
) {
// process inputs
const { snapshotInfos, sortedConditions } =
await processClaimConditionInputs(
claimConditionInputs,
0,
this.contractWrapper.getProvider(),
this.storage,
);

const merkleInfo: { [key: string]: string } = {};
snapshotInfos.forEach((s) => {
merkleInfo[s.merkleRoot] = s.snapshotUri;
});
const processedClaimConditions = await Promise.all(
claimConditionsForToken.map(async ({ tokenId, claimConditions }) => {
// process inputs
const { snapshotInfos, sortedConditions } =
await processClaimConditionInputs(
claimConditions,
0,
this.contractWrapper.getProvider(),
this.storage,
);

snapshotInfos.forEach((s) => {
merkleInfo[s.merkleRoot] = s.snapshotUri;
});
return {
tokenId,
sortedConditions,
};
}),
);

const metadata = await this.metadata.get();
const encoded = [];

Expand All @@ -382,7 +400,7 @@ export class DropErc1155ClaimConditions {
);
}

tokenIds.forEach((tokenId) => {
processedClaimConditions.forEach(({ tokenId, sortedConditions }) => {
encoded.push(
this.contractWrapper.readContract.interface.encodeFunctionData(
"setClaimConditions",
Expand Down
7 changes: 6 additions & 1 deletion src/types/claim-conditions/claim-conditions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
ClaimConditionOutputSchema,
PartialClaimConditionInputSchema,
} from "../../schema/contracts/common/claim-conditions";
import { BigNumber, BytesLike, CallOverrides } from "ethers";
import { BigNumber, BigNumberish, BytesLike, CallOverrides } from "ethers";

/**
* Represents a claim condition fetched from the SDK
Expand Down Expand Up @@ -56,3 +56,8 @@ export type ClaimVerification = {
price: BigNumber;
currencyAddress: string;
};

export type ClaimConditionsForToken = {
tokenId: BigNumberish;
claimConditions: ClaimConditionInput[];
};
35 changes: 25 additions & 10 deletions test/edition-drop.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -387,16 +387,31 @@ describe("Edition Drop Contract", async () => {
description: "test2",
},
]);
await bdContract.claimConditions.setBatch(
[0, 1],
[
{
price: 1,
},
],
);
await bdContract.claim("0", 1);
await bdContract.claim("1", 1);
await bdContract.claimConditions.setBatch([
{
tokenId: 0,
claimConditions: [
{
price: 1,
},
],
},
{
tokenId: 1,
claimConditions: [
{
price: 0.1,
maxQuantity: 1,
},
],
},
]);
await bdContract.claim("0", 2);
try {
await bdContract.claim("1", 2);
} catch (e) {
expectError(e, "exceed max mint supply");
}
});

describe("eligibility", () => {
Expand Down