Skip to content

Commit 733de6a

Browse files
authored
IThirdwebWallet.Transfer Optional ERC20 Override (#96)
1 parent d4be241 commit 733de6a

File tree

2 files changed

+31
-5
lines changed

2 files changed

+31
-5
lines changed

Thirdweb.Tests/Thirdweb.Extensions/Thirdweb.Extensions.Tests.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,16 @@ public async Task Transfer()
308308
Assert.True(receipt.TransactionHash.Length == 66);
309309
}
310310

311+
[Fact(Timeout = 120000)]
312+
public async Task TransferERC20Override()
313+
{
314+
var token = await this.GetTokenERC20Contract();
315+
var wallet = await this.GetSmartWallet();
316+
var receipt = await wallet.Transfer(this._chainId, await wallet.GetAddress(), BigInteger.Zero, token.Address);
317+
Assert.NotNull(receipt);
318+
Assert.True(receipt.TransactionHash.Length == 66);
319+
}
320+
311321
[Fact(Timeout = 120000)]
312322
public async Task Contract_Read()
313323
{

Thirdweb/Thirdweb.Extensions/ThirdwebExtensions.cs

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -265,17 +265,25 @@ public static async Task<BigInteger> GetTransactionCount(this IThirdwebWallet wa
265265
}
266266

267267
/// <summary>
268-
/// Transfers the specified amount of Wei to the specified address.
268+
/// Transfers the specified amount of Wei to the specified address. Passing tokenAddress will override this function to transfer ERC20 tokens.
269269
/// </summary>
270270
/// <param name="wallet">The wallet to transfer from.</param>
271271
/// <param name="chainId">The chain ID to transfer on.</param>
272272
/// <param name="toAddress">The address to transfer to.</param>
273273
/// <param name="weiAmount">The amount of Wei to transfer.</param>
274+
/// <param name="tokenAddress">The optional token address to transfer from. Defaults to the native token address (ETH or equivalent).</param>
275+
/// <returns>A task that represents the asynchronous operation. The task result contains the transaction receipt.</returns>
274276
/// <returns>A task that represents the asynchronous operation. The task result contains the transaction receipt.</returns>
275277
/// <exception cref="ArgumentNullException">Thrown when the wallet is null.</exception>
276278
/// <exception cref="ArgumentOutOfRangeException">Thrown when the chain ID is less than or equal to 0.</exception>
277279
/// <exception cref="ArgumentException">Thrown when the recipient address is null or empty.</exception>
278-
public static async Task<ThirdwebTransactionReceipt> Transfer(this IThirdwebWallet wallet, BigInteger chainId, string toAddress, BigInteger weiAmount)
280+
public static async Task<ThirdwebTransactionReceipt> Transfer(
281+
this IThirdwebWallet wallet,
282+
BigInteger chainId,
283+
string toAddress,
284+
BigInteger weiAmount,
285+
string tokenAddress = Constants.NATIVE_TOKEN_ADDRESS
286+
)
279287
{
280288
if (wallet == null)
281289
{
@@ -297,9 +305,17 @@ public static async Task<ThirdwebTransactionReceipt> Transfer(this IThirdwebWall
297305
throw new ArgumentOutOfRangeException(nameof(weiAmount), "Amount must be 0 or greater.");
298306
}
299307

300-
var txInput = new ThirdwebTransactionInput(chainId) { To = toAddress, Value = new HexBigInteger(weiAmount) };
301-
var tx = await ThirdwebTransaction.Create(wallet, txInput).ConfigureAwait(false);
302-
return await ThirdwebTransaction.SendAndWaitForTransactionReceipt(tx).ConfigureAwait(false);
308+
if (tokenAddress != Constants.NATIVE_TOKEN_ADDRESS)
309+
{
310+
var erc20Contract = await ThirdwebContract.Create(wallet.Client, tokenAddress, chainId).ConfigureAwait(false);
311+
return await erc20Contract.ERC20_Transfer(wallet, toAddress, weiAmount).ConfigureAwait(false);
312+
}
313+
else
314+
{
315+
var txInput = new ThirdwebTransactionInput(chainId) { To = toAddress, Value = new HexBigInteger(weiAmount) };
316+
var tx = await ThirdwebTransaction.Create(wallet, txInput).ConfigureAwait(false);
317+
return await ThirdwebTransaction.SendAndWaitForTransactionReceipt(tx).ConfigureAwait(false);
318+
}
303319
}
304320

305321
/// <summary>

0 commit comments

Comments
 (0)