Skip to content

Commit

Permalink
Package built-in dependencies fix (#925)
Browse files Browse the repository at this point in the history
* Package built-in dependencies fix

* Added polling and timeout to GetTransaction method
Introduced DelayUtil

---------

Co-authored-by: oleksandr <[email protected]>
Co-authored-by: Robel Getnet Geremew <[email protected]>
  • Loading branch information
3 people committed Jan 16, 2025
1 parent 4086d02 commit 470929d
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 16 deletions.
4 changes: 2 additions & 2 deletions scripts/data/published_dependencies.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
Packages/io.chainsafe.web3-unity/Runtime/Libraries/:ADRaffy.ENSNormalize;Nethereum.Model;BouncyCastle.Crypto;Nethereum.RLP;ChainSafe.Gaming.Debugging;Nethereum.RPC;ChainSafe.Gaming.Gelato;ChainSafe.Gaming.SygmaClient;Nethereum.Signer.EIP712;ChainSafe.Gaming.InProcessSigner;Nethereum.Signer;ChainSafe.Gaming.InProcessTransactionExecutor;Nethereum.Util;ChainSafe.Gaming.Unity.ThirdParty;Nethereum.Web3;ChainSafe.Gaming.Unity;System.Buffers;ChainSafe.Gaming.WalletConnect;System.Memory;ChainSafe.Gaming;System.Numerics.Vectors;Microsoft.Bcl.AsyncInterfaces;System.Reactive;Microsoft.Extensions.DependencyInjection.Abstractions;System.Runtime.CompilerServices.Unsafe;Microsoft.Extensions.DependencyInjection;System.Runtime.InteropServices.WindowsRuntime;Microsoft.Extensions.Logging.Abstractions;System.Security.Cryptography.Cng;Microsoft.IdentityModel.Abstractions;System.Text.Encodings.Web;Microsoft.IdentityModel.Logging;System.Text.Json;Microsoft.IdentityModel.Tokens;System.Threading.Channels;NBitcoin;System.Threading.Tasks.Extensions;Nethereum.ABI;WalletConnectSharp.Auth;Nethereum.Accounts;WalletConnectSharp.Common;WalletConnectSharp.Events;Nethereum.BlockchainProcessing;WalletConnectSharp.Core;Nethereum.Contracts;WalletConnectSharp.Crypto;Nethereum.Hex;Nethereum.JsonRpc.Client;WalletConnectSharp.Network.Websocket;Nethereum.JsonRpc.IpcClient;WalletConnectSharp.Network;Nethereum.JsonRpc.RpcClient;WalletConnectSharp.Sign;Nethereum.KeyStore;WalletConnectSharp.Storage;Nethereum.Merkle.Patricia;WalletConnectSharp.Web3Wallet;Nethereum.Merkle;Websocket.Client;Nethereum.Metamask;Nethereum.Siwe.Core;Nethereum.Siwe;Nethereum.UI;Nethereum.Unity.Metamask;Nethereum.Unity;ChainSafe.Gaming.MetaMask;ChainSafe.Gaming.MetaMask.Unity;ChainSafe.Gaming.InProcessTransactionExecutor.Unity
Packages/io.chainsafe.web3-unity.lootboxes/Chainlink/Runtime/Libraries/:Chainsafe.Gaming.Chainlink;ChainSafe.Gaming.Lootboxes.Chainlink;ChainSafe.Gaming.Marketplace
Packages/io.chainsafe.web3-unity/Runtime/Libraries/:ADRaffy.ENSNormalize;Nethereum.Model;BouncyCastle.Crypto;Nethereum.RLP;ChainSafe.Gaming.Debugging;Nethereum.RPC;ChainSafe.Gaming.Gelato;ChainSafe.Gaming.SygmaClient;Nethereum.Signer.EIP712;ChainSafe.Gaming.InProcessSigner;Nethereum.Signer;ChainSafe.Gaming.InProcessTransactionExecutor;Nethereum.Util;ChainSafe.Gaming.Unity.ThirdParty;Nethereum.Web3;ChainSafe.Gaming.Unity;System.Buffers;ChainSafe.Gaming.WalletConnect;System.Memory;ChainSafe.Gaming;System.Numerics.Vectors;Microsoft.Bcl.AsyncInterfaces;System.Reactive;Microsoft.Extensions.DependencyInjection.Abstractions;System.Runtime.CompilerServices.Unsafe;Microsoft.Extensions.DependencyInjection;System.Runtime.InteropServices.WindowsRuntime;Microsoft.Extensions.Logging.Abstractions;System.Security.Cryptography.Cng;Microsoft.IdentityModel.Abstractions;System.Text.Encodings.Web;Microsoft.IdentityModel.Logging;System.Text.Json;Microsoft.IdentityModel.Tokens;System.Threading.Channels;NBitcoin;System.Threading.Tasks.Extensions;Nethereum.ABI;WalletConnectSharp.Auth;Nethereum.Accounts;WalletConnectSharp.Common;WalletConnectSharp.Events;Nethereum.BlockchainProcessing;WalletConnectSharp.Core;Nethereum.Contracts;WalletConnectSharp.Crypto;Nethereum.Hex;Nethereum.JsonRpc.Client;WalletConnectSharp.Network.Websocket;Nethereum.JsonRpc.IpcClient;WalletConnectSharp.Network;Nethereum.JsonRpc.RpcClient;WalletConnectSharp.Sign;Nethereum.KeyStore;WalletConnectSharp.Storage;Nethereum.Merkle.Patricia;WalletConnectSharp.Web3Wallet;Nethereum.Merkle;Websocket.Client;Nethereum.Metamask;Nethereum.Siwe.Core;Nethereum.Siwe;Nethereum.UI;Nethereum.Unity.Metamask;Nethereum.Unity;ChainSafe.Gaming.MetaMask;ChainSafe.Gaming.MetaMask.Unity;ChainSafe.Gaming.InProcessTransactionExecutor.Unity;ChainSafe.Gaming.Marketplace
Packages/io.chainsafe.web3-unity.lootboxes/Chainlink/Runtime/Libraries/:Chainsafe.Gaming.Chainlink;ChainSafe.Gaming.Lootboxes.Chainlink
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,6 @@ public async Task<TransactionResponse> SendTransaction(TransactionRequest transa

WCLogger.Log($"Transaction executed successfully. Hash: {hash}.");

await Task.Delay(TimeSpan.FromSeconds(1)); // hack: rpcProvider.GetTransaction can't find transaction by hash

return await rpcProvider.GetTransaction(hash);

bool ValidateResponseHash(string hash)
Expand Down
26 changes: 26 additions & 0 deletions src/ChainSafe.Gaming/Web3/Core/DelayUtil.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System;
using System.Threading.Tasks;

namespace ChainSafe.Gaming.Web3.Core
{
/// <summary>
/// Utility class for delay operations.
/// </summary>
public static class DelayUtil
{
/// <summary>
/// Replicates Task.Delay behaviour. Works on a single-threaded platforms like Unity WebGL.
/// </summary>
/// <param name="delay">The time to wait.</param>
/// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
public static async Task SafeDelay(TimeSpan delay) // TODO: use default method if platform supports multithreading
{
var start = DateTime.UtcNow;

while (DateTime.UtcNow - start < delay)
{
await Task.Yield();
}
}
}
}
38 changes: 26 additions & 12 deletions src/ChainSafe.Gaming/Web3/Core/Evm/RpcProviderExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
using System;
using System.Collections.Generic;
using System.Numerics;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
using ChainSafe.Gaming.Evm.Transactions;
using ChainSafe.Gaming.Web3;
using ChainSafe.Gaming.Web3.Core;
using Nethereum.Hex.HexConvertors.Extensions;
using Nethereum.Hex.HexTypes;
using Nethereum.RPC.Eth.DTOs;
Expand Down Expand Up @@ -289,38 +288,53 @@ public static async Task<HexBigInteger> EstimateGas(this IRpcProvider provider,
/// </summary>
/// <param name="provider">The RPC provider.</param>
/// <param name="transactionHash">The hash of the transaction to retrieve.</param>
/// <param name="timeOut">(Optional) The time after which the method will fail if we can't find the transaction by hash. 15 seconds by default.</param>
/// <returns>
/// A <see cref="Task"/> representing the asynchronous operation.
/// The task result contains the transaction details as a <see cref="TransactionResponse"/>.
/// </returns>
public static async Task<TransactionResponse> GetTransaction(this IRpcProvider provider, string transactionHash)
public static async Task<TransactionResponse> GetTransaction(this IRpcProvider provider, string transactionHash, TimeSpan? timeOut = null)
{
timeOut ??= TimeSpan.FromSeconds(15);

// Poll transaction till it's available on the given node
TransactionResponse transaction = null;
var parameters = new object[] { transactionHash };
var pollStartTime = DateTime.UtcNow;
while (DateTime.UtcNow - pollStartTime < timeOut)
{
transaction = await provider.Perform<TransactionResponse>("eth_getTransactionByHash", parameters);

var result = await provider.Perform<TransactionResponse>("eth_getTransactionByHash", parameters);
if (transaction != null)
{
break;
}

if (result == null)
await DelayUtil.SafeDelay(TimeSpan.FromSeconds(1));
}

if (transaction == null)
{
throw new Web3Exception("transaction not found");
throw new Web3Exception("Transaction not found.");
}

if (result.BlockNumber == null)
if (transaction.BlockNumber == null)
{
result.Confirmations = 0;
transaction.Confirmations = 0;
}
else if (result.Confirmations == null)
else if (transaction.Confirmations == null)
{
var blockNumber = await provider.GetBlockNumber();
var confirmations = (blockNumber.ToUlong() - result.BlockNumber.ToUlong()) + 1;
var confirmations = (blockNumber.ToUlong() - transaction.BlockNumber.ToUlong()) + 1;
if (confirmations <= 0)
{
confirmations = 1;
}

result.Confirmations = confirmations;
transaction.Confirmations = confirmations;
}

return result;
return transaction;
}

/// <summary>
Expand Down

0 comments on commit 470929d

Please sign in to comment.