Skip to content

dkincer87/csharp-flow-sdk

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

CSharp Flow SDK

Creating a Client

To create a new FlowClient there is a Create factory method that takes in a target string that represents the spork endpoint you would like to connect to. In this exmplae we will use the static Spork class under the Flow.Sdk.Nodes namespace that contains all the possible main net sporks.This will need to be maintained as sporks are updated, but if you are reading from the latest block the MainNet will always continue to work.
var flowClient = FlowClient.Create(Sporks.MainNet.NODE); 

You can test if the Client is working by calling the Ping function.

await flowClient.Ping()

Client Change Channel

While searching through Flow if you are unsure which Spork the block height may belong to you can call ChangeChannel before any function with an execution at BlockHeight. The client will check the range of the sporks and change to the correct Spork channel if needed. If you are in the correct spork it will continue to use the channel you created.
//Looping through blockheight to get a block 1 by 1
var blockHeight = 12345678
flowClient.ChangeChannel(blockHeight);
flowClient.GetEventsForHeightRangeAsync("YOUR_EVENT", blockHeight, blockHeight);

Protobuf

All the flow protobuffs are stored under the Proto folder.

Reading Events

When reading events off Flow you can use the FlowCompositeTypeConverter in JSON.net. This will break down Cadence Composite Types to a Flow SDK CompositeType. Composite Types consist of the Composite Type under the Type property, Id which is the token fully qualified type identifier, and a Dictionary<string,string>. They Keys in the dictionary are the property names and the Values are the cadence values from the Block Event.

 var response = await client.GetEventsForHeightRangeAsync(MOMENT_LISTED_EVENT, startBlockHeight, endBlockHeight);
 foreach (var block in response.Results.ToList())
  {
    foreach (var @event in block.Events)
    {
      //Decode the Protobuf BysteString
      var momentEvent = @event.Payload.ToString(Encoding.Default);

      //We need to deserialize with our custom serializer for composite types
      JsonSerializerOptions options = new();
      options.Converters.Add(new FlowCompositeTypeConverter());
      var momentComposite = JsonSerializer.Deserialize<CompositeType>(momentEvent, options);     
    } 
  }

In the above example you can now access Event data either via key or by using the built in value type converts that will convert Flow Composite Types to Flow.SDK.ValueTypes.

By Key

momentComposite.Fields.FirstOrDefault(x => x.Key == "id").Value)

Straight into a SDK ValueType

UInt64Type.FromJson(momentComposite.Fields.FirstOrDefault(x => x.Key == "id").Value);

Executing Cadence Scripts With Variables

ExecuteScriptAtBlockHeightAsync allows you to execute your Cadence script at the desired Blockheight. If your Cadence script has arguments the last parameter of ExecuteScriptAtBlockHeightAsync will take a list of FlowValueType. You can pass in as many as you like but please keep in mind ORDER MATTERS. Put the arguments in the list the same order they need to be injected into the script.The Value Types have a built in Cadence Serializer that will make sure your args are in the correct format. The script bytes will also be converted to the proper Google Protobuf ByteString.

 //Convert your script into a byte array
 var scriptBytes = Encoding.ASCII.GetBytes("Your raw Cadence script");
 //Create our Flow.SDK.ValueTypes
 var address = new AddressType("MyAddress");
 var momentId = new UInt64Type(123456789);
 
 //Execute the script
 var momentData = await client.ExecuteScriptAtBlockHeightAsync(blockHeight, scriptBytes, new List<FlowValueType>() { address, momentId });

About

C# SDK to interact with the Flow Blockchain (https://www.onflow.org/)

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Languages