From 34459f352bc12037b635ed2b836c5e98a8a5c0d4 Mon Sep 17 00:00:00 2001 From: Joaquim Verges Date: Tue, 10 May 2022 14:06:44 -0700 Subject: [PATCH] Fix token drop claim unit conversion --- docs/sdk.dropclaimconditions._constructor_.md | 4 +-- etc/sdk.api.md | 32 +++---------------- src/common/claim-conditions.ts | 5 ++- src/common/currency.ts | 6 +++- src/core/classes/drop-claim-conditions.ts | 9 ++---- src/schema/contracts/drop-erc20.ts | 15 +++------ test/token-drop.test.ts | 11 +++++++ 7 files changed, 33 insertions(+), 49 deletions(-) diff --git a/docs/sdk.dropclaimconditions._constructor_.md b/docs/sdk.dropclaimconditions._constructor_.md index d3ff8e15d..ee5f6b750 100644 --- a/docs/sdk.dropclaimconditions._constructor_.md +++ b/docs/sdk.dropclaimconditions._constructor_.md @@ -9,7 +9,7 @@ Constructs a new instance of the `DropClaimConditions` class Signature: ```typescript -constructor(contractWrapper: ContractWrapper, metadata: ContractMetadata, storage: IStorage); +constructor(contractWrapper: ContractWrapper, metadata: ContractMetadata, storage: IStorage); ``` ## Parameters @@ -17,6 +17,6 @@ constructor(contractWrapper: ContractWrapper, metadata: ContractMetad | Parameter | Type | Description | | --- | --- | --- | | contractWrapper | ContractWrapper<TContract> | | -| metadata | [ContractMetadata](./sdk.contractmetadata.md)<TContract, typeof DropErc721ContractSchema \| typeof DropErc20ContractSchema> | | +| metadata | [ContractMetadata](./sdk.contractmetadata.md)<TContract, any> | | | storage | [IStorage](./sdk.istorage.md) | | diff --git a/etc/sdk.api.md b/etc/sdk.api.md index 4dfaa1764..dff1fae53 100644 --- a/etc/sdk.api.md +++ b/etc/sdk.api.md @@ -871,9 +871,7 @@ export interface DirectListing { // // @public export class DropClaimConditions { - // Warning: (ae-forgotten-export) The symbol "DropErc721ContractSchema" needs to be exported by the entry point index.d.ts - // Warning: (ae-forgotten-export) The symbol "DropErc20ContractSchema" needs to be exported by the entry point index.d.ts - constructor(contractWrapper: ContractWrapper, metadata: ContractMetadata, storage: IStorage); + constructor(contractWrapper: ContractWrapper, metadata: ContractMetadata, storage: IStorage); canClaim(quantity: Amount, addressToCheck?: string): Promise; getActive(): Promise; getAll(): Promise; @@ -885,6 +883,7 @@ export class DropClaimConditions { // @public export class DropErc1155ClaimConditions { // Warning: (ae-forgotten-export) The symbol "DropERC1155" needs to be exported by the entry point index.d.ts + // Warning: (ae-forgotten-export) The symbol "DropErc721ContractSchema" needs to be exported by the entry point index.d.ts constructor(contractWrapper: ContractWrapper, metadata: ContractMetadata, storage: IStorage); canClaim(tokenId: BigNumberish, quantity: BigNumberish, addressToCheck?: string): Promise; getActive(tokenId: BigNumberish): Promise; @@ -4485,15 +4484,12 @@ export class TokenDrop extends Erc20 { sales: ContractPrimarySale; // (undocumented) static schema: { - deploy: ZodObject; image: ZodOptional>; external_link: ZodOptional; }, { - seller_fee_basis_points: ZodDefault; - fee_recipient: ZodDefault>; - }>, { merkle: ZodDefault>; }>, { symbol: ZodDefault>; @@ -4511,8 +4507,6 @@ export class TokenDrop extends Erc20 { merkle: Record; image?: any; external_link?: string | undefined; - seller_fee_basis_points: number; - fee_recipient: string; primary_sale_recipient: string; platform_fee_basis_points: number; platform_fee_recipient: string; @@ -4524,14 +4518,12 @@ export class TokenDrop extends Erc20 { merkle?: Record | undefined; image?: any; external_link?: string | undefined; - seller_fee_basis_points?: number | undefined; - fee_recipient?: string | undefined; primary_sale_recipient: string; platform_fee_basis_points?: number | undefined; platform_fee_recipient?: string | undefined; trusted_forwarders?: string[] | undefined; }>; - output: ZodObject; image: ZodOptional>; @@ -4539,9 +4531,6 @@ export class TokenDrop extends Erc20 { }, { image: ZodOptional; }>, { - seller_fee_basis_points: ZodDefault; - fee_recipient: ZodDefault>; - }>, { merkle: ZodDefault>; }>, { symbol: ZodDefault>; @@ -4552,8 +4541,6 @@ export class TokenDrop extends Erc20 { merkle: Record; image?: string | undefined; external_link?: string | undefined; - seller_fee_basis_points: number; - fee_recipient: string; symbol: string; }, { [x: string]: Json; @@ -4562,19 +4549,14 @@ export class TokenDrop extends Erc20 { merkle?: Record | undefined; image?: string | undefined; external_link?: string | undefined; - seller_fee_basis_points?: number | undefined; - fee_recipient?: string | undefined; symbol?: string | undefined; }>; - input: ZodObject; image: ZodOptional>; external_link: ZodOptional; }, { - seller_fee_basis_points: ZodDefault; - fee_recipient: ZodDefault>; - }>, { merkle: ZodDefault>; }>, { symbol: ZodDefault>; @@ -4584,8 +4566,6 @@ export class TokenDrop extends Erc20 { merkle: Record; image?: any; external_link?: string | undefined; - seller_fee_basis_points: number; - fee_recipient: string; symbol: string; }, { name: string; @@ -4593,8 +4573,6 @@ export class TokenDrop extends Erc20 { merkle?: Record | undefined; image?: any; external_link?: string | undefined; - seller_fee_basis_points?: number | undefined; - fee_recipient?: string | undefined; symbol?: string | undefined; }>; }; diff --git a/src/common/claim-conditions.ts b/src/common/claim-conditions.ts index c1a2e2e40..29fd5d44c 100644 --- a/src/common/claim-conditions.ts +++ b/src/common/claim-conditions.ts @@ -91,13 +91,16 @@ export async function prepareClaim( const currencyAddress = activeClaimCondition.currencyAddress; if (price.gt(0)) { if (isNativeToken(currencyAddress)) { - overrides["value"] = BigNumber.from(price).mul(quantity); + overrides["value"] = BigNumber.from(price) + .mul(quantity) + .div(ethers.utils.parseUnits("1", tokenDecimals)); } else { await approveErc20Allowance( contractWrapper, currencyAddress, price, quantity, + tokenDecimals, ); } } diff --git a/src/common/currency.ts b/src/common/currency.ts index 49c24e5d5..e3d690419 100644 --- a/src/common/currency.ts +++ b/src/common/currency.ts @@ -6,6 +6,7 @@ import { constants, providers, utils, + ethers, } from "ethers"; import { getNativeTokenByChainId, @@ -112,6 +113,7 @@ export async function approveErc20Allowance( currencyAddress: string, price: BigNumber, quantity: BigNumberish, + tokenDecimals: number, ) { const signer = contractToApprove.getSigner(); const provider = contractToApprove.getProvider(); @@ -124,7 +126,9 @@ export async function approveErc20Allowance( const owner = await contractToApprove.getSignerAddress(); const spender = contractToApprove.readContract.address; const allowance = await erc20.readContract.allowance(owner, spender); - const totalPrice = BigNumber.from(price).mul(BigNumber.from(quantity)); + const totalPrice = BigNumber.from(price) + .mul(BigNumber.from(quantity)) + .div(ethers.utils.parseUnits("1", tokenDecimals)); if (allowance.lt(totalPrice)) { await erc20.sendTransaction("approve", [ spender, diff --git a/src/core/classes/drop-claim-conditions.ts b/src/core/classes/drop-claim-conditions.ts index 252861703..d095d75bd 100644 --- a/src/core/classes/drop-claim-conditions.ts +++ b/src/core/classes/drop-claim-conditions.ts @@ -1,8 +1,7 @@ import { IStorage } from "../interfaces/IStorage"; -import { DropErc721ContractSchema } from "../../schema/contracts/drop-erc721"; import { ContractMetadata } from "./contract-metadata"; import { DropERC20, DropERC721, IERC20, IERC20Metadata } from "contracts"; -import { BigNumber, ethers, constants } from "ethers"; +import { BigNumber, constants, ethers } from "ethers"; import { isNativeToken } from "../../common/currency"; import { ContractWrapper } from "./contract-wrapper"; import { Amount, ClaimCondition, ClaimConditionInput } from "../../types"; @@ -17,7 +16,6 @@ import { } from "../../common/claim-conditions"; import { isBrowser } from "../../common/utils"; -import { DropErc20ContractSchema } from "../../schema/contracts/drop-erc20"; import { detectContractFeature } from "../../common/feature-detection"; import { PriceSchema } from "../../schema"; import { includesErrorMessage } from "../../common"; @@ -34,10 +32,7 @@ export class DropClaimConditions { constructor( contractWrapper: ContractWrapper, - metadata: ContractMetadata< - TContract, - typeof DropErc721ContractSchema | typeof DropErc20ContractSchema - >, + metadata: ContractMetadata, storage: IStorage, ) { this.storage = storage; diff --git a/src/schema/contracts/drop-erc20.ts b/src/schema/contracts/drop-erc20.ts index 15851d712..72a0b45fb 100644 --- a/src/schema/contracts/drop-erc20.ts +++ b/src/schema/contracts/drop-erc20.ts @@ -3,23 +3,16 @@ import { CommonContractSchema, CommonPlatformFeeSchema, CommonPrimarySaleSchema, - CommonRoyaltySchema, CommonSymbolSchema, CommonTrustedForwarderSchema, } from "./common"; import { MerkleSchema } from "./common/snapshots"; -export const DropErc20ContractInput = CommonContractSchema.merge( - CommonRoyaltySchema, -) - .merge(MerkleSchema) - .merge(CommonSymbolSchema); +export const DropErc20ContractInput = + CommonContractSchema.merge(MerkleSchema).merge(CommonSymbolSchema); -export const DropErc20ContractOutput = CommonContractOutputSchema.merge( - CommonRoyaltySchema, -) - .merge(MerkleSchema) - .merge(CommonSymbolSchema); +export const DropErc20ContractOutput = + CommonContractOutputSchema.merge(MerkleSchema).merge(CommonSymbolSchema); export const DropErc20ContractDeploy = DropErc20ContractInput.merge( CommonPlatformFeeSchema, diff --git a/test/token-drop.test.ts b/test/token-drop.test.ts index 28586ae4b..4e8427e20 100644 --- a/test/token-drop.test.ts +++ b/test/token-drop.test.ts @@ -207,6 +207,17 @@ describe("Token Drop Contract", async () => { expect(balance.displayValue).to.eq("142.69"); }); + it("should allow claiming with a price", async () => { + await dropContract.claimConditions.set([ + { + price: 0.1, + }, + ]); + await dropContract.claim(100); + const balance = await dropContract.balance(); + expect(balance.displayValue).to.eq("100.0"); + }); + it("should allow setting max claims per wallet", async () => { await dropContract.claimConditions.set([ {