Basalt.Generators.Contracts
0.3.0
dotnet add package Basalt.Generators.Contracts --version 0.3.0
NuGet\Install-Package Basalt.Generators.Contracts -Version 0.3.0
<PackageReference Include="Basalt.Generators.Contracts" Version="0.3.0" />
<PackageVersion Include="Basalt.Generators.Contracts" Version="0.3.0" />
<PackageReference Include="Basalt.Generators.Contracts" />
paket add Basalt.Generators.Contracts --version 0.3.0
#r "nuget: Basalt.Generators.Contracts, 0.3.0"
#:package Basalt.Generators.Contracts@0.3.0
#addin nuget:?package=Basalt.Generators.Contracts&version=0.3.0
#tool nuget:?package=Basalt.Generators.Contracts&version=0.3.0
Basalt.Generators.Contracts
Incremental source generator for smart contract ABI dispatch. Generates method dispatch tables, parameter deserialization, return value serialization, and ABI metadata for classes annotated with [BasaltContract].
Trigger Attributes
| Attribute | Target | Purpose |
|---|---|---|
[BasaltContract] |
Class | Marks a class as a Basalt smart contract. Triggers code generation. |
[BasaltEntrypoint] |
Method | Marks a state-mutating entry point. Mutability: "mutable". |
[BasaltView] |
Method | Marks a read-only view method. Mutability: "view". |
[BasaltConstructor] |
Method | Declared in the SDK but not yet wired into dispatch generation. Reserved for future use. |
[BasaltEvent] |
Nested class/struct | Declares an event type that can be emitted by the contract. |
[Indexed] |
Property | Marks an event property as indexed (included in event topics). |
All attributes are in the Basalt.Sdk.Contracts namespace.
Usage
[BasaltContract]
public partial class MyToken
{
[BasaltEntrypoint]
public void Transfer(Address to, UInt256 amount) { /* ... */ }
[BasaltView]
public UInt256 BalanceOf(Address account) { /* ... */ }
[BasaltEvent]
public class TransferEvent
{
[Indexed] public Address From { get; set; }
[Indexed] public Address To { get; set; }
public UInt256 Amount { get; set; }
}
}
Generated Code
For each [BasaltContract] class, the generator emits {Namespace}.{TypeName}.g.cs containing a partial class extension with:
Dispatch Method
public byte[] Dispatch(byte[] selector, byte[] calldata)
Reads the first 4 bytes of selector as a little-endian uint, then dispatches to the matching contract method via a switch statement. Parameters are deserialized from calldata using BasaltReader. Return values are serialized into a byte[] using BasaltWriter. Void methods return Array.Empty<byte>(). An InvalidOperationException is thrown for unknown selectors or selectors shorter than 4 bytes.
Selector Computation
Method selectors are computed using FNV-1a hash of the method name:
hash = 2166136261
for each char c in methodName:
hash ^= (byte)c
hash *= 16777619
The resulting 4-byte value is stored in little-endian order. Selectors appear as 0x{hash:X8} in the ABI.
ABI Metadata
A const string ContractAbi field is generated containing a JSON object with:
methods-- array of{ name, selector, mutability, params: [{ name, type }], returns }.events-- array of{ name, properties: [{ name, type, indexed }] }.
Supported Parameter / Return Types
| Type | Read Expression | Write Expression | Buffer Size |
|---|---|---|---|
byte |
reader.ReadByte() |
writer.WriteByte(v) |
1 |
ushort |
reader.ReadUInt16() |
writer.WriteUInt16(v) |
2 |
uint |
reader.ReadUInt32() |
writer.WriteUInt32(v) |
4 |
int |
reader.ReadInt32() |
writer.WriteInt32(v) |
4 |
ulong |
reader.ReadUInt64() |
writer.WriteUInt64(v) |
8 |
long |
reader.ReadInt64() |
writer.WriteInt64(v) |
8 |
bool |
reader.ReadBool() |
writer.WriteBool(v) |
1 |
string |
reader.ReadString() |
writer.WriteString(v) |
variable (4096 buffer) |
byte[] |
reader.ReadBytes().ToArray() |
writer.WriteBytes(v) |
variable (4096 buffer) |
Hash256 |
reader.ReadHash256() |
writer.WriteHash256(v) |
32 |
Address |
reader.ReadAddress() |
writer.WriteAddress(v) |
20 |
UInt256 |
reader.ReadUInt256() |
writer.WriteUInt256(v) |
32 |
Unsupported parameter or return types throw NotSupportedException at runtime.
Event Discovery
Events are collected from three sources:
- Nested types -- classes/structs nested directly inside the
[BasaltContract]class that have[BasaltEvent]. - Base type hierarchy -- walks the base class chain and collects
[BasaltEvent]-annotated nested types from each ancestor. - Interfaces and namespaces -- for each interface the contract implements, scans the interface's containing type, containing namespace, and the contract's own containing namespace for
[BasaltEvent]types. This discovers events declared alongside interfaces (e.g. aTransferEventnext to anIBST20interface). AHashSet<string>prevents duplicate entries.
Dependencies
| Package | Purpose |
|---|---|
Microsoft.CodeAnalysis.CSharp |
Roslyn compiler APIs |
Microsoft.CodeAnalysis.Analyzers |
Analyzer development helpers |
Target framework: netstandard2.0 (required for source generators).
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. 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. |
| .NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
| .NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
| .NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
| MonoAndroid | monoandroid was computed. |
| MonoMac | monomac was computed. |
| MonoTouch | monotouch was computed. |
| Tizen | tizen40 was computed. tizen60 was computed. |
| Xamarin.iOS | xamarinios was computed. |
| Xamarin.Mac | xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.0
- No dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.