Class: Solace::Transaction

Inherits:
Object
  • Object
show all
Includes:
Concerns::BinarySerializable
Defined in:
lib/solace/transaction.rb

Overview

Class representing a Solana transaction

Transactions are the basic building blocks of Solana. They contain a message and an array of signatures. The message contains the instructions to be executed and the accounts that are used by the instructions. The signatures are the required signatures of the accounts that are used by the instructions. This class provides methods for signing, serializing, and deserializing transactions.

The BufferLayout is:

- Signatures (variable length)
- Version (1 byte if versioned)
- Message header (3 bytes)
- Account keys (variable length)
- Recent blockhash (32 bytes)
- Instructions (variable length)
- Address lookup table (variable length if versioned)

Examples:

# Create a new transaction
tx = Solace::Transaction.new

# Add a message to the transaction
tx.message = Solace::Message.new(**message_params)

# Sign the transaction
tx.sign(payer_keypair)

Since:

  • 0.0.1

Constant Summary collapse

SERIALIZER =

The serializer for the transaction

Since:

  • 0.0.1

Solace::Serializers::TransactionSerializer
DESERIALIZER =

The deserializer for the transaction

Since:

  • 0.0.1

Solace::Serializers::TransactionDeserializer
SIGNATURE_PLACEHOLDER =

Placeholder for a signature in the transaction

Since:

  • 0.0.1

Solace::Utils::Codecs.base58_to_binary('1' * 64)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Concerns::BinarySerializable

included, #serialize, #to_binary, #to_bytes, #to_io

Constructor Details

#initialize(signatures: [], message: Solace::Message.new) ⇒ Transaction

Initialize a new transaction

Since:

  • 0.0.1



63
64
65
66
67
68
69
70
# File 'lib/solace/transaction.rb', line 63

def initialize(
  signatures: [],
  message: Solace::Message.new
)
  super()
  @signatures = signatures
  @message = message
end

Instance Attribute Details

#messageSolace::Message

Returns Message of the transaction.

Returns:

Since:

  • 0.0.1



48
49
50
# File 'lib/solace/transaction.rb', line 48

def message
  @message
end

#signaturesArray<String>

Returns Signatures of the transaction (binary).

Returns:

  • (Array<String>)

    Signatures of the transaction (binary)

Since:

  • 0.0.1



45
46
47
# File 'lib/solace/transaction.rb', line 45

def signatures
  @signatures
end

Class Method Details

.from(base64_tx) ⇒ Transaction

Deserialize a base64 encoded transaction into a Solace::Transaction object

Parameters:

  • base64_tx (String)

    The base64 encoded transaction

Returns:

Since:

  • 0.0.1



55
56
57
# File 'lib/solace/transaction.rb', line 55

def from(base64_tx)
  DESERIALIZER.new(Solace::Utils::Codecs.base64_to_bytestream(base64_tx)).call
end

Instance Method Details

#sign(*keypairs) ⇒ Array<String>

Sign the transaction

Calls sign_and_update_signatures for each keypair passed in.

Parameters:

  • keypairs (Array<Solace::Keypair>)

    The keypairs to sign the transaction with

Returns:

  • (Array<String>)

    The signatures of the transaction

Since:

  • 0.0.1



86
87
88
# File 'lib/solace/transaction.rb', line 86

def sign(*keypairs)
  keypairs.map { |keypair| sign_and_update_signatures(keypair) }
end

#signatureString?

Returns the first signature of the transaction (signature of the transaction fee payer)

Returns:

  • (String, nil)

    The first signature of the transaction or nil if there are no signatures

Since:

  • 0.1.0



76
77
78
# File 'lib/solace/transaction.rb', line 76

def signature
  Utils::Codecs.binary_to_base58(signatures.first) unless signatures.empty?
end