Skip to content

Commit

Permalink
Merge pull request #14 from blend-capital/transfer-from
Browse files Browse the repository at this point in the history
fix: add option to use transfer_from
  • Loading branch information
Ryang-21 authored Dec 17, 2024
2 parents 5e436be + 86f1208 commit b3325d1
Show file tree
Hide file tree
Showing 12 changed files with 483 additions and 61 deletions.
12 changes: 12 additions & 0 deletions backstop/src/backstop/deposit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,12 @@ mod tests {
mock_pool_factory_client.set_pool(&pool_0_id);
mock_pool_factory_client.set_pool(&pool_1_id);

backstop_token_client.approve(
&frodo,
&backstop_address,
&25_0000000,
&e.ledger().sequence(),
);
// initialize pool 0 with funds + some profit
e.as_contract(&backstop_address, || {
execute_deposit(&e, &frodo, &pool_0_id, 25_0000000);
Expand Down Expand Up @@ -234,6 +240,12 @@ mod tests {
mock_pool_factory_client.set_pool(&pool_0_id);
mock_pool_factory_client.set_pool(&pool_1_id);

backstop_token_client.approve(
&frodo,
&backstop_address,
&(10_000_000 * SCALAR_7),
&e.ledger().sequence(),
);
// initialize pool 0 with funds + some profit
e.as_contract(&backstop_address, || {
execute_deposit(&e, &frodo, &pool_0_id, SCALAR_7);
Expand Down
8 changes: 7 additions & 1 deletion backstop/src/backstop/fund_management.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,12 @@ pub fn execute_donate(e: &Env, from: &Address, pool_address: &Address, amount: i
require_is_from_pool_factory(e, pool_address, pool_balance.shares);

let backstop_token = TokenClient::new(e, &storage::get_backstop_token(e));
backstop_token.transfer(from, &e.current_contract_address(), &amount);
backstop_token.transfer_from(
&e.current_contract_address(),
from,
&e.current_contract_address(),
&amount,
);

pool_balance.deposit(amount, 0);
storage::set_pool_balance(e, pool_address, &pool_balance);
Expand Down Expand Up @@ -102,6 +107,7 @@ mod tests {
execute_deposit(&e, &frodo, &pool_0_id, 25_0000000);
});

backstop_token_client.approve(&samwise, &backstop_id, &30_0000000, &e.ledger().sequence());
e.as_contract(&backstop_id, || {
execute_donate(&e, &samwise, &pool_0_id, 30_0000000);

Expand Down
12 changes: 12 additions & 0 deletions backstop/src/backstop/withdrawal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,12 @@ mod tests {
max_entry_ttl: 3110400,
});

backstop_token_client.approve(
&samwise,
&backstop_address,
&50_0000000,
&e.ledger().sequence(),
);
// setup pool with queue for withdrawal and allow the backstop to incur a profit
e.as_contract(&backstop_address, || {
execute_deposit(&e, &samwise, &pool_address, 100_0000000);
Expand Down Expand Up @@ -395,6 +401,12 @@ mod tests {
max_entry_ttl: 3110400,
});

backstop_token_client.approve(
&samwise,
&backstop_address,
&50_0000000,
&e.ledger().sequence(),
);
// setup pool with queue for withdrawal and allow the backstop to incur a profit
e.as_contract(&backstop_address, || {
execute_deposit(&e, &samwise, &pool_address, 100_0000000);
Expand Down
2 changes: 1 addition & 1 deletion backstop/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ pub trait Backstop {
/// * `amount` - The amount of BLND to add
///
/// ### Errors
/// If the `pool_address` is not valid, or if the pool does not
/// If the `pool_address` is not valid, backstop does not have sufficient allowance from `from`, or if the pool does not
/// authorize the call
fn donate(e: Env, from: Address, pool_address: Address, amount: i128);

Expand Down
7 changes: 7 additions & 0 deletions pool/src/auctions/backstop_interest_auction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -744,6 +744,13 @@ mod tests {
],
block: 51,
};

backstop_token_client.approve(
&samwise,
&backstop_address,
&75_0000000,
&e.ledger().sequence(),
);
e.as_contract(&pool_address, || {
e.mock_all_auths_allowing_non_root_auth();
storage::set_auction(
Expand Down
38 changes: 37 additions & 1 deletion pool/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,26 @@ pub trait Pool {
requests: Vec<Request>,
) -> Positions;

/// Submit a set of requests to the pool where 'from' takes on the position, 'spender' sends any
/// required tokens to the pool USING transfer_from and 'to' receives any tokens sent from the pool.
///
/// Returns the new positions for 'from'
///
/// ### Arguments
/// * `from` - The address of the user whose positions are being modified
/// * `spender` - The address of the user who is sending tokens to the pool
/// * `to` - The address of the user who is receiving tokens from the pool
/// * `requests` - A vec of requests to be processed
///
/// ### Panics
/// If the request is not able to be completed for cases like insufficient funds, insufficient allowance, or invalid health factor
fn submit_with_allowance(
e: Env,
from: Address,
spender: Address,
to: Address,
requests: Vec<Request>,
) -> Positions;
/// Manage bad debt. Debt is considered "bad" if there is no longer has any collateral posted.
///
/// To manage a user's bad debt, all collateralized reserves for the user must be liquidated
Expand Down Expand Up @@ -358,7 +378,23 @@ impl Pool for PoolContract {
from.require_auth();
}

pool::execute_submit(&e, &from, &spender, &to, requests)
pool::execute_submit(&e, &from, &spender, &to, requests, false)
}

fn submit_with_allowance(
e: Env,
from: Address,
spender: Address,
to: Address,
requests: Vec<Request>,
) -> Positions {
storage::extend_instance(&e);
spender.require_auth();
if from != spender {
from.require_auth();
}

pool::execute_submit(&e, &from, &spender, &to, requests, true)
}

fn bad_debt(e: Env, user: Address) {
Expand Down
Loading

0 comments on commit b3325d1

Please sign in to comment.