Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NFT Extensions + RPC Caching #35

Merged
merged 7 commits into from
Jun 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 2 additions & 5 deletions Thirdweb.Tests/Thirdweb.CommonExtension.Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,16 @@ public async Task NullChecks()
var client = ThirdwebClient.Create(secretKey: _secretKey);
var contract = await GetContract();

client = null;
_ = await Assert.ThrowsAsync<ArgumentNullException>(async () => await contract.GetBalance(client));

contract = null;
_ = await Assert.ThrowsAsync<ArgumentNullException>(async () => await contract.GetBalance(client));
_ = await Assert.ThrowsAsync<ArgumentNullException>(async () => await contract.GetBalance());
}

[Fact]
public async Task GetBalance()
{
var client = ThirdwebClient.Create(secretKey: _secretKey);
var contract = await GetContract();
var balance = await contract.GetBalance(client);
var balance = await contract.GetBalance();
Assert.True(balance >= 0);
}
}
Expand Down
27 changes: 27 additions & 0 deletions Thirdweb.Tests/Thirdweb.ERC1155Extension.Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,10 @@ public async Task NullChecks()

_ = await Assert.ThrowsAsync<ArgumentNullException>(async () => await contract.ERC1155_IsApprovedForAll(null, null));

_ = await Assert.ThrowsAsync<ArgumentNullException>(async () => await contract.ERC1155_TotalSupply());

_ = await Assert.ThrowsAsync<ArgumentNullException>(async () => await contract.ERC1155_TotalSupply(BigInteger.Zero));

_ = await Assert.ThrowsAsync<ArgumentNullException>(
async () => await contract.ERC1155_SafeTransferFrom(wallet, Constants.ADDRESS_ZERO, Constants.ADDRESS_ZERO, BigInteger.Zero, BigInteger.Zero, null)
);
Expand Down Expand Up @@ -155,6 +159,29 @@ public async Task ERC1155_SetApprovalForAll()
Assert.True(receipt.TransactionHash.Length == 66);
}

[Fact]
public async Task ERC1155_TotalSupply()
{
var client = ThirdwebClient.Create(secretKey: _secretKey);
var contract = await ThirdwebContract.Create(client, _erc1155ContractAddress, _chainId);

var totalSupply = await contract.ERC1155_TotalSupply();

Assert.True(totalSupply >= 0);
}

[Fact]
public async Task ERC1155_TotalSupply_WithTokenId()
{
var client = ThirdwebClient.Create(secretKey: _secretKey);
var contract = await ThirdwebContract.Create(client, _erc1155ContractAddress, _chainId);
var tokenId = BigInteger.Parse("1");

var totalSupply = await contract.ERC1155_TotalSupply(tokenId);

Assert.True(totalSupply >= 0);
}

// TODO: Update when mint implemented
// [Fact]
// public async Task ERC1155_SafeTransferFrom()
Expand Down
31 changes: 31 additions & 0 deletions Thirdweb.Tests/Thirdweb.ERC721Extension.Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ public async Task NullChecks()
_ = await Assert.ThrowsAsync<ArgumentException>(async () => await contract.ERC721_SetApprovalForAll(wallet, null, false));
_ = await Assert.ThrowsAsync<ArgumentException>(async () => await contract.ERC721_SetApprovalForAll(wallet, string.Empty, false));

_ = await Assert.ThrowsAsync<ArgumentException>(async () => await contract.ERC721_TokenOfOwnerByIndex(null, BigInteger.Zero));
_ = await Assert.ThrowsAsync<ArgumentException>(async () => await contract.ERC721_TokenOfOwnerByIndex(string.Empty, BigInteger.Zero));

contract = null;

_ = await Assert.ThrowsAsync<ArgumentNullException>(async () => await contract.ERC721_BalanceOf(Constants.ADDRESS_ZERO));
Expand All @@ -80,6 +83,10 @@ public async Task NullChecks()
_ = await Assert.ThrowsAsync<ArgumentNullException>(async () => await contract.ERC721_SafeTransferFrom(null, null, null, BigInteger.Zero));

_ = await Assert.ThrowsAsync<ArgumentNullException>(async () => await contract.ERC721_Approve(null, null, BigInteger.Zero));

_ = await Assert.ThrowsAsync<ArgumentNullException>(async () => await contract.ERC721_TotalSupply());

_ = await Assert.ThrowsAsync<ArgumentNullException>(async () => await contract.ERC721_TokenOfOwnerByIndex(Constants.ADDRESS_ZERO, BigInteger.Zero));
}

[Fact]
Expand Down Expand Up @@ -166,6 +173,30 @@ public async Task ERC721_IsApprovedForAll()
Assert.True(isApproved || !isApproved);
}

[Fact]
public async Task ERC721_TotalSupply()
{
var client = ThirdwebClient.Create(secretKey: _secretKey);
var contract = await ThirdwebContract.Create(client, _erc721ContractAddress, _chainId);

var totalSupply = await contract.ERC721_TotalSupply();

Assert.True(totalSupply >= 0);
}

[Fact]
public async Task ERC721_TokenOfOwnerByIndex()
{
var client = ThirdwebClient.Create(secretKey: _secretKey);
var contract = await ThirdwebContract.Create(client, _erc721ContractAddress, _chainId);
var ownerAddress = "0xE33653ce510Ee767d8824b5EcDeD27125D49889D";
var index = BigInteger.Zero;

var tokenId = await contract.ERC721_TokenOfOwnerByIndex(ownerAddress, index);

Assert.True(tokenId >= 0);
}

[Fact]
public async Task ERC721_SetApprovalForAll()
{
Expand Down
141 changes: 141 additions & 0 deletions Thirdweb.Tests/Thirdweb.NFTExtension.Tests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
using System;
using System.Collections.Generic;
using System.Numerics;
using System.Threading.Tasks;
using Thirdweb;
using Xunit;
using Xunit.Abstractions;

namespace Thirdweb.Tests
{
public class NFTExtensionTests : BaseTests
{
private readonly string _erc721ContractAddress = "0xD811CB13169C175b64bf8897e2Fd6a69C6343f5C";
private readonly string _erc1155ContractAddress = "0x6A7a26c9a595E6893C255C9dF0b593e77518e0c3";
private readonly BigInteger _chainId = 421614;

public NFTExtensionTests(ITestOutputHelper output)
: base(output) { }

private async Task<ThirdwebContract> Get721Contract()
{
var client = ThirdwebClient.Create(secretKey: _secretKey);
var contract = await ThirdwebContract.Create(client, _erc721ContractAddress, _chainId);
return contract;
}

private async Task<ThirdwebContract> Get1155Contract()
{
var client = ThirdwebClient.Create(secretKey: _secretKey);
var contract = await ThirdwebContract.Create(client, _erc1155ContractAddress, _chainId);
return contract;
}

[Fact]
public async Task NullChecks()
{
var client = ThirdwebClient.Create(secretKey: _secretKey);
var contract721 = await Get721Contract();
var contract1155 = await Get1155Contract();

// ERC721 Null Checks
contract721 = null;
_ = await Assert.ThrowsAsync<ArgumentNullException>(async () => await contract721.ERC721_GetNFT(0));
_ = await Assert.ThrowsAsync<ArgumentNullException>(async () => await contract721.ERC721_GetAllNFTs());
_ = await Assert.ThrowsAsync<ArgumentNullException>(async () => await contract721.ERC721_GetOwnedNFTs("owner"));

// ERC1155 Null Checks
contract1155 = null;
_ = await Assert.ThrowsAsync<ArgumentNullException>(async () => await contract1155.ERC1155_GetNFT(0));
_ = await Assert.ThrowsAsync<ArgumentNullException>(async () => await contract1155.ERC1155_GetAllNFTs());
_ = await Assert.ThrowsAsync<ArgumentNullException>(async () => await contract1155.ERC1155_GetOwnedNFTs("owner"));
}

[Fact]
public async Task GetNFTBytes_721()
{
var contract = await Get721Contract();
var nft = await contract.ERC721_GetNFT(0);
var bytes = await nft.GetNFTImageBytes(contract.Client);
Assert.NotNull(bytes);
Assert.NotEmpty(bytes);
}

[Fact]
public async Task GetNFT_721()
{
var contract = await Get721Contract();
var nft = await contract.ERC721_GetNFT(0);
Assert.NotNull(nft.Owner);
Assert.NotEmpty(nft.Owner);
Assert.Equal(NFTType.ERC721, nft.Type);
Assert.True(nft.Supply == -1 || nft.Supply > 0);
Assert.True(nft.QuantityOwned == -1);
}

[Fact]
public async Task GetAllNFTs_721()
{
var contract = await Get721Contract();
var nfts = await contract.ERC721_GetAllNFTs();
Assert.NotNull(nfts);
Assert.NotEmpty(nfts);
}

[Fact]
public async Task GetAllNFTs_721_WithRange()
{
var contract = await Get721Contract();
var nfts = await contract.ERC721_GetAllNFTs(1, 2);
Assert.NotNull(nfts);
Assert.NotEmpty(nfts);
Assert.True(nfts.Count == 1);
}

[Fact]
public async Task GetOwnedNFTs_721()
{
var contract = await Get721Contract();
var ownerAddress = contract.Address;
var nfts = await contract.ERC721_GetOwnedNFTs(ownerAddress);
Assert.NotNull(nfts);
}

[Fact]
public async Task GetNFT_1155()
{
var contract = await Get1155Contract();
var nft = await contract.ERC1155_GetNFT(0);
Assert.Equal(NFTType.ERC1155, nft.Type);
Assert.True(nft.Supply >= 0);
}

[Fact]
public async Task GetAllNFTs_1155()
{
var contract = await Get1155Contract();
var nfts = await contract.ERC1155_GetAllNFTs();
Assert.NotNull(nfts);
Assert.NotEmpty(nfts);
}

[Fact]
public async Task GetAllNFTs_1155_WithRange()
{
var contract = await Get1155Contract();
var nfts = await contract.ERC1155_GetAllNFTs(1, 2);
Assert.NotNull(nfts);
Assert.NotEmpty(nfts);
Assert.True(nfts.Count == 1);
}

[Fact]
public async Task GetOwnedNFTs_1155()
{
var contract = await Get1155Contract();
var ownerAddress = contract.Address;
var nfts = await contract.ERC1155_GetOwnedNFTs(ownerAddress);
Assert.NotNull(nfts);
}
}
}
26 changes: 21 additions & 5 deletions Thirdweb/Thirdweb.Contracts/ThirdwebContract.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System.Numerics;
using Nethereum.Contracts;
using Nethereum.Hex.HexTypes;
using Nethereum.RPC.Eth.DTOs;

namespace Thirdweb
{
Expand Down Expand Up @@ -62,8 +62,8 @@ public static async Task<T> Read<T>(ThirdwebContract contract, string method, pa
{
var rpc = ThirdwebRPC.GetRpcInstance(contract.Client, contract.Chain);

var service = new Nethereum.Contracts.Contract(null, contract.Abi, contract.Address);
var function = service.GetFunction(method);
var contractRaw = new Contract(null, contract.Abi, contract.Address);
var function = GetFunctionMatchSignature(contractRaw, method, parameters);
var data = function.GetData(parameters);

var resultData = await rpc.SendRequestAsync<string>("eth_call", new { to = contract.Address, data = data, }, "latest");
Expand All @@ -72,8 +72,8 @@ public static async Task<T> Read<T>(ThirdwebContract contract, string method, pa

public static async Task<ThirdwebTransaction> Prepare(IThirdwebWallet wallet, ThirdwebContract contract, string method, BigInteger weiValue, params object[] parameters)
{
var service = new Nethereum.Contracts.Contract(null, contract.Abi, contract.Address);
var function = service.GetFunction(method);
var contractRaw = new Contract(null, contract.Abi, contract.Address);
var function = GetFunctionMatchSignature(contractRaw, method, parameters);
var data = function.GetData(parameters);
var transaction = new ThirdwebTransactionInput
{
Expand All @@ -91,5 +91,21 @@ public static async Task<ThirdwebTransactionReceipt> Write(IThirdwebWallet walle
var thirdwebTx = await Prepare(wallet, contract, method, weiValue, parameters);
return await ThirdwebTransaction.SendAndWaitForTransactionReceipt(thirdwebTx);
}

private static Function GetFunctionMatchSignature(Contract contract, string functionName, params object[] args)
{
var abi = contract.ContractBuilder.ContractABI;
var functions = abi.Functions;
var paramsCount = args?.Length ?? 0;
foreach (var function in functions)
{
if (function.Name == functionName && function.InputParameters.Length == paramsCount)
{
var sha = function.Sha3Signature;
return contract.GetFunctionBySignature(sha);
}
}
return null;
}
}
}
Loading
Loading