Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove deposit "on behalf of" [audit] #230

Merged
merged 1 commit into from
Jul 5, 2023
Merged
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
18 changes: 10 additions & 8 deletions contracts/oracle/base/src/pyth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,8 @@ pub fn assert_pyth_current_price_not_too_old(
return Err(InvalidPrice {
reason: format!(
"current price publish time is too old/stale. published: {}, now: {}",
price_feed.get_price_unchecked().publish_time, current_time
price_feed.get_price_unchecked().publish_time,
current_time
),
});
};
Expand All @@ -163,13 +164,14 @@ pub fn assert_pyth_ema_price_not_too_old(
) -> ContractResult<Price> {
let ema_price_opt = price_feed.get_ema_price_no_older_than(current_time as i64, max_staleness);
let Some(ema_price) = ema_price_opt else {
return Err(InvalidPrice {
reason: format!(
"EMA price publish time is too old/stale. published: {}, now: {}",
price_feed.get_ema_price_unchecked().publish_time, current_time
),
});
};
return Err(InvalidPrice {
reason: format!(
"EMA price publish time is too old/stale. published: {}, now: {}",
price_feed.get_ema_price_unchecked().publish_time,
current_time
),
});
};
Ok(ema_price)
}

Expand Down
6 changes: 2 additions & 4 deletions contracts/red-bank/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,9 @@ pub fn execute(
let user_addr = deps.api.addr_validate(&user)?;
execute::update_uncollateralized_loan_limit(deps, info, user_addr, denom, new_limit)
}
ExecuteMsg::Deposit {
on_behalf_of,
} => {
ExecuteMsg::Deposit {} => {
let sent_coin = cw_utils::one_coin(&info)?;
execute::deposit(deps, env, info, on_behalf_of, sent_coin.denom, sent_coin.amount)
execute::deposit(deps, env, info, sent_coin.denom, sent_coin.amount)
}
ExecuteMsg::Withdraw {
denom,
Expand Down
12 changes: 1 addition & 11 deletions contracts/red-bank/src/execute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -277,18 +277,9 @@ pub fn deposit(
deps: DepsMut,
env: Env,
info: MessageInfo,
on_behalf_of: Option<String>,
denom: String,
deposit_amount: Uint128,
) -> Result<Response, ContractError> {
let user_addr: Addr;
let user = if let Some(address) = on_behalf_of {
user_addr = deps.api.addr_validate(&address)?;
User(&user_addr)
} else {
User(&info.sender)
};

let mut market = MARKETS.load(deps.storage, &denom)?;

let config = CONFIG.load(deps.storage)?;
Expand Down Expand Up @@ -343,7 +334,7 @@ pub fn deposit(
let deposit_amount_scaled =
get_scaled_liquidity_amount(deposit_amount, &market, env.block.time.seconds())?;

response = user.increase_collateral(
response = User(&info.sender).increase_collateral(
deps.storage,
&market,
deposit_amount_scaled,
Expand All @@ -357,7 +348,6 @@ pub fn deposit(
Ok(response
.add_attribute("action", "deposit")
.add_attribute("sender", &info.sender)
.add_attribute("on_behalf_of", user)
.add_attribute("denom", denom)
.add_attribute("amount", deposit_amount)
.add_attribute("amount_scaled", deposit_amount_scaled))
Expand Down
128 changes: 10 additions & 118 deletions contracts/red-bank/tests/test_deposit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,7 @@ fn depositing_with_no_coin_sent() {
deps.as_mut(),
mock_env(),
mock_info(depositor_addr.as_str(), &[]),
ExecuteMsg::Deposit {
on_behalf_of: None,
},
ExecuteMsg::Deposit {},
)
.unwrap_err();
assert_eq!(err, PaymentError::NoFunds {}.into());
Expand All @@ -118,9 +116,7 @@ fn depositing_with_multiple_coins_sent() {
deps.as_mut(),
mock_env(),
mock_info(depositor_addr.as_str(), &sent_coins),
ExecuteMsg::Deposit {
on_behalf_of: None,
},
ExecuteMsg::Deposit {},
)
.unwrap_err();
assert_eq!(err, PaymentError::MultipleDenoms {}.into());
Expand All @@ -141,9 +137,7 @@ fn depositing_to_non_existent_market() {
deps.as_mut(),
mock_env(),
mock_info(depositor_addr.as_str(), &coins(123, false_denom)),
ExecuteMsg::Deposit {
on_behalf_of: None,
},
ExecuteMsg::Deposit {},
)
.unwrap_err();
assert_eq!(err, StdError::not_found(type_name::<Market>()).into());
Expand Down Expand Up @@ -182,9 +176,7 @@ fn depositing_to_disabled_market() {
deps.as_mut(),
mock_env(),
mock_info(depositor_addr.as_str(), &coins(123, denom)),
ExecuteMsg::Deposit {
on_behalf_of: None,
},
ExecuteMsg::Deposit {},
)
.unwrap_err();
assert_eq!(
Expand Down Expand Up @@ -236,9 +228,7 @@ fn depositing_above_cap() {
deps.as_mut(),
mock_env_at_block_time(10000100),
mock_info(depositor_addr.as_str(), &coins(1_000_001, denom)),
ExecuteMsg::Deposit {
on_behalf_of: None,
},
ExecuteMsg::Deposit {},
)
.unwrap_err();
assert_eq!(
Expand All @@ -253,9 +243,7 @@ fn depositing_above_cap() {
deps.as_mut(),
mock_env_at_block_time(10000100),
mock_info(depositor_addr.as_str(), &coins(123, denom)),
ExecuteMsg::Deposit {
on_behalf_of: None,
},
ExecuteMsg::Deposit {},
);
assert!(result.is_ok());
}
Expand Down Expand Up @@ -286,9 +274,7 @@ fn depositing_without_existing_position() {
deps.as_mut(),
mock_env_at_block_time(block_time),
mock_info(depositor_addr.as_str(), &coins(deposit_amount, denom)),
ExecuteMsg::Deposit {
on_behalf_of: None,
},
ExecuteMsg::Deposit {},
)
.unwrap();

Expand All @@ -315,7 +301,6 @@ fn depositing_without_existing_position() {
vec![
attr("action", "deposit"),
attr("sender", &depositor_addr),
attr("on_behalf_of", &depositor_addr),
attr("denom", denom),
attr("amount", deposit_amount.to_string()),
attr("amount_scaled", expected_mint_amount),
Expand Down Expand Up @@ -376,9 +361,7 @@ fn depositing_with_existing_position() {
deps.as_mut(),
mock_env_at_block_time(block_time),
mock_info(depositor_addr.as_str(), &coins(deposit_amount, denom)),
ExecuteMsg::Deposit {
on_behalf_of: None,
},
ExecuteMsg::Deposit {},
)
.unwrap();

Expand Down Expand Up @@ -414,93 +397,6 @@ fn depositing_with_existing_position() {
);
}

#[test]
fn depositing_on_behalf_of() {
let TestSuite {
mut deps,
denom,
depositor_addr,
initial_market,
} = setup_test();

let deposit_amount = 123456u128;
let on_behalf_of_addr = Addr::unchecked("jake");

// compute expected market parameters
let block_time = 10000300;
let expected_params =
th_get_expected_indices_and_rates(&initial_market, block_time, Default::default());
let expected_mint_amount = compute_scaled_amount(
Uint128::from(deposit_amount),
expected_params.liquidity_index,
ScalingOperation::Truncate,
)
.unwrap();
let expected_reward_amount_scaled = compute_scaled_amount(
expected_params.protocol_rewards_to_distribute,
expected_params.liquidity_index,
ScalingOperation::Truncate,
)
.unwrap();

let res = execute(
deps.as_mut(),
mock_env_at_block_time(block_time),
mock_info(depositor_addr.as_str(), &coins(deposit_amount, denom)),
ExecuteMsg::Deposit {
on_behalf_of: Some(on_behalf_of_addr.clone().into()),
},
)
.unwrap();

// NOTE: For this test, the accrued protocol reward is non-zero, so we do expect a message to
// update the index of the rewards collector.
assert_eq!(
res.messages,
vec![
SubMsg::new(WasmMsg::Execute {
contract_addr: MarsAddressType::Incentives.to_string(),
msg: to_binary(&incentives::ExecuteMsg::BalanceChange {
user_addr: Addr::unchecked(MarsAddressType::RewardsCollector.to_string()),
denom: initial_market.denom.clone(),
user_amount_scaled_before: Uint128::zero(),
total_amount_scaled_before: initial_market.collateral_total_scaled,
})
.unwrap(),
funds: vec![]
}),
SubMsg::new(WasmMsg::Execute {
contract_addr: MarsAddressType::Incentives.to_string(),
msg: to_binary(&incentives::ExecuteMsg::BalanceChange {
user_addr: on_behalf_of_addr.clone(),
denom: initial_market.denom.clone(),
user_amount_scaled_before: Uint128::zero(),
// NOTE: New collateral shares were minted to the rewards collector first, so
// for the depositor this should be initial total supply + rewards shares minted
total_amount_scaled_before: initial_market.collateral_total_scaled
+ expected_reward_amount_scaled,
})
.unwrap(),
funds: vec![]
})
]
);

// depositor should not have created a new collateral position
let opt = COLLATERALS.may_load(deps.as_ref().storage, (&depositor_addr, denom)).unwrap();
assert!(opt.is_none());

// the recipient should have created a new collateral position
let collateral = COLLATERALS.load(deps.as_ref().storage, (&on_behalf_of_addr, denom)).unwrap();
assert_eq!(
collateral,
Collateral {
amount_scaled: expected_mint_amount,
enabled: true,
}
);
}

#[test]
fn depositing_on_behalf_of_cannot_enable_collateral() {
let TestSuite {
Expand All @@ -521,9 +417,7 @@ fn depositing_on_behalf_of_cannot_enable_collateral() {
deps.as_mut(),
mock_env_at_block_time(block_time),
mock_info(on_behalf_of_addr.as_str(), &coins(1u128, denom)),
ExecuteMsg::Deposit {
on_behalf_of: None,
},
ExecuteMsg::Deposit {},
)
.unwrap();

Expand Down Expand Up @@ -552,9 +446,7 @@ fn depositing_on_behalf_of_cannot_enable_collateral() {
deps.as_mut(),
mock_env_at_block_time(block_time),
mock_info(depositor_addr.as_str(), &coins(1u128, denom)),
ExecuteMsg::Deposit {
on_behalf_of: Some(on_behalf_of_addr.to_string()),
},
ExecuteMsg::Deposit {},
)
.unwrap();

Expand Down
2 changes: 1 addition & 1 deletion contracts/swapper/base/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ where

let transfer_msg = CosmosMsg::Bank(BankMsg::Send {
to_address: recipient.to_string(),
amount: vec![denom_in_balance, denom_out_balance]
amount: [denom_in_balance, denom_out_balance]
.iter()
.filter(|c| !c.amount.is_zero())
.cloned()
Expand Down
20 changes: 2 additions & 18 deletions integration-tests/tests/test_oracles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -949,15 +949,7 @@ fn redbank_should_fail_if_no_price() {
)
.unwrap();

wasm.execute(
&red_bank_addr,
&Deposit {
on_behalf_of: None,
},
&[coin(1_000_000, "uatom")],
depositor,
)
.unwrap();
wasm.execute(&red_bank_addr, &Deposit {}, &[coin(1_000_000, "uatom")], depositor).unwrap();

// execute msg should fail since it is attempting to query an asset from the oracle contract that doesn't have an LP pool set up
wasm.execute(
Expand Down Expand Up @@ -1011,15 +1003,7 @@ fn redbank_quering_oracle_successfully() {
)
.unwrap();

wasm.execute(
&red_bank_addr,
&Deposit {
on_behalf_of: None,
},
&[coin(1_000_000, "uatom")],
depositor,
)
.unwrap();
wasm.execute(&red_bank_addr, &Deposit {}, &[coin(1_000_000, "uatom")], depositor).unwrap();

wasm.execute(
&red_bank_addr,
Expand Down
4 changes: 1 addition & 3 deletions packages/testing/src/integration/mock_env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,9 +231,7 @@ impl RedBank {
env.app.execute_contract(
sender.clone(),
self.contract_addr.clone(),
&red_bank::ExecuteMsg::Deposit {
on_behalf_of: None,
},
&red_bank::ExecuteMsg::Deposit {},
&[coin],
)
}
Expand Down
5 changes: 1 addition & 4 deletions packages/types/src/red_bank/msg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,7 @@ pub enum ExecuteMsg {

/// Deposit native coins. Deposited coins must be sent in the transaction
/// this call is made
Deposit {
/// Address that will receive the coins
on_behalf_of: Option<String>,
},
Deposit {},

/// Withdraw native coins
Withdraw {
Expand Down
9 changes: 0 additions & 9 deletions schemas/mars-red-bank/mars-red-bank.json
Original file line number Diff line number Diff line change
Expand Up @@ -189,15 +189,6 @@
"properties": {
"deposit": {
"type": "object",
"properties": {
"on_behalf_of": {
"description": "Address that will receive the coins",
"type": [
"string",
"null"
]
}
},
"additionalProperties": false
}
},
Expand Down
1 change: 0 additions & 1 deletion scripts/deploy/base/deployer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,6 @@ export class Deployer {
owner: this.deployerAddress,
config: {
address_provider: this.storage.addresses['address-provider']!,
close_factor: '0.5',
},
}
await this.instantiate('red-bank', this.storage.codeIds['red-bank']!, msg)
Expand Down
Loading
Loading