Class: Solace::Programs::ZarTrustlessEscrow

Inherits:
Base
  • Object
show all
Includes:
EscrowDepositOperations, MediatedEscrowDepositOperations
Defined in:
lib/solace/zar_trustless_escrow/programs/zar_trustless_escrow.rb,
lib/solace/zar_trustless_escrow/programs/zar_trustless_escrow/escrow_deposit_operations.rb,
lib/solace/zar_trustless_escrow/programs/zar_trustless_escrow/mediated_escrow_deposit_operations.rb

Overview

High-level client for the ZAR Trustless Escrow program.

Provides PDA derivation (class methods), account fetching, the shared vault setup, and a compose/send pair per instruction. The instruction operations live in two mixins included below, split by escrow type:

- {EscrowDepositOperations} — two-party escrow (deposit/claim/reclaim).
- {MediatedEscrowDepositOperations} — three-party mediated escrow.

Examples:

program = Solace::Programs::ZarTrustlessEscrow.new(connection:)
vault, = program.get_vault_address(mint: mint_address)

Defined Under Namespace

Modules: EscrowDepositOperations, MediatedEscrowDepositOperations

Class Method Summary collapse

Instance Method Summary collapse

Methods included from MediatedEscrowDepositOperations

#compose_mediated_deposit, #compose_mediated_reclaim, #compose_mediated_release, #fetch_mediated_escrow_deposit, #mediated_deposit, #mediated_escrow_deposit_exists?, #mediated_reclaim, #mediated_release

Methods included from EscrowDepositOperations

#claim, #compose_claim, #compose_deposit, #compose_reclaim, #deposit, #escrow_deposit_exists?, #fetch_escrow_deposit, #reclaim

Constructor Details

#initialize(connection:, program_id: Solace::ZarTrustlessEscrow::PROGRAM_ID) ⇒ ZarTrustlessEscrow

Initializes a new ZAR Trustless Escrow client.

Parameters:

  • connection (Solace::Connection)

    The connection to the Solana cluster.

  • program_id (String) (defaults to: Solace::ZarTrustlessEscrow::PROGRAM_ID)

    The escrow program id (default: mainnet PROGRAM_ID).



68
69
70
# File 'lib/solace/zar_trustless_escrow/programs/zar_trustless_escrow.rb', line 68

def initialize(connection:, program_id: Solace::ZarTrustlessEscrow::PROGRAM_ID)
  super
end

Class Method Details

.get_escrow_deposit_address(claim_authority:, program_id: Solace::ZarTrustlessEscrow::PROGRAM_ID) ⇒ Array<String, Integer>

Derives the EscrowDeposit PDA for a claim authority, from the on-chain seeds [“escrow_deposit”, claim_authority].

Parameters:

  • claim_authority (#to_s)

    Base58 authority that can claim the deposit.

  • program_id (#to_s) (defaults to: Solace::ZarTrustlessEscrow::PROGRAM_ID)

    The escrow program id (default: mainnet PROGRAM_ID).

Returns:

  • (Array<String, Integer>)

    The escrow deposit address and bump seed.



30
31
32
33
34
35
# File 'lib/solace/zar_trustless_escrow/programs/zar_trustless_escrow.rb', line 30

def get_escrow_deposit_address(claim_authority:, program_id: Solace::ZarTrustlessEscrow::PROGRAM_ID)
  Solace::Utils::PDA.find_program_address(
    [Solace::ZarTrustlessEscrow::ESCROW_DEPOSIT_SEED, claim_authority.to_s],
    program_id.to_s
  )
end

.get_mediated_escrow_deposit_address(id:, program_id: Solace::ZarTrustlessEscrow::PROGRAM_ID) ⇒ Array<String, Integer>

Derives the MediatedEscrowDeposit PDA for an id, from the on-chain seeds [“mediated_escrow_deposit”, id].

Parameters:

  • id (#to_s)

    Base58 unique id the escrow was created with.

  • program_id (#to_s) (defaults to: Solace::ZarTrustlessEscrow::PROGRAM_ID)

    The escrow program id (default: mainnet PROGRAM_ID).

Returns:

  • (Array<String, Integer>)

    The mediated escrow deposit address and bump seed.



43
44
45
46
47
48
# File 'lib/solace/zar_trustless_escrow/programs/zar_trustless_escrow.rb', line 43

def get_mediated_escrow_deposit_address(id:, program_id: Solace::ZarTrustlessEscrow::PROGRAM_ID)
  Solace::Utils::PDA.find_program_address(
    [Solace::ZarTrustlessEscrow::MEDIATED_ESCROW_DEPOSIT_SEED, id.to_s],
    program_id.to_s
  )
end

.get_vault_address(mint:, program_id: Solace::ZarTrustlessEscrow::PROGRAM_ID) ⇒ Array<String, Integer>

Derives the per-mint program vault PDA, from the on-chain seeds [“vault”, mint]. A single vault is shared across all deposits for a mint.

Parameters:

  • mint (#to_s)

    Base58 mint the vault holds.

  • program_id (#to_s) (defaults to: Solace::ZarTrustlessEscrow::PROGRAM_ID)

    The escrow program id (default: mainnet PROGRAM_ID).

Returns:

  • (Array<String, Integer>)

    The vault address and bump seed.



56
57
58
59
60
61
# File 'lib/solace/zar_trustless_escrow/programs/zar_trustless_escrow.rb', line 56

def get_vault_address(mint:, program_id: Solace::ZarTrustlessEscrow::PROGRAM_ID)
  Solace::Utils::PDA.find_program_address(
    [Solace::ZarTrustlessEscrow::VAULT_SEED, mint.to_s],
    program_id.to_s
  )
end

Instance Method Details

#compose_token_account_init(mint:, fee_payer:, token_program_id: Solace::Constants::TOKEN_PROGRAM_ID) ⇒ Solace::TransactionComposer

Prepares a token-account-init transaction. Derives the program vault PDA.

Parameters:

  • mint (#to_s)

    Mint the vault holds.

  • fee_payer (#to_s, Keypair)

    The fee payer (writable signer).

  • token_program_id (String) (defaults to: Solace::Constants::TOKEN_PROGRAM_ID)

    Token program that owns the mint (default: legacy SPL Token).

Returns:

  • (Solace::TransactionComposer)

    A composer with the token-account-init instruction.



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/solace/zar_trustless_escrow/programs/zar_trustless_escrow.rb', line 129

def (
  mint:,
  fee_payer:,
  token_program_id: Solace::Constants::TOKEN_PROGRAM_ID
)
  , = get_vault_address(mint:)

  ix = Composers::ZarTrustlessEscrowTokenAccountInitComposer.new(
    mint:,
    program_token_account:,
    fee_payer:,
    program_id:,
    token_program_id:
  )

  TransactionComposer.new(connection:).add_instruction(ix)
end

#get_escrow_deposit_address(**options) ⇒ Array<String, Integer>

Alias for the class method, defaulting to this client’s program id.

Parameters:

  • options (Hash)

    Options for the get_escrow_deposit_address class method.

Returns:

  • (Array<String, Integer>)

    The escrow deposit address and bump seed.



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

def get_escrow_deposit_address(**options)
  self.class.get_escrow_deposit_address(program_id:, **options)
end

#get_mediated_escrow_deposit_address(**options) ⇒ Array<String, Integer>

Alias for the class method, defaulting to this client’s program id.

Parameters:

  • options (Hash)

    Options for the get_mediated_escrow_deposit_address class method.

Returns:

  • (Array<String, Integer>)

    The mediated escrow deposit address and bump seed.



84
85
86
# File 'lib/solace/zar_trustless_escrow/programs/zar_trustless_escrow.rb', line 84

def get_mediated_escrow_deposit_address(**options)
  self.class.get_mediated_escrow_deposit_address(program_id:, **options)
end

#get_vault_address(**options) ⇒ Array<String, Integer>

Alias for the class method, defaulting to this client’s program id.

Parameters:

  • options (Hash)

    Options for the get_vault_address class method.

Returns:

  • (Array<String, Integer>)

    The vault address and bump seed.



92
93
94
# File 'lib/solace/zar_trustless_escrow/programs/zar_trustless_escrow.rb', line 92

def get_vault_address(**options)
  self.class.get_vault_address(program_id:, **options)
end

#token_account_init(payer:, sign: true, execute: true, **composer_opts) {|composer| ... } ⇒ Solace::Transaction

Initializes the per-mint program vault, signs, and (optionally) sends it. The vault is shared across both escrow types, so this lives on the client itself rather than in an escrow-type mixin. The fee payer is the only signer.

Parameters:

  • payer (Solace::Keypair)

    The fee payer (pays fees and rent, signs).

  • sign (Boolean) (defaults to: true)

    Whether to sign the transaction.

  • execute (Boolean) (defaults to: true)

    Whether to send the transaction.

  • composer_opts (Hash)

Yields:

  • (composer)

Returns:

  • (Solace::Transaction)

    The created or sent transaction.



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/solace/zar_trustless_escrow/programs/zar_trustless_escrow.rb', line 105

def (payer:, sign: true, execute: true, **composer_opts)
  composer = (fee_payer: payer, **composer_opts)

  yield composer if block_given?

  tx = composer
       .set_fee_payer(payer)
       .compose_transaction

  if sign
    tx.sign(payer)

    connection.send_transaction(tx.serialize) if execute
  end

  tx
end