Skip to content

Commit 30661bb

Browse files
committed
Fix claim/transfer issues with non-standard tokens
1 parent 3059836 commit 30661bb

File tree

7 files changed

+85
-32
lines changed

7 files changed

+85
-32
lines changed

Assets/Thirdweb/Core/Scripts/ERC1155.cs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,7 @@ public async Task<TransactionResult> ClaimTo(string address, string tokenId, int
319319
else
320320
{
321321
var claimCondition = await claimConditions.GetActive(tokenId);
322+
BigInteger rawPrice = BigInteger.Parse(claimCondition.currencyMetadata.value);
322323
return await TransactionManager.ThirdwebWrite(
323324
contractAddress,
324325
new DropERC1155Contract.ClaimFunction()
@@ -327,16 +328,17 @@ public async Task<TransactionResult> ClaimTo(string address, string tokenId, int
327328
TokenId = BigInteger.Parse(tokenId),
328329
Quantity = quantity,
329330
Currency = claimCondition.currencyAddress,
330-
PricePerToken = BigInteger.Parse(claimCondition.currencyMetadata.value),
331+
PricePerToken = rawPrice,
331332
AllowlistProof = new DropERC1155Contract.AllowlistProof
332333
{
333334
Proof = new List<byte[]>(),
334335
Currency = claimCondition.currencyAddress,
335-
PricePerToken = BigInteger.Parse(claimCondition.currencyMetadata.value),
336+
PricePerToken = rawPrice,
336337
QuantityLimitPerWallet = BigInteger.Parse(claimCondition.maxClaimablePerWallet),
337338
}, // TODO add support for allowlists
338339
Data = new byte[] { }
339-
}
340+
},
341+
claimCondition.currencyAddress == Utils.NativeTokenAddress ? quantity * rawPrice : 0
340342
);
341343
}
342344
}
@@ -474,7 +476,13 @@ public async Task<ClaimConditions> GetActive(string tokenId)
474476
{
475477
availableSupply = (data.Condition.MaxClaimableSupply - data.Condition.SupplyClaimed).ToString(),
476478
currencyAddress = data.Condition.Currency,
477-
currencyMetadata = new CurrencyValue(currency.name, currency.symbol, currency.decimals, data.Condition.PricePerToken.ToString(), data.Condition.PricePerToken.ToString().ToEth()),
479+
currencyMetadata = new CurrencyValue(
480+
currency.name,
481+
currency.symbol,
482+
currency.decimals,
483+
data.Condition.PricePerToken.ToString(),
484+
data.Condition.PricePerToken.ToString().FormatERC20(4, int.Parse(currency.decimals), true)
485+
),
478486
currentMintSupply = data.Condition.SupplyClaimed.ToString(),
479487
maxClaimablePerWallet = data.Condition.QuantityLimitPerWallet.ToString(),
480488
maxClaimableSupply = data.Condition.MaxClaimableSupply.ToString(),

Assets/Thirdweb/Core/Scripts/ERC20.cs

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ public async Task<CurrencyValue> BalanceOf(string address)
9393
contractAddress,
9494
new TokenERC20Contract.BalanceOfFunction() { Account = address }
9595
);
96-
return new CurrencyValue(c.name, c.symbol, c.decimals, balance.ReturnValue1.ToString(), balance.ReturnValue1.ToString().ToEth());
96+
return new CurrencyValue(c.name, c.symbol, c.decimals, balance.ReturnValue1.ToString(), balance.ReturnValue1.ToString().FormatERC20(4, int.Parse(c.decimals), true));
9797
}
9898
}
9999

@@ -128,7 +128,7 @@ public async Task<CurrencyValue> AllowanceOf(string owner, string spender)
128128
contractAddress,
129129
new TokenERC20Contract.AllowanceFunction() { Owner = owner, Spender = spender }
130130
);
131-
return new CurrencyValue(c.name, c.symbol, c.decimals, allowance.ReturnValue1.ToString(), allowance.ReturnValue1.ToString().ToEth());
131+
return new CurrencyValue(c.name, c.symbol, c.decimals, allowance.ReturnValue1.ToString(), allowance.ReturnValue1.ToString().FormatERC20(4, int.Parse(c.decimals), true));
132132
}
133133
}
134134

@@ -148,7 +148,7 @@ public async Task<CurrencyValue> TotalSupply()
148148
contractAddress,
149149
new TokenERC20Contract.TotalSupplyFunction() { }
150150
);
151-
return new CurrencyValue(c.name, c.symbol, c.decimals, totalSupply.ReturnValue1.ToString(), totalSupply.ReturnValue1.ToString().ToEth());
151+
return new CurrencyValue(c.name, c.symbol, c.decimals, totalSupply.ReturnValue1.ToString(), totalSupply.ReturnValue1.ToString().FormatERC20(4, int.Parse(c.decimals), true));
152152
}
153153
}
154154

@@ -198,7 +198,9 @@ public async Task<TransactionResult> Transfer(string to, string amount)
198198
}
199199
else
200200
{
201-
return await TransactionManager.ThirdwebWrite(contractAddress, new TokenERC20Contract.TransferFunction() { To = to, Amount = BigInteger.Parse(amount.ToWei()) });
201+
var currency = await Get();
202+
var rawAmountToTransfer = BigInteger.Parse(amount.ToWei()).AdjustDecimals(18, int.Parse(currency.decimals));
203+
return await TransactionManager.ThirdwebWrite(contractAddress, new TokenERC20Contract.TransferFunction() { To = to, Amount = rawAmountToTransfer });
202204
}
203205
}
204206

@@ -248,24 +250,26 @@ public async Task<TransactionResult> ClaimTo(string address, string amount)
248250
contractAddress,
249251
new TokenERC20Contract.DecimalsFunction()
250252
);
253+
var rawAmountToClaim = BigInteger.Parse(amount.ToWei()).AdjustDecimals(18, int.Parse(decimals.ReturnValue1.ToString()));
254+
var rawPrice = BigInteger.Parse(claimCondition.currencyMetadata.value);
251255
return await TransactionManager.ThirdwebWrite(
252256
contractAddress,
253257
new DropERC20Contract.ClaimFunction()
254258
{
255259
Receiver = address,
256-
Quantity = BigInteger.Parse(amount.ToWei()),
260+
Quantity = rawAmountToClaim,
257261
Currency = claimCondition.currencyAddress,
258-
PricePerToken = BigInteger.Parse(claimCondition.currencyMetadata.value),
262+
PricePerToken = rawPrice,
259263
AllowlistProof = new DropERC20Contract.AllowlistProof
260264
{
261265
Proof = new List<byte[]>(),
262266
Currency = claimCondition.currencyAddress,
263-
PricePerToken = BigInteger.Parse(claimCondition.currencyMetadata.value),
267+
PricePerToken = rawPrice,
264268
QuantityLimitPerWallet = BigInteger.Parse(claimCondition.maxClaimablePerWallet),
265269
},
266270
Data = new byte[] { }
267271
},
268-
(BigInteger.Parse(amount.ToWei()) * BigInteger.Parse(claimCondition.currencyMetadata.value)) / BigInteger.Parse(decimals.ReturnValue1.ToString())
272+
claimCondition.currencyAddress == Utils.NativeTokenAddress ? BigInteger.Parse((decimal.Parse(amount) * (decimal)rawPrice).ToString("F0")) : 0
269273
);
270274
}
271275
}
@@ -395,7 +399,13 @@ public async Task<ClaimConditions> GetActive()
395399
{
396400
availableSupply = (data.Condition.MaxClaimableSupply - data.Condition.SupplyClaimed).ToString(),
397401
currencyAddress = data.Condition.Currency,
398-
currencyMetadata = new CurrencyValue(currency.name, currency.symbol, currency.decimals, data.Condition.PricePerToken.ToString(), data.Condition.PricePerToken.ToString().ToEth()),
402+
currencyMetadata = new CurrencyValue(
403+
currency.name,
404+
currency.symbol,
405+
currency.decimals,
406+
data.Condition.PricePerToken.ToString(),
407+
data.Condition.PricePerToken.ToString().FormatERC20(4, int.Parse(currency.decimals), true)
408+
),
399409
currentMintSupply = data.Condition.SupplyClaimed.ToString(),
400410
maxClaimablePerWallet = data.Condition.QuantityLimitPerWallet.ToString(),
401411
maxClaimableSupply = data.Condition.MaxClaimableSupply.ToString(),

Assets/Thirdweb/Core/Scripts/ERC721.cs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,7 @@ public async Task<TransactionResult[]> ClaimTo(string address, int quantity)
382382
else
383383
{
384384
var claimCondition = await claimConditions.GetActive();
385+
BigInteger rawPrice = BigInteger.Parse(claimCondition.currencyMetadata.value);
385386
return new TransactionResult[]
386387
{
387388
await TransactionManager.ThirdwebWrite(
@@ -391,17 +392,17 @@ await TransactionManager.ThirdwebWrite(
391392
Receiver = address,
392393
Quantity = quantity,
393394
Currency = claimCondition.currencyAddress,
394-
PricePerToken = BigInteger.Parse(claimCondition.currencyMetadata.value),
395+
PricePerToken = rawPrice,
395396
AllowlistProof = new DropERC721Contract.AllowlistProof
396397
{
397398
Proof = new List<byte[]>(),
398399
Currency = claimCondition.currencyAddress,
399-
PricePerToken = BigInteger.Parse(claimCondition.currencyMetadata.value),
400+
PricePerToken = rawPrice,
400401
QuantityLimitPerWallet = BigInteger.Parse(claimCondition.maxClaimablePerWallet),
401402
}, // TODO add support for allowlists
402403
Data = new byte[] { }
403404
},
404-
quantity * BigInteger.Parse(claimCondition.currencyMetadata.value)
405+
claimCondition.currencyAddress == Utils.NativeTokenAddress ? quantity * rawPrice : 0
405406
)
406407
};
407408
}
@@ -512,7 +513,13 @@ public async Task<ClaimConditions> GetActive()
512513
{
513514
availableSupply = (data.MaxClaimableSupply - data.SupplyClaimed).ToString(),
514515
currencyAddress = data.Currency,
515-
currencyMetadata = new CurrencyValue(currency.name, currency.symbol, currency.decimals, data.PricePerToken.ToString(), data.PricePerToken.ToString().ToEth()),
516+
currencyMetadata = new CurrencyValue(
517+
currency.name,
518+
currency.symbol,
519+
currency.decimals,
520+
data.PricePerToken.ToString(),
521+
data.PricePerToken.ToString().FormatERC20(4, int.Parse(currency.decimals), true)
522+
),
516523
currentMintSupply = data.SupplyClaimed.ToString(),
517524
maxClaimablePerWallet = data.QuantityLimitPerWallet.ToString(),
518525
maxClaimableSupply = data.MaxClaimableSupply.ToString(),

Assets/Thirdweb/Core/Scripts/Marketplace.cs

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ public async Task<DirectListing> GetListing(string listingID)
146146
currency.symbol,
147147
currency.decimals,
148148
result.Listing.PricePerToken.ToString(),
149-
result.Listing.PricePerToken.ToString().ToEth()
149+
result.Listing.PricePerToken.ToString().FormatERC20(4, int.Parse(currency.decimals), true)
150150
),
151151
pricePerToken = result.Listing.PricePerToken.ToString(),
152152
asset = metadata,
@@ -277,7 +277,7 @@ public async Task<TransactionResult> CreateListing(CreateListingInput input)
277277
AssetContract = input.assetContractAddress,
278278
TokenId = BigInteger.Parse(input.tokenId),
279279
Quantity = BigInteger.Parse(input.quantity ?? "1"),
280-
Currency = input.currencyContractAddress ?? Utils.NativeTokenAddressV2,
280+
Currency = input.currencyContractAddress ?? Utils.NativeTokenAddress,
281281
PricePerToken = BigInteger.Parse(input.pricePerToken),
282282
StartTimestamp = (BigInteger)(input.startTimestamp ?? await Utils.GetCurrentBlockTimeStamp() + 60),
283283
EndTimestamp = (BigInteger)(input.endTimestamp ?? Utils.GetUnixTimeStampNow() + 60 * 60 * 24 * 7),
@@ -480,15 +480,15 @@ public async Task<Auction> GetAuction(string auctionId)
480480
currency.symbol,
481481
currency.decimals,
482482
result.Auction.MinimumBidAmount.ToString(),
483-
result.Auction.MinimumBidAmount.ToString().ToEth()
483+
result.Auction.MinimumBidAmount.ToString().FormatERC20(4, int.Parse(currency.decimals), true)
484484
),
485485
buyoutBidAmount = result.Auction.BuyoutBidAmount.ToString(),
486486
buyoutCurrencyValue = new CurrencyValue(
487487
currency.name,
488488
currency.symbol,
489489
currency.decimals,
490490
result.Auction.BuyoutBidAmount.ToString(),
491-
result.Auction.BuyoutBidAmount.ToString().ToEth()
491+
result.Auction.BuyoutBidAmount.ToString().FormatERC20(4, int.Parse(currency.decimals), true)
492492
),
493493
timeBufferInSeconds = (int)result.Auction.TimeBufferInSeconds,
494494
bidBufferBps = (int)result.Auction.BidBufferBps,
@@ -525,7 +525,7 @@ public async Task<CurrencyValue> GetMinimumNextBid(string auctionId)
525525
var winningBid = await GetWinningBid(auctionId);
526526
var cv = auction.minimumBidCurrencyValue.Value;
527527
cv.value = (BigInteger.Parse(cv.value) + BigInteger.Parse(winningBid.bidAmount)).ToString();
528-
cv.displayValue = cv.value.ToEth();
528+
cv.displayValue = cv.value.FormatERC20(4, int.Parse(cv.decimals), true);
529529
return cv;
530530
}
531531
}
@@ -583,7 +583,13 @@ public async Task<Bid> GetWinningBid(string auctionId)
583583
bidderAddress = winningBid.Bidder,
584584
currencyContractAddress = winningBid.Currency,
585585
bidAmount = winningBid.BidAmount.ToString(),
586-
bidAmountCurrencyValue = new CurrencyValue(c.name, c.symbol, c.decimals, winningBid.BidAmount.ToString(), winningBid.BidAmount.ToString().ToEth())
586+
bidAmountCurrencyValue = new CurrencyValue(
587+
c.name,
588+
c.symbol,
589+
c.decimals,
590+
winningBid.BidAmount.ToString(),
591+
winningBid.BidAmount.ToString().FormatERC20(4, int.Parse(c.decimals), true)
592+
)
587593
};
588594
}
589595
}
@@ -676,7 +682,7 @@ public async Task<TransactionResult> CreateAuction(CreateAuctionInput input)
676682
AssetContract = input.assetContractAddress,
677683
TokenId = BigInteger.Parse(input.tokenId),
678684
Quantity = BigInteger.Parse(input.quantity ?? "1"),
679-
Currency = input.currencyContractAddress ?? Utils.NativeTokenAddressV2,
685+
Currency = input.currencyContractAddress ?? Utils.NativeTokenAddress,
680686
MinimumBidAmount = BigInteger.Parse(input.minimumBidAmount.ToWei()),
681687
BuyoutBidAmount = BigInteger.Parse(input.buyoutBidAmount.ToWei()),
682688
TimeBufferInSeconds = ulong.Parse(input.timeBufferInSeconds ?? "900"),
@@ -834,7 +840,13 @@ public async Task<Offer> GetOffer(string offerID)
834840
tokenId = result.Offer.TokenId.ToString(),
835841
quantity = result.Offer.Quantity.ToString(),
836842
currencyContractAddress = result.Offer.Currency,
837-
currencyValue = new CurrencyValue(currency.name, currency.symbol, currency.decimals, result.Offer.TotalPrice.ToString(), result.Offer.TotalPrice.ToString().ToEth()),
843+
currencyValue = new CurrencyValue(
844+
currency.name,
845+
currency.symbol,
846+
currency.decimals,
847+
result.Offer.TotalPrice.ToString(),
848+
result.Offer.TotalPrice.ToString().FormatERC20(4, int.Parse(currency.decimals), true)
849+
),
838850
totalPrice = result.Offer.TotalPrice.ToString(),
839851
asset = metadata,
840852
endTimeInSeconds = (long)result.Offer.ExpirationTimestamp,

Assets/Thirdweb/Core/Scripts/Utils.cs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@ namespace Thirdweb
1717
public static class Utils
1818
{
1919
public const string AddressZero = "0x0000000000000000000000000000000000000000";
20-
public const string NativeTokenAddress = "0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee";
21-
public const string NativeTokenAddressV2 = "0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE";
20+
public const string NativeTokenAddress = "0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE";
2221
public const double DECIMALS_18 = 1000000000000000000;
2322

2423
public static string[] ToJsonStringArray(params object[] args)
@@ -79,6 +78,7 @@ public static string ToEth(this string wei, int decimalsToDisplay = 4, bool addC
7978

8079
public static string FormatERC20(this string wei, int decimalsToDisplay = 4, int decimals = 18, bool addCommas = true)
8180
{
81+
decimals = decimals == 0 ? 18 : decimals;
8282
if (!BigInteger.TryParse(wei, out BigInteger weiBigInt))
8383
throw new ArgumentException("Invalid wei value.");
8484
double eth = (double)weiBigInt / Math.Pow(10.0, decimals);
@@ -90,6 +90,22 @@ public static string FormatERC20(this string wei, int decimalsToDisplay = 4, int
9090
return eth.ToString(format);
9191
}
9292

93+
public static BigInteger AdjustDecimals(this BigInteger value, int fromDecimals, int toDecimals)
94+
{
95+
int differenceInDecimals = fromDecimals - toDecimals;
96+
97+
if (differenceInDecimals > 0)
98+
{
99+
return value / BigInteger.Pow(10, differenceInDecimals);
100+
}
101+
else if (differenceInDecimals < 0)
102+
{
103+
return value * BigInteger.Pow(10, -differenceInDecimals);
104+
}
105+
106+
return value;
107+
}
108+
93109
public static string ShortenAddress(this string address)
94110
{
95111
if (address.Length != 42)

0 commit comments

Comments
 (0)