Skip to content

Commit 10a6caa

Browse files
author
Edward (OpenClaw)
committed
docs+code: spell out all abbreviations in asset-leasing
No abbreviations anywhere — full English words for everything visible. Drops fake "COLLA" / "LEASED" placeholder tickers, expands ts → timestamp, bps → basis_points, pda → program-derived address, ata → associated token account, cpi → cross-program invocation in comments and local names, plus the usual ctx/acc/ auth/addr/tx/cfg/amt expansions. Anchor / upstream type names (CpiContext, AccountInfo, etc.) left as-is.
1 parent 5f35c76 commit 10a6caa

25 files changed

Lines changed: 932 additions & 926 deletions

defi/asset-leasing/anchor/README.md

Lines changed: 183 additions & 177 deletions
Large diffs are not rendered by default.

defi/asset-leasing/anchor/programs/asset-leasing/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ custom-panic = []
2121

2222
[dependencies]
2323
# `init-if-needed` is required because several instructions lazily create the
24-
# counterparty's ATAs (keeper's collateral ATA on first liquidation, lessor's
25-
# leased ATA on first return, etc.). Anchor forces an opt-in to make us
24+
# counterparty's associated token accounts (keeper's collateral associated token account on first liquidation, lessor's
25+
# leased associated token account on first return, etc.). Anchor forces an opt-in to make us
2626
# re-affirm that we verify ownership on every touch — which we do via the
2727
# `associated_token::authority = ...` constraints.
2828
anchor-lang = { version = "1.0.0", features = ["init-if-needed"] }

defi/asset-leasing/anchor/programs/asset-leasing/src/constants.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,26 @@
1-
/// PDA seed for the `Lease` account. Combined with the lessor pubkey and a
1+
/// program-derived address seed for the `Lease` account. Combined with the lessor pubkey and a
22
/// u64 `lease_id` so one lessor can run many leases in parallel.
33
pub const LEASE_SEED: &[u8] = b"lease";
44

5-
/// PDA seed for the token vault that holds the leased tokens while the lease
5+
/// program-derived address seed for the token vault that holds the leased tokens while the lease
66
/// is `Listed` and that accepts returned tokens on settlement.
77
pub const LEASED_VAULT_SEED: &[u8] = b"leased_vault";
88

9-
/// PDA seed for the token vault that escrows the lessee's collateral for the
9+
/// program-derived address seed for the token vault that escrows the lessee's collateral for the
1010
/// life of the lease.
1111
pub const COLLATERAL_VAULT_SEED: &[u8] = b"collateral_vault";
1212

13-
/// Denominator for basis-point (bps) ratios used for the maintenance margin
14-
/// and the liquidation bounty. 10_000 bps = 100%.
15-
pub const BPS_DENOMINATOR: u64 = 10_000;
13+
/// Denominator for basis-point (basis points) ratios used for the maintenance margin
14+
/// and the liquidation bounty. 10_000 basis points = 100%.
15+
pub const BASIS_POINTS_DENOMINATOR: u64 = 10_000;
1616

17-
/// Maximum allowed maintenance margin: 50_000 bps = 500%. Prevents the lessor
17+
/// Maximum allowed maintenance margin: 50_000 basis points = 500%. Prevents the lessor
1818
/// setting an impossible margin that would let them liquidate on day one.
19-
pub const MAX_MAINTENANCE_MARGIN_BPS: u16 = 50_000;
19+
pub const MAX_MAINTENANCE_MARGIN_BASIS_POINTS: u16 = 50_000;
2020

21-
/// Maximum liquidation bounty the keeper can claim: 2_000 bps = 20%. Keeps
21+
/// Maximum liquidation bounty the keeper can claim: 2_000 basis points = 20%. Keeps
2222
/// most of the collateral flowing to the lessor on default.
23-
pub const MAX_LIQUIDATION_BOUNTY_BPS: u16 = 2_000;
23+
pub const MAX_LIQUIDATION_BOUNTY_BASIS_POINTS: u16 = 2_000;
2424

2525
/// A Pyth price update is considered stale if its `publish_time` is older
2626
/// than this many seconds versus the current on-chain clock. 60 s matches the

defi/asset-leasing/anchor/programs/asset-leasing/src/instructions/close_expired.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use crate::{
88
constants::{COLLATERAL_VAULT_SEED, LEASED_VAULT_SEED, LEASE_SEED},
99
errors::AssetLeasingError,
1010
instructions::{
11-
pay_lease_fee::update_last_paid_ts,
11+
pay_lease_fee::update_last_paid_timestamp,
1212
shared::{close_vault, transfer_tokens_from_vault},
1313
},
1414
state::{Lease, LeaseStatus},
@@ -18,7 +18,7 @@ use crate::{
1818
///
1919
/// - The lease sat in `Listed` and the lessor wants to cancel it, recovering
2020
/// the leased tokens they pre-funded. Allowed any time.
21-
/// - The lease was `Active` but the lessee ghosted past `end_ts`. The lessor
21+
/// - The lease was `Active` but the lessee ghosted past `end_timestamp`. The lessor
2222
/// takes the collateral as compensation and closes the books.
2323
#[derive(Accounts)]
2424
pub struct CloseExpired<'info> {
@@ -93,7 +93,7 @@ pub fn handle_close_expired(context: Context<CloseExpired>) -> Result<()> {
9393
// no start/end so the check is skipped.
9494
if status == LeaseStatus::Active {
9595
require!(
96-
now >= context.accounts.lease.end_ts,
96+
now >= context.accounts.lease.end_timestamp,
9797
AssetLeasingError::LeaseNotExpired
9898
);
9999
}
@@ -159,18 +159,18 @@ pub fn handle_close_expired(context: Context<CloseExpired>) -> Result<()> {
159159
//
160160
// We are not forwarding any accrued lease fees to the lessor here — on default
161161
// the lessor takes the whole collateral vault as compensation — but we
162-
// still bump \`last_paid_ts\` so the invariant
163-
// \`last_paid_ts <= now.min(end_ts)\` stays intact. That matters for
162+
// still bump \`last_paid_timestamp\` so the invariant
163+
// \`last_paid_timestamp <= now.min(end_timestamp)\` stays intact. That matters for
164164
// any future version of the program that wants to split the collateral
165165
// differently (pro-rata lease fees, partial refund on default, haircut to the
166166
// lessee for unused time): such a version can read
167-
// \`last_paid_ts\` and trust that everything up to \`now\` is already
167+
// \`last_paid_timestamp\` and trust that everything up to \`now\` is already
168168
// settled, rather than having to reason about whether this branch ever
169169
// bumped the timestamp.
170170
//
171171
// No-op on the \`Listed\` branch because Lease fees never started accruing.
172172
if status == LeaseStatus::Active {
173-
update_last_paid_ts(&mut context.accounts.lease, now);
173+
update_last_paid_timestamp(&mut context.accounts.lease, now);
174174
}
175175
context.accounts.lease.collateral_amount = 0;
176176
context.accounts.lease.status = LeaseStatus::Closed;

defi/asset-leasing/anchor/programs/asset-leasing/src/instructions/create_lease.rs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ use anchor_spl::token_interface::{Mint, TokenAccount, TokenInterface};
33

44
use crate::{
55
constants::{
6-
COLLATERAL_VAULT_SEED, LEASED_VAULT_SEED, LEASE_SEED, MAX_LIQUIDATION_BOUNTY_BPS,
7-
MAX_MAINTENANCE_MARGIN_BPS,
6+
COLLATERAL_VAULT_SEED, LEASED_VAULT_SEED, LEASE_SEED, MAX_LIQUIDATION_BOUNTY_BASIS_POINTS,
7+
MAX_MAINTENANCE_MARGIN_BASIS_POINTS,
88
},
99
errors::AssetLeasingError,
1010
instructions::shared::transfer_tokens_from_user,
@@ -40,8 +40,8 @@ pub struct CreateLease<'info> {
4040
)]
4141
pub lease: Account<'info, Lease>,
4242

43-
/// PDA-owned vault holding the leased tokens while `Listed`. Authority is
44-
/// the vault PDA itself so the lease account does not need to sign for
43+
/// program-derived address-owned vault holding the leased tokens while `Listed`. Authority is
44+
/// the vault program-derived address itself so the lease account does not need to sign for
4545
/// returns / liquidation; any handler just signs with the vault seeds.
4646
#[account(
4747
init,
@@ -77,8 +77,8 @@ pub fn handle_create_lease(
7777
required_collateral_amount: u64,
7878
lease_fee_per_second: u64,
7979
duration_seconds: i64,
80-
maintenance_margin_bps: u16,
81-
liquidation_bounty_bps: u16,
80+
maintenance_margin_basis_points: u16,
81+
liquidation_bounty_basis_points: u16,
8282
feed_id: [u8; 32],
8383
) -> Result<()> {
8484
// Reject leased_mint == collateral_mint. Allowing both to be the same
@@ -99,11 +99,11 @@ pub fn handle_create_lease(
9999
require!(lease_fee_per_second > 0, AssetLeasingError::InvalidLeaseFeePerSecond);
100100
require!(duration_seconds > 0, AssetLeasingError::InvalidDuration);
101101
require!(
102-
maintenance_margin_bps > 0 && maintenance_margin_bps <= MAX_MAINTENANCE_MARGIN_BPS,
102+
maintenance_margin_basis_points > 0 && maintenance_margin_basis_points <= MAX_MAINTENANCE_MARGIN_BASIS_POINTS,
103103
AssetLeasingError::InvalidMaintenanceMargin
104104
);
105105
require!(
106-
liquidation_bounty_bps <= MAX_LIQUIDATION_BOUNTY_BPS,
106+
liquidation_bounty_basis_points <= MAX_LIQUIDATION_BOUNTY_BASIS_POINTS,
107107
AssetLeasingError::InvalidLiquidationBounty
108108
);
109109

@@ -133,13 +133,13 @@ pub fn handle_create_lease(
133133
required_collateral_amount,
134134
lease_fee_per_second,
135135
duration_seconds,
136-
// start_ts / end_ts / last_paid_ts are set when the lease
136+
// start_timestamp / end_timestamp / last_paid_timestamp are set when the lease
137137
// activates in `take_lease`.
138-
start_ts: 0,
139-
end_ts: 0,
140-
last_paid_ts: 0,
141-
maintenance_margin_bps,
142-
liquidation_bounty_bps,
138+
start_timestamp: 0,
139+
end_timestamp: 0,
140+
last_paid_timestamp: 0,
141+
maintenance_margin_basis_points,
142+
liquidation_bounty_basis_points,
143143
feed_id,
144144
status: LeaseStatus::Listed,
145145
bump: context.bumps.lease,

defi/asset-leasing/anchor/programs/asset-leasing/src/instructions/liquidate.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use anchor_spl::{
66

77
use crate::{
88
constants::{
9-
BPS_DENOMINATOR, COLLATERAL_VAULT_SEED, LEASED_VAULT_SEED, LEASE_SEED,
9+
BASIS_POINTS_DENOMINATOR, COLLATERAL_VAULT_SEED, LEASED_VAULT_SEED, LEASE_SEED,
1010
PYTH_MAX_AGE_SECONDS,
1111
},
1212
errors::AssetLeasingError,
@@ -34,7 +34,7 @@ pub struct Liquidate<'info> {
3434
#[account(mut)]
3535
pub keeper: Signer<'info>,
3636

37-
/// CHECK: PDA seed + lease-fee / collateral destination.
37+
/// CHECK: program-derived address seed + lease-fee / collateral destination.
3838
#[account(mut)]
3939
pub lessor: UncheckedAccount<'info>,
4040

@@ -174,7 +174,7 @@ pub fn handle_liquidate(context: Context<Liquidate>) -> Result<()> {
174174
AssetLeasingError::PositionHealthy
175175
);
176176

177-
// Settle accrued lease fees first (up to end_ts) so the lessor is paid for the
177+
// Settle accrued lease fees first (up to end_timestamp) so the lessor is paid for the
178178
// time the lessee actually used. Only then slice off bounty + remainder.
179179
let lease_fee_due = compute_lease_fee_due(&context.accounts.lease, now)?;
180180
let lease_fee_payable = lease_fee_due.min(context.accounts.lease.collateral_amount);
@@ -215,9 +215,9 @@ pub fn handle_liquidate(context: Context<Liquidate>) -> Result<()> {
215215
// Bounty is a percentage of the collateral *after* lease fees — guarantees we
216216
// never try to pay out more than what actually sits in the vault.
217217
let bounty = (remaining as u128)
218-
.checked_mul(context.accounts.lease.liquidation_bounty_bps as u128)
218+
.checked_mul(context.accounts.lease.liquidation_bounty_basis_points as u128)
219219
.ok_or(AssetLeasingError::MathOverflow)?
220-
.checked_div(BPS_DENOMINATOR as u128)
220+
.checked_div(BASIS_POINTS_DENOMINATOR as u128)
221221
.ok_or(AssetLeasingError::MathOverflow)? as u64;
222222

223223
if bounty > 0 {
@@ -264,7 +264,7 @@ pub fn handle_liquidate(context: Context<Liquidate>) -> Result<()> {
264264
)?;
265265

266266
context.accounts.lease.collateral_amount = 0;
267-
context.accounts.lease.last_paid_ts = now.min(context.accounts.lease.end_ts);
267+
context.accounts.lease.last_paid_timestamp = now.min(context.accounts.lease.end_timestamp);
268268
context.accounts.lease.status = LeaseStatus::Liquidated;
269269

270270
Ok(())
@@ -285,8 +285,8 @@ pub fn is_underwater(lease: &Lease, price: &DecodedPriceUpdate, now: i64) -> Res
285285

286286
let leased_amount = lease.leased_amount as u128;
287287
let collateral_amount = lease.collateral_amount as u128;
288-
let margin_bps = lease.maintenance_margin_bps as u128;
289-
let denom = BPS_DENOMINATOR as u128;
288+
let margin_basis_points = lease.maintenance_margin_basis_points as u128;
289+
let denom = BASIS_POINTS_DENOMINATOR as u128;
290290

291291
let (collateral_scaled, debt_scaled) = if price.exponent >= 0 {
292292
let scale = ten_pow(price.exponent as u32)?;
@@ -310,7 +310,7 @@ pub fn is_underwater(lease: &Lease, price: &DecodedPriceUpdate, now: i64) -> Res
310310
.checked_mul(denom)
311311
.ok_or(AssetLeasingError::MathOverflow)?;
312312
let rhs = debt_scaled
313-
.checked_mul(margin_bps)
313+
.checked_mul(margin_basis_points)
314314
.ok_or(AssetLeasingError::MathOverflow)?;
315315

316316
Ok(lhs < rhs)

defi/asset-leasing/anchor/programs/asset-leasing/src/instructions/pay_lease_fee.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ pub struct PayLeaseFee<'info> {
1919
#[account(mut)]
2020
pub payer: Signer<'info>,
2121

22-
/// CHECK: Referenced only for PDA derivation + has_one check on `lease`.
22+
/// CHECK: Referenced only for program-derived address derivation + has_one check on `lease`.
2323
pub lessor: UncheckedAccount<'info>,
2424

2525
#[account(
@@ -44,7 +44,7 @@ pub struct PayLeaseFee<'info> {
4444
)]
4545
pub collateral_vault: Box<InterfaceAccount<'info, TokenAccount>>,
4646

47-
/// Lessor's collateral-mint ATA, created on demand so the lessor does not
47+
/// Lessor's collateral-mint associated token account, created on demand so the lessor does not
4848
/// need to pre-fund it with the lease fee.
4949
#[account(
5050
init_if_needed,
@@ -65,9 +65,9 @@ pub fn handle_pay_lease_fee(context: Context<PayLeaseFee>) -> Result<()> {
6565

6666
let lease_fee_amount = compute_lease_fee_due(&context.accounts.lease, now)?;
6767

68-
// No time has passed (or already capped at end_ts). Nothing to do.
68+
// No time has passed (or already capped at end_timestamp). Nothing to do.
6969
if lease_fee_amount == 0 {
70-
update_last_paid_ts(&mut context.accounts.lease, now);
70+
update_last_paid_timestamp(&mut context.accounts.lease, now);
7171
return Ok(());
7272
}
7373

@@ -104,28 +104,28 @@ pub fn handle_pay_lease_fee(context: Context<PayLeaseFee>) -> Result<()> {
104104
.ok_or(AssetLeasingError::MathOverflow)?;
105105
}
106106

107-
update_last_paid_ts(&mut context.accounts.lease, now);
107+
update_last_paid_timestamp(&mut context.accounts.lease, now);
108108
Ok(())
109109
}
110110

111-
/// Lease fee accrues linearly: `(min(now, end_ts) - last_paid_ts) * rate`.
111+
/// Lease fee accrues linearly: `(min(now, end_timestamp) - last_paid_timestamp) * rate`.
112112
/// Extracted so it can be re-used by `return_lease` and `liquidate` for a
113113
/// final settlement before closing the lease.
114114
pub fn compute_lease_fee_due(lease: &Lease, now: i64) -> Result<u64> {
115-
let cutoff = now.min(lease.end_ts);
116-
if cutoff <= lease.last_paid_ts {
115+
let cutoff = now.min(lease.end_timestamp);
116+
if cutoff <= lease.last_paid_timestamp {
117117
return Ok(0);
118118
}
119-
let elapsed = (cutoff - lease.last_paid_ts) as u64;
119+
let elapsed = (cutoff - lease.last_paid_timestamp) as u64;
120120
elapsed
121121
.checked_mul(lease.lease_fee_per_second)
122122
.ok_or(AssetLeasingError::MathOverflow.into())
123123
}
124124

125-
/// Advance `last_paid_ts` but never past the lease end — after end_ts
125+
/// Advance `last_paid_timestamp` but never past the lease end — after end_timestamp
126126
/// the lease is settled and extra Lease fees do not accrue.
127-
pub fn update_last_paid_ts(lease: &mut Lease, now: i64) {
128-
lease.last_paid_ts = now.min(lease.end_ts);
127+
pub fn update_last_paid_timestamp(lease: &mut Lease, now: i64) {
128+
lease.last_paid_timestamp = now.min(lease.end_timestamp);
129129
}
130130

131131
impl<'info> PayLeaseFee<'info> {

defi/asset-leasing/anchor/programs/asset-leasing/src/instructions/return_lease.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use crate::{
88
constants::{COLLATERAL_VAULT_SEED, LEASED_VAULT_SEED, LEASE_SEED},
99
errors::AssetLeasingError,
1010
instructions::{
11-
pay_lease_fee::{compute_lease_fee_due, update_last_paid_ts},
11+
pay_lease_fee::{compute_lease_fee_due, update_last_paid_timestamp},
1212
shared::{close_vault, transfer_tokens_from_user, transfer_tokens_from_vault},
1313
},
1414
state::{Lease, LeaseStatus},
@@ -77,7 +77,7 @@ pub struct ReturnLease<'info> {
7777
)]
7878
pub lessee_collateral_account: Box<InterfaceAccount<'info, TokenAccount>>,
7979

80-
/// Lessor's leased-mint ATA, created on demand. They may have sent the
80+
/// Lessor's leased-mint associated token account, created on demand. They may have sent the
8181
/// original tokens from a different account.
8282
#[account(
8383
init_if_needed,
@@ -194,7 +194,7 @@ pub fn handle_return_lease(context: Context<ReturnLease>) -> Result<()> {
194194
&[collateral_vault_seeds],
195195
)?;
196196

197-
update_last_paid_ts(&mut context.accounts.lease, now);
197+
update_last_paid_timestamp(&mut context.accounts.lease, now);
198198
context.accounts.lease.collateral_amount = 0;
199199
context.accounts.lease.status = LeaseStatus::Closed;
200200

defi/asset-leasing/anchor/programs/asset-leasing/src/instructions/shared.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ pub fn transfer_tokens_from_user<'info>(
2727
)
2828
}
2929

30-
/// Transfer tokens out of a PDA-owned vault using the supplied signer seeds.
30+
/// Transfer tokens out of a program-derived address-owned vault using the supplied signer seeds.
3131
/// Used by the program when moving tokens held under its authority.
3232
pub fn transfer_tokens_from_vault<'info>(
3333
from: &InterfaceAccount<'info, TokenAccount>,
@@ -51,10 +51,10 @@ pub fn transfer_tokens_from_vault<'info>(
5151
)
5252
}
5353

54-
/// Close a PDA-owned token vault and forward its rent-exempt lamports to
54+
/// Close a program-derived address-owned token vault and forward its rent-exempt lamports to
5555
/// `destination`. The vault is its own token-account authority, so the caller
5656
/// just passes the same vault `AccountInfo` as both the account and the
57-
/// authority, with the vault's signer seeds for the CPI.
57+
/// authority, with the vault's signer seeds for the cross-program invocation.
5858
///
5959
/// `destination` is an `AccountInfo` so callers can pass whichever wrapper
6060
/// they hold (`Signer`, `UncheckedAccount`, etc.) via `.to_account_info()`.

defi/asset-leasing/anchor/programs/asset-leasing/src/instructions/take_lease.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ pub struct TakeLease<'info> {
1616
#[account(mut)]
1717
pub lessee: Signer<'info>,
1818

19-
/// CHECK: Only used as a reference for the PDA seeds; no data accessed.
19+
/// CHECK: Only used as a reference for the program-derived address seeds; no data accessed.
2020
pub lessor: UncheckedAccount<'info>,
2121

2222
#[account(
@@ -63,7 +63,7 @@ pub struct TakeLease<'info> {
6363
)]
6464
pub lessee_collateral_account: Box<InterfaceAccount<'info, TokenAccount>>,
6565

66-
/// Lessee's ATA for the leased mint. Created on-demand if missing so the
66+
/// Lessee's associated token account for the leased mint. Created on-demand if missing so the
6767
/// UI only has to hand over a lessee keypair plus the two mints.
6868
#[account(
6969
init_if_needed,
@@ -98,7 +98,7 @@ pub fn handle_take_lease(context: Context<TakeLease>) -> Result<()> {
9898
&context.accounts.token_program,
9999
)?;
100100

101-
// Pay out leased tokens from the vault PDA.
101+
// Pay out leased tokens from the vault program-derived address.
102102
let lease_key = context.accounts.lease.key();
103103
let leased_vault_bump = context.accounts.lease.leased_vault_bump;
104104
let leased_vault_seeds: &[&[u8]] = &[
@@ -118,16 +118,16 @@ pub fn handle_take_lease(context: Context<TakeLease>) -> Result<()> {
118118
&signer_seeds,
119119
)?;
120120

121-
let end_ts = now
121+
let end_timestamp = now
122122
.checked_add(duration_seconds)
123123
.ok_or(AssetLeasingError::MathOverflow)?;
124124

125125
let lease = &mut context.accounts.lease;
126126
lease.lessee = context.accounts.lessee.key();
127127
lease.collateral_amount = required_collateral_amount;
128-
lease.start_ts = now;
129-
lease.end_ts = end_ts;
130-
lease.last_paid_ts = now;
128+
lease.start_timestamp = now;
129+
lease.end_timestamp = end_timestamp;
130+
lease.last_paid_timestamp = now;
131131
lease.status = LeaseStatus::Active;
132132

133133
Ok(())

0 commit comments

Comments
 (0)