Skip to content

Commit

Permalink
feat: update compounder to use broker-bank library
Browse files Browse the repository at this point in the history
  • Loading branch information
matthiasmatt committed Mar 21, 2024
1 parent effd11b commit abf8865
Show file tree
Hide file tree
Showing 12 changed files with 946 additions and 807 deletions.
3 changes: 3 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

293 changes: 167 additions & 126 deletions contracts/broker-bank/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,155 +51,192 @@ pub fn execute(
let contract_addr = env.contract.address.to_string();
match msg {
ExecuteMsg::BankSend { coins, to } => {
// assert sender is operator
Permissions::assert_operator(deps.storage, info.sender.to_string())?;
// assert: Operator execute calls should not be halted.
let is_halted = IS_HALTED.load(deps.storage)?;
assert_not_halted(is_halted)?;

// assert: Recipient addr must be in the TO_ADDRS set.
if !TO_ADDRS.load(deps.storage)?.contains(&to) {
return Err(ContractError::ToAddrNotAllowed {
to_addr: to.to_string(),
});
}

// Events and tx history logging
let coins_json = serde_json::to_string(&coins)?;
let event = event_bank_send(&coins_json, info.sender.as_str());
LOGS.push_front(
deps.storage,
&Log {
block_height: env.block.height,
sender_addr: info.sender.to_string(),
event: event.clone(),
},
)?;

// Reply with TxMsg to send funds
Ok(Response::new()
.add_message(BankMsg::Send {
to_address: to,
amount: coins,
})
.add_event(event))
bank_send(deps, env, info, coins, to)
}

ExecuteMsg::ToggleHalt {} => {
cw_ownable::assert_owner(deps.storage, &info.sender)?;
let new_is_halted = !IS_HALTED.load(deps.storage)?;
IS_HALTED.save(deps.storage, &new_is_halted)?;
Ok(Response::new().add_event(event_toggle_halt(&new_is_halted)))
}

ExecuteMsg::ToggleHalt {} => toggle_halt(deps, env, info),
ExecuteMsg::UpdateOwnership(action) => {
Ok(execute_update_ownership(deps, env, info, action)?)
execute_update_ownership(deps, env, info, action)
}
ExecuteMsg::EditOpers(action) => edit_opers(deps, env, info, action),
ExecuteMsg::WithdrawAll { to } => {
withdraw_all(deps, env, info, to, contract_addr)
}
ExecuteMsg::Withdraw { to, denoms } => {
withdraw(deps, env, info, to, denoms, contract_addr)
}
}
}

ExecuteMsg::EditOpers(action) => {
cw_ownable::assert_owner(deps.storage, &info.sender)?;
let mut perms = Permissions::load(deps.storage)?;
let api = deps.api;
match action {
oper_perms::Action::AddOper { address } => {
let addr = api.addr_validate(address.as_str())?;
perms.operators.insert(addr.into_string());
OPERATORS.save(deps.storage, &perms.operators)?;

let res = Response::new().add_attributes(vec![
attr("action", "add_operator"),
attr("address", address),
]);
Ok(res)
}
pub fn withdraw(
deps: DepsMut,
env: Env,
info: MessageInfo,
to: Option<String>,
denoms: BTreeSet<String>,
contract_addr: String,
) -> Result<Response, ContractError> {
cw_ownable::assert_owner(deps.storage, &info.sender)?;
let to_addr: String = match to {
Some(given_to_addr) => given_to_addr,
None => info.sender.to_string(),

Check warning on line 81 in contracts/broker-bank/src/contract.rs

View check run for this annotation

Codecov / codecov/patch

contracts/broker-bank/src/contract.rs#L81

Added line #L81 was not covered by tests
};
let balances: AllBalanceResponse =
query_bank_balances(contract_addr, deps.as_ref())?;
let balances: Vec<cw_std::Coin> = balances
.amount
.iter()
.filter(|b_coin| denoms.contains(&b_coin.denom))
.cloned()
.collect();

let tx_msg = BankMsg::Send {
to_address: to_addr.to_string(),
amount: balances.clone(),
};
let event =
event_withdraw(serde_json::to_string(&balances)?.as_str(), &to_addr);
LOGS.push_front(
deps.storage,
&Log {
block_height: env.block.height,
sender_addr: info.sender.to_string(),
event: event.clone(),
},
)?;
Ok(Response::new().add_message(tx_msg).add_event(event))
}

oper_perms::Action::RemoveOper { address } => {
perms.operators.remove(address.as_str());
OPERATORS.save(deps.storage, &perms.operators)?;
pub fn withdraw_all(
deps: DepsMut,
env: Env,
info: MessageInfo,
to: Option<String>,
contract_addr: String,
) -> Result<Response, ContractError> {
cw_ownable::assert_owner(deps.storage, &info.sender)?;
let to_addr: String = match to {
Some(given_to_addr) => given_to_addr,
None => info.sender.to_string(),

Check warning on line 119 in contracts/broker-bank/src/contract.rs

View check run for this annotation

Codecov / codecov/patch

contracts/broker-bank/src/contract.rs#L119

Added line #L119 was not covered by tests
};
let balances = query_bank_balances(contract_addr, deps.as_ref())?;
let tx_msg = BankMsg::Send {
to_address: to_addr.to_string(),
amount: balances.amount.clone(),
};
let event = event_withdraw(
serde_json::to_string(&balances.amount)?.as_str(),
&to_addr,
);
LOGS.push_front(
deps.storage,
&Log {
block_height: env.block.height,
sender_addr: info.sender.to_string(),
event: event.clone(),
},
)?;
Ok(Response::new().add_message(tx_msg).add_event(event))
}

let res = Response::new().add_attributes(vec![
attr("action", "remove_operator"),
attr("address", address),
]);
Ok(res)
}
}
pub fn edit_opers(
deps: DepsMut,
_env: Env,
info: MessageInfo,
action: oper_perms::Action,
) -> Result<Response, ContractError> {
cw_ownable::assert_owner(deps.storage, &info.sender)?;
let mut perms = Permissions::load(deps.storage)?;
let api = deps.api;
match action {
oper_perms::Action::AddOper { address } => {
let addr = api.addr_validate(address.as_str())?;
perms.operators.insert(addr.into_string());
OPERATORS.save(deps.storage, &perms.operators)?;

let res = Response::new().add_attributes(vec![
attr("action", "add_operator"),
attr("address", address),
]);
Ok(res)
}

ExecuteMsg::WithdrawAll { to } => {
cw_ownable::assert_owner(deps.storage, &info.sender)?;
let to_addr: String = match to {
Some(given_to_addr) => given_to_addr,
None => info.sender.to_string(),
};
let balances = query_bank_balances(contract_addr, deps.as_ref())?;
let tx_msg = BankMsg::Send {
to_address: to_addr.to_string(),
amount: balances.amount.clone(),
};
let event = event_withdraw(
serde_json::to_string(&balances.amount)?.as_str(),
&to_addr,
);
LOGS.push_front(
deps.storage,
&Log {
block_height: env.block.height,
sender_addr: info.sender.to_string(),
event: event.clone(),
},
)?;
Ok(Response::new().add_message(tx_msg).add_event(event))
oper_perms::Action::RemoveOper { address } => {
perms.operators.remove(address.as_str());
OPERATORS.save(deps.storage, &perms.operators)?;

let res = Response::new().add_attributes(vec![
attr("action", "remove_operator"),
attr("address", address),
]);
Ok(res)
}
}
}

ExecuteMsg::Withdraw { to, denoms } => {
cw_ownable::assert_owner(deps.storage, &info.sender)?;
let to_addr: String = match to {
Some(given_to_addr) => given_to_addr,
None => info.sender.to_string(),
};
let balances: AllBalanceResponse =
query_bank_balances(contract_addr, deps.as_ref())?;
let balances: Vec<cw_std::Coin> = balances
.amount
.iter()
.filter(|b_coin| denoms.contains(&b_coin.denom))
.cloned()
.collect();
pub fn toggle_halt(
deps: DepsMut,
_env: Env,
info: MessageInfo,
) -> Result<Response, ContractError> {
cw_ownable::assert_owner(deps.storage, &info.sender)?;
let new_is_halted = !IS_HALTED.load(deps.storage)?;
IS_HALTED.save(deps.storage, &new_is_halted)?;
Ok(Response::new().add_event(event_toggle_halt(&new_is_halted)))
}

let tx_msg = BankMsg::Send {
to_address: to_addr.to_string(),
amount: balances.clone(),
};
let event = event_withdraw(
serde_json::to_string(&balances)?.as_str(),
&to_addr,
);
LOGS.push_front(
deps.storage,
&Log {
block_height: env.block.height,
sender_addr: info.sender.to_string(),
event: event.clone(),
},
)?;
Ok(Response::new().add_message(tx_msg).add_event(event))
}
pub fn bank_send(
deps: DepsMut,
env: Env,
info: MessageInfo,
coins: Vec<cw_std::Coin>,
to: String,
) -> Result<Response, ContractError> {
// assert sender is operator
Permissions::assert_operator(deps.storage, info.sender.to_string())?;
// assert: Operator execute calls should not be halted.
let is_halted = IS_HALTED.load(deps.storage)?;
assert_not_halted(is_halted)?;

// assert: Recipient addr must be in the TO_ADDRS set.
if !TO_ADDRS.load(deps.storage)?.contains(&to) {
return Err(ContractError::ToAddrNotAllowed {
to_addr: to.to_string(),
});
}

// Events and tx history logging
let coins_json = serde_json::to_string(&coins)?;
let event = event_bank_send(&coins_json, info.sender.as_str());
LOGS.push_front(
deps.storage,
&Log {
block_height: env.block.height,
sender_addr: info.sender.to_string(),
event: event.clone(),
},
)?;

// Reply with TxMsg to send funds
Ok(Response::new()
.add_message(BankMsg::Send {
to_address: to,
amount: coins,
})
.add_event(event))
}

fn execute_update_ownership(
pub fn execute_update_ownership(
deps: DepsMut,
env: Env,
info: MessageInfo,
action: cw_ownable::Action,
) -> Result<Response, cw_ownable::OwnershipError> {
) -> Result<Response, ContractError> {
let ownership =
cw_ownable::update_ownership(deps, &env.block, &info.sender, action)?;
Ok(Response::new().add_attributes(ownership.into_attributes()))
}

fn assert_not_halted(is_halted: bool) -> Result<(), ContractError> {
pub fn assert_not_halted(is_halted: bool) -> Result<(), ContractError> {
match is_halted {
true => Err(ContractError::OperationsHalted),
false => Ok(()),
Expand All @@ -223,6 +260,10 @@ pub fn query(
QueryMsg::Ownership {} => {
Ok(to_json_binary(&cw_ownable::get_ownership(deps.storage)?)?)
}
QueryMsg::IsHalted {} => {
let is_halted = IS_HALTED.load(deps.storage)?;
Ok(to_json_binary(&is_halted)?)

Check warning on line 265 in contracts/broker-bank/src/contract.rs

View check run for this annotation

Codecov / codecov/patch

contracts/broker-bank/src/contract.rs#L264-L265

Added lines #L264 - L265 were not covered by tests
}
}
}

Expand Down
2 changes: 0 additions & 2 deletions contracts/broker-bank/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
pub mod msgs;
pub mod oper_perms;

#[cfg(test)]
pub mod tutil;

#[cfg(not(feature = "library"))]
Expand Down
4 changes: 3 additions & 1 deletion contracts/broker-bank/src/msgs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ pub enum QueryMsg {
/// operator set is "halted".
#[returns(PermsStatus)]
Perms {},
// TODO: feat(broker-bank): Logs query

#[returns(bool)]
IsHalted {},
}

#[cw_serde]
Expand Down
3 changes: 3 additions & 0 deletions contracts/core-compounder/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ backtraces = ["cosmwasm-std/backtraces"]

[dependencies]
cosmwasm-schema = "1.4.0"
cw-ownable = { workspace = true }
nibiru-std = { workspace = true }
cosmwasm-std = { version = "1.5.0", features = ["staking"] }
cw20 = "1.1.1"
cw-utils = { version = "1.0.2" }
Expand All @@ -30,6 +32,7 @@ cw-storage-plus = "1.1.0"
schemars = "0.8.15"
serde = { version = "1.0.188", default-features = false, features = ["derive"] }
serde_json = { version = "1.0", default-features = false, features = ["alloc"] }
broker-bank = { version = "0.1.0", path = "../broker-bank" }

[dev-dependencies]
anyhow = { workspace = true }
3 changes: 2 additions & 1 deletion contracts/core-compounder/examples/schema.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use std::env::current_dir;
use std::fs::create_dir_all;

use core_compounder::msg::{ExecuteMsg, InstantiateMsg, QueryMsg};
use broker_bank::msgs::{InstantiateMsg, QueryMsg};
use core_compounder::msg::ExecuteMsg;
use cosmwasm_schema::{export_schema, remove_schemas, schema_for};

fn main() {
Expand Down
Loading

0 comments on commit abf8865

Please sign in to comment.