22 type BaseTransactionOptions ,
33 type ThirdwebClient ,
44 toTokens ,
5+ toUnits ,
56} from "thirdweb" ;
67import type { OverrideEntry } from "thirdweb/dist/types/utils/extensions/drops/types" ;
78import type { Prettify } from "thirdweb/dist/types/utils/type-utils" ;
@@ -42,6 +43,7 @@ type CombinedClaimCondition = Prettify<
4243type Options =
4344 | {
4445 type : "erc20" ;
46+ decimals ?: number ;
4547 }
4648 | {
4749 type : "erc721" ;
@@ -91,12 +93,24 @@ export async function getClaimPhasesInLegacyFormat(
9193 startTime : new Date ( Number ( condition . startTimestamp * 1000n ) ) ,
9294 currencyAddress : condition . currency ,
9395 price : condition . pricePerToken ,
94- maxClaimableSupply : toUnlimited ( condition . maxClaimableSupply ) ,
96+ // This value from ERC20Ext.getClaimConditions is in wei
97+ // so we have to convert it using toTokens for readability
98+ // (when user updates this value, we convert it back to wei - see `function setClaimPhasesTx`)
99+ maxClaimableSupply : toUnlimited (
100+ condition . maxClaimableSupply ,
101+ options . type === "erc20" ? currencyMetadata . decimals : undefined ,
102+ ) ,
95103 currencyMetadata,
96104 currentMintSupply : (
97105 condition . maxClaimableSupply - condition . supplyClaimed
98106 ) . toString ( ) ,
99- maxClaimablePerWallet : toUnlimited ( condition . quantityLimitPerWallet ) ,
107+ // This value from ERC20Ext.getClaimConditions is in wei
108+ // so we have to convert it using toTokens for readability
109+ // (when user updates this value, we convert it back to wei - see `function setClaimPhasesTx`)
110+ maxClaimablePerWallet : toUnlimited (
111+ condition . quantityLimitPerWallet ,
112+ options . type === "erc20" ? currencyMetadata . decimals : undefined ,
113+ ) ,
100114 merkleRootHash : condition . merkleRoot ,
101115 metadata,
102116 snapshot,
@@ -114,8 +128,18 @@ export function setClaimPhasesTx(
114128 const phases = rawPhases . map ( ( phase ) => {
115129 return {
116130 startTime : toDate ( phase . startTime ) ,
117- maxClaimableSupply : toBigInt ( phase . maxClaimableSupply ) ,
118- maxClaimablePerWallet : toBigInt ( phase . maxClaimablePerWallet ) ,
131+ // The input from client-side is non-wei, but the extension is expecting value in wei
132+ // so we need to convert it using toUnits
133+ maxClaimableSupply : toBigInt (
134+ phase . maxClaimableSupply ,
135+ baseOptions . type === "erc20" ? baseOptions . decimals : undefined ,
136+ ) ,
137+ // The input from client-side is non-wei, but the ERC20 extension is expecting value in wei
138+ // so we need to convert it using toUnits
139+ maxClaimablePerWallet : toBigInt (
140+ phase . maxClaimablePerWallet ,
141+ baseOptions . type === "erc20" ? baseOptions . decimals : undefined ,
142+ ) ,
119143 merkleRootHash : phase . merkleRootHash as string | undefined ,
120144 overrideList : phase . snapshot ?. length
121145 ? snapshotToOverrides ( phase . snapshot )
@@ -175,18 +199,33 @@ function toDate(timestamp: number | Date | undefined) {
175199 }
176200 return new Date ( timestamp ) ;
177201}
178- function toBigInt ( value : string | number | undefined ) {
202+ function toBigInt (
203+ value : string | number | undefined ,
204+ decimals ?: number ,
205+ ) : bigint | undefined {
179206 if ( value === undefined ) {
180207 return undefined ;
181208 }
182209 if ( value === "unlimited" ) {
183210 return maxUint256 ;
184211 }
212+ // The ERC20Claim condition extension in v5 does not convert to wei for us
213+ // so we have to, manually
214+ if ( decimals ) {
215+ return toUnits ( value . toString ( ) , decimals ) ;
216+ }
217+
185218 return BigInt ( value ) ;
186219}
187220
188- function toUnlimited ( value : bigint ) {
189- return value === maxUint256 ? "unlimited" : value . toString ( ) ;
221+ function toUnlimited ( value : bigint , decimals ?: number ) {
222+ if ( value === maxUint256 ) {
223+ return "unlimited" ;
224+ }
225+ if ( decimals ) {
226+ return toTokens ( value , decimals ) ;
227+ }
228+ return value . toString ( ) ;
190229}
191230
192231async function fetchSnapshot (
0 commit comments