From 733de6ac3ad6f933b0f4fd9e7b06daff1cd61f99 Mon Sep 17 00:00:00 2001 From: Firekeeper <0xFirekeeper@gmail.com> Date: Thu, 7 Nov 2024 22:09:29 +0700 Subject: [PATCH] IThirdwebWallet.Transfer Optional ERC20 Override (#96) --- .../Thirdweb.Extensions.Tests.cs | 10 +++++++ .../Thirdweb.Extensions/ThirdwebExtensions.cs | 26 +++++++++++++++---- 2 files changed, 31 insertions(+), 5 deletions(-) diff --git a/Thirdweb.Tests/Thirdweb.Extensions/Thirdweb.Extensions.Tests.cs b/Thirdweb.Tests/Thirdweb.Extensions/Thirdweb.Extensions.Tests.cs index b446709..681202b 100644 --- a/Thirdweb.Tests/Thirdweb.Extensions/Thirdweb.Extensions.Tests.cs +++ b/Thirdweb.Tests/Thirdweb.Extensions/Thirdweb.Extensions.Tests.cs @@ -308,6 +308,16 @@ public async Task Transfer() Assert.True(receipt.TransactionHash.Length == 66); } + [Fact(Timeout = 120000)] + public async Task TransferERC20Override() + { + var token = await this.GetTokenERC20Contract(); + var wallet = await this.GetSmartWallet(); + var receipt = await wallet.Transfer(this._chainId, await wallet.GetAddress(), BigInteger.Zero, token.Address); + Assert.NotNull(receipt); + Assert.True(receipt.TransactionHash.Length == 66); + } + [Fact(Timeout = 120000)] public async Task Contract_Read() { diff --git a/Thirdweb/Thirdweb.Extensions/ThirdwebExtensions.cs b/Thirdweb/Thirdweb.Extensions/ThirdwebExtensions.cs index 2b114db..9ed9c62 100644 --- a/Thirdweb/Thirdweb.Extensions/ThirdwebExtensions.cs +++ b/Thirdweb/Thirdweb.Extensions/ThirdwebExtensions.cs @@ -265,17 +265,25 @@ public static async Task GetTransactionCount(this IThirdwebWallet wa } /// - /// Transfers the specified amount of Wei to the specified address. + /// Transfers the specified amount of Wei to the specified address. Passing tokenAddress will override this function to transfer ERC20 tokens. /// /// The wallet to transfer from. /// The chain ID to transfer on. /// The address to transfer to. /// The amount of Wei to transfer. + /// The optional token address to transfer from. Defaults to the native token address (ETH or equivalent). + /// A task that represents the asynchronous operation. The task result contains the transaction receipt. /// A task that represents the asynchronous operation. The task result contains the transaction receipt. /// Thrown when the wallet is null. /// Thrown when the chain ID is less than or equal to 0. /// Thrown when the recipient address is null or empty. - public static async Task Transfer(this IThirdwebWallet wallet, BigInteger chainId, string toAddress, BigInteger weiAmount) + public static async Task Transfer( + this IThirdwebWallet wallet, + BigInteger chainId, + string toAddress, + BigInteger weiAmount, + string tokenAddress = Constants.NATIVE_TOKEN_ADDRESS + ) { if (wallet == null) { @@ -297,9 +305,17 @@ public static async Task Transfer(this IThirdwebWall throw new ArgumentOutOfRangeException(nameof(weiAmount), "Amount must be 0 or greater."); } - var txInput = new ThirdwebTransactionInput(chainId) { To = toAddress, Value = new HexBigInteger(weiAmount) }; - var tx = await ThirdwebTransaction.Create(wallet, txInput).ConfigureAwait(false); - return await ThirdwebTransaction.SendAndWaitForTransactionReceipt(tx).ConfigureAwait(false); + if (tokenAddress != Constants.NATIVE_TOKEN_ADDRESS) + { + var erc20Contract = await ThirdwebContract.Create(wallet.Client, tokenAddress, chainId).ConfigureAwait(false); + return await erc20Contract.ERC20_Transfer(wallet, toAddress, weiAmount).ConfigureAwait(false); + } + else + { + var txInput = new ThirdwebTransactionInput(chainId) { To = toAddress, Value = new HexBigInteger(weiAmount) }; + var tx = await ThirdwebTransaction.Create(wallet, txInput).ConfigureAwait(false); + return await ThirdwebTransaction.SendAndWaitForTransactionReceipt(tx).ConfigureAwait(false); + } } ///