Module: Solace::Programs::ZarTrustlessEscrow::MediatedEscrowDepositOperations

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

Overview

Three-party mediated escrow operations for Solace::Programs::ZarTrustlessEscrow: deposit, release, and reclaim against the MediatedEscrowDeposit 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

#compose_mediated_deposit(mint:, depositor:, id:, mediator:, beneficiary:, amount:, fee_payer:, rent_collector: nil, expires_at: nil, token_program_id: Solace::Constants::TOKEN_PROGRAM_ID) ⇒ Solace::TransactionComposer

Prepares a mediated-deposit transaction. Derives the MediatedEscrowDeposit 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.

  • id (#to_s)

    Unique id used to derive the escrow PDA.

  • mediator (#to_s)

    Mediator who decides the outcome.

  • beneficiary (#to_s)

    Beneficiary the mediator may release to.

  • amount (Integer)

    u64 amount to deposit.

  • fee_payer (#to_s, Keypair)

    The fee payer (writable signer).

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

    (Optional) Rent collector on close (default: nil).

  • expires_at (Integer) (defaults to: nil)

    (Optional) Unix timestamp for depositor reclaim (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 mediated-deposit instruction.



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
109
110
111
112
113
114
115
116
117
# File 'lib/solace/zar_trustless_escrow/programs/zar_trustless_escrow/mediated_escrow_deposit_operations.rb', line 79

def compose_mediated_deposit(
  mint:,
  depositor:,
  id:,
  mediator:,
  beneficiary:,
  amount:,
  fee_payer:,
  rent_collector: nil,
  expires_at: nil,
  token_program_id: Solace::Constants::TOKEN_PROGRAM_ID
)
  mediated_escrow_deposit, = get_mediated_escrow_deposit_address(id:)
  , = get_vault_address(mint:)
  , = Solace::Programs::AssociatedTokenAccount.get_address(
    owner:            depositor,
    mint:,
    token_program_id:
  )

  ix = Composers::ZarTrustlessEscrowMediatedDepositComposer.new(
    mint:,
    depositor:,
    depositor_token_account:,
    mediated_escrow_deposit:,
    program_token_account:,
    fee_payer:,
    amount:,
    id:,
    mediator:,
    beneficiary:,
    rent_collector:,
    expires_at:,
    program_id:,
    token_program_id:
  )

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

#compose_mediated_reclaim(mint:, depositor:, id:, fee_payer:, token_program_id: Solace::Constants::TOKEN_PROGRAM_ID) ⇒ Solace::TransactionComposer

Prepares a mediated-reclaim transaction. Derives the MediatedEscrowDeposit 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 (receives the tokens and rent).

  • id (#to_s)

    Unique id used to derive the escrow PDA.

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



227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
# File 'lib/solace/zar_trustless_escrow/programs/zar_trustless_escrow/mediated_escrow_deposit_operations.rb', line 227

def compose_mediated_reclaim(
  mint:,
  depositor:,
  id:,
  fee_payer:,
  token_program_id: Solace::Constants::TOKEN_PROGRAM_ID
)
  mediated_escrow_deposit, = get_mediated_escrow_deposit_address(id:)
  , = get_vault_address(mint:)
  , = Solace::Programs::AssociatedTokenAccount.get_address(
    owner:            depositor,
    mint:,
    token_program_id:
  )

  ix = Composers::ZarTrustlessEscrowMediatedReclaimComposer.new(
    mint:,
    depositor:,
    depositor_token_account:,
    mediated_escrow_deposit:,
    program_token_account:,
    fee_payer:,
    id:,
    program_id:,
    token_program_id:
  )

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

#compose_mediated_release(mint:, mediator:, recipient:, rent_collector:, id:, fee_payer:, token_program_id: Solace::Constants::TOKEN_PROGRAM_ID) ⇒ Solace::TransactionComposer

Prepares a mediated-release transaction. Derives the MediatedEscrowDeposit PDA, the program vault, and the recipient’s associated token account.

Parameters:

  • mint (#to_s)

    Mint of the escrowed tokens.

  • mediator (#to_s, Keypair)

    The mediator releasing the funds.

  • recipient (#to_s)

    Depositor or beneficiary receiving the tokens.

  • rent_collector (#to_s)

    Account that receives the closed-account rent.

  • id (#to_s)

    Unique id used to derive the escrow PDA.

  • 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 mediated-release instruction.



157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/solace/zar_trustless_escrow/programs/zar_trustless_escrow/mediated_escrow_deposit_operations.rb', line 157

def compose_mediated_release(
  mint:,
  mediator:,
  recipient:,
  rent_collector:,
  id:,
  fee_payer:,
  token_program_id: Solace::Constants::TOKEN_PROGRAM_ID
)
  mediated_escrow_deposit, = get_mediated_escrow_deposit_address(id:)
  , = get_vault_address(mint:)
  , = Solace::Programs::AssociatedTokenAccount.get_address(
    owner:            recipient,
    mint:,
    token_program_id:
  )

  ix = Composers::ZarTrustlessEscrowMediatedReleaseComposer.new(
    mint:,
    mediator:,
    recipient:,
    recipient_token_account:,
    mediated_escrow_deposit:,
    program_token_account:,
    rent_collector:,
    fee_payer:,
    id:,
    program_id:,
    token_program_id:
  )

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

#fetch_mediated_escrow_deposit(address:) ⇒ Solace::ZarTrustlessEscrow::MediatedEscrowDeposit

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

Parameters:

  • address (#to_s)

    Base58 address of the MediatedEscrowDeposit account.

Returns:

Raises:

  • (RuntimeError)

    If no account exists at the address.



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

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

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

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

Deposits funds into a three-party mediated escrow, signs, and (optionally) sends it. The depositor and fee payer sign.

Parameters:

  • payer (Solace::Keypair)

    The fee payer (signs).

  • 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)

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/mediated_escrow_deposit_operations.rb', line 47

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

#mediated_escrow_deposit_exists?(address:) ⇒ Boolean

Returns whether a MediatedEscrowDeposit account exists at the given address. Derive the address first with Solace::Programs::ZarTrustlessEscrow#get_mediated_escrow_deposit_address.

Parameters:

  • address (#to_s)

    Base58 address of the MediatedEscrowDeposit account.

Returns:

  • (Boolean)

    True if an account exists at the address.



34
35
36
# File 'lib/solace/zar_trustless_escrow/programs/zar_trustless_escrow/mediated_escrow_deposit_operations.rb', line 34

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

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

Reclaims an expired mediated escrow as the depositor, signs, and (optionally) sends it. The depositor and fee payer sign.

Parameters:

  • payer (Solace::Keypair)

    The fee payer (signs).

  • depositor (Solace::Keypair)

    The depositor (signs, receives tokens and rent).

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



200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# File 'lib/solace/zar_trustless_escrow/programs/zar_trustless_escrow/mediated_escrow_deposit_operations.rb', line 200

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

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

Releases a mediated escrow on behalf of the mediator, signs, and (optionally) sends it. The mediator and fee payer sign.

Parameters:

  • payer (Solace::Keypair)

    The fee payer (signs).

  • mediator (Solace::Keypair)

    The mediator (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.



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

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

  yield composer if block_given?

  tx = composer
       .set_fee_payer(payer)
       .compose_transaction

  if sign
    tx.sign(payer, mediator)

    connection.send_transaction(tx.serialize) if execute
  end

  tx
end