Skip to content

Commit

Permalink
➕ Add getFeeForMessage (#40)
Browse files Browse the repository at this point in the history
  • Loading branch information
GabrielePicco committed Jul 20, 2023
1 parent fe708bc commit efae324
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 1 deletion.
9 changes: 9 additions & 0 deletions src/Solana.Unity.Rpc/IRpcClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,15 @@ Task<RequestResult<ResponseValue<FeeCalculatorInfo>>> GetFeeCalculatorForBlockha
/// <returns>Returns a task that holds the asynchronous operation result and state.</returns>
Task<RequestResult<ResponseValue<FeesInfo>>> GetFeesAsync(Commitment commitment = default);

/// <summary>
/// Sends a transaction.
/// </summary>
/// <param name="message">The transaction message</param>
/// <param name="commitment">The block commitment used to retrieve block hashes and verify success.</param>
/// <returns>Returns a task that holds the asynchronous operation result and state.</returns>
Task<RequestResult<ResponseValue<ulong?>>> GetFeeForMessageAsync(byte[] message, Commitment commitment = default);


/// <summary>
/// Returns the slot of the lowest confirmed block that has not been purged from the ledger.
/// </summary>
Expand Down
14 changes: 14 additions & 0 deletions src/Solana.Unity.Rpc/Models/Fees.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using Newtonsoft.Json;

namespace Solana.Unity.Rpc.Models
{
/// <summary>
Expand Down Expand Up @@ -67,4 +69,16 @@ public class FeesInfo
/// </summary>
public ulong LastValidBlockHeight { get; set; }
}

/// <summary>
/// Represents the message fees.
/// </summary>
public class MessageFeesInfo
{

/// <summary>
/// Fee corresponding to the message at the specified blockhash
/// </summary>
public ulong Value { get; set; }
}
}
15 changes: 14 additions & 1 deletion src/Solana.Unity.Rpc/SolanaRpcClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -552,7 +552,20 @@ public async Task<RequestResult<ResponseValue<FeesInfo>>> GetFeesAsync(
return await SendRequestAsync<ResponseValue<FeesInfo>>("getFees",
Parameters.Create(ConfigObject.Create(HandleCommitment(commitment))));
}


/// <inheritdoc cref="IRpcClient.GetFeeForMessageAsync(byte[], Commitment)"/>
public async Task<RequestResult<ResponseValue<ulong?>>> GetFeeForMessageAsync(
byte[] message,
Commitment commitment = default)
{
return await SendRequestAsync<ResponseValue<ulong?>>("getFeeForMessage",
Parameters.Create(
Convert.ToBase64String(message),
ConfigObject.Create(
KeyValue.Create("commitment", commitment)
)
));
}

/// <inheritdoc cref="IRpcClient.GetFees"/>
public RequestResult<ResponseValue<FeesInfo>> GetFees(Commitment commitment = default)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"method":"getFeeForMessage","params":["AQABAgIAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAEBAQAA",{"commitment":"processed"}],"jsonrpc":"2.0","id":0}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"jsonrpc": "2.0",
"result": {
"context": {
"apiVersion": "1.14.20",
"slot": 206417151
},
"value": 5000
},
"id": 0
}
12 changes: 12 additions & 0 deletions test/Solana.Unity.Rpc.Test/Solana.Unity.Rpc.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -591,6 +591,12 @@
<None Update="Resources\Http\Fees\GetFeesResponse.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Resources\Http\Fees\GetMessageFeesRequest.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Resources\Http\Fees\GetMessageFeesResponse.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Resources\Http\Epoch\GetEpochScheduleResponse.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
Expand Down Expand Up @@ -687,6 +693,12 @@
<None Update="Resources\Http\Signatures\GetConfirmedSignaturesForAddress2BeforeConfirmedRequest.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Resources\Http\Fees\GetMessageFeesRequest.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Resources\Http\Fees\GetMessageFeesResponse.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>

</Project>
27 changes: 27 additions & 0 deletions test/Solana.Unity.Rpc.Test/SolanaRpcClientFeeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,33 @@ public void TestGetFees()

FinishTest(messageHandlerMock, TestnetUri);
}

[TestMethod]
public async Task TestGetMessageFees()
{
var message = "AQABAgIAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAQAAAAAAAAAAAAAAAAAAAAAA" +
"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAEBAQAA";
var responseData = File.ReadAllText("Resources/Http/Fees/GetMessageFeesResponse.json");
var requestData = File.ReadAllText("Resources/Http/Fees/GetMessageFeesRequest.json");
var sentMessage = string.Empty;
var messageHandlerMock = SetupTest(
(s => sentMessage = s), responseData);

var httpClient = new HttpClient(messageHandlerMock.Object)
{
BaseAddress = TestnetUri,
};

var sut = new SolanaRpcClient(TestnetUrl, null, httpClient);
var result = await sut.GetFeeForMessageAsync(message: Convert.FromBase64String(message), Commitment.Processed);

Assert.AreEqual(requestData, sentMessage);
Assert.IsNotNull(result.Result);
Assert.IsTrue(result.WasSuccessful);
Assert.AreEqual(5000UL, result.Result.Value);

FinishTest(messageHandlerMock, TestnetUri);
}

[TestMethod]
public void TestGetFeesConfirmed()
Expand Down

0 comments on commit efae324

Please sign in to comment.