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

Commit 05e7034

Browse files
consistent namespacing for erc20 drop (#562)
* consistent namespacing for erc20 drop * fix tests
1 parent a55ae71 commit 05e7034

File tree

4 files changed

+135
-76
lines changed

4 files changed

+135
-76
lines changed

src/contracts/token-drop.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import { Amount, CurrencyValue } from "../types";
1717
import { DropErc20ContractSchema } from "../schema/contracts/drop-erc20";
1818
import { getRoleHash } from "../common";
1919
import { Erc20Burnable } from "../core/classes/erc-20-burnable";
20-
import { Erc20Droppable } from "../core/classes/erc-20-droppable";
20+
import { Erc20Claimable } from "../core/classes/erc-20-claimable";
2121

2222
/**
2323
* Create a Drop contract for a standard crypto token or cryptocurrency.
@@ -42,7 +42,7 @@ export class TokenDrop extends Erc20<DropERC20> {
4242
static schema = DropErc20ContractSchema;
4343

4444
private _burn = this.burn as Erc20Burnable;
45-
private _drop = this.drop as Erc20Droppable;
45+
private _claim = this.drop?.claim as Erc20Claimable;
4646

4747
public metadata: ContractMetadata<DropERC20, typeof TokenDrop.schema>;
4848
public roles: ContractRoles<
@@ -213,7 +213,7 @@ export class TokenDrop extends Erc20<DropERC20> {
213213
amount: Amount,
214214
checkERC20Allowance = true,
215215
): Promise<TransactionResult> {
216-
return this._drop.claimTo(destinationAddress, amount, checkERC20Allowance);
216+
return this._claim.to(destinationAddress, amount, checkERC20Allowance);
217217
}
218218

219219
/**

src/core/classes/erc-20-claimable.ts

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
import { CustomContractSchema } from "../../schema/contracts/custom";
2+
import { ClaimVerification } from "../../types";
3+
import { Amount } from "../../types/currency";
4+
import { BaseDropERC20 } from "../../types/eips";
5+
import { IStorage } from "@thirdweb-dev/storage";
6+
import { TransactionResult } from "../types";
7+
import { ContractMetadata } from "./contract-metadata";
8+
import { ContractWrapper } from "./contract-wrapper";
9+
import { DropClaimConditions } from "./drop-claim-conditions";
10+
import { Erc20 } from "./erc-20";
11+
12+
/**
13+
* Configure and claim ERC20 tokens
14+
* @remarks Manage claim phases and claim ERC20 tokens that have been lazily minted.
15+
* @example
16+
* ```javascript
17+
* const contract = await sdk.getContract("{{contract_address}}");
18+
* await contract.token.drop.claim.to("0x...", quantity);
19+
* ```
20+
*/
21+
export class Erc20Claimable {
22+
/**
23+
* Configure claim conditions
24+
* @remarks Define who can claim NFTs in the collection, when and how many.
25+
* @example
26+
* ```javascript
27+
* const presaleStartTime = new Date();
28+
* const publicSaleStartTime = new Date(Date.now() + 60 * 60 * 24 * 1000);
29+
* const claimConditions = [
30+
* {
31+
* startTime: presaleStartTime, // start the presale now
32+
* maxQuantity: 2, // limit how many mints for this presale
33+
* price: 0.01, // presale price
34+
* snapshot: ['0x...', '0x...'], // limit minting to only certain addresses
35+
* },
36+
* {
37+
* startTime: publicSaleStartTime, // 24h after presale, start public sale
38+
* price: 0.08, // public sale price
39+
* }
40+
* ]);
41+
* await contract.nft.drop.claim.conditions.set(claimConditions);
42+
* ```
43+
*/
44+
public conditions: DropClaimConditions<BaseDropERC20>;
45+
private contractWrapper: ContractWrapper<BaseDropERC20>;
46+
private erc20: Erc20;
47+
private storage: IStorage;
48+
49+
constructor(
50+
erc20: Erc20,
51+
contractWrapper: ContractWrapper<BaseDropERC20>,
52+
storage: IStorage,
53+
) {
54+
this.erc20 = erc20;
55+
this.contractWrapper = contractWrapper;
56+
57+
this.storage = storage;
58+
const metadata = new ContractMetadata(
59+
this.contractWrapper,
60+
CustomContractSchema,
61+
this.storage,
62+
);
63+
this.conditions = new DropClaimConditions(
64+
this.contractWrapper,
65+
metadata,
66+
this.storage,
67+
);
68+
}
69+
70+
/**
71+
* Claim a certain amount of tokens to a specific Wallet
72+
*
73+
* @remarks Let the specified wallet claim Tokens.
74+
*
75+
* @example
76+
* ```javascript
77+
* const address = "{{wallet_address}}"; // address of the wallet you want to claim the NFTs
78+
* const quantity = 42.69; // how many tokens you want to claim
79+
*
80+
* const tx = await contract.token.drop.claim.to(address, quantity);
81+
* const receipt = tx.receipt; // the transaction receipt
82+
* ```
83+
*
84+
* @param destinationAddress - Address you want to send the token to
85+
* @param amount - Quantity of the tokens you want to claim
86+
* @param checkERC20Allowance - Optional, check if the wallet has enough ERC20 allowance to claim the tokens, and if not, approve the transfer
87+
* @param claimData
88+
* @returns - The transaction receipt
89+
*/
90+
public async to(
91+
destinationAddress: string,
92+
amount: Amount,
93+
checkERC20Allowance = true,
94+
claimData?: ClaimVerification,
95+
): Promise<TransactionResult> {
96+
const quantity = await this.erc20.normalizeAmount(amount);
97+
98+
let claimVerification = claimData;
99+
if (this.conditions && !claimData) {
100+
claimVerification = await this.conditions.prepareClaim(
101+
quantity,
102+
checkERC20Allowance,
103+
await this.contractWrapper.readContract.decimals(),
104+
);
105+
}
106+
if (!claimVerification) {
107+
throw new Error(
108+
"Claim verification Data is required - either pass it in as 'claimData' or set claim conditions via 'conditions.set()'",
109+
);
110+
}
111+
112+
const receipt = await this.contractWrapper.sendTransaction(
113+
"claim",
114+
[
115+
destinationAddress,
116+
quantity,
117+
claimVerification.currencyAddress,
118+
claimVerification.price,
119+
claimVerification.proofs,
120+
claimVerification.maxQuantityPerTransaction,
121+
],
122+
claimVerification.overrides,
123+
);
124+
return { receipt };
125+
}
126+
}

src/core/classes/erc-20-droppable.ts

Lines changed: 4 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,10 @@
11
import { FEATURE_TOKEN_DROPPABLE } from "../../constants/erc20-features";
2-
import { CustomContractSchema } from "../../schema/contracts/custom";
3-
import { ClaimVerification } from "../../types";
4-
import { Amount } from "../../types/currency";
52
import { BaseDropERC20 } from "../../types/eips";
63
import { IStorage } from "@thirdweb-dev/storage";
74
import { DetectableFeature } from "../interfaces/DetectableFeature";
8-
import { TransactionResult } from "../types";
9-
import { ContractMetadata } from "./contract-metadata";
105
import { ContractWrapper } from "./contract-wrapper";
11-
import { DropClaimConditions } from "./drop-claim-conditions";
126
import { Erc20 } from "./erc-20";
7+
import { Erc20Claimable } from "./erc-20-claimable";
138

149
/**
1510
* Configure and claim ERC20 tokens
@@ -45,7 +40,7 @@ export class Erc20Droppable implements DetectableFeature {
4540
* await contract.nft.drop.claim.conditions.set(claimConditions);
4641
* ```
4742
*/
48-
public claimConditions: DropClaimConditions<BaseDropERC20>;
43+
public claim: Erc20Claimable;
4944
private contractWrapper: ContractWrapper<BaseDropERC20>;
5045
private erc20: Erc20;
5146
private storage: IStorage;
@@ -59,72 +54,10 @@ export class Erc20Droppable implements DetectableFeature {
5954
this.contractWrapper = contractWrapper;
6055

6156
this.storage = storage;
62-
const metadata = new ContractMetadata(
57+
this.claim = new Erc20Claimable(
58+
this.erc20,
6359
this.contractWrapper,
64-
CustomContractSchema,
6560
this.storage,
6661
);
67-
this.claimConditions = new DropClaimConditions(
68-
this.contractWrapper,
69-
metadata,
70-
this.storage,
71-
);
72-
}
73-
74-
/**
75-
* Claim a certain amount of tokens to a specific Wallet
76-
*
77-
* @remarks Let the specified wallet claim Tokens.
78-
*
79-
* @example
80-
* ```javascript
81-
* const address = "{{wallet_address}}"; // address of the wallet you want to claim the NFTs
82-
* const quantity = 42.69; // how many tokens you want to claim
83-
*
84-
* const tx = await contract.token.drop.claim.to(address, quantity);
85-
* const receipt = tx.receipt; // the transaction receipt
86-
* ```
87-
*
88-
* @param destinationAddress - Address you want to send the token to
89-
* @param amount - Quantity of the tokens you want to claim
90-
* @param checkERC20Allowance - Optional, check if the wallet has enough ERC20 allowance to claim the tokens, and if not, approve the transfer
91-
*
92-
* @returns - The transaction receipt
93-
*/
94-
public async claimTo(
95-
destinationAddress: string,
96-
amount: Amount,
97-
checkERC20Allowance = true,
98-
claimData?: ClaimVerification,
99-
): Promise<TransactionResult> {
100-
const quantity = await this.erc20.normalizeAmount(amount);
101-
102-
let claimVerification = claimData;
103-
if (this.claimConditions && !claimData) {
104-
claimVerification = await this.claimConditions.prepareClaim(
105-
quantity,
106-
checkERC20Allowance,
107-
await this.contractWrapper.readContract.decimals(),
108-
);
109-
}
110-
if (!claimVerification) {
111-
throw new Error(
112-
"Claim verification Data is required - either pass it in as 'claimData' or set claim conditions via 'conditions.set()'",
113-
);
114-
}
115-
116-
const receipt = await this.contractWrapper.sendTransaction(
117-
"claim",
118-
[
119-
destinationAddress,
120-
quantity,
121-
claimVerification.currencyAddress,
122-
claimVerification.price,
123-
claimVerification.proofs,
124-
claimVerification.maxQuantityPerTransaction,
125-
],
126-
claimVerification.overrides,
127-
);
128-
return { receipt };
12962
}
13063
}

test/custom.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ describe("Custom Contracts", async () => {
268268
invariant(c.token, "ERC20 undefined");
269269
invariant(c.token.drop, "ERC20 drop undefined");
270270

271-
await c.token.drop.claimConditions.set([
271+
await c.token.drop.claim.conditions.set([
272272
{
273273
startTime: new Date(new Date().getTime() - 1000 * 60 * 60),
274274
price: 0,
@@ -279,7 +279,7 @@ describe("Custom Contracts", async () => {
279279
let b = await c.token.balance();
280280
expect(b.displayValue).to.equal("0.0");
281281

282-
await c.token.drop.claimTo(adminWallet.address, 5);
282+
await c.token.drop.claim.to(adminWallet.address, 5);
283283

284284
b = await c.token.balance();
285285
expect(b.displayValue).to.equal("5.0");

0 commit comments

Comments
 (0)