Basalt.Sdk.Testing 0.1.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package Basalt.Sdk.Testing --version 0.1.0
                    
NuGet\Install-Package Basalt.Sdk.Testing -Version 0.1.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Basalt.Sdk.Testing" Version="0.1.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Basalt.Sdk.Testing" Version="0.1.0" />
                    
Directory.Packages.props
<PackageReference Include="Basalt.Sdk.Testing" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Basalt.Sdk.Testing --version 0.1.0
                    
#r "nuget: Basalt.Sdk.Testing, 0.1.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Basalt.Sdk.Testing@0.1.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Basalt.Sdk.Testing&version=0.1.0
                    
Install as a Cake Addin
#tool nuget:?package=Basalt.Sdk.Testing&version=0.1.0
                    
Install as a Cake Tool

Basalt.Sdk.Testing

Testing framework for Basalt smart contracts. Provides an in-process blockchain emulator that lets you deploy, call, and test contracts without running a full node.

Usage

using Basalt.Sdk.Testing;
using Xunit;

public class MyTokenTests : IDisposable
{
    private readonly BasaltTestHost _host = new();
    private readonly MyToken _token = new();

    [Fact]
    public void Transfer_Updates_Balances()
    {
        var deployer = BasaltTestHost.CreateAddress(1);
        var recipient = BasaltTestHost.CreateAddress(2);

        _host.SetCaller(deployer);
        _host.Call(() => _token.Initialize(1_000_000));

        _host.Call(() => _token.Transfer(recipient, 500));

        var balance = _host.Call(() => _token.BalanceOf(recipient));
        Assert.Equal(500ul, balance);
    }

    [Fact]
    public void Transfer_Insufficient_Balance_Reverts()
    {
        var user = BasaltTestHost.CreateAddress(1);
        _host.SetCaller(user);

        string? error = _host.ExpectRevert(() => _token.Transfer(user, 999));
        Assert.Contains("Insufficient", error);
    }

    public void Dispose() => _host.Dispose();
}

BasaltTestHost API

Setup

var host = new BasaltTestHost();
host.SetCaller(address);              // Set msg.sender
host.SetBlockTimestamp(1700000000);    // Set block timestamp
host.SetBlockHeight(100);             // Set block number
host.AdvanceBlocks(10);               // Advance by N blocks (400ms per block)
host.Deploy(contractAddress, contract); // Deploy a contract for cross-contract calls

AdvanceBlocks(count) increments the block height by count and advances the timestamp by count * 400 (400ms per block).

Cross-contract calls are automatically wired: BasaltTestHost sets Context.CrossContractCallHandler to a reflection-based dispatcher that routes calls to contracts registered via Deploy. The handler looks up the target contract by address and invokes the named method via reflection.

Execution

host.Call(() => contract.Method());           // Execute state-mutating call
T result = host.Call(() => contract.View());  // Execute view call
string? err = host.ExpectRevert(() => ...);   // Expect revert

Snapshots

host.TakeSnapshot();     // Save current state
// ... make changes ...
host.RestoreSnapshot();  // Roll back to snapshot

Events

host.Call(() => contract.DoSomething());
var events = host.GetEvents<TransferEvent>();       // Get events of a specific type
var all = host.EmittedEvents;                       // All emitted events as IReadOnlyList<(string EventName, object Event)>
host.ClearEvents();

Address Helpers

byte[] addr1 = BasaltTestHost.CreateAddress(1);       // From seed byte (20 bytes, seed in last byte)
byte[] addr2 = BasaltTestHost.CreateAddress("0A");     // From hex string (left-padded to 40 hex chars)
byte[] addr3 = BasaltTestHost.CreateAddress("0x1234"); // "0x" prefix is stripped automatically

Note: CreateAddress(string hex) expects hex-encoded strings, not arbitrary names. The input is left-padded with zeros to 40 hex characters (20 bytes) and converted via Convert.FromHexString.

Dependencies

Package Purpose
Basalt.Core Hash256, Address, UInt256
Basalt.Execution TransactionExecutor, BlockHeader
Basalt.Storage InMemoryStateDb
Basalt.Sdk.Contracts Contract attributes, Context, storage
Product Compatible and additional computed target framework versions.
.NET net9.0 is compatible.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
0.3.0 108 3/17/2026
0.2.0 94 2/22/2026
0.1.0 103 2/15/2026