Skip to content

Conversation

@lcovar
Copy link
Contributor

@lcovar lcovar commented Jan 26, 2026

This commit adds comprehensive transaction building capabilities to wasm-solana,
enabling the construction of both legacy and versioned (MessageV0) Solana
transactions without requiring @solana/web3.js dependency.

Transaction Building

Core Builder (Rust)

  • src/builder/build.rs - Main transaction building logic from TransactionIntent
  • src/builder/types.rs - Type definitions (TransactionIntent, Instruction, Nonce, etc.)
  • src/builder/versioned.rs - MessageV0/versioned transaction building from raw data

Supported Instructions

  • Transfer (SOL and SPL tokens)
  • CreateAssociatedTokenAccount
  • StakingActivate, StakingDeactivate, StakingWithdraw
  • StakingAuthorize, StakingAuthorizeRaw
  • StakingPartialDeactivate (split + deactivate)
  • StakingSplit
  • AdvanceNonceAccount (durable nonce support)
  • Memo
  • SetComputeUnitLimit, SetComputeUnitPrice
  • TokenTransfer (SPL Token Program transfers)
  • CustomInstruction (raw instruction support)

TypeScript API (js/builder.ts)

  • buildTransaction() - Build transactions from high-level intents
  • Type definitions for all instruction kinds and nonce sources
  • Support for Address Lookup Tables in versioned transactions

Versioned Transaction Support

Parsing (js/versioned.ts, src/versioned.rs)

  • VersionedTransaction class for parsing both legacy and MessageV0 transactions
  • isVersionedTransaction() utility to detect transaction format
  • Extract Address Lookup Tables, static account keys, and instructions
  • Support for adding signatures to parsed transactions

Building from Raw Data

  • buildFromRawVersionedData() - Build MessageV0 from pre-compiled instruction data
  • Preserves account indexes, ALT references, and message header from source
  • Enables rebuilding transactions with different blockhash/nonce

WASM Exports

Constants (src/wasm/constants.rs)

  • Program IDs: systemProgramId, tokenProgramId, token2022ProgramId, etc.
  • Sysvar addresses: sysvarClockAddress, sysvarRecentBlockhashes, etc.
  • Well-known addresses: memoV1ProgramId, associatedTokenProgramId, etc.

Builder Functions (src/wasm/builder.rs)

  • buildTransaction() - WASM entry point for transaction building
  • buildFromRawVersionedData() - WASM entry point for versioned building

Transaction Methods

  • serializeMessage() - Returns message bytes for web3.js API compatibility
  • Property-based API (feePayer, recentBlockhash, signatures)
  • CustomInstruction type for passing raw instructions to builder

Additional Changes

CreateATA Fix

  • Fixed programId return value for CreateAssociatedTokenAccount instructions
  • Now correctly returns Token Program ID instead of ATA Program ID

Stake Split Support

  • Added StakeSplitIntent for stake account splitting
  • Proper handling of split instruction in both building and parsing

Test Coverage

  • test/builder.ts - Comprehensive builder tests (693 lines)
  • test/versioned.ts - Versioned transaction tests
  • test/transaction.ts - Extended transaction tests with building

Architecture

The builder follows an intent-based architecture:

  1. Caller creates a TransactionIntent with fee payer, nonce, and instructions
  2. Builder converts intents to native Solana instructions
  3. Transaction is serialized as legacy or versioned based on ALT presence
  4. Returns raw transaction bytes for signing

This design eliminates @solana/web3.js dependency while maintaining full
compatibility with the Solana transaction format.

@lcovar lcovar force-pushed the BTC-2955 branch 3 times, most recently from 02904b6 to 5bff3d8 Compare January 27, 2026 00:04
Copy link
Contributor

@OttoAllmendinger OttoAllmendinger left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

make sure we use bigint instead of string when dealing with amounts

Comment on lines 57 to 58
/** Lamports to transfer (as string) */
lamports: string;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bigint

Comment on lines 159 to 160
/** Amount in lamports to withdraw (as string) */
lamports: string;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bigint

use solana_system_interface::instruction::{self as system_ix, SystemInstruction};
use spl_stake_pool::instruction::StakePoolInstruction;

/// Well-known program IDs and sysvars
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

where are these defined?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added links

@lcovar lcovar force-pushed the BTC-2955 branch 20 times, most recently from 6a37e03 to 3ea21e0 Compare January 29, 2026 08:50
… support

This commit adds comprehensive transaction building capabilities to wasm-solana,
enabling the construction of both legacy and versioned (MessageV0) Solana
transactions without requiring @solana/web3.js dependency.

## Transaction Building

### Core Builder (Rust)
- `src/builder/build.rs` - Main transaction building logic from TransactionIntent
- `src/builder/types.rs` - Type definitions (TransactionIntent, Instruction, Nonce, etc.)
- `src/builder/versioned.rs` - MessageV0/versioned transaction building from raw data

### Supported Instructions
- Transfer (SOL and SPL tokens)
- CreateAssociatedTokenAccount
- StakingActivate, StakingDeactivate, StakingWithdraw
- StakingAuthorize, StakingAuthorizeRaw
- StakingPartialDeactivate (split + deactivate)
- StakingSplit
- AdvanceNonceAccount (durable nonce support)
- Memo
- SetComputeUnitLimit, SetComputeUnitPrice
- TokenTransfer (SPL Token Program transfers)
- CustomInstruction (raw instruction support)

### TypeScript API (`js/builder.ts`)
- `buildTransaction()` - Build transactions from high-level intents
- Type definitions for all instruction kinds and nonce sources
- Support for Address Lookup Tables in versioned transactions

## Versioned Transaction Support

### Parsing (`js/versioned.ts`, `src/versioned.rs`)
- `VersionedTransaction` class for parsing both legacy and MessageV0 transactions
- `isVersionedTransaction()` utility to detect transaction format
- Extract Address Lookup Tables, static account keys, and instructions
- Support for adding signatures to parsed transactions

### Building from Raw Data
- `buildFromRawVersionedData()` - Build MessageV0 from pre-compiled instruction data
- Preserves account indexes, ALT references, and message header from source
- Enables rebuilding transactions with different blockhash/nonce

## WASM Exports

### Constants (`src/wasm/constants.rs`)
- Program IDs: systemProgramId, tokenProgramId, token2022ProgramId, etc.
- Sysvar addresses: sysvarClockAddress, sysvarRecentBlockhashes, etc.
- Well-known addresses: memoV1ProgramId, associatedTokenProgramId, etc.

### Builder Functions (`src/wasm/builder.rs`)
- `buildTransaction()` - WASM entry point for transaction building
- `buildFromRawVersionedData()` - WASM entry point for versioned building

### Transaction Methods
- `serializeMessage()` - Returns message bytes for web3.js API compatibility
- Property-based API (feePayer, recentBlockhash, signatures)
- `CustomInstruction` type for passing raw instructions to builder

## Additional Changes

### CreateATA Fix
- Fixed `programId` return value for CreateAssociatedTokenAccount instructions
- Now correctly returns Token Program ID instead of ATA Program ID

### Stake Split Support
- Added `StakeSplitIntent` for stake account splitting
- Proper handling of split instruction in both building and parsing

### Test Coverage
- `test/builder.ts` - Comprehensive builder tests (693 lines)
- `test/versioned.ts` - Versioned transaction tests
- `test/transaction.ts` - Extended transaction tests with building

## Architecture

The builder follows an intent-based architecture:
1. Caller creates a `TransactionIntent` with fee payer, nonce, and instructions
2. Builder converts intents to native Solana instructions
3. Transaction is serialized as legacy or versioned based on ALT presence
4. Returns raw transaction bytes for signing

This design eliminates @solana/web3.js dependency while maintaining full
compatibility with the Solana transaction format.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if we already expose a Transaction type, maybe we should return that here instead of the serialized for of it? The caller can serialize if they need

@lcovar lcovar marked this pull request as ready for review January 29, 2026 19:09
@lcovar lcovar requested a review from a team as a code owner January 29, 2026 19:09
@lcovar lcovar merged commit 0c22062 into master Jan 29, 2026
6 checks passed
@lcovar lcovar deleted the BTC-2955 branch January 29, 2026 19:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants