Call FA from Netezos

CSharp · Console

/Program.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

using Netezos.Contracts;
using Netezos.Encoding;
using Netezos.Forging;
using Netezos.Forging.Models;
using Netezos.Keys;
using Netezos.Rpc;
					
public class Program
{
	public static async Task Main()
	{
		var rpc = new TezosRpc("https://rpc.tzkt.io/ghostnet/");
		var key = Key.FromBase58("edsk35mfZXZJiYUxqcmsK5K6ggg3owD2dpbRgFHp4zZzmrPy9RBdj8");
		
		//await TransferFa12(rpc, key, "KT1EwXFWoG9bYebmF4pYw72aGjwEnBWefgW5");
		await TransferFa2(rpc, key, "KT1DYk1XDzHredJq1EyNkDindiWDqZyekXGj");
	}
	
	public static async Task TransferFa12(TezosRpc rpc, Key key, string contract)
	{
		var head = await rpc.Blocks.Head.Hash.GetAsync<string>();
		var counter = await rpc.Blocks.Head.Context.Contracts[key.PubKey.Address].Counter.GetAsync<int>();
		
		var script = await rpc.Blocks.Head.Context.Contracts[contract].Script.GetAsync();
		var code = Micheline.FromJson(script.code);

		var cs = new ContractScript(code);
		var param = cs.BuildParameter( 
			"transfer",
			new // see parameter schema on https://ghostnet.tzkt.io/KT1EwXFWoG9bYebmF4pYw72aGjwEnBWefgW5/entrypoints
			{
				from = key.PubKey.Address,
				to = key.PubKey.Address,
				value = "123"
			});
		
		var tx = new TransactionContent
		{
			Source = key.PubKey.Address,
			Counter = ++counter,
			GasLimit = 100_000,
			Fee = 100_000,
			Destination = contract,
			Parameters = new Parameters
			{
				Entrypoint = "transfer",
				Value = param
			}
		};
		
		var opBytes = await new LocalForge().ForgeOperationAsync(head, tx);
		var opSig = key.SignOperation(opBytes);
		
		var opHash = await rpc.Inject.Operation.PostAsync(opBytes.Concat((byte[])opSig));
		Console.WriteLine((string)opHash);
	}
	
	public static async Task TransferFa2(TezosRpc rpc, Key key, string contract)
	{
		var head = await rpc.Blocks.Head.Hash.GetAsync<string>();
		var counter = await rpc.Blocks.Head.Context.Contracts[key.PubKey.Address].Counter.GetAsync<int>();
		
		var script = await rpc.Blocks.Head.Context.Contracts[contract].Script.GetAsync();
		var code = Micheline.FromJson(script.code);

		var cs = new ContractScript(code);
		var param = cs.BuildParameter(
			"transfer",
			new List<object> // see parameter schema on https://ghostnet.tzkt.io/KT1DYk1XDzHredJq1EyNkDindiWDqZyekXGj/entrypoints
            {
                new
                {
                    from_ = key.PubKey.Address,
                    txs = new List<object>
                    {
                        new
                        {
                            to_ = key.PubKey.Address,
                            token_id = 0,
                            amount = "10"
                        }
                    }
                }
            });
		
		var tx = new TransactionContent
		{
			Source = key.PubKey.Address,
			Counter = ++counter,
			GasLimit = 100_000,
			Fee = 100_000,
			Destination = contract,
			Parameters = new Parameters
			{
				Entrypoint = "transfer",
				Value = param
			}
		};
		
		var opBytes = await new LocalForge().ForgeOperationAsync(head, tx);
		var opSig = key.SignOperation(opBytes);
		
		var opHash = await rpc.Inject.Operation.PostAsync(opBytes.Concat((byte[])opSig));
		Console.WriteLine((string)opHash);
	}
}
;