Skip to content

Commit

Permalink
Bath requests added to common rpc methods
Browse files Browse the repository at this point in the history
EthGetBlockWithTransactionsHashesByHash, EthGetBlockWithTransactionsByNumber, EthGetBlockWithTransactionsHashesByNumber, EthGetBalance, EthCall, EthGetTransactionByHash, EthGetTransactionReceipt

Have now SendBatchRequestAsync included with an array of parameters
  • Loading branch information
juanfranblanco committed Jul 27, 2023
1 parent b1a51c6 commit b031a3b
Show file tree
Hide file tree
Showing 17 changed files with 229 additions and 11 deletions.
Expand Up @@ -34,7 +34,7 @@ public void DecodeResponse(RpcResponseMessage rpcResponse)
catch
{
this.HasError = true;
this.RpcError = new RpcError(-1, "Invalid format excecption");
this.RpcError = new RpcError(-1, "Invalid format exception");
}
}
}
Expand Down
33 changes: 33 additions & 0 deletions src/Nethereum.RPC/Eth/Blocks/EthGetBlockByHash.cs
@@ -1,4 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Nethereum.Hex.HexConvertors.Extensions;
using Nethereum.JsonRpc.Client;
Expand Down Expand Up @@ -82,6 +84,21 @@ public Task<BlockWithTransactions> SendRequestAsync(string blockHash, object id
return base.SendRequestAsync(id, blockHash.EnsureHexPrefix(), true);
}

#if !DOTNET35
public async Task<List<BlockWithTransactions>> SendBatchRequestAsync(params string[] blockHashes)
{
var batchRequest = new RpcRequestResponseBatch();
for (int i = 0; i < blockHashes.Length; i++)
{
batchRequest.BatchItems.Add(new RpcRequestResponseBatchItem<EthGetBlockWithTransactionsByHash, BlockWithTransactions>(this, BuildRequest(blockHashes[i], i)));
}

var response = await Client.SendBatchRequestAsync(batchRequest);
return response.BatchItems.Select(x => ((RpcRequestResponseBatchItem<EthGetBlockWithTransactionsByHash, BlockWithTransactions>)x).Response).ToList();

}
#endif

public RpcRequest BuildRequest(string blockHash, object id = null)
{
return base.BuildRequest(id, blockHash.EnsureHexPrefix(), true);
Expand Down Expand Up @@ -166,6 +183,22 @@ public Task<BlockWithTransactionHashes> SendRequestAsync(string blockHash, objec
return base.SendRequestAsync(id, blockHash.EnsureHexPrefix(), false);
}


#if !DOTNET35
public async Task<List<BlockWithTransactionHashes>> SendBatchRequestAsync(params string[] blockHashes)
{
var batchRequest = new RpcRequestResponseBatch();
for (int i = 0; i < blockHashes.Length; i++)
{
batchRequest.BatchItems.Add(new RpcRequestResponseBatchItem<EthGetBlockWithTransactionsHashesByHash, BlockWithTransactionHashes>(this, BuildRequest(blockHashes[i], i)));
}

var response = await Client.SendBatchRequestAsync(batchRequest);
return response.BatchItems.Select(x => ((RpcRequestResponseBatchItem<EthGetBlockWithTransactionsHashesByHash, BlockWithTransactionHashes>)x).Response).ToList();

}
#endif

public RpcRequest BuildRequest(string blockHash, object id = null)
{
if (blockHash == null) throw new ArgumentNullException(nameof(blockHash));
Expand Down
@@ -1,4 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

using Nethereum.Hex.HexTypes;
Expand Down Expand Up @@ -96,6 +98,21 @@ public Task<BlockWithTransactions> SendRequestAsync(HexBigInteger number, object
return base.SendRequestAsync(id, number, true);
}

#if !DOTNET35
public async Task<List<BlockWithTransactions>> SendBatchRequestAsync(params HexBigInteger[] numbers)
{
var batchRequest = new RpcRequestResponseBatch();
for (int i = 0; i < numbers.Length; i++)
{
batchRequest.BatchItems.Add(new RpcRequestResponseBatchItem<EthGetBlockWithTransactionsByNumber, BlockWithTransactions>(this, BuildRequest(numbers[i], i)));
}

var response = await Client.SendBatchRequestAsync(batchRequest);
return response.BatchItems.Select(x => ((RpcRequestResponseBatchItem<EthGetBlockWithTransactionsByNumber, BlockWithTransactions>)x).Response).ToList();

}
#endif

public RpcRequest BuildRequest(HexBigInteger number, object id = null)
{
if (number == null) throw new ArgumentNullException(nameof(number));
Expand Down
@@ -1,4 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

using Nethereum.Hex.HexTypes;
Expand Down Expand Up @@ -96,6 +98,21 @@ public Task<BlockWithTransactionHashes> SendRequestAsync(BlockParameter blockPar
return base.SendRequestAsync(id, blockParameter, false);
}

#if !DOTNET35
public async Task<List<BlockWithTransactionHashes>> SendBatchRequestAsync(params HexBigInteger[] numbers)
{
var batchRequest = new RpcRequestResponseBatch();
for (int i = 0; i < numbers.Length; i++)
{
batchRequest.BatchItems.Add(new RpcRequestResponseBatchItem<EthGetBlockWithTransactionsHashesByNumber, BlockWithTransactionHashes>(this, BuildRequest(numbers[i], i)));
}

var response = await Client.SendBatchRequestAsync(batchRequest);
return response.BatchItems.Select(x => ((RpcRequestResponseBatchItem<EthGetBlockWithTransactionsHashesByNumber, BlockWithTransactionHashes>)x).Response).ToList();

}
#endif

public RpcRequest BuildRequest(HexBigInteger number, object id = null)
{
if (number == null) throw new ArgumentNullException(nameof(number));
Expand Down
@@ -1,4 +1,5 @@
using System.Threading.Tasks;
using System.Collections.Generic;
using System.Threading.Tasks;
using Nethereum.JsonRpc.Client;
using Nethereum.RPC.Eth.DTOs;

Expand All @@ -7,6 +8,9 @@ namespace Nethereum.RPC.Eth.Blocks
public interface IEthGetBlockWithTransactionsByHash
{
RpcRequest BuildRequest(string blockHash, object id = null);
#if !DOTNET35
Task<List<BlockWithTransactions>> SendBatchRequestAsync(params string[] blockHashes);
#endif
Task<BlockWithTransactions> SendRequestAsync(string blockHash, object id = null);
}
}
@@ -1,4 +1,5 @@
using System.Threading.Tasks;
using System.Collections.Generic;
using System.Threading.Tasks;
using Nethereum.Hex.HexTypes;
using Nethereum.JsonRpc.Client;
using Nethereum.RPC.Eth.DTOs;
Expand All @@ -9,6 +10,10 @@ public interface IEthGetBlockWithTransactionsByNumber
{
RpcRequest BuildRequest(BlockParameter blockParameter, object id = null);
RpcRequest BuildRequest(HexBigInteger number, object id = null);

#if !DOTNET35
Task<List<BlockWithTransactions>> SendBatchRequestAsync(params HexBigInteger[] numbers);
#endif
Task<BlockWithTransactions> SendRequestAsync(BlockParameter blockParameter, object id = null);
Task<BlockWithTransactions> SendRequestAsync(HexBigInteger number, object id = null);
}
Expand Down
@@ -1,4 +1,5 @@
using System.Threading.Tasks;
using System.Collections.Generic;
using System.Threading.Tasks;
using Nethereum.JsonRpc.Client;
using Nethereum.RPC.Eth.DTOs;

Expand All @@ -7,6 +8,9 @@ namespace Nethereum.RPC.Eth.Blocks
public interface IEthGetBlockWithTransactionsHashesByHash
{
RpcRequest BuildRequest(string blockHash, object id = null);
#if !DOTNET35
Task<List<BlockWithTransactionHashes>> SendBatchRequestAsync(params string[] blockHashes);
#endif
Task<BlockWithTransactionHashes> SendRequestAsync(string blockHash, object id = null);
}
}
@@ -1,4 +1,5 @@
using System.Threading.Tasks;
using System.Collections.Generic;
using System.Threading.Tasks;
using Nethereum.Hex.HexTypes;
using Nethereum.JsonRpc.Client;
using Nethereum.RPC.Eth.DTOs;
Expand All @@ -9,6 +10,9 @@ public interface IEthGetBlockWithTransactionsHashesByNumber
{
RpcRequest BuildRequest(BlockParameter blockParameter, object id = null);
RpcRequest BuildRequest(HexBigInteger number, object id = null);
#if !DOTNET35
Task<List<BlockWithTransactionHashes>> SendBatchRequestAsync(params HexBigInteger[] numbers);
#endif
Task<BlockWithTransactionHashes> SendRequestAsync(BlockParameter blockParameter, object id = null);
Task<BlockWithTransactionHashes> SendRequestAsync(HexBigInteger number, object id = null);
}
Expand Down
23 changes: 23 additions & 0 deletions src/Nethereum.RPC/Eth/EthGetBalance.cs
@@ -1,4 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

using Nethereum.Hex.HexConvertors.Extensions;
Expand Down Expand Up @@ -59,11 +61,32 @@ public Task<HexBigInteger> SendRequestAsync(string address, object id = null)
return base.SendRequestAsync(id, address.EnsureHexPrefix(), DefaultBlock);
}

#if !DOTNET35
public async Task<List<HexBigInteger>> SendBatchRequestAsync(string[] addresses, BlockParameter block)
{
var batchRequest = new RpcRequestResponseBatch();
for (int i = 0; i < addresses.Length; i++)
{
batchRequest.BatchItems.Add(new RpcRequestResponseBatchItem<EthGetBalance, HexBigInteger>(this, BuildRequest(addresses[i], block, i)));
}

var response = await Client.SendBatchRequestAsync(batchRequest);
return response.BatchItems.Select(x => ((RpcRequestResponseBatchItem<EthGetBalance, HexBigInteger>)x).Response).ToList();

}

public Task<List<HexBigInteger>> SendBatchRequestAsync(params string[] addresses)
{
return SendBatchRequestAsync(addresses, DefaultBlock);
}
#endif

public RpcRequest BuildRequest(string address, BlockParameter block, object id = null)
{
if (address == null) throw new ArgumentNullException(nameof(address));
if (block == null) throw new ArgumentNullException(nameof(block));
return base.BuildRequest(id, address.EnsureHexPrefix(), block);
}

}
}
7 changes: 6 additions & 1 deletion src/Nethereum.RPC/Eth/IEthGetBalance.cs
@@ -1,4 +1,5 @@
using System.Threading.Tasks;
using System.Collections.Generic;
using System.Threading.Tasks;
using Nethereum.Hex.HexTypes;
using Nethereum.JsonRpc.Client;
using Nethereum.RPC.Eth.DTOs;
Expand All @@ -10,6 +11,10 @@ public interface IEthGetBalance
BlockParameter DefaultBlock { get; set; }

RpcRequest BuildRequest(string address, BlockParameter block, object id = null);
#if !DOTNET35
Task<List<HexBigInteger>> SendBatchRequestAsync(string[] addresses, BlockParameter block);
Task<List<HexBigInteger>> SendBatchRequestAsync(params string[] addresses);
#endif
Task<HexBigInteger> SendRequestAsync(string address, object id = null);
Task<HexBigInteger> SendRequestAsync(string address, BlockParameter block, object id = null);
}
Expand Down
22 changes: 22 additions & 0 deletions src/Nethereum.RPC/Eth/Transactions/EthCall.cs
@@ -1,4 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

using Nethereum.JsonRpc.Client;
Expand Down Expand Up @@ -54,6 +56,26 @@ public Task<string> SendRequestAsync(CallInput callInput, object id = null)
return base.SendRequestAsync(id, callInput, DefaultBlock);
}

#if !DOTNET35
public Task<List<string>> SendBatchRequestAsync(params CallInput[] callInputs)
{
return SendBatchRequestAsync(callInputs, DefaultBlock);
}

public async Task<List<string>> SendBatchRequestAsync(CallInput[] callInputs, BlockParameter block)
{
var batchRequest = new RpcRequestResponseBatch();
for (int i = 0; i < callInputs.Length; i++)
{
batchRequest.BatchItems.Add(new RpcRequestResponseBatchItem<EthCall, string>(this, BuildRequest(callInputs[i], block, i)));
}

var response = await Client.SendBatchRequestAsync(batchRequest).ConfigureAwait(false);
return response.BatchItems.Select(x => ((RpcRequestResponseBatchItem<EthCall, string>)x).Response).ToList();

}
#endif

public RpcRequest BuildRequest(CallInput callInput, BlockParameter block, object id = null)
{
if (callInput == null) throw new ArgumentNullException(nameof(callInput));
Expand Down
17 changes: 17 additions & 0 deletions src/Nethereum.RPC/Eth/Transactions/EthGetTransactionByHash.cs
@@ -1,3 +1,5 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

using Nethereum.Hex.HexConvertors.Extensions;
Expand Down Expand Up @@ -61,6 +63,21 @@ public Task<Transaction> SendRequestAsync(string hashTransaction, object id = nu
return base.SendRequestAsync(id, hashTransaction.EnsureHexPrefix());
}

#if !DOTNET35
public async Task<List<Transaction>> SendBatchRequestAsync(string[] transactionHashes)
{
var batchRequest = new RpcRequestResponseBatch();
for (int i = 0; i < transactionHashes.Length; i++)
{
batchRequest.BatchItems.Add(new RpcRequestResponseBatchItem<EthGetTransactionByHash, Transaction>(this, BuildRequest(transactionHashes[i], i)));
}

var response = await Client.SendBatchRequestAsync(batchRequest);
return response.BatchItems.Select(x => ((RpcRequestResponseBatchItem<EthGetTransactionByHash, Transaction>)x).Response).ToList();

}
#endif

public RpcRequest BuildRequest(string hashTransaction, object id = null)
{
return base.BuildRequest(id, hashTransaction.EnsureHexPrefix());
Expand Down
17 changes: 17 additions & 0 deletions src/Nethereum.RPC/Eth/Transactions/EthGetTransactionReceipt.cs
@@ -1,4 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

using Nethereum.Hex.HexConvertors.Extensions;
Expand Down Expand Up @@ -60,6 +62,21 @@ public Task<TransactionReceipt> SendRequestAsync(string transactionHash, object
return base.SendRequestAsync(id, transactionHash.EnsureHexPrefix());
}

#if !DOTNET35
public async Task<List<TransactionReceipt>> SendBatchRequestAsync(string[] transactionHashes)
{
var batchRequest = new RpcRequestResponseBatch();
for (int i = 0; i < transactionHashes.Length; i++)
{
batchRequest.BatchItems.Add(new RpcRequestResponseBatchItem<EthGetTransactionReceipt, TransactionReceipt>(this, BuildRequest(transactionHashes[i], i)));
}

var response = await Client.SendBatchRequestAsync(batchRequest);
return response.BatchItems.Select(x => ((RpcRequestResponseBatchItem<EthGetTransactionReceipt, TransactionReceipt>)x).Response).ToList();

}
#endif

public RpcRequest BuildRequest(string transactionHash, object id = null)
{
if (transactionHash == null) throw new ArgumentNullException(nameof(transactionHash));
Expand Down
7 changes: 6 additions & 1 deletion src/Nethereum.RPC/Eth/Transactions/IEthCall.cs
@@ -1,4 +1,5 @@
using System.Threading.Tasks;
using System.Collections.Generic;
using System.Threading.Tasks;
using Nethereum.JsonRpc.Client;
using Nethereum.RPC.Eth.DTOs;

Expand All @@ -9,6 +10,10 @@ public interface IEthCall
BlockParameter DefaultBlock { get; set; }

RpcRequest BuildRequest(CallInput callInput, BlockParameter block, object id = null);
#if !DOTNET35
Task<List<string>> SendBatchRequestAsync(params CallInput[] callInputs);
Task<List<string>> SendBatchRequestAsync(CallInput[] callInputs, BlockParameter block);
#endif
Task<string> SendRequestAsync(CallInput callInput, object id = null);
Task<string> SendRequestAsync(CallInput callInput, BlockParameter block, object id = null);
}
Expand Down
@@ -1,4 +1,5 @@
using System.Threading.Tasks;
using System.Collections.Generic;
using System.Threading.Tasks;
using Nethereum.JsonRpc.Client;
using Nethereum.RPC.Eth.DTOs;

Expand All @@ -7,6 +8,9 @@ namespace Nethereum.RPC.Eth.Transactions
public interface IEthGetTransactionByHash
{
RpcRequest BuildRequest(string hashTransaction, object id = null);
#if !DOTNET35
Task<List<Transaction>> SendBatchRequestAsync(string[] transactionHashes);
#endif
Task<Transaction> SendRequestAsync(string hashTransaction, object id = null);
}
}
@@ -1,4 +1,5 @@
using System.Threading.Tasks;
using System.Collections.Generic;
using System.Threading.Tasks;
using Nethereum.JsonRpc.Client;
using Nethereum.RPC.Eth.DTOs;

Expand All @@ -7,6 +8,9 @@ namespace Nethereum.RPC.Eth.Transactions
public interface IEthGetTransactionReceipt
{
RpcRequest BuildRequest(string transactionHash, object id = null);
#if !DOTNET35
Task<List<TransactionReceipt>> SendBatchRequestAsync(string[] transactionHashes);
#endif
Task<TransactionReceipt> SendRequestAsync(string transactionHash, object id = null);
}
}

0 comments on commit b031a3b

Please sign in to comment.