Skip to content

Commit

Permalink
ref: apply andr macros for non fungible tokens contracts
Browse files Browse the repository at this point in the history
  • Loading branch information
joemonem committed Jan 7, 2025
1 parent 0898b46 commit 58a96c0
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 172 deletions.
55 changes: 6 additions & 49 deletions contracts/non-fungible-tokens/andromeda-auction/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ use andromeda_std::{
InstantiateMsg as BaseInstantiateMsg, MigrateMsg,
},
amp::Recipient,
andr_execute_fn,
common::{
actions::call_action,
denom::{
authorize_addresses, execute_authorize_contract, execute_deauthorize_contract,
validate_native_denom, Asset, AuthorizedAddressesResponse, PermissionAction,
Expand All @@ -34,7 +34,6 @@ use cosmwasm_std::{
};
use cw20::{Cw20Coin, Cw20ExecuteMsg, Cw20ReceiveMsg};
use cw721::{Cw721ExecuteMsg, Cw721QueryMsg, Cw721ReceiveMsg, OwnerOfResponse};
use cw_utils::nonpayable;

const CONTRACT_NAME: &str = "crates.io:andromeda-auction";
const CONTRACT_VERSION: &str = env!("CARGO_PKG_VERSION");
Expand Down Expand Up @@ -75,35 +74,10 @@ 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),
}
}

pub fn handle_execute(mut ctx: ExecuteContext, msg: ExecuteMsg) -> Result<Response, ContractError> {
#[andr_execute_fn]
pub fn execute(ctx: ExecuteContext, msg: ExecuteMsg) -> Result<Response, ContractError> {
let action = msg.as_ref().to_string();

let action_response = call_action(
&mut ctx.deps,
&ctx.info,
&ctx.env,
&ctx.amp_ctx,
msg.as_ref(),
)?;

let res = match msg {
match msg {
ExecuteMsg::ReceiveNft(msg) => handle_receive_cw721(ctx, msg),
ExecuteMsg::Receive(msg) => handle_receive_cw20(ctx, msg),
ExecuteMsg::UpdateAuction {
Expand Down Expand Up @@ -155,11 +129,7 @@ pub fn handle_execute(mut ctx: ExecuteContext, msg: ExecuteMsg) -> Result<Respon
execute_deauthorize_contract(ctx.deps, ctx.info, action, addr)
}
_ => ADOContract::default().execute(ctx, msg),
}?;
Ok(res
.add_submessages(action_response.messages)
.add_attributes(action_response.attributes)
.add_events(action_response.events))
}
}

fn handle_receive_cw721(
Expand Down Expand Up @@ -219,7 +189,6 @@ pub fn handle_receive_cw20(
);

let ExecuteContext { ref info, .. } = ctx;
nonpayable(info)?;

let asset_sent = info.sender.clone().into_string();
let amount_sent = receive_msg.amount;
Expand Down Expand Up @@ -375,7 +344,6 @@ fn execute_update_auction(
env,
..
} = ctx;
nonpayable(&info)?;
let (coin_denom, uses_cw20) = coin_denom.get_verified_asset(deps.branch(), env.clone())?;

if uses_cw20 {
Expand Down Expand Up @@ -589,10 +557,7 @@ fn execute_buy_now(
action: String,
) -> Result<Response, ContractError> {
let ExecuteContext {
mut deps,
info,
env,
..
deps, info, env, ..
} = ctx;
let mut token_auction_state =
get_existing_token_auction_state(deps.storage, &token_id, &token_address)?;
Expand All @@ -602,13 +567,6 @@ fn execute_buy_now(
.buy_now_price
.map_or_else(|| Err(ContractError::NoBuyNowOption {}), Ok)?;

ADOContract::default().is_permissioned(
deps.branch(),
env.clone(),
token_auction_state.auction_id,
info.sender.clone(),
)?;

validate_auction(token_auction_state.clone(), info.clone(), &env.block)?;

ensure!(
Expand Down Expand Up @@ -956,7 +914,6 @@ fn execute_cancel(
let ExecuteContext {
deps, info, env, ..
} = ctx;
nonpayable(&info)?;

let mut token_auction_state =
get_existing_token_auction_state(deps.storage, &token_id, &token_address)?;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -450,7 +450,7 @@ fn execute_place_bid_whitelist_cw20() {
});

let invalid_asset = "invalid_asset";
let info = mock_info(invalid_asset, &coins(100, "uusd".to_string()));
let info = mock_info(invalid_asset, &[]);
env.block.time = env.block.time.plus_seconds(1);
let res = execute(deps.as_mut(), env.clone(), info, msg.clone());
assert_eq!(
Expand Down
93 changes: 9 additions & 84 deletions contracts/non-fungible-tokens/andromeda-crowdfund/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ use andromeda_std::{
},
ado_contract::ADOContract,
amp::{messages::AMPPkt, AndrAddr, Recipient},
andr_execute_fn,
common::{
actions::call_action,
context::ExecuteContext,
denom::{Asset, SEND_CW20_ACTION},
encode_binary,
Expand Down Expand Up @@ -104,33 +104,9 @@ pub fn migrate(deps: DepsMut, _env: Env, _msg: MigrateMsg) -> Result<Response, C
ADOContract::default().migrate(deps, CONTRACT_NAME, CONTRACT_VERSION)
}

#[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);

#[andr_execute_fn]
pub fn execute(ctx: ExecuteContext, msg: ExecuteMsg) -> Result<Response, ContractError> {
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> {
let action_response = call_action(
&mut ctx.deps,
&ctx.info,
&ctx.env,
&ctx.amp_ctx,
msg.as_ref(),
)?;

let res = match msg {
ExecuteMsg::AddTier { tier } => execute_add_tier(ctx, tier),
ExecuteMsg::UpdateTier { tier } => execute_update_tier(ctx, tier),
ExecuteMsg::RemoveTier { level } => execute_remove_tier(ctx, level),
Expand All @@ -145,22 +121,11 @@ pub fn handle_execute(mut ctx: ExecuteContext, msg: ExecuteMsg) -> Result<Respon
ExecuteMsg::DiscardCampaign {} => execute_discard_campaign(ctx),
ExecuteMsg::Claim {} => execute_claim(ctx),
_ => ADOContract::default().execute(ctx, msg),
}?;

Ok(res
.add_submessages(action_response.messages)
.add_attributes(action_response.attributes)
.add_events(action_response.events))
}
}

fn execute_add_tier(ctx: ExecuteContext, tier: Tier) -> Result<Response, ContractError> {
let ExecuteContext { deps, info, .. } = ctx;

let contract = ADOContract::default();
ensure!(
contract.is_contract_owner(deps.storage, info.sender.as_str())?,
ContractError::Unauthorized {}
);
let ExecuteContext { deps, .. } = ctx;

tier.validate()?;

Expand Down Expand Up @@ -189,13 +154,7 @@ fn execute_add_tier(ctx: ExecuteContext, tier: Tier) -> Result<Response, Contrac
}

fn execute_update_tier(ctx: ExecuteContext, tier: Tier) -> Result<Response, ContractError> {
let ExecuteContext { deps, info, .. } = ctx;

let contract = ADOContract::default();
ensure!(
contract.is_contract_owner(deps.storage, info.sender.as_str())?,
ContractError::Unauthorized {}
);
let ExecuteContext { deps, .. } = ctx;

tier.validate()?;

Expand Down Expand Up @@ -224,13 +183,7 @@ fn execute_update_tier(ctx: ExecuteContext, tier: Tier) -> Result<Response, Cont
}

fn execute_remove_tier(ctx: ExecuteContext, level: Uint64) -> Result<Response, ContractError> {
let ExecuteContext { deps, info, .. } = ctx;

let contract = ADOContract::default();
ensure!(
contract.is_contract_owner(deps.storage, info.sender.as_str())?,
ContractError::Unauthorized {}
);
let ExecuteContext { deps, .. } = ctx;

let curr_stage = get_current_stage(deps.storage);
ensure!(
Expand All @@ -256,16 +209,7 @@ fn execute_start_campaign(
end_time: Expiry,
presale: Option<Vec<PresaleTierOrder>>,
) -> Result<Response, ContractError> {
let ExecuteContext {
deps, info, env, ..
} = ctx;

// Only owner can start the campaign
let contract = ADOContract::default();
ensure!(
contract.is_contract_owner(deps.storage, info.sender.as_str())?,
ContractError::Unauthorized {}
);
let ExecuteContext { deps, env, .. } = ctx;

ensure!(is_valid_tiers(deps.storage), ContractError::InvalidTiers {});

Expand Down Expand Up @@ -373,18 +317,7 @@ fn handle_receive_cw20(
fn execute_discard_campaign(mut ctx: ExecuteContext) -> Result<Response, ContractError> {
nonpayable(&ctx.info)?;

let ExecuteContext {
ref mut deps,
ref info,
..
} = ctx;

// Only owner can discard the campaign
let contract = ADOContract::default();
ensure!(
contract.is_contract_owner(deps.storage, info.sender.as_str())?,
ContractError::Unauthorized {}
);
let ExecuteContext { ref mut deps, .. } = ctx;

let curr_stage = get_current_stage(deps.storage);
// Ensure that the campaign is in ONGOING, or READY stage
Expand All @@ -409,18 +342,10 @@ fn execute_end_campaign(mut ctx: ExecuteContext) -> Result<Response, ContractErr

let ExecuteContext {
ref mut deps,
ref info,
ref env,
..
} = ctx;

// Only owner can end the campaign
let contract = ADOContract::default();
ensure!(
contract.is_contract_owner(deps.storage, info.sender.as_str())?,
ContractError::Unauthorized {}
);

let curr_stage = get_current_stage(deps.storage);
ensure!(
curr_stage == CampaignStage::ONGOING,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ use andromeda_std::{
ado_base::{InstantiateMsg as BaseInstantiateMsg, MigrateMsg},
ado_contract::ADOContract,
amp::Recipient,
andr_execute_fn,
common::{
actions::call_action,
context::ExecuteContext,
denom::{
authorize_addresses, execute_authorize_contract, execute_deauthorize_contract, Asset,
Expand All @@ -37,7 +37,7 @@ use cosmwasm_std::{
WasmQuery,
};

use cw_utils::{nonpayable, Expiration};
use cw_utils::Expiration;

const CONTRACT_NAME: &str = "crates.io:andromeda-marketplace";
const CONTRACT_VERSION: &str = env!("CARGO_PKG_VERSION");
Expand Down Expand Up @@ -75,35 +75,10 @@ pub fn instantiate(
Ok(inst_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),
}
}

pub fn handle_execute(mut ctx: ExecuteContext, msg: ExecuteMsg) -> Result<Response, ContractError> {
#[andr_execute_fn]
pub fn execute(ctx: ExecuteContext, msg: ExecuteMsg) -> Result<Response, ContractError> {
let action = msg.as_ref().to_string();

let action_response = call_action(
&mut ctx.deps,
&ctx.info,
&ctx.env,
&ctx.amp_ctx,
msg.as_ref(),
)?;

let res = match msg {
match msg {
ExecuteMsg::ReceiveNft(msg) => handle_receive_cw721(ctx, msg),
ExecuteMsg::Receive(msg) => handle_receive_cw20(ctx, msg),
ExecuteMsg::UpdateSale {
Expand Down Expand Up @@ -132,11 +107,7 @@ pub fn handle_execute(mut ctx: ExecuteContext, msg: ExecuteMsg) -> Result<Respon
}

_ => ADOContract::default().execute(ctx, msg),
}?;
Ok(res
.add_submessages(action_response.messages)
.add_attributes(action_response.attributes)
.add_events(action_response.events))
}
}

fn handle_receive_cw721(
Expand Down Expand Up @@ -183,7 +154,6 @@ pub fn handle_receive_cw20(
ctx.info.sender.clone(),
)?;
let ExecuteContext { ref info, .. } = ctx;
nonpayable(info)?;

let asset_sent = info.sender.clone();
let amount_sent = receive_msg.amount;
Expand Down Expand Up @@ -296,7 +266,6 @@ fn execute_update_sale(
..
} = ctx;
let (coin_denom, uses_cw20) = coin_denom.get_verified_asset(deps.branch(), env)?;
nonpayable(&info)?;

let mut token_sale_state =
get_existing_token_sale_state(deps.storage, &token_id, &token_address)?;
Expand Down Expand Up @@ -611,7 +580,6 @@ fn execute_cancel(
token_address: String,
) -> Result<Response, ContractError> {
let ExecuteContext { deps, info, .. } = ctx;
nonpayable(&info)?;

let mut token_sale_state =
get_existing_token_sale_state(deps.storage, &token_id, &token_address)?;
Expand Down
Loading

0 comments on commit 58a96c0

Please sign in to comment.