-
Notifications
You must be signed in to change notification settings - Fork 244
Expand file tree
/
Copy pathHyperPlayTransactionExecutor.cs
More file actions
69 lines (62 loc) 路 2.66 KB
/
Copy pathHyperPlayTransactionExecutor.cs
File metadata and controls
69 lines (62 loc) 路 2.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using ChainSafe.Gaming.Evm.Providers;
using ChainSafe.Gaming.Evm.Signers;
using ChainSafe.Gaming.Evm.Transactions;
using ChainSafe.Gaming.Web3;
using ChainSafe.Gaming.Web3.Core.Evm;
using ChainSafe.Gaming.Web3.Evm.Wallet;
using Nethereum.RPC.Eth.DTOs;
namespace ChainSafe.Gaming.HyperPlay
{
/// <summary>
/// Concrete implementation of <see cref="ITransactionExecutor"/> via HyperPlay desktop client.
/// </summary>
public class HyperPlayTransactionExecutor : ITransactionExecutor
{
private readonly IWalletProvider walletProvider;
private readonly ISigner signer;
/// <summary>
/// Initializes a new instance of the <see cref="HyperPlayTransactionExecutor"/> class.
/// </summary>
/// <param name="walletProvider">HyperPlay provider for making RPC requests.</param>
/// <param name="signer">Signer for fetching public address.</param>
public HyperPlayTransactionExecutor(IWalletProvider walletProvider, ISigner signer)
{
this.walletProvider = walletProvider;
this.signer = signer;
}
/// <summary>
/// Send a Transaction via HyperPlay desktop Client.
/// This prompts user to approve a transaction on HyperPlay.
/// </summary>
/// <param name="transaction">Transaction to send.</param>
/// <returns>Hash response of a successfully executed transaction.</returns>
/// <exception cref="Web3Exception">Throws Exception if executing transaction fails.</exception>
public async Task<TransactionResponse> SendTransaction(TransactionRequest transaction)
{
if (string.IsNullOrEmpty(transaction.From))
{
transaction.From = signer.PublicAddress;
}
TransactionInput transactionInput = new TransactionInput
{
From = transaction.From,
To = transaction.To,
Gas = transaction.GasLimit,
GasPrice = transaction.GasPrice,
Value = transaction.Value,
Data = transaction.Data ?? "0x",
Nonce = transaction.Nonce,
AccessList = transaction.AccessList,
};
string hash = await walletProvider.Perform<string>("eth_sendTransaction", transactionInput);
string hashPattern = @"^0x[a-fA-F0-9]{64}$";
if (!Regex.IsMatch(hash, hashPattern))
{
throw new Web3Exception($"incorrect txn response format {hash}");
}
return await walletProvider.GetTransaction(hash);
}
}
}