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

updating the deposit to no longer accept funds from the investor. #3

Merged
merged 5 commits into from
Aug 29, 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
3 changes: 3 additions & 0 deletions crates/contract/src/core/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ pub enum ContractError {
#[error("The deposit exceeds the commitment amount")]
ExcessiveDeposit {},

#[error("The deposit is empty")]
EmptyDeposit {},

#[error("The commitment will exceed the remaining amount of a security")]
CommitmentExceedsRemainingSecurityAmount {},

Expand Down
2 changes: 1 addition & 1 deletion crates/contract/src/execute/router.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ pub fn route(deps: ProvDepsMut, env: Env, info: MessageInfo, msg: ExecuteMsg) ->
accept_commitments::handle(deps, env, info.sender, commitments)
}
ExecuteMsg::DepositCommitment { securities } => {
deposit_commitment::handle(deps, env, info.sender, info.funds, securities)
deposit_commitment::handle(deps, env, info.sender, securities)
}
ExecuteMsg::WithdrawCommitment { lp } => {
withdraw_commitment::handle(deps, env, info.sender, lp)
Expand Down
149 changes: 99 additions & 50 deletions crates/contract/src/execute/settlement/deposit_commitment.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
use cosmwasm_std::{Addr, Coin, Env, Response};
use cosmwasm_std::{Addr, Coin, Env, Event, Response};
use provwasm_std::transfer_marker_coins;

use crate::storage::{securities, state};
use crate::{
core::{
aliases::{ProvDepsMut, ProvTxResponse},
aliases::{ProvDepsMut, ProvMsg, ProvTxResponse},
error::ContractError,
security::SecurityCommitment,
},
Expand All @@ -11,8 +13,7 @@ use crate::{
available_capital::{self},
commits::{self},
paid_in_capital::{self},
securities::{self},
state::{self},
// securities::{self},
},
util,
};
Expand All @@ -23,7 +24,6 @@ pub fn handle(
deps: ProvDepsMut,
env: Env,
sender: Addr,
funds: Vec<Coin>,
deposit: Vec<SecurityCommitment>,
) -> ProvTxResponse {
let state = state::get(deps.storage)?;
Expand All @@ -40,19 +40,34 @@ pub fn handle(
return Err(crate::core::error::ContractError::InvalidSecurityCommitment {});
}

/*
if !funds_match_deposit(&deps, &funds, &deposit, &state.capital_denom)? {
return Err(crate::core::error::ContractError::FundMismatch {});
}
*/

if deposit_exceeds_commitment(&deps, sender.clone(), &deposit)? {
return Err(crate::core::error::ContractError::ExcessiveDeposit {});
}

update_depositer_capital(deps, sender.clone(), funds, deposit)?;
// convert the security commitment into actual fund coin
// assumes for now that the deposit == commitment
let funds = calculate_funds(&deps, &deposit, &state.capital_denom)?;
if funds.is_empty() {
return Err(crate::core::error::ContractError::EmptyDeposit {});
}

let deposit_message =
process_deposit(sender.clone(), env.contract.address.clone(), funds.clone())?;
update_depositer_capital(deps, sender.clone(), funds.clone(), deposit)?;

Ok(Response::default()
Ok(Response::new()
.add_messages(deposit_message)
.add_event(Event::new("deposited").add_attribute("lp", sender.clone()))
.add_attribute("action", "deposit_commitment")
.add_attribute("lp", sender))
.add_attribute("sender", sender)
.add_attribute("recipient", env.contract.address)
.add_attribute("funds_amount", funds[0].amount.clone()))
}

// Check if they have a commitment - Shouldn't really matter
Expand Down Expand Up @@ -95,6 +110,26 @@ fn deposit_exceeds_commitment(
Ok(!can_deposit)
}

fn process_deposit(
sender: Addr,
contract: Addr,
funds: Vec<Coin>,
) -> Result<Vec<ProvMsg>, ContractError> {
let mut messages = vec![];
for fund in funds {
if !fund.amount.is_zero() {
messages.push(transfer_marker_coins(
fund.amount.u128(),
fund.denom,
contract.clone(),
sender.clone(),
)?);
}
}

Ok(messages)
}
ChrisKenison marked this conversation as resolved.
Show resolved Hide resolved

// This updates the AVAILABLE_CAPITAL and the PAID_IN_CAPITAL
fn update_depositer_capital(
deps: ProvDepsMut,
Expand Down Expand Up @@ -126,7 +161,7 @@ fn securities_match(
.iter()
.all(|deposit_security| commitment_securities.contains(&deposit_security.name)))
}

/*
fn funds_match_deposit(
deps: &ProvDepsMut,
funds: &Vec<Coin>,
Expand All @@ -137,7 +172,7 @@ fn funds_match_deposit(
let has_funds = expected_funds.iter().all(|coin| funds.contains(coin));
Ok(expected_funds.len() == funds.len() && has_funds)
}

*/
// We are strict that all capital must be in the same denom
fn calculate_funds(
deps: &ProvDepsMut,
Expand All @@ -163,6 +198,7 @@ fn is_accepted(deps: &ProvDepsMut, sender: &Addr) -> Result<bool, ContractError>

#[cfg(test)]
mod tests {
use cosmwasm_std::testing::MOCK_CONTRACT_ADDR;
use cosmwasm_std::{testing::mock_env, Addr, Attribute, Coin, Uint128, Uint64};
use provwasm_mocks::mock_dependencies;

Expand All @@ -181,7 +217,8 @@ mod tests {
util::testing::SettlementTester,
};

use super::{calculate_funds, funds_match_deposit, handle, is_accepted, securities_match};
// use super::{calculate_funds, funds_match_deposit, handle, is_accepted, securities_match};
use super::{calculate_funds, handle, is_accepted, securities_match};

#[test]
fn test_is_accepted_throws_error_on_invalid_lp() {
Expand Down Expand Up @@ -271,6 +308,7 @@ mod tests {
assert_eq!(vec![Coin::new(85, capital_denom)], funds);
}

/*
#[test]
fn test_funds_match_deposit() {
let mut deps = mock_dependencies(&[]);
Expand Down Expand Up @@ -356,6 +394,7 @@ mod tests {
let res = funds_match_deposit(&deps.as_mut(), &funds, &deposit, &capital_denom).unwrap();
assert_eq!(false, res);
}
*/

#[test]
fn test_securities_match_can_handle_empty() {
Expand Down Expand Up @@ -527,32 +566,29 @@ mod tests {
fn test_handle_should_throw_error_with_invalid_state() {
let mut deps = mock_dependencies(&[]);
let sender = Addr::unchecked("lp");
let funds = vec![];
let deposit = vec![];
let settlement_tester = SettlementTester::new();
settlement_tester.setup_test_state(deps.as_mut().storage);
let commitment = Commitment::new(sender.clone(), vec![]);
commits::set(deps.as_mut().storage, &commitment).unwrap();

let error = handle(deps.as_mut(), mock_env(), sender, funds, deposit)
.expect_err("should throw error");
let error =
handle(deps.as_mut(), mock_env(), sender, deposit).expect_err("should throw error");
assert_eq!(
crate::core::error::ContractError::InvalidCommitmentState {}.to_string(),
error.to_string()
);
}

/*
#[test]
fn test_handle_should_throw_error_when_deposit_exceeds_commitment() {
let mut deps = mock_dependencies(&[]);
let sender = Addr::unchecked("lp");
let mut settlement_tester = SettlementTester::new();
settlement_tester.setup_test_state(deps.as_mut().storage);
settlement_tester.create_security_commitments(1);
let funds = vec![Coin::new(
settlement_tester.security_commitments[0].amount.u128(),
"denom".to_string(),
)];


let deposit = settlement_tester.security_commitments.clone();
let mut commitment = Commitment::new(
Expand All @@ -576,13 +612,14 @@ mod tests {
)
.unwrap();

let error = handle(deps.as_mut(), mock_env(), sender, funds, deposit)
.expect_err("should throw error");
let error =
handle(deps.as_mut(), mock_env(), sender, deposit).expect_err("should throw error");
assert_eq!(
crate::core::error::ContractError::ExcessiveDeposit {}.to_string(),
error.to_string()
);
}
*/

#[test]
fn test_handle_should_throw_error_when_settlement_expired() {
Expand Down Expand Up @@ -619,8 +656,8 @@ mod tests {
)
.unwrap();

let error = handle(deps.as_mut(), mock_env(), sender, funds, deposit)
.expect_err("should throw error");
let error =
handle(deps.as_mut(), mock_env(), sender, deposit).expect_err("should throw error");
assert_eq!(
crate::core::error::ContractError::SettlmentExpired {}.to_string(),
error.to_string()
Expand All @@ -641,14 +678,15 @@ mod tests {
settlement_tester.setup_test_state(deps.as_mut().storage);
commits::set(deps.as_mut().storage, &commitment).unwrap();

let error = handle(deps.as_mut(), mock_env(), sender, funds, deposit)
.expect_err("should throw error");
let error =
handle(deps.as_mut(), mock_env(), sender, deposit).expect_err("should throw error");
assert_eq!(
crate::core::error::ContractError::InvalidSecurityCommitment {}.to_string(),
error.to_string()
);
}

/* Taken out because we don't check if funds match - because SC is pulling funds committed
#[test]
fn test_handle_should_throw_error_when_funds_mismatch() {
let mut deps = mock_dependencies(&[]);
Expand Down Expand Up @@ -678,25 +716,23 @@ mod tests {
)
.unwrap();

let error = handle(deps.as_mut(), mock_env(), sender, funds, deposit)
.expect_err("should throw error");
let error =
handle(deps.as_mut(), mock_env(), sender, deposit).expect_err("should throw error");
assert_eq!(
crate::core::error::ContractError::FundMismatch {}.to_string(),
error.to_string()
);
}
*/

#[test]
fn test_handle_should_work() {
let mut deps = mock_dependencies(&[]);
let sender = Addr::unchecked("lp");
let sender = Addr::unchecked("sender");
let mock_env = mock_env();
let mut settlement_tester = SettlementTester::new();
settlement_tester.setup_test_state(deps.as_mut().storage);
settlement_tester.create_security_commitments(1);
let funds = vec![Coin::new(
settlement_tester.security_commitments[0].amount.u128(),
"denom".to_string(),
)];
settlement_tester.create_security_commitments(1); // should be 12 units

let deposit = settlement_tester.security_commitments.clone();
let mut commitment = Commitment::new(
Expand All @@ -719,32 +755,34 @@ mod tests {
)
.unwrap();

let response = handle(deps.as_mut(), mock_env(), sender.clone(), funds, deposit)
let response = handle(deps.as_mut(), mock_env.clone(), sender.clone(), deposit)
.expect("Should not throw error");
assert_eq!(0, response.messages.len());
assert_eq!(2, response.attributes.len());
assert_eq!(1, response.messages.len());

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we add any assertions to ensure we have the correct sender / recipient relationship on the marker transfer message?

assert_eq!(4, response.attributes.len());
assert_eq!(
Attribute::new("action", "deposit_commitment"),
response.attributes[0]
);
assert_eq!(Attribute::new("lp", sender), response.attributes[1]);
assert_eq!(Attribute::new("sender", sender), response.attributes[1]);
assert_eq!(
Attribute::new("recipient", mock_env.contract.address.clone()),
response.attributes[2]
);
assert_eq!(Attribute::new("funds_amount", "11"), response.attributes[3]);
}

#[test]
fn test_handle_should_work_with_zero_on_a_security() {
let mut deps = mock_dependencies(&[]);
let sender = Addr::unchecked("lp");
let sender = Addr::unchecked("sender");
let mock_env = mock_env();
let mut settlement_tester = SettlementTester::new();
settlement_tester.setup_test_state(deps.as_mut().storage);
settlement_tester.create_security_commitments(2);
settlement_tester.security_commitments[1].amount = Uint128::new(0);

let mut deposit = settlement_tester.security_commitments.clone();
deposit[1].amount = Uint128::new(0);
let funds = vec![Coin::new(
deposit[0].amount.u128() + deposit[1].amount.u128(),
"denom".to_string(),
)];

let mut commitment = Commitment::new(
sender.clone(),
Expand Down Expand Up @@ -777,21 +815,27 @@ mod tests {
)
.unwrap();

let response = handle(deps.as_mut(), mock_env(), sender.clone(), funds, deposit)
let response = handle(deps.as_mut(), mock_env.clone(), sender.clone(), deposit)
.expect("Should not throw error");
assert_eq!(0, response.messages.len());
assert_eq!(2, response.attributes.len());
assert_eq!(1, response.messages.len());
assert_eq!(4, response.attributes.len());
assert_eq!(
Attribute::new("action", "deposit_commitment"),
response.attributes[0]
);
assert_eq!(Attribute::new("lp", sender), response.attributes[1]);
assert_eq!(Attribute::new("sender", sender), response.attributes[1]);
assert_eq!(
Attribute::new("recipient", mock_env.contract.address.clone()),
response.attributes[2]
);
assert_eq!(Attribute::new("funds_amount", "11"), response.attributes[3]);
}

#[test]
fn test_handle_should_work_with_multiple() {
let mut deps = mock_dependencies(&[]);
let sender = Addr::unchecked("lp");
let sender = Addr::unchecked("sender");
let mock_env = mock_env();
let mut settlement_tester = SettlementTester::new();
settlement_tester.setup_test_state(deps.as_mut().storage);
settlement_tester.create_security_commitments(2);
Expand Down Expand Up @@ -834,15 +878,20 @@ mod tests {
)
.unwrap();

let response = handle(deps.as_mut(), mock_env(), sender.clone(), funds, deposit)
let response = handle(deps.as_mut(), mock_env.clone(), sender.clone(), deposit)
.expect("Should not throw error");
assert_eq!(0, response.messages.len());
assert_eq!(2, response.attributes.len());
assert_eq!(1, response.messages.len());
assert_eq!(4, response.attributes.len());
assert_eq!(
Attribute::new("action", "deposit_commitment"),
response.attributes[0]
);
assert_eq!(Attribute::new("lp", sender), response.attributes[1]);
assert_eq!(Attribute::new("sender", sender), response.attributes[1]);
assert_eq!(
Attribute::new("recipient", mock_env.contract.address.clone()),
response.attributes[2]
);
assert_eq!(Attribute::new("funds_amount", "11"), response.attributes[3]);
}

#[test]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ fn process_withdraw(
capital.amount.u128(),
capital.denom,
gp.clone(),
commitment.lp.clone(),
contract.clone(),
)?);
}

Expand Down
Loading