Skip to content

Commit

Permalink
feat: Upgrade everything for CosmWasm V2 (#148)
Browse files Browse the repository at this point in the history
* feat(nibiru-ownable): cw-ownable with updates deps

* fix(deps): Cargo.toml sync and fix broker-bank contract

* quick save wip

* fix(contracts): compile again

* fix: Make everything compatible
  • Loading branch information
Unique-Divine authored May 28, 2024
1 parent 4452beb commit 334f706
Show file tree
Hide file tree
Showing 85 changed files with 1,986 additions and 590 deletions.
634 changes: 379 additions & 255 deletions Cargo.lock

Large diffs are not rendered by default.

49 changes: 30 additions & 19 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
members = ["scripts", "packages/*", "nibiru-std", "contracts/*"]
exclude = ["packages/msig-ez"]
resolver = "2"
package.version = "0.1.0"
package.homepage = "https://github.com/NibiruChain/cw-nibiru"
package.repository = "https://github.com/NibiruChain/cw-nibiru"
package.edition = "2021"

# Dependencies can be inherited from a workspace by specifying the dependency in
# the `[workspace.dependencies]` table. To use workspace crates, add them to the
Expand All @@ -14,34 +18,38 @@ resolver = "2"
serde = { version = "1.0.189", default-features = false, features = ["derive"] }
serde_json = "1.0.108"

# deps: packages
nibiru-std = { path = "nibiru-std" }
prost = "0.12.3"
prost-types = "0.12.3"
bash-rs = { path = "packages/bash-rs" }
bash-rs = { path = "packages/bash-rs" }
# Macros for controlling ownership of CosmWasm smart contracts
nibiru-ownable = { path = "packages/nibiru-ownable" }
ownable-derive = { path = "packages/nibiru-ownable/derive" }
cw-address-like = { path = "packages/cw-address-like" }
easy-addr = { path = "packages/easy-addr" }

# deps: CosmWasm
cosmwasm-std = { version = "1.5.0", features = ["stargate", "staking"] }
cosmwasm-schema = "1.5.0"
cw-storage-plus = { version = "1.2.0" }
cw-multi-test = { version = "0.20.0" }
cw-utils = { version = "1.0.3" }
# Macros for controlling ownership of CosmWasm smart contracts
cw-ownable = { version = "0.5.1"}
cosmwasm-std = { version = "2.0.2", features = ["stargate", "staking"] }
cosmwasm-schema = "2.0.2"
cw-storage-plus = { version = "2.0.0" }
cw-multi-test = { version = "2.0.1" }
cw-utils = { version = "2.0.0" }

# deps: cw-plus
cw2 = { version = "1.1.1" }
cw3 = { version = "1.1.1" }
cw3-fixed-multisig = { version = "1.1.1", features = ["library"] }
cw4 = { version = "1.1.1" }
cw20 = { version = "1.1.1" }
cw2 = { version = "2.0.0" }
cw3 = { version = "2.0.0" }
cw3-fixed-multisig = { version = "2.0.0", features = ["library"] }
cw4 = { version = "2.0.0" }
cw20 = { version = "2.0.0" }
cw4-group = { version = "2.0" }
cw20-base = { version = "2.0" }

# deps: core contracts
# TODO: revive bindings-perp with stargate
# bindings-perp = { path = "core/core-bindings-perp" }
shifter = { path = "contracts/core-shifter" }
controller = { path = "contracts/core-controller" }
lockup = { path = "contracts/lockup", features = ["library"] }
incentives = { path = "contracts/incentives", features = ["library"] }
shifter = { path = "contracts/core-shifter" }
controller = { path = "contracts/core-controller" }
lockup = { path = "contracts/lockup", features = ["library"] }
incentives = { path = "contracts/incentives", features = ["library"] }
broker-bank = { path = "contracts/broker-bank", features = ["library"] }

# deps: else
Expand All @@ -53,6 +61,9 @@ clap = { version = "4.4.7", features = ["derive", "cargo", "env", "string"] }
schemars = "0.8.15"
home = "0.5"
toml = "0.8"
quote = "1.0"
syn = { version = "1", features = ["full"] } # NOTE: This needs to stay v1
proc-macro2 = "1.0"

[profile.release]
opt-level = 3
Expand Down
4 changes: 0 additions & 4 deletions contracts/broker-bank/.cargo/config

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
[alias]
wasm = "build --release --lib --target wasm32-unknown-unknown"
wasm-debug = "build --lib --target wasm32-unknown-unknown"
unit-test = "test --lib"
integration-test = "test --test integration"
schema = "run --bin schema"

# OLDER:
# wasm = "build --release --target wasm32-unknown-unknown"
# wasm-debug = "build --target wasm32-unknown-unknown"
# schema = "run --example schema"
3 changes: 1 addition & 2 deletions contracts/broker-bank/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ repository = "https://github.com/NibiruChain/cw-nibiru"
crate-type = ["cdylib", "rlib"]

[features]
backtraces = ["cosmwasm-std/backtraces"]
# features.library: Use the library feature to disable all
# instantiate/execute/query exports. This is necessary use this as a dependency
# for another smart contract crate.
Expand All @@ -26,5 +25,5 @@ thiserror = { workspace = true }
nibiru-std = { workspace = true }
cw2 = { workspace = true }
serde_json = { workspace = true }
cw-ownable = { workspace = true }
nibiru-ownable = { workspace = true }
anyhow = { workspace = true }
61 changes: 29 additions & 32 deletions contracts/broker-bank/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use cosmwasm_std::{
BankQuery, Binary, Deps, DepsMut, Env, MessageInfo, QueryRequest, Response,
StdResult,
};
use cw_std::Coin;

use crate::oper_perms::Permissions;
use crate::{
Expand Down Expand Up @@ -34,7 +35,7 @@ pub fn instantiate(
format!("crates.io:{CONTRACT_NAME}"),
CONTRACT_VERSION,
)?;
cw_ownable::initialize_owner(deps.storage, deps.api, Some(&msg.owner))?;
nibiru_ownable::initialize_owner(deps.storage, Some(&msg.owner))?;
TO_ADDRS.save(deps.storage, &msg.to_addrs)?;
OPERATORS.save(deps.storage, &msg.opers)?;
IS_HALTED.save(deps.storage, &false)?;
Expand Down Expand Up @@ -75,15 +76,14 @@ pub fn withdraw(
denoms: BTreeSet<String>,
contract_addr: String,
) -> Result<Response, ContractError> {
cw_ownable::assert_owner(deps.storage, &info.sender)?;
nibiru_ownable::assert_owner(deps.storage, info.sender.as_str())?;
let to_addr: String = match to {
Some(given_to_addr) => given_to_addr,
None => info.sender.to_string(),
};
let balances: AllBalanceResponse =
let balances: Vec<cw_std::Coin> =
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()
Expand Down Expand Up @@ -113,20 +113,18 @@ pub fn withdraw_all(
to: Option<String>,
contract_addr: String,
) -> Result<Response, ContractError> {
cw_ownable::assert_owner(deps.storage, &info.sender)?;
nibiru_ownable::assert_owner(deps.storage, info.sender.as_str())?;
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(),
amount: balances.clone(),
};
let event = event_withdraw(
serde_json::to_string(&balances.amount)?.as_str(),
&to_addr,
);
let event =
event_withdraw(serde_json::to_string(&balances)?.as_str(), &to_addr);
LOGS.push_front(
deps.storage,
&Log {
Expand All @@ -144,13 +142,11 @@ pub fn edit_opers(
info: MessageInfo,
action: oper_perms::Action,
) -> Result<Response, ContractError> {
cw_ownable::assert_owner(deps.storage, &info.sender)?;
nibiru_ownable::assert_owner(deps.storage, info.sender.as_str())?;
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());
perms.operators.insert(address.clone());
OPERATORS.save(deps.storage, &perms.operators)?;

let res = Response::new().add_attributes(vec![
Expand Down Expand Up @@ -178,7 +174,7 @@ pub fn toggle_halt(
_env: Env,
info: MessageInfo,
) -> Result<Response, ContractError> {
cw_ownable::assert_owner(deps.storage, &info.sender)?;
nibiru_ownable::assert_owner(deps.storage, info.sender.as_str())?;
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)))
Expand Down Expand Up @@ -229,10 +225,14 @@ pub fn execute_update_ownership(
deps: DepsMut,
env: Env,
info: MessageInfo,
action: cw_ownable::Action,
action: nibiru_ownable::Action,
) -> Result<Response, ContractError> {
let ownership =
cw_ownable::update_ownership(deps, &env.block, &info.sender, action)?;
let ownership = nibiru_ownable::update_ownership(
deps,
&env.block,
info.sender.as_str(),
action,
)?;
Ok(Response::new().add_attributes(ownership.into_attributes()))
}

Expand All @@ -257,9 +257,9 @@ pub fn query(
let perms_status: PermsStatus = query_perms_status(deps)?;
Ok(to_json_binary(&perms_status)?)
}
QueryMsg::Ownership {} => {
Ok(to_json_binary(&cw_ownable::get_ownership(deps.storage)?)?)
}
QueryMsg::Ownership {} => Ok(to_json_binary(
&nibiru_ownable::get_ownership(deps.storage)?,
)?),
}
}

Expand All @@ -273,28 +273,25 @@ pub fn query_accepted_denoms(deps: Deps) -> StdResult<BTreeSet<String>> {
/// use broker_bank::contract::query_bank_balances;
/// use cosmwasm_std::{
/// testing::{mock_dependencies, mock_env},
/// AllBalanceResponse, DepsMut, Env, StdResult};
/// DepsMut, Env, StdResult, Coin};
///
/// let env: Env = mock_env();
/// let mut deps = mock_dependencies();
/// let mut deps: DepsMut = deps.as_mut();
/// let contract_addr = env.contract.address.to_string();
/// let balances: StdResult<AllBalanceResponse> =
/// let balances: StdResult<Vec<Coin>> =
/// query_bank_balances(contract_addr.to_string(), deps.as_ref());
/// assert!(balances.is_ok())
/// ```
pub fn query_bank_balances(
addr: String,
deps: Deps,
) -> StdResult<AllBalanceResponse> {
let query_result =
pub fn query_bank_balances(addr: String, deps: Deps) -> StdResult<Vec<Coin>> {
let query_result: Option<AllBalanceResponse> =
deps.querier
.query(&QueryRequest::Bank(BankQuery::AllBalances {
address: addr,
}))?;
let balances: AllBalanceResponse = match query_result {
Some(res) => res,
None => AllBalanceResponse::default(),
let balances: Vec<Coin> = match query_result {
Some(res) => res.amount,
None => Vec::new(),
};
Ok(balances)
}
Expand Down Expand Up @@ -369,7 +366,7 @@ pub mod tests {
opers: opers.to_vec(),
sender: not_owner,
exec_msg: ExecuteMsg::UpdateOwnership(
cw_ownable::Action::TransferOwnership {
nibiru_ownable::Action::TransferOwnership {
new_owner: String::from("new_owner"),
expiry: None,
},
Expand Down
2 changes: 1 addition & 1 deletion contracts/broker-bank/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub enum ContractError {
SerdeJson(String),

#[error("{0}")]
Ownership(#[from] cw_ownable::OwnershipError),
Ownership(#[from] nibiru_ownable::OwnershipError),

// #[error("serde_json error: {err:?}")]
// SerdeJson { err: serde_json::error::Error },
Expand Down
4 changes: 2 additions & 2 deletions contracts/broker-bank/src/msgs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use cosmwasm_std as cw;

use crate::oper_perms;

#[cw_ownable::cw_ownable_execute]
#[nibiru_ownable::ownable_execute]
#[cw_serde]
pub enum ExecuteMsg {
/// Send coins to an account the set of "TO_ADDRS", appending transaction
Expand All @@ -32,7 +32,7 @@ pub enum ExecuteMsg {
// TODO: feat(broker-bank): Clear logs tx
}

#[cw_ownable::cw_ownable_query]
#[nibiru_ownable::ownable_query]
#[cw_serde]
#[derive(cosmwasm_schema::QueryResponses)]
pub enum QueryMsg {
Expand Down
4 changes: 2 additions & 2 deletions contracts/broker-bank/src/oper_perms.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,10 @@ impl Permissions {
}

pub fn load(storage: &dyn Storage) -> Result<Self, ContractError> {
let owner = cw_ownable::get_ownership(storage)?.owner;
let owner = nibiru_ownable::get_ownership(storage)?.owner;
let opers = OPERATORS.load(storage)?;
Ok(Permissions {
owner: owner.map(|addr| addr.into_string()),
owner: owner.map(|addr| addr.to_string()),
operators: opers,
})
}
Expand Down
7 changes: 0 additions & 7 deletions contracts/broker-staking/.cargo/config

This file was deleted.

9 changes: 9 additions & 0 deletions contracts/broker-staking/.cargo/config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[alias]
wasm = "build --release --lib --target wasm32-unknown-unknown"
wasm-debug = "build --lib --target wasm32-unknown-unknown"
schema = "run --bin schema"

# OLDER:
# wasm = "build --release --target wasm32-unknown-unknown"
# wasm-debug = "build --target wasm32-unknown-unknown"
# schema = "run --example schema"
4 changes: 1 addition & 3 deletions contracts/broker-staking/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,10 @@ crate-type = ["cdylib", "rlib"]

[features]
# for quicker tests, cargo test --lib
# for more explicit tests, cargo test --features=backtraces
backtraces = ["cosmwasm-std/backtraces"]

[dependencies]
cosmwasm-schema = { workspace = true }
cw-ownable = { workspace = true }
nibiru-ownable = { workspace = true }
nibiru-std = { workspace = true }
cosmwasm-std = { workspace = true }
cw20.workspace = true
Expand Down
10 changes: 5 additions & 5 deletions contracts/broker-staking/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ pub fn instantiate(
msg: BrokerBankInstantiateMsg,
) -> StdResult<Response> {
// Managers validation
cw_ownable::initialize_owner(deps.storage, deps.api, Some(&msg.owner))?;
nibiru_ownable::initialize_owner(deps.storage, Some(&msg.owner))?;
TO_ADDRS.save(deps.storage, &msg.to_addrs)?;
OPERATORS.save(deps.storage, &msg.opers)?;
IS_HALTED.save(deps.storage, &false)?;
Expand Down Expand Up @@ -68,7 +68,7 @@ pub fn unstake(
info: MessageInfo,
unstake_msgs: Vec<UnstakeMsg>,
) -> Result<Response, ContractError> {
cw_ownable::assert_owner(deps.storage, &info.sender)?;
nibiru_ownable::assert_owner(deps.storage, info.sender.as_str())?;

let mut messages: Vec<CosmosMsg> = vec![];
for msg in unstake_msgs.iter() {
Expand Down Expand Up @@ -168,8 +168,8 @@ pub fn query(
let perms_status: PermsStatus = query_perms_status(deps)?;
Ok(to_json_binary(&perms_status)?)
}
QueryMsg::Ownership {} => {
Ok(to_json_binary(&cw_ownable::get_ownership(deps.storage)?)?)
}
QueryMsg::Ownership {} => Ok(to_json_binary(
&nibiru_ownable::get_ownership(deps.storage)?,
)?),
}
}
2 changes: 1 addition & 1 deletion contracts/broker-staking/src/msg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use cosmwasm_std::Uint128;
/// Enum respresenting message types for the execute entry point.
/// These express the different ways in which one can invoke the contract
/// and broadcast tx messages against it.
#[cw_ownable::cw_ownable_execute]
#[nibiru_ownable::ownable_execute]
#[cw_serde]
pub enum ExecuteMsg {
/// Toggles whether "operators" can invoke the smart contract. This acts a
Expand Down
2 changes: 1 addition & 1 deletion contracts/broker-staking/src/testing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ pub fn test_assert_owner() -> TestResult {
opers: opers.to_vec(),
sender: not_owner,
exec_msg: ExecuteMsg::UpdateOwnership(
cw_ownable::Action::TransferOwnership {
nibiru_ownable::Action::TransferOwnership {
new_owner: String::from("new_owner"),
expiry: None,
},
Expand Down
Loading

0 comments on commit 334f706

Please sign in to comment.