Module: Solace::Programs::ZarTrustlessEscrow::EscrowDepositOperations

Included in:
Solace::Programs::ZarTrustlessEscrow
Defined in:
lib/solace/zar_trustless_escrow/programs/zar_trustless_escrow/escrow_deposit_operations.rb

Overview

Two-party escrow operations for Solace::Programs::ZarTrustlessEscrow: deposit, claim, and reclaim against the EscrowDeposit account. Mixed into the program client; methods run in the client’s instance context (connection, program_id, and the get_*_address derivations are available). The per-mint vault setup (token_account_init) is shared across escrow types and lives on the client itself, not here.

Instance Method Summary collapse

Instance Method Details

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

Claims a two-party escrow on behalf of the claim authority, signs, and (optionally) sends it. The claim authority and fee payer sign.

Parameters:

  • payer (Solace::Keypair)

    The fee payer (signs).

  • claim_authority (Solace::Keypair)

    The claim authority (signs).

  • sign (Boolean) (defaults to: true)

    Whether to sign the transaction.

  • execute (Boolean) (defaults to: true)

    Whether to send the transaction.

  • composer_opts (Hash)

    Options for #compose_claim.

Yields:

  • (composer)

Returns:

  • (Solace::Transaction)

    The created or sent transaction.



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/solace/zar_trustless_escrow/programs/zar_trustless_escrow/escrow_deposit_operations.rb', line 119

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

  yield composer if block_given?

  tx = composer
       .set_fee_payer(payer)
       .compose_transaction

  if sign
    tx.sign(payer, claim_authority)

    connection.send_transaction(tx.serialize) if execute
  end

  tx
end

#compose_claim(mint:, claim_authority:, claimant:, fee_payer:, token_program_id: Solace::Constants::TOKEN_PROGRAM_ID) ⇒ Solace::TransactionComposer

Prepares a claim transaction. Derives the EscrowDeposit PDA, the program vault, and the claimant’s associated token account.

Parameters:

  • mint (#to_s)

    Mint of the escrowed tokens.

  • claim_authority (#to_s, Keypair)

    Authority that can claim the deposit.

  • claimant (#to_s)

    Recipient of the released tokens.

  • 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 claim instruction.



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/solace/zar_trustless_escrow/programs/zar_trustless_escrow/escrow_deposit_operations.rb', line 146

def compose_claim(
  mint:,
  claim_authority:,
  claimant:,
  fee_payer:,
  token_program_id: Solace::Constants::TOKEN_PROGRAM_ID
)
  escrow_deposit, = get_escrow_deposit_address(claim_authority:)
  , = get_vault_address(mint:)
  , = Solace::Programs::AssociatedTokenAccount.get_address(
    owner:            claimant,
    mint:,
    token_program_id:
  )

  ix = Composers::ZarTrustlessEscrowClaimComposer.new(
    mint:,
    claim_authority:,
    claimant:,
    claimant_token_account:,
    escrow_deposit:,
    program_token_account:,
    fee_payer:,
    program_id:,
    token_program_id:
  )

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

#compose_deposit(mint:, depositor:, claim_authority:, amount:, fee_payer:, sponsor: nil, token_program_id: Solace::Constants::TOKEN_PROGRAM_ID) ⇒ Solace::TransactionComposer

Prepares a deposit transaction. Derives the EscrowDeposit PDA, the program vault, and the depositor’s associated token account.

Parameters:

  • mint (#to_s)

    Mint of the escrowed tokens.

  • depositor (#to_s, Keypair)

    The depositor.

  • claim_authority (#to_s)

    Authority that can claim the deposit.

  • amount (Integer)

    u64 amount to deposit.

  • fee_payer (#to_s, Keypair)

    The fee payer (writable signer).

  • sponsor (#to_s) (defaults to: nil)

    (Optional) Fee sponsor recorded on the deposit (default: nil).

  • 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 deposit instruction.



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/solace/zar_trustless_escrow/programs/zar_trustless_escrow/escrow_deposit_operations.rb', line 76

def compose_deposit(
  mint:,
  depositor:,
  claim_authority:,
  amount:,
  fee_payer:,
  sponsor: nil,
  token_program_id: Solace::Constants::TOKEN_PROGRAM_ID
)
  escrow_deposit, = get_escrow_deposit_address(claim_authority:)
  , = get_vault_address(mint:)
  , = Solace::Programs::AssociatedTokenAccount.get_address(
    owner:            depositor,
    mint:,
    token_program_id:
  )

  ix = Composers::ZarTrustlessEscrowDepositComposer.new(
    mint:,
    depositor:,
    depositor_token_account:,
    escrow_deposit:,
    program_token_account:,
    fee_payer:,
    amount:,
    claim_authority:,
    sponsor:,
    program_id:,
    token_program_id:
  )

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

#compose_reclaim(mint:, depositor:, claim_authority:, fee_payer:, token_program_id: Solace::Constants::TOKEN_PROGRAM_ID) ⇒ Solace::TransactionComposer

Prepares a reclaim transaction. Derives the EscrowDeposit PDA, the program vault, and the depositor’s associated token account.

Parameters:

  • mint (#to_s)

    Mint of the escrowed tokens.

  • depositor (#to_s)

    Original depositor (receives the tokens).

  • claim_authority (#to_s)

    Authority used to derive the escrow PDA.

  • fee_payer (#to_s, Keypair)

    The fee payer (the depositor or the sponsor).

  • 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 reclaim instruction.



211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
# File 'lib/solace/zar_trustless_escrow/programs/zar_trustless_escrow/escrow_deposit_operations.rb', line 211

def compose_reclaim(
  mint:,
  depositor:,
  claim_authority:,
  fee_payer:,
  token_program_id: Solace::Constants::TOKEN_PROGRAM_ID
)
  escrow_deposit, = get_escrow_deposit_address(claim_authority:)
  , = get_vault_address(mint:)
  , = Solace::Programs::AssociatedTokenAccount.get_address(
    owner:            depositor,
    mint:,
    token_program_id:
  )

  ix = Composers::ZarTrustlessEscrowReclaimComposer.new(
    mint:,
    depositor:,
    depositor_token_account:,
    escrow_deposit:,
    program_token_account:,
    fee_payer:,
    claim_authority:,
    program_id:,
    token_program_id:
  )

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

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

Deposits funds into a two-party escrow, signs, and (optionally) sends it. The depositor and fee payer sign; when a sponsor pays, pass the sponsor keypair as payer and its address as sponsor.

Parameters:

  • payer (Solace::Keypair)

    The fee payer (the depositor or the sponsor).

  • depositor (Solace::Keypair)

    The depositor (signs).

  • sign (Boolean) (defaults to: true)

    Whether to sign the transaction.

  • execute (Boolean) (defaults to: true)

    Whether to send the transaction.

  • composer_opts (Hash)

    Options for #compose_deposit.

Yields:

  • (composer)

Returns:

  • (Solace::Transaction)

    The created or sent transaction.



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/solace/zar_trustless_escrow/programs/zar_trustless_escrow/escrow_deposit_operations.rb', line 47

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

  yield composer if block_given?

  tx = composer
       .set_fee_payer(payer)
       .compose_transaction

  if sign
    tx.sign(payer, depositor)

    connection.send_transaction(tx.serialize) if execute
  end

  tx
end

#escrow_deposit_exists?(address:) ⇒ Boolean

Returns whether an EscrowDeposit account exists at the given address. Derive the address first with Solace::Programs::ZarTrustlessEscrow#get_escrow_deposit_address.

Parameters:

  • address (#to_s)

    Base58 address of the EscrowDeposit account.

Returns:

  • (Boolean)

    True if an account exists at the address.



33
34
35
# File 'lib/solace/zar_trustless_escrow/programs/zar_trustless_escrow/escrow_deposit_operations.rb', line 33

def escrow_deposit_exists?(address:)
  !connection.(address.to_s).nil?
end

#fetch_escrow_deposit(address:) ⇒ Solace::ZarTrustlessEscrow::EscrowDeposit

Fetches and deserializes the EscrowDeposit account at the given address. Derive the address first with Solace::Programs::ZarTrustlessEscrow#get_escrow_deposit_address.

Parameters:

  • address (#to_s)

    Base58 address of the EscrowDeposit account.

Returns:

Raises:

  • (RuntimeError)

    If no account exists at the address.



19
20
21
22
23
24
25
26
# File 'lib/solace/zar_trustless_escrow/programs/zar_trustless_escrow/escrow_deposit_operations.rb', line 19

def fetch_escrow_deposit(address:)
   = connection.(address.to_s)
  raise "EscrowDeposit account not found at #{address}" unless 

  Solace::ZarTrustlessEscrow::EscrowDeposit.deserialize(
    Solace::Utils::Codecs.base64_to_bytestream(['data'][0])
  )
end

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

Reclaims an unclaimed two-party escrow, signs, and (optionally) sends it. The fee payer (the depositor or the sponsor) is the only signer.

Parameters:

  • payer (Solace::Keypair)

    The fee payer (the depositor or the sponsor).

  • sign (Boolean) (defaults to: true)

    Whether to sign the transaction.

  • execute (Boolean) (defaults to: true)

    Whether to send the transaction.

  • composer_opts (Hash)

    Options for #compose_reclaim.

Yields:

  • (composer)

Returns:

  • (Solace::Transaction)

    The created or sent transaction.



184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
# File 'lib/solace/zar_trustless_escrow/programs/zar_trustless_escrow/escrow_deposit_operations.rb', line 184

def reclaim(payer:, sign: true, execute: true, **composer_opts)
  composer = compose_reclaim(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