Skip to content

Commit

Permalink
make it arrow (#133)
Browse files Browse the repository at this point in the history
* make it arrow

* another try

* empty lines
  • Loading branch information
Lomet authored Aug 14, 2023
1 parent ee9fb4f commit 25e1e55
Showing 1 changed file with 11 additions and 27 deletions.
38 changes: 11 additions & 27 deletions src/EthSmartContractIO/ContractIO/ContractReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,53 +9,37 @@ namespace EthSmartContractIO.ContractIO;
/// </summary>
public class ContractReader : IContractIO
{
private const string resultName = "result";
private readonly RpcRequest request;

/// <summary>
/// Initializes a new instance of the <see cref="ContractReader"/> class.
/// </summary>
/// <param name="request">The <see cref="RpcRequest"/> to execute.</param>
public ContractReader(RpcRequest request)
{
this.request = request;
}
public ContractReader(RpcRequest request) => this.request = request;

/// <summary>
/// Executes a read action on the Ethereum network.
/// </summary>
/// <returns>The result of the action.</returns>
/// <exception cref="FlurlHttpException">Thrown when the HTTP request fails.</exception>
/// <exception cref="KeyNotFoundException">Thrown when the response does not contain the key 'result'.</exception>
public virtual string RunContractAction()
{
var input = CreateActionInput();

var response = request.RpcUrl.PostJsonAsync(input)
.GetAwaiter()
.GetResult();

return ParseResponse(response);
}
public virtual string RunContractAction() =>
PostRequest().TryGetValue(resultName, out var result)
? result.ToString()
: throw new KeyNotFoundException("Response does not contain the key 'result'.");

/// <summary>
/// Creates the input for the read action.
/// </summary>
/// <returns>The created <see cref="ReadRpcRequest"/>.</returns>
private ReadRpcRequest CreateActionInput() =>
private ReadRpcRequest CreateActionInput =>
new(request.To, request.Data);

/// <summary>
/// Parses the response from the Ethereum network.
/// Sends the HTTP request to the Ethereum network.
/// </summary>
/// <param name="flurlResponse">The response to parse.</param>
/// <returns>The parsed response.</returns>
/// <exception cref="KeyNotFoundException">Thrown when the response does not contain the key 'result'.</exception>
private static string ParseResponse(IFlurlResponse flurlResponse)
{
var response = flurlResponse.GetJsonAsync<JObject>()
.GetAwaiter()
.GetResult();

return response["result"]?.ToString() ?? throw new KeyNotFoundException("Response does not contain the key 'result'.");
}
/// <returns></returns>
private JObject PostRequest() =>
Task.Run(() => request.RpcUrl.PostJsonAsync(CreateActionInput).ReceiveJson<JObject>()).Result;
}

0 comments on commit 25e1e55

Please sign in to comment.