Skip to content

Commit 73b71e8

Browse files
chore: enabled configs for urls (#12)
1 parent 88a058e commit 73b71e8

File tree

6 files changed

+156
-29
lines changed

6 files changed

+156
-29
lines changed

README.md

+48-16
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,16 @@
33
<h4>A gRPC interface for UTxO Blockchains</h4>
44
</div>
55
<div align="center">
6-
<a href="https://www.nuget.org/packages/Utxorpc.Sdk/">
7-
<img src="https://img.shields.io/nuget/v/Utxorpc.Sdk.svg" alt="NuGet">
8-
</a>
9-
6+
107
![Forks](https://img.shields.io/github/forks/utxorpc/dotnet-sdk.svg?style=social)
118
![Stars](https://img.shields.io/github/stars/utxorpc/dotnet-sdk.svg?style=social)
129
![Contributors](https://img.shields.io/github/contributors/utxorpc/dotnet-sdk.svg)
1310
![Issues](https://img.shields.io/github/issues/utxorpc/dotnet-sdk.svg)
1411
![Issues Closed](https://img.shields.io/github/issues-closed/utxorpc/dotnet-sdk.svg)
1512

13+
<a href="https://www.nuget.org/packages/Utxorpc.Sdk/">
14+
<img src="https://img.shields.io/nuget/v/Utxorpc.Sdk.svg" alt="NuGet">
15+
</a>
1616
</div>
1717

1818

@@ -43,23 +43,55 @@ The `SyncServiceClient` allows you to fetch blocks and follow chain tips with ea
4343
```csharp
4444
using Utxorpc.Sdk;
4545
using Utxorpc.Sdk.Models;
46+
using Utxorpc.Sdk.Models.Enums;
4647

47-
var syncServiceClient = new SyncServiceClient("http://localhost:50051");
48-
49-
BlockRef blockRef = new BlockRef("1dace9bc646e9225251db04ff27397c199b04ec3f83c94cad28c438c3e7eeb50", 67823979);
50-
Block? block = await syncServiceClient.FetchBlockAsync(blockRef);
51-
52-
if (block is not null)
48+
var headers = new Dictionary<string, string>
5349
{
54-
Console.WriteLine($"Block Hash: {block.Hash}");
55-
Console.WriteLine($"Slot: {block.Slot}");
56-
Console.WriteLine($"Native Bytes: {BitConverter.ToString(block.NativeBytes)}");
57-
}
58-
else
50+
{ "dmtr-api-key", "dmtr_utxorpc1vc0m93rynmltysttwm7ns9m3n5cklws6" },
51+
};
52+
53+
var client = new SyncServiceClient(
54+
url: "https://preview.utxorpc-v0.demeter.run",
55+
headers
56+
);
57+
58+
await foreach (NextResponse? response in client.FollowTipAsync(
59+
new BlockRef
60+
(
61+
"b977e548f3364b114505f3311a10f89e5f5cf47e815765bce6750a5de48e5951",
62+
58717900
63+
)))
5964
{
60-
Console.WriteLine("Block not found.");
65+
Console.WriteLine("___________________");
66+
switch (response.Action)
67+
{
68+
case NextResponseAction.Apply:
69+
Block? applyBlock = response.AppliedBlock;
70+
if (applyBlock is not null)
71+
{
72+
Console.WriteLine($"Apply Block: {applyBlock.Hash} Slot: {applyBlock.Slot}");
73+
Console.WriteLine($"Cbor: {Convert.ToHexString(applyBlock.NativeBytes ?? [])}");
74+
}
75+
break;
76+
case NextResponseAction.Undo:
77+
Block? undoBlock = response.UndoneBlock;
78+
if (undoBlock is not null)
79+
{
80+
Console.WriteLine($"Undo Block: {undoBlock.Hash} Slot: {undoBlock.Slot}");
81+
}
82+
break;
83+
case NextResponseAction.Reset:
84+
BlockRef? resetRef = response.ResetRef;
85+
if (resetRef is not null)
86+
{
87+
Console.WriteLine($"Reset to Block: {resetRef.Hash} Slot: {resetRef.Index}");
88+
}
89+
break;
90+
}
91+
Console.WriteLine("___________________");
6192
}
6293

94+
6395
```
6496

6597
### Roadmap

src/Utxorpc.Sdk.Example/Program.cs

+13-5
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,21 @@
22
using Utxorpc.Sdk.Models;
33
using Utxorpc.Sdk.Models.Enums;
44

5-
SyncServiceClient? syncServiceClient = new("http://localhost:50051");
5+
var headers = new Dictionary<string, string>
6+
{
7+
{ "dmtr-api-key", "dmtr_utxorpc1vc0m93rynmltysttwm7ns9m3n5cklws6" },
8+
};
9+
10+
var client = new SyncServiceClient(
11+
url: "https://preview.utxorpc-v0.demeter.run",
12+
headers
13+
);
614

7-
await foreach (NextResponse? response in syncServiceClient.FollowTipAsync(
15+
await foreach (NextResponse? response in client.FollowTipAsync(
816
new BlockRef
917
(
10-
"1dace9bc646e9225251db04ff27397c199b04ec3f83c94cad28c438c3e7eeb50",
11-
67823979
18+
"b977e548f3364b114505f3311a10f89e5f5cf47e815765bce6750a5de48e5951",
19+
58717900
1220
)))
1321
{
1422
Console.WriteLine("___________________");
@@ -38,4 +46,4 @@
3846
break;
3947
}
4048
Console.WriteLine("___________________");
41-
}
49+
}

src/Utxorpc.Sdk/QueryServiceClient.cs

+24-2
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,31 @@
33

44
namespace Utxorpc.Sdk;
55

6-
public class QueryServiceClient(string url)
6+
public class QueryServiceClient
77
{
8-
private readonly QueryService.QueryServiceClient _client = new(GrpcChannel.ForAddress(url));
8+
private readonly QueryService.QueryServiceClient _client;
9+
10+
public QueryServiceClient(string url, IDictionary<string, string>? headers = null)
11+
{
12+
var httpClientHandler = new HttpClientHandler();
13+
var httpClient = new HttpClient(httpClientHandler);
14+
15+
if (headers != null)
16+
{
17+
foreach (var header in headers)
18+
{
19+
httpClient.DefaultRequestHeaders.Add(header.Key, header.Value);
20+
}
21+
}
22+
23+
var channelOptions = new GrpcChannelOptions
24+
{
25+
HttpClient = httpClient
26+
};
27+
28+
var channel = GrpcChannel.ForAddress(url, channelOptions);
29+
_client = new QueryService.QueryServiceClient(channel);
30+
}
931

1032
// TODO: Implement ReadData, ReadParams, ReadUtxos, SearchUtxos methods
1133
}

src/Utxorpc.Sdk/SubmitServiceClient.cs

+23-2
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,30 @@
33

44
namespace Utxorpc.Sdk;
55

6-
public class SubmitServiceClient(string url)
6+
public class SubmitServiceClient
77
{
8-
private readonly SubmitService.SubmitServiceClient _client = new(GrpcChannel.ForAddress(url));
8+
private readonly SubmitService.SubmitServiceClient _client;
99

10+
public SubmitServiceClient(string url, IDictionary<string, string>? headers = null)
11+
{
12+
var httpClientHandler = new HttpClientHandler();
13+
var httpClient = new HttpClient(httpClientHandler);
14+
15+
if (headers != null)
16+
{
17+
foreach (var header in headers)
18+
{
19+
httpClient.DefaultRequestHeaders.Add(header.Key, header.Value);
20+
}
21+
}
22+
23+
var channelOptions = new GrpcChannelOptions
24+
{
25+
HttpClient = httpClient
26+
};
27+
28+
var channel = GrpcChannel.ForAddress(url, channelOptions);
29+
_client = new SubmitService.SubmitServiceClient(channel);
30+
}
1031
// TODO: Implement EvalTx, ReadMempool, SubmitTx, WaitForTx, WatchMempool methods
1132
}

src/Utxorpc.Sdk/SyncServiceClient.cs

+24-2
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,31 @@
88

99
namespace Utxorpc.Sdk;
1010

11-
public class SyncServiceClient(string url)
11+
public class SyncServiceClient
1212
{
13-
private readonly SyncService.SyncServiceClient _client = new(GrpcChannel.ForAddress(url));
13+
private readonly SyncService.SyncServiceClient _client;
14+
15+
public SyncServiceClient(string url, IDictionary<string, string>? headers = null)
16+
{
17+
var httpClientHandler = new HttpClientHandler();
18+
var httpClient = new HttpClient(httpClientHandler);
19+
20+
if (headers != null)
21+
{
22+
foreach (var header in headers)
23+
{
24+
httpClient.DefaultRequestHeaders.Add(header.Key, header.Value);
25+
}
26+
}
27+
28+
var channelOptions = new GrpcChannelOptions
29+
{
30+
HttpClient = httpClient
31+
};
32+
33+
var channel = GrpcChannel.ForAddress(url, channelOptions);
34+
_client = new SyncService.SyncServiceClient(channel);
35+
}
1436

1537
public async Task<Block?> FetchBlockAsync(BlockRef blockRef)
1638
{

src/Utxorpc.Sdk/WatchServiceClient.cs

+24-2
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,31 @@
33

44
namespace Utxorpc.Sdk;
55

6-
public class WatchServiceClient(string url)
6+
public class WatchServiceClient
77
{
8-
private readonly WatchService.WatchServiceClient _client = new(GrpcChannel.ForAddress(url));
8+
private readonly WatchService.WatchServiceClient _client;
99

10+
public WatchServiceClient(string url, IDictionary<string, string>? headers = null)
11+
{
12+
var httpClientHandler = new HttpClientHandler();
13+
var httpClient = new HttpClient(httpClientHandler);
14+
15+
if (headers != null)
16+
{
17+
foreach (var header in headers)
18+
{
19+
httpClient.DefaultRequestHeaders.Add(header.Key, header.Value);
20+
}
21+
}
22+
23+
var channelOptions = new GrpcChannelOptions
24+
{
25+
HttpClient = httpClient
26+
};
27+
28+
var channel = GrpcChannel.ForAddress(url, channelOptions);
29+
_client = new WatchService.WatchServiceClient(channel);
30+
}
31+
1032
// TODO: Implement WatchTx method
1133
}

0 commit comments

Comments
 (0)