Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions contracts/tlend_airdrop/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,31 @@ impl TlendAirdropContract {
);
}

/// Recover unclaimed TLEND from the pool to `to`. Admin only; intended for
/// use once an airdrop round has closed or expired, so tokens allocated to
/// addresses that never claimed are not trapped in the contract.
pub fn sweep_unclaimed(env: Env, caller: Address, to: Address, amount: i128) {
caller.require_auth();
let admin = Self::get_admin(env.clone());
if caller != admin {
panic!("Unauthorised caller: not admin");
}
if amount <= 0 {
panic!("amount must be positive");
}
let token_addr = Self::get_token(env.clone());
let token_client = token::Client::new(&env, &token_addr);
if token_client.balance(&env.current_contract_address()) < amount {
panic!("Insufficient airdrop balance");
}
token_client.transfer(&env.current_contract_address(), &to, &amount);

env.events().publish(
(symbol_short!("airdrop"), symbol_short!("sweep")),
(to, amount),
);
}

// ── Queries ──────────────────────────────────────────────────────────────

pub fn get_admin(env: Env) -> Address {
Expand Down
45 changes: 45 additions & 0 deletions contracts/tlend_airdrop/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,3 +168,48 @@ fn test_non_admin_cannot_set_root() {
let attacker = Address::generate(&w.env);
w.airdrop.set_merkle_root(&attacker, &w.root);
}

#[test]
fn test_admin_sweeps_unclaimed_tokens() {
let w = setup();
w.airdrop.claim(&w.c0, &AMOUNTS[0], &w.proof0);

let treasury = Address::generate(&w.env);
let remaining = w.token.balance(&w.airdrop.address);
assert_eq!(remaining, 900);

w.airdrop.sweep_unclaimed(&w.admin, &treasury, &remaining);
assert_eq!(w.token.balance(&treasury), 900);
assert_eq!(w.token.balance(&w.airdrop.address), 0);
}

#[test]
fn test_admin_can_partially_sweep() {
let w = setup();
let treasury = Address::generate(&w.env);
w.airdrop.sweep_unclaimed(&w.admin, &treasury, &250);
assert_eq!(w.token.balance(&treasury), 250);
assert_eq!(w.token.balance(&w.airdrop.address), 750);
}

#[test]
#[should_panic(expected = "not admin")]
fn test_non_admin_cannot_sweep() {
let w = setup();
let attacker = Address::generate(&w.env);
w.airdrop.sweep_unclaimed(&attacker, &attacker, &100);
}

#[test]
#[should_panic(expected = "amount must be positive")]
fn test_sweep_rejects_non_positive_amount() {
let w = setup();
w.airdrop.sweep_unclaimed(&w.admin, &w.admin, &0);
}

#[test]
#[should_panic(expected = "Insufficient airdrop balance")]
fn test_sweep_rejects_amount_above_balance() {
let w = setup();
w.airdrop.sweep_unclaimed(&w.admin, &w.admin, &1_001);
}