Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 58 additions & 18 deletions Docs/Articles/GettingStarted.md
Original file line number Diff line number Diff line change
@@ -1,48 +1,88 @@
# Getting started

Get started with "Casper.Network.SDK" quickly by adding a reference to the NuGet package or linking to the source code project. Or start developing in a remote environment with a Gitpod workspace.
Get started with `Casper.Network.SDK` quickly by adding a reference to the NuGet package or linking to the source code project.

## Prerequisites

The SDK targets **.NET 8**. Make sure you have the [.NET 8 SDK](https://dotnet.microsoft.com/download/dotnet/8.0) installed on your machine.

## Add the NuGet package to your project

The Casper.Network.SDK for .NET is published as a nuget package in [nuget.org](https://www.nuget.org/packages/Casper.Network.SDK).
The `Casper.Network.SDK` is published as a NuGet package on [nuget.org](https://www.nuget.org/packages/Casper.Network.SDK).

To add a reference to the SDK in your project, use the Package Manager in Visual Studio or the `dotnet` cli tool.
To add a reference to the SDK in your project, use the Package Manager in Visual Studio or the `dotnet` CLI tool.

### Package Manager (Windows)
```
Install-Package Casper.Network.SDK
```

### dotnet cli tool (Windows/Mac/Linux)
### dotnet CLI tool (Windows/Mac/Linux)
```
dotnet add package Casper.Network.SDK
```

## Add a reference to the source code

Clone the GitHub repository in your solution folder and add a reference in your project to the Casper.Network.SDK.csproj file.
Clone the GitHub repository in your solution folder and add a reference in your project to the `Casper.Network.SDK.csproj` file.

## Create a Gitpod workspace

Click the button below to start a workspace in Gitpod with a clone of the SDK repository:
```bash
git clone https://github.com/make-software/casper-net-sdk.git
cd your-project
dotnet add reference ../casper-net-sdk/Casper.Network.SDK/Casper.Network.SDK.csproj
```

[![Open in Gitpod](https://gitpod.io/button/open-in-gitpod.svg)](https://gitpod.io/#https://github.com/make-software/casper-net-sdk)
## Create a minimal console application

Once the workspace is ready, go to the terminal and create a new console app:
Create a new console app and add the SDK package:

```
```bash
dotnet new console -o GetStarted
cd GetStarted
dotnet add package Casper.Network.SDK
```

Now, add a reference to the SDK:

```
cd GetStarted
dotnet add reference ../Casper.Network.SDK/Casper.Network.SDK.csproj
Replace the contents of `Program.cs` with the following example. This program connects to a public Casper testnet node and prints the latest block height:

```csharp
using System;
using System.Threading.Tasks;
using Casper.Network.SDK;
using Casper.Network.SDK.JsonRpc;

namespace GetStarted
{
class Program
{
static async Task Main(string[] args)
{
var client = new NetCasperClient("https://rpc.testnet.casperlabs.io/rpc");

try
{
var response = await client.GetBlock();
var block = response.Parse().Block;
Console.WriteLine($"Latest block height: {block.Height}");
Console.WriteLine($"Block hash: {block.Hash}");
}
catch (RpcClientException e)
{
Console.WriteLine($"RPC Error: {e.RpcError.Message}");
}
}
}
}
```

Copy the code in one of the examples from `Docs\Examples` and run the program:
Run the program:

```
```bash
dotnet run
```

## Next steps

- Read [Working with TransactionV1](./WorkingWithTransactionV1.md) to learn how to send the new Casper 2.0 transaction type.
- Read [Key management](./KeyManagement.md) to learn how to create and load key pairs for signing transactions.
- Browse the [Examples](../Examples/index.md) for more complete sample programs.
- Follow one of the [Tutorials](../Tutorials/index.md) to deploy and interact with smart contracts.
114 changes: 114 additions & 0 deletions Docs/Articles/QueryingBalances.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
# Querying Balances

On Casper 2.0 networks, the recommended way to query an account's balance is the `QueryBalance()` method. It replaces the older `GetAccountBalance()` method (removed in SDK v3) and supports multiple ways to identify a purse or account.

## Balance identifiers

`QueryBalance()` accepts any object implementing the `IPurseIdentifier` interface. The following types implement this interface:

| Type | Description |
|---|---|
| `PublicKey` | The public key of an account. |
| `AccountHashKey` | The account hash derived from a public key. |
| `URef` | The main purse or any other purse URef. |
| `AddressableEntityKey` | An entity key (account or smart contract) introduced in Casper 2.0. |

## Querying by public key

The simplest way to get a balance is to pass the account's public key:

```csharp
using System;
using System.Threading.Tasks;
using Casper.Network.SDK;
using Casper.Network.SDK.JsonRpc;
using Casper.Network.SDK.Types;

public class BalanceExample
{
public static async Task Main(string[] args)
{
var client = new NetCasperClient("http://127.0.0.1:11101/rpc");

var publicKey = PublicKey.FromHexString(
"0184f6d260F4EE6869DDB36affe15456dE6aE045278FA2f467bb677561cE0daD55");

try
{
var response = await client.QueryBalance(publicKey);
var balance = response.Parse().BalanceValue;
Console.WriteLine($"Balance: {balance} motes");
}
catch (RpcClientException e)
{
Console.WriteLine($"RPC Error: {e.RpcError.Message}");
}
}
}
```

## Querying by purse URef

If you already know the main purse URef of an account, you can query it directly:

```csharp
var mainPurse = new URef("uref-0d0b57865e41b9e39170c038993997af432f66545f56838f1bf602c6d56e0e54-007");
var response = await client.QueryBalance(mainPurse);
Console.WriteLine($"Purse balance: {response.Parse().BalanceValue} motes");
```

## Querying by account hash

You can also use the account hash:

```csharp
var accountHash = new AccountHashKey("account-hash-56befc13a6fd62e18f361700a5e08f966901c34df8041b36ec97d54d605c23de");
var response = await client.QueryBalance(accountHash);
Console.WriteLine($"Balance: {response.Parse().BalanceValue} motes");
```

## Querying by AddressableEntity (Casper 2.0)

On Casper 2.0 networks, accounts and smart contracts are represented as `AddressableEntity`. You can query the balance of an entity using its key:

```csharp
var entityKey = new AddressableEntityKey(
"entity-account-56befc13a6fd62e18f361700a5e08f966901c34df8041b36ec97d54d605c23de");

var response = await client.QueryBalance(entityKey);
Console.WriteLine($"Entity balance: {response.Parse().BalanceValue} motes");
```

## Querying at a specific block

By default, `QueryBalance()` returns the balance at the latest block. You can query at a specific block hash or height:

```csharp
// By block hash
var response = await client.QueryBalance(publicKey, "block-hash-...");

// By block height
var response = await client.QueryBalance(publicKey, 12345UL);
```

## Querying balance details

In addition to the total balance, you can retrieve detailed balance information (available and total hold amounts) using `QueryBalanceDetails()`:

```csharp
var response = await client.QueryBalanceDetails(publicKey);
var details = response.Parse();

Console.WriteLine($"Total balance: {details.TotalBalance}");
Console.WriteLine($"Available balance: {details.AvailableBalance}");
Console.WriteLine($"Total holds: {details.TotalHolds}");
```

## Legacy method: GetBalance

If you need to use the older `state_get_balance` RPC method directly (for example, with a specific state root hash), use `GetBalance()`:

```csharp
var response = await client.GetBalance("uref-...-007", stateRootHash: "...");
Console.WriteLine($"Balance: {response.Parse().BalanceValue}");
```
150 changes: 150 additions & 0 deletions Docs/Articles/WorkingWithAddressableEntity.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
# Working with AddressableEntity

> **Info:** `AddressableEntity` is currently not enabled on Casper testnet or mainnet.

Casper 2.0 introduced the **AddressableEntity** model, which unifies how accounts, smart contracts, and system contracts are represented on the network. Instead of treating accounts and contracts as completely separate concepts, Casper 2.0 wraps them in a common `AddressableEntity` structure.

The Casper .NET SDK provides the `GetEntity()` method to retrieve this information and the `AddressableEntityKey` type to reference entities in global state.

> **Note:** `GetEntity()` replaces `GetAccountInfo()` for Casper 2.0 networks. On a Casper 2.0 node, `GetAccountInfo()` may return legacy account data, while `GetEntity()` returns the new unified model.

## What is an AddressableEntity?

An `AddressableEntity` represents any of the following:

- **Account** — A user account with associated keys and action thresholds.
- **Smart Contract** — A Wasm-based contract deployed on the network.
- **System Contract** — A built-in contract such as Mint, HandlePayment, StandardPayment, or Auction.

Each entity has:

| Property | Description |
|---|---|
| `ProtocolVersion` | The Casper protocol version at which the entity was created or last updated. |
| `EntityKind` | Indicates whether the entity is an `Account`, `SmartContract`, or `System` contract. |
| `Package` | The package hash associated with the entity (for contracts). |
| `ByteCodeHash` | The hash of the contract's Wasm bytecode (for smart contracts). |
| `MainPurse` | The URef of the entity's main purse (used for balance queries). |
| `AssociatedKeys` | Public keys authorized to act on behalf of the entity. |
| `ActionThresholds` | Minimum weights required for deployment and key management actions. |

## Entity kinds

The `EntityKind` property tells you what type of entity you are working with:

```csharp
var entity = response.Parse().Entity;

if (entity.EntityKind.Account != null)
{
Console.WriteLine("This is an account entity.");
}
else if (entity.EntityKind.SmartContract.HasValue)
{
Console.WriteLine($"This is a smart contract with runtime: {entity.EntityKind.SmartContract}");
}
else if (entity.EntityKind.System.HasValue)
{
Console.WriteLine($"This is a system contract: {entity.EntityKind.System}");
}
```

Possible `System` values are:
- `Mint`
- `HandlePayment`
- `StandardPayment`
- `Auction`

## Querying an entity by public key

The simplest way to query an entity is to pass the account's public key:

```csharp
using System;
using System.Threading.Tasks;
using Casper.Network.SDK;
using Casper.Network.SDK.JsonRpc;
using Casper.Network.SDK.Types;

public class EntityExample
{
public static async Task Main(string[] args)
{
var client = new NetCasperClient("http://127.0.0.1:11101/rpc");

var publicKey = PublicKey.FromHexString(
"0184f6d260F4EE6869DDB36affe15456dE6aE045278FA2f467bb677561cE0daD55");

try
{
var response = await client.GetEntity(publicKey);
var entity = response.Parse().Entity;

Console.WriteLine($"Protocol version: {entity.ProtocolVersion}");
Console.WriteLine($"Main purse: {entity.MainPurse}");
Console.WriteLine($"Associated keys: {entity.AssociatedKeys.Count}");
Console.WriteLine($"Deployment threshold: {entity.ActionThresholds.Deployment}");
}
catch (RpcClientException e)
{
Console.WriteLine($"RPC Error: {e.RpcError.Message}");
}
}
}
```

## Querying by account hash

You can also query using an `AccountHashKey`:

```csharp
var accountHash = new AccountHashKey("account-hash-56befc13a6fd62e18f361700a5e08f966901c34df8041b36ec97d54d605c23de");
var response = await client.GetEntity(accountHash);
var entity = response.Parse().Entity;
```

## Querying by entity address

Casper 2.0 uses a new addressing scheme for entities. You can query directly by the entity address string:

```csharp
var entityAddr = "entity-account-56befc13a6fd62e18f361700a5e08f966901c34df8041b36ec97d54d605c23de";
var response = await client.GetEntity(entityAddr);
var entity = response.Parse().Entity;
```

## Querying at a specific block

You can query the entity state at a specific block hash or height:

```csharp
// By block hash
var response = await client.GetEntity(publicKey, "block-hash-...");

// By block height
var response = await client.GetEntity(publicKey, 12345UL);
```

## Using the main purse for balance queries

One of the most common uses of `GetEntity()` is to obtain the `MainPurse` URef for balance queries:

```csharp
var response = await client.GetEntity(publicKey);
var mainPurse = response.Parse().Entity.MainPurse;

var balanceResponse = await client.QueryBalance(mainPurse);
Console.WriteLine($"Balance: {balanceResponse.Parse().BalanceValue} motes");
```

## Differences from GetAccountInfo

| Feature | `GetAccountInfo()` | `GetEntity()` |
|---|---|---|
| Casper 1.x | Returns full account info | Returns entity wrapper |
| Casper 2.x | Returns legacy account data | Returns unified entity model |
| Named keys | Included directly | May be structured differently |
| Main purse | Included | Included |
| Action thresholds | Included | Included |

For new applications targeting Casper 2.0, prefer `GetEntity()`. For backward compatibility with Casper 1.x, you can continue to use `GetAccountInfo()` or branch based on the node version.
Loading
Loading