Skip to content

Latest commit

 

History

History
212 lines (160 loc) · 5.32 KB

README.md

File metadata and controls

212 lines (160 loc) · 5.32 KB

.NET

blockfrost-dotnet


.NET SDK for Blockfrost.io API.

Getting startedInstallationUsage


Getting started

To use this SDK, first go to blockfrost.io and create your project to retrive your API token.


Setup environment

blocfrost-dotnet supports the following environment variables.

$> $env:BFCLI_NETWORK
testnet

$> $env:BFCLI_API_KEY
yourawesomeapikeyforblockfrostio

Make sure you have configured them if you add blockfrost-dotnet using services.AddBlockfrost();

There are other extension methods to configure blockfrost-dotnet where the environment variables are not required. One of them is shown in the sample below.

Setup

The SDK is hosted on nuget.org, so you can directly import it using your favorite package manager.

$> dotnet new console -n blockfrost-client
$> cd blockfrost-client
$> dotnet add package Blockfrost.Api
$> dotnet add package Blockfrost.Extensions

Usage

Using the SDK is pretty straight-forward as you can see from the following example.

Cardano Services

using System.IO;
using Blockfrost.Api.Extensions;
using Blockfrost.Api.Models.Extensions;
using Blockfrost.Api.Services;
using Blockfrost.Api.Services.Extensions;
using Microsoft.Extensions.DependencyInjection;

/*
 * Parameters
 */
string apiKey = "YOUR_BLOCKFROST_PROJECT_ID";
string network = "NETWORK_OF_THE_PROJECT_ID";
string sender_address = "SENDER_ADDR";
string receiver_address = "RECEIVER_ADDR";
string signedTx = File.ReadAllText("path/to/your/signed/transaction");

/*
 * Init Services using apiKey and network
 */
var cardano = new ServiceCollection()
    .AddBlockfrost(network, apiKey)
    .BuildServiceProvider()
    .GetRequiredService<ICardanoService>();

/*
 * Show metrics for your account
 */
var metrics = await cardano.Metrics.GetMetricsAsync();
var opt = new System.Text.Json.JsonSerializerOptions() { WriteIndented = true };
System.Console.WriteLine($"Metrics: {metrics.ToJson(opt)}");

/*
 * Show sender UTxO
 */
var utxoSender = await cardano.Addresses.GetUtxosAsync(sender_address);
long totalSender = utxoSender.SumAmounts("lovelace");
System.Console.WriteLine($"Sender Total: {totalSender} lovelace");

/*
 * Sum receiver UTxO
 */
var utxoReceiver = await cardano.Addresses.GetUtxosAsync(receiver_address);
long totalReceiver = utxoReceiver.SumAmounts("lovelace");
System.Console.WriteLine($"Receiver Total: {totalReceiver} lovelace");

/*
 * Query tip
 */
var tip = await cardano.Blocks.GetLatestAsync();
long? latestSlot = tip.Slot;

System.Console.WriteLine($"Tip now at Epoch {tip.Epoch} Slot {tip.Slot} Block {tip.Height}");

/*
 * Send submit tx
 */
System.Console.WriteLine(signedTx);
string txid = await cardano.Transactions.PostTxSubmitAsync(signedTx);

System.Console.WriteLine($"Your Transaction was transmitted to the {network}");
System.Console.WriteLine($"https://explorer.cardano-{network}.iohkdev.io/en/transaction?id={txid}");

/*
 * Wait two blocks
 */
tip = await cardano.Blocks.WaitAsync(
    count: 2,
    interval: System.TimeSpan.FromSeconds(5),
    callback: latest => System.Console.WriteLine(latest.Slot),
    cancellationToken: System.Threading.CancellationToken.None
);
System.Console.WriteLine($"Tip now at Epoch {tip.Epoch} Slot {tip.Slot} Block {tip.Height}");

Run the sample

$ dotnet run
Metrics: [
  {
    "time": 1631750400,
    "calls": 3
  },
  ...
]
Sender Total: 988258310 lovelace
Receiver Total: 10000000 lovelace
Tip now at Epoch 160 Slot 38978334 Block 2965005

Your Transaction was transmitted to the testnet
https://explorer.cardano-testnet.iohkdev.io/en/transaction?id=2b1ca81b94c5dd737fe939444264046c6fbbe96ff403e49ee99e8022b0e512bb
Tip: 38978334
Tip: 38978334
Tip: 38978334
Tip: 38978334
Tip: 38978334
Tip now at Epoch 160 Slot 38978436 Block 2965007