TypeScript SDK#
@solux/sdk does everything the CLI does, from Node 20+ or a browser, plus
local runs, estimates and verification. It depends only on @solana/kit, and
embeds the Solux core as 60 KB of WebAssembly.
npm install @solux/sdk @solana/kit
The package lives in packages/sdk until it is published to npm.
Connect#
import { createKeyPairSignerFromBytes } from "@solana/kit";
import { SoluxClient } from "@solux/sdk";
const payer = await createKeyPairSignerFromBytes(secretKeyBytes);
const solux = new SoluxClient({ rpc: "http://127.0.0.1:8899", payer });
| Option | Default | |
|---|---|---|
rpc |
An RPC URL, or a kit RPC from createSolanaRpc |
|
payer |
Pays fees and rent, and becomes the machines' authority | |
programId |
SOLUX_PROGRAM_ID |
Another deployment of the program |
stepComputeUnits |
1_400_000 |
Compute-unit limit of each Step transaction |
confirmTimeout |
60 |
Seconds before a transaction is sent again |
The client sends over plain HTTP and confirms by polling getSignatureStatuses,
so it needs no websocket and works with any RPC.
Create a machine#
const machine = await solux.createMachine(elfBytes, {
ramSize: 1 << 20,
onProgress: ({ phase, done, total }) => console.log(phase, done, total),
});
// { machine: Address, ram: Address, chunks: 8 }
It boots the image locally to lay out memory, creates both accounts and runs
Init in one transaction, uploads the RAM as compressed WriteLz chunks wave by
wave, and finally reads the RAM account back and compares it byte for byte.
Linux takes 2,013 upload transactions; the starter takes 8.
ramSize defaults to 1 MiB for an ELF and 10 MiB for anything bigger than
512 KB. Pick the smallest size that fits: the rent is proportional to it, and
you get all of it back with close.
Run it#
const { steps, lines, machine: state } = await solux.crank(machine, {
until: "> ", // stop once the program prints this
maxSteps: 500, // or after this many Steps
window: 8, // Steps in flight at once
});
crank sends Steps until the machine halts, prints until, or reaches
maxSteps. It returns every console line, in order. The first Step goes alone
to measure the cost per instruction; the following ones are sized to fit the
compute limit.
For one Step at a time:
const log = await solux.step(machine, 25_000);
// { step, ran, total, pc, exit: "Budget" | "PowerOff" | ..., lines, computeUnits, signature }
Type into it#
await solux.type(machine, "12.50\n7.25\n\n");
Each call is one Type transaction. The keys wait in the machine account (248
bytes at most) until the program reads them during the next Steps. Longer input
is split, and when the buffer is full type runs one Step to let the program
read before it sends the rest.
For shells, run types a command and cranks until the prompt comes back:
const out = await solux.run(machine, "echo solux $((6*7))", { prompt: "# " });
// "echo solux $((6*7))\nsolux 42\n~ # "
Read it#
const state = await solux.readMachine(machine.machine);
state.status; // "running" | "powered off" | ...
state.cpu.instret; // instructions retired
state.console; // the last 4 KB printed
state.pendingInput; // keys typed and not read yet
Verify any machine#
import { verifyMachine } from "@solux/sdk";
const v = await verifyMachine("https://your-rpc", machineAddress);
v.ok; // CPU state and RAM equal the accounts, byte for byte
v.instret; // { replayed, onchain }
v.ramHash; // SHA-256 of both
v.output; // what the program printed, from the replay
It needs no key and trusts no one: see Verification.
Run locally, estimate the cost#
import { Emulator, estimate } from "@solux/sdk";
const emu = await Emulator.boot(elfBytes, 1 << 20);
emu.type("12.50\n\n");
const r = emu.run({ perStep: 25_000 });
// { exit: "power-off", steps, instructions, output }
const e = await estimate(elfBytes, 1 << 20);
// { uploadTransactions, stepTransactions, computeUnits, feeLamports, rentLamports, ... }
Emulator is the same core as the program. estimate counts the upload with
the client's own compressor, so its transaction count is exact; compute units
use the measured average of 52 per instruction.
Low-level pieces#
Every instruction has a builder, so you can compose your own transactions:
initInstruction(accounts, authority, entry, dtb) |
Writes the header and the CPU reset state |
writeInstruction(accounts, authority, offset, bytes) |
Raw upload |
writeLzInstruction(accounts, authority, offset, outLen, lz) |
Compressed upload; chunks from Emulator.uploadChunks() |
stepInstruction(accounts, instructions, nonce?) |
Run; no signer needed |
typeInstruction(accounts, authority, keys) |
Queue keys |
closeInstruction(accounts, authority, recipient) |
Delete both accounts, refund the rent |
decodeInstruction(data), decodeMachine(data), parseStepLogs(logs) |
The other way round |
Errors#
A failed transaction throws TransactionFailed, with the signature, the raw
error and, for the program's own errors, code:
import { SoluxError, soluxErrorName } from "@solux/sdk";
try {
await otherClient.type(machine, "ls
");
} catch (e) {
if (e.code === SoluxError.NotAuthority) { /* only the machine's authority can type */ }
}
The full list is in the program reference.