Netezos is .NET Standard 2.0 libraries pack for working with Tezos. At the current stage, Netezos.Rpc is implemented. It provides access to the Tezos node via RPC API. Let’s a little dig into it and look at how it works.
# Installation
Netezos.Rpc package is available on NuGet, a package manager for .NET developers. So you can install Netezos.Rpc via Nuget Package Manager GUI or by the following command:
PM> Install-Package Netezos.Rpc
Or just clone the project from GitHub repository.
# Basic usage
There is the main class TezosRpc
which you need to build the queries, supported by Tezos RPC API.
Let’s create an instance of TezosRpc
class, build a simple GET query and execute it by calling GetAsync()
.
using (var rpc = new TezosRpc("https://mainnet.tezrpc.me"))
{
// get the head block
var head = await rpc.Blocks.Head.GetAsync();
// get only the hash of the head block
var hash = await rpc.Blocks.Head.Hash.GetAsync();
}
Note that the real HTTP request is sent only when you call GetAsync()
. Until then, you work with just the query object, which can also be used to get subqueries.
# Accessing Tezos blocks
You can access any block in two ways: by forward-indexing and by backward-indexing.
// gets the block with level = 1
var firstBlock = rpc.Blocks[1];
// gets the last block (e.g. with level = 400000)
var lastBlock = rpc.Blocks.Head;
// gets the block with level = 400000 - 10 = 399990
var tenthFromLast = rpc.Blocks[-10];
# RpcList and RpcDictionary
The results of many RPC API methods can be interpreted as arrays or dictionaries that allow you to get many objects or only one by specifying a key or index.
// Accessing an object in the array at the specified index
var operations = rpc.Blocks.Head.Operations;
var firstEndorsement = rpc.Blocks.Head.Operations[0][0];
// Accessing an object in the dictionary by the specified key
var contracts = rpc.Blocks.Head.Context.Contracts;
var myContract = rpc.Blocks.Head.Context.Contracts["KT1..."];
# Query parameters
If some RPC API method has query parameters, the corresponding query object will have the overridden GetAsync()
methods.
var activeDelegates = await rpc.Blocks.Head.Context.Delegates.GetAsync(DelegateStatus.Active);
var rights = await rpc.Blocks.Head.Helpers.BakingRights.GetAsync(maxPriority: 1, all: true);
# Post methods
Using POST methods, you can pass the JSON string input.
var operationBytes = await rpc.Blocks.Head.Helpers.Forge.Operations.PostAsync("{" +
"\"branch\": \"BMDuBKz9ZnQVygvz3Ys1vVArj5YfeQ4yhqajG43SWGSN7Yr7DAM\"," +
"\"contents\": [" +
"{" +
" \"kind\": \"transaction\"," +
" \"amount\": \"10\"," +
" \"source\": \"tz1PVAAVKbvASA79wBVVsgTVHvkEakupHJ9Y\"," +
" \"destination\": \"KT1AVi98QseKo9joH2J5AzQaNC89qsttG1Ff\"," +
" \"storage_limit\": \"0\", "+
" \"gas_limit\": \"127\"," +
" \"fee\": \"1285\"," +
" \"counter\": \"68760\" " +
"}" +
"]" +
"}");
The second option — pass an object to the PostAsync method.
var operationBytes = await rpc.Blocks.Head.Helpers.Forge.Operations.PostAsync(new
{
branch = "BMDuBKz9ZnQVygvz3Ys1vVArj5YfeQ4yhqajG43SWGSN7Yr7DAM",
contents = new List<object>
{
new
{
kind = "transaction",
amount = "10",
source = "tz1PVAAVKbvASA79wBVVsgTVHvkEakupHJ9Y",
destination = "KT1AVi98QseKo9joH2J5AzQaNC89qsttG1Ff",
storage_limit = "0",
gas_limit = "127",
fee = "1285",
counter = "68760"
}
}
});
And the third option — pass specific arguments to the PostAsync method.
var source = wallet.PublichHash;
var counter = await rpc.Blocks.Head.Context.Contracts[source].Counter.GetAsync();
var branchHash = (await rpc.Blocks.Head.Hash.GetAsync()).ToString();
var txContents = new List<object>
{
new
{
kind = "transaction",
amount = "10",
source,
destination = "tz1PVAAVKbvASA79wBVVsgTVHvkEakupHJ9Y",
storage_limit = "0",
gas_limit = "10200",
fee = "1500",
counter = (counter.ToObject<int>() + 1).ToString()
}
};
var opBytes = await rpc.Blocks.Head.Helpers.Forge.Operations.PostAsync(branchHash, txContents);
# Conclusion
In this article, we have described the most common use cases. Netezos.RPC is pretty flexible so can actually be used for much more cases related to working with Tezos blockchain via RPC API.
Netezos is an open-development project, so do not hesitate to participate by creating issues or making pull requests to our GitHub repo. Also, we are always glad to discuss any features in our Baking Bad telegram chat.
Cheers!