Skip to content

Commit

Permalink
ref: apply andr macros to vault, primitive, and app contract
Browse files Browse the repository at this point in the history
  • Loading branch information
joemonem committed Jan 9, 2025
1 parent 0e18ffe commit 349d7c1
Show file tree
Hide file tree
Showing 11 changed files with 54 additions and 138 deletions.
2 changes: 0 additions & 2 deletions Cargo.lock

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

37 changes: 12 additions & 25 deletions contracts/app/andromeda-app-contract/src/contract.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
use crate::reply::on_component_instantiation;
use crate::state::{add_app_component, create_cross_chain_message, ADO_ADDRESSES, APP_NAME};
use crate::{
reply::on_component_instantiation,
state::{add_app_component, create_cross_chain_message, ADO_ADDRESSES, APP_NAME},
};
use andromeda_app::app::{ExecuteMsg, InstantiateMsg, QueryMsg};
use andromeda_std::ado_contract::ADOContract;
use andromeda_std::amp::AndrAddr;
use andromeda_std::common::context::ExecuteContext;
use andromeda_std::common::reply::ReplyId;
use andromeda_std::os::vfs::{convert_component_name, ExecuteMsg as VFSExecuteMsg};
use andromeda_std::{
ado_base::InstantiateMsg as BaseInstantiateMsg, ado_base::MigrateMsg, common::encode_binary,
ado_base::{InstantiateMsg as BaseInstantiateMsg, MigrateMsg},
ado_contract::ADOContract,
amp::AndrAddr,
andr_execute_fn,
common::{encode_binary, reply::ReplyId},
error::ContractError,
os::vfs::{convert_component_name, ExecuteMsg as VFSExecuteMsg},
};
#[cfg(not(feature = "library"))]
use cosmwasm_std::entry_point;
Expand Down Expand Up @@ -165,23 +167,8 @@ pub fn reply(deps: DepsMut, _env: Env, msg: Reply) -> Result<Response, ContractE
}
}

#[cfg_attr(not(feature = "library"), entry_point)]
pub fn execute(
deps: DepsMut,
env: Env,
info: MessageInfo,
msg: ExecuteMsg,
) -> Result<Response, ContractError> {
let ctx = ExecuteContext::new(deps, info, env);
match msg {
ExecuteMsg::AMPReceive(pkt) => {
ADOContract::default().execute_amp_receive(ctx, pkt, handle_execute)
}
_ => handle_execute(ctx, msg),
}
}

pub fn handle_execute(ctx: ExecuteContext, msg: ExecuteMsg) -> Result<Response, ContractError> {
#[andr_execute_fn]
pub fn execute(ctx: ExecuteContext, msg: ExecuteMsg) -> Result<Response, ContractError> {
match msg {
ExecuteMsg::AddAppComponent { component } => {
execute::handle_add_app_component(ctx, component)
Expand Down
5 changes: 0 additions & 5 deletions contracts/app/andromeda-app-contract/src/execute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,6 @@ pub fn handle_add_app_component(
!matches!(component.component_type, ComponentType::CrossChain(..)),
ContractError::CrossChainComponentsCurrentlyDisabled {}
);
let contract = ADOContract::default();
ensure!(
contract.is_contract_owner(ctx.deps.storage, sender)?,
ContractError::Unauthorized {}
);

let idx = add_app_component(ctx.deps.storage, &component)?;
ensure!(idx < 50, ContractError::TooManyAppComponents {});
Expand Down
1 change: 0 additions & 1 deletion contracts/data-storage/andromeda-primitive/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ testing = ["cw-multi-test", "andromeda-testing"]
cosmwasm-std = { workspace = true }
cosmwasm-schema = { workspace = true }
cw-storage-plus = { workspace = true }
cw-utils = { workspace = true }
cw20 = { workspace = true }

andromeda-std = { workspace = true, features = ["rates"] }
Expand Down
41 changes: 25 additions & 16 deletions contracts/data-storage/andromeda-primitive/src/contract.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
#[cfg(not(feature = "library"))]
use cosmwasm_std::entry_point;
use cosmwasm_std::{Binary, Deps, DepsMut, Env, MessageInfo, Reply, Response, StdError};
use cosmwasm_std::{ensure, Binary, Deps, DepsMut, Env, MessageInfo, Reply, Response, StdError};

use crate::execute::update_restriction;
use andromeda_data_storage::primitive::{ExecuteMsg, InstantiateMsg, QueryMsg};
use andromeda_std::ado_base::rates::{Rate, RatesMessage};
use andromeda_std::{
ado_base::{InstantiateMsg as BaseInstantiateMsg, MigrateMsg},
ado_contract::ADOContract,
common::{context::ExecuteContext, encode_binary},
andr_execute_fn,
common::encode_binary,
error::ContractError,
};

use crate::{
execute::handle_execute,
execute::{delete_value, set_value},
query::{all_keys, get_type, get_value, owner_keys},
state::RESTRICTION,
};
Expand Down Expand Up @@ -44,19 +47,25 @@ pub fn instantiate(
Ok(resp)
}

#[cfg_attr(not(feature = "library"), entry_point)]
pub fn execute(
deps: DepsMut,
env: Env,
info: MessageInfo,
msg: ExecuteMsg,
) -> Result<Response, ContractError> {
let ctx = ExecuteContext::new(deps, info, env);
match msg {
ExecuteMsg::AMPReceive(pkt) => {
ADOContract::default().execute_amp_receive(ctx, pkt, handle_execute)
}
_ => handle_execute(ctx, msg),
#[andr_execute_fn]
pub fn execute(ctx: ExecuteContext, msg: ExecuteMsg) -> Result<Response, ContractError> {
let action = msg.as_ref().to_string();
match msg.clone() {
ExecuteMsg::UpdateRestriction { restriction } => update_restriction(ctx, restriction),
ExecuteMsg::SetValue { key, value } => set_value(ctx, key, value, action),
ExecuteMsg::DeleteValue { key } => delete_value(ctx, key),
ExecuteMsg::Rates(rates_message) => match rates_message {
RatesMessage::SetRate { rate, .. } => match rate {
Rate::Local(local_rate) => {
// Percent rates aren't applicable in this case, so we enforce Flat rates
ensure!(local_rate.value.is_flat(), ContractError::InvalidRate {});
ADOContract::default().execute(ctx, msg)
}
Rate::Contract(_) => ADOContract::default().execute(ctx, msg),
},
RatesMessage::RemoveRate { .. } => ADOContract::default().execute(ctx, msg),
},
_ => ADOContract::default().execute(ctx, msg),
}
}

Expand Down
50 changes: 6 additions & 44 deletions contracts/data-storage/andromeda-primitive/src/execute.rs
Original file line number Diff line number Diff line change
@@ -1,59 +1,22 @@
use andromeda_data_storage::primitive::{ExecuteMsg, Primitive, PrimitiveRestriction};
use crate::{
query::{get_key_or_default, has_key_permission},
state::{DATA, KEY_OWNER, RESTRICTION},
};
use andromeda_data_storage::primitive::{Primitive, PrimitiveRestriction};
use andromeda_std::{
ado_base::rates::{Rate, RatesMessage},
ado_contract::ADOContract,
common::{actions::call_action, context::ExecuteContext, rates::get_tax_amount, Funds},
common::{context::ExecuteContext, rates::get_tax_amount, Funds},
error::ContractError,
};
use cosmwasm_std::{
coin, ensure, BankMsg, Coin, CosmosMsg, Deps, MessageInfo, Response, StdError, SubMsg, Uint128,
};
use cw_utils::nonpayable;

use crate::{
query::{get_key_or_default, has_key_permission},
state::{DATA, KEY_OWNER, RESTRICTION},
};

pub fn handle_execute(mut ctx: ExecuteContext, msg: ExecuteMsg) -> Result<Response, ContractError> {
let action = msg.as_ref().to_string();
call_action(
&mut ctx.deps,
&ctx.info,
&ctx.env,
&ctx.amp_ctx,
msg.as_ref(),
)?;

match msg.clone() {
ExecuteMsg::UpdateRestriction { restriction } => update_restriction(ctx, restriction),
ExecuteMsg::SetValue { key, value } => set_value(ctx, key, value, action),
ExecuteMsg::DeleteValue { key } => delete_value(ctx, key),
ExecuteMsg::Rates(rates_message) => match rates_message {
RatesMessage::SetRate { rate, .. } => match rate {
Rate::Local(local_rate) => {
// Percent rates aren't applicable in this case, so we enforce Flat rates
ensure!(local_rate.value.is_flat(), ContractError::InvalidRate {});
ADOContract::default().execute(ctx, msg)
}
Rate::Contract(_) => ADOContract::default().execute(ctx, msg),
},
RatesMessage::RemoveRate { .. } => ADOContract::default().execute(ctx, msg),
},
_ => ADOContract::default().execute(ctx, msg),
}
}

pub fn update_restriction(
ctx: ExecuteContext,
restriction: PrimitiveRestriction,
) -> Result<Response, ContractError> {
nonpayable(&ctx.info)?;
let sender = ctx.info.sender;
ensure!(
ADOContract::default().is_owner_or_operator(ctx.deps.storage, sender.as_ref())?,
ContractError::Unauthorized {}
);
RESTRICTION.save(ctx.deps.storage, &restriction)?;
Ok(Response::new()
.add_attribute("method", "update_restriction")
Expand Down Expand Up @@ -108,7 +71,6 @@ pub fn set_value(
}

pub fn delete_value(ctx: ExecuteContext, key: Option<String>) -> Result<Response, ContractError> {
nonpayable(&ctx.info)?;
let sender = ctx.info.sender;

let key = get_key_or_default(&key);
Expand Down
2 changes: 0 additions & 2 deletions contracts/ecosystem/andromeda-vault/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ cosmwasm-std = { workspace = true }
cosmwasm-schema = { workspace = true }
cw-storage-plus = { workspace = true }

cw-utils = { workspace = true }

andromeda-ecosystem = { workspace = true }
andromeda-std = { workspace = true }

Expand Down
41 changes: 4 additions & 37 deletions contracts/ecosystem/andromeda-vault/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use andromeda_ecosystem::vault::{
use andromeda_std::ado_base::ownership::ContractOwnerResponse;
use andromeda_std::ado_contract::ADOContract;
use andromeda_std::amp::{AndrAddr, Recipient};
use andromeda_std::common::actions::call_action;
use andromeda_std::andr_execute_fn;
use andromeda_std::common::context::ExecuteContext;
use andromeda_std::common::encode_binary;
use andromeda_std::{
Expand All @@ -19,8 +19,6 @@ use cosmwasm_std::{
ContractResult, CosmosMsg, Deps, DepsMut, Empty, Env, MessageInfo, Order, QueryRequest, Reply,
ReplyOn, Response, StdError, SubMsg, SystemResult, Uint128, WasmMsg, WasmQuery,
};
use cw_utils::nonpayable;

// version info for migration info
const CONTRACT_NAME: &str = "crates.io:andromeda-vault";
const CONTRACT_VERSION: &str = env!("CARGO_PKG_VERSION");
Expand Down Expand Up @@ -58,31 +56,8 @@ pub fn reply(_deps: DepsMut, _env: Env, msg: Reply) -> Result<Response, Contract
Ok(Response::default())
}

#[cfg_attr(not(feature = "library"), entry_point)]
pub fn execute(
deps: DepsMut,
env: Env,
info: MessageInfo,
msg: ExecuteMsg,
) -> Result<Response, ContractError> {
let ctx = ExecuteContext::new(deps, info, env);

match msg {
ExecuteMsg::AMPReceive(pkt) => {
ADOContract::default().execute_amp_receive(ctx, pkt, handle_execute)
}
_ => handle_execute(ctx, msg),
}
}

pub fn handle_execute(mut ctx: ExecuteContext, msg: ExecuteMsg) -> Result<Response, ContractError> {
call_action(
&mut ctx.deps,
&ctx.info,
&ctx.env,
&ctx.amp_ctx,
msg.as_ref(),
)?;
#[andr_execute_fn]
pub fn execute(ctx: ExecuteContext, msg: ExecuteMsg) -> Result<Response, ContractError> {
match msg {
ExecuteMsg::Deposit { recipient, msg } => execute_deposit(ctx, recipient, msg),
ExecuteMsg::WithdrawVault {
Expand Down Expand Up @@ -216,8 +191,6 @@ pub fn execute_withdraw(
strategy: Option<StrategyType>,
) -> Result<Response, ContractError> {
let ExecuteContext { info, deps, .. } = ctx;
nonpayable(&info)?;

ensure!(
!withdrawals.is_empty(),
ContractError::InvalidTokensToWithdraw {
Expand Down Expand Up @@ -339,13 +312,7 @@ fn execute_update_strategy(
strategy: StrategyType,
address: AndrAddr,
) -> Result<Response, ContractError> {
let ExecuteContext {
deps, env, info, ..
} = ctx;
ensure!(
ADOContract::default().is_contract_owner(deps.storage, info.sender.as_ref())?,
ContractError::Unauthorized {}
);
let ExecuteContext { deps, env, .. } = ctx;
let strategy_addr = address.get_raw_address(&deps.as_ref())?;

//The vault contract must be an operator for the given contract in order to enable withdrawals
Expand Down
1 change: 1 addition & 0 deletions packages/andromeda-app/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,7 @@ pub struct InstantiateMsg {
#[andr_exec]
#[cw_serde]
pub enum ExecuteMsg {
#[attrs(restricted)]
AddAppComponent {
component: AppComponent,
},
Expand Down
10 changes: 4 additions & 6 deletions packages/andromeda-data-storage/src/primitive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,10 @@ pub enum ExecuteMsg {
value: Primitive,
},
/// If key is not specified the default key will be used.
DeleteValue {
key: Option<String>,
},
UpdateRestriction {
restriction: PrimitiveRestriction,
},
#[attrs(nonpayable)]
DeleteValue { key: Option<String> },
#[attrs(restricted, nonpayable)]
UpdateRestriction { restriction: PrimitiveRestriction },
}

#[andr_query]
Expand Down
2 changes: 2 additions & 0 deletions packages/andromeda-ecosystem/src/vault.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,13 @@ pub struct InstantiateMsg {}
#[andr_exec]
#[cw_serde]
pub enum ExecuteMsg {
#[attrs(nonpayable)]
WithdrawVault {
recipient: Option<Recipient>,
withdrawals: Vec<Withdrawal>,
strategy: Option<StrategyType>,
},
#[attrs(restriced)]
UpdateStrategy {
strategy: StrategyType,
address: AndrAddr,
Expand Down

0 comments on commit 349d7c1

Please sign in to comment.