Skip to content

Commit

Permalink
Remove TokenFactoryMsg and TokenFactoryQuery, no token-bindings dep
Browse files Browse the repository at this point in the history
  • Loading branch information
JakeHartnell authored and Jake Hartnell committed Mar 26, 2024
1 parent 33927a1 commit 84811db
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 96 deletions.
1 change: 0 additions & 1 deletion Cargo.lock

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

1 change: 0 additions & 1 deletion contracts/external/cw-abc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ integer-sqrt = { workspace = true }
integer-cbrt = { workspace = true }
getrandom = { workspace = true, features = ["js"] }
thiserror = { workspace = true }
token-bindings = { workspace = true }

[dev-dependencies]
speculoos = { workspace = true }
Expand Down
80 changes: 42 additions & 38 deletions contracts/external/cw-abc/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,16 @@ use cw_tokenfactory_issuer::msg::ExecuteMsg as IssuerExecuteMsg;
use cw_utils::must_pay;
use std::collections::HashSet;
use std::ops::Deref;
use token_bindings::{TokenFactoryMsg, TokenFactoryQuery};

use crate::abc::{CommonsPhase, CurveType};
use crate::contract::CwAbcResult;
use crate::msg::UpdatePhaseConfigMsg;
use crate::state::{
CURVE_STATE, CURVE_TYPE, DONATIONS, FEES_RECIPIENT, HATCHERS, HATCHER_ALLOWLIST, MAX_SUPPLY,
PHASE, PHASE_CONFIG, SUPPLY_DENOM, TOKEN_ISSUER_CONTRACT,
};
use crate::ContractError;

pub fn execute_buy(deps: DepsMut<TokenFactoryQuery>, _env: Env, info: MessageInfo) -> CwAbcResult {
pub fn execute_buy(deps: DepsMut, _env: Env, info: MessageInfo) -> Result<Response, ContractError> {
let curve_type = CURVE_TYPE.load(deps.storage)?;
let curve_fn = curve_type.to_curve_fn();

Expand Down Expand Up @@ -93,7 +91,7 @@ pub fn execute_buy(deps: DepsMut<TokenFactoryQuery>, _env: Env, info: MessageInf

// Mint tokens for sender by calling mint on the cw-tokenfactory-issuer contract
let issuer_addr = TOKEN_ISSUER_CONTRACT.load(deps.storage)?;
let mut msgs: Vec<CosmosMsg<TokenFactoryMsg>> = vec![CosmosMsg::Wasm(WasmMsg::Execute {
let mut msgs: Vec<CosmosMsg> = vec![CosmosMsg::Wasm(WasmMsg::Execute {
contract_addr: issuer_addr.to_string(),
msg: to_json_binary(&IssuerExecuteMsg::Mint {
to_address: info.sender.to_string(),
Expand Down Expand Up @@ -151,7 +149,11 @@ fn update_hatcher_contributions(
}

/// Sell tokens on the bonding curve
pub fn execute_sell(deps: DepsMut<TokenFactoryQuery>, _env: Env, info: MessageInfo) -> CwAbcResult {
pub fn execute_sell(
deps: DepsMut,
_env: Env,
info: MessageInfo,
) -> Result<Response, ContractError> {
let curve_type = CURVE_TYPE.load(deps.storage)?;
let curve_fn = curve_type.to_curve_fn();

Expand All @@ -161,17 +163,17 @@ pub fn execute_sell(deps: DepsMut<TokenFactoryQuery>, _env: Env, info: MessageIn
let issuer_addr = TOKEN_ISSUER_CONTRACT.load(deps.storage)?;

// Burn the sent supply tokens
let burn_msgs: Vec<CosmosMsg<TokenFactoryMsg>> = vec![
let burn_msgs: Vec<CosmosMsg> = vec![
// Send tokens to the issuer contract to be burned
CosmosMsg::<TokenFactoryMsg>::Bank(BankMsg::Send {
CosmosMsg::Bank(BankMsg::Send {
to_address: issuer_addr.to_string().clone(),
amount: vec![Coin {
amount: burn_amount,
denom: supply_denom,
}],
}),
// Execute burn on the cw-tokenfactory-issuer contract
CosmosMsg::<TokenFactoryMsg>::Wasm(WasmMsg::Execute {
CosmosMsg::Wasm(WasmMsg::Execute {
contract_addr: issuer_addr.to_string(),
msg: to_json_binary(&IssuerExecuteMsg::Burn {
from_address: issuer_addr.to_string(),
Expand Down Expand Up @@ -214,14 +216,13 @@ pub fn execute_sell(deps: DepsMut<TokenFactoryQuery>, _env: Env, info: MessageIn
.map_err(StdError::overflow)?;

// Now send the tokens to the sender and any fees to the DAO
let mut send_msgs: Vec<CosmosMsg<TokenFactoryMsg>> =
vec![CosmosMsg::<TokenFactoryMsg>::Bank(BankMsg::Send {
to_address: info.sender.to_string(),
amount: vec![Coin {
amount: released,
denom: curve_state.reserve_denom.clone(),
}],
})];
let mut send_msgs: Vec<CosmosMsg> = vec![CosmosMsg::Bank(BankMsg::Send {
to_address: info.sender.to_string(),
amount: vec![Coin {
amount: released,
denom: curve_state.reserve_denom.clone(),
}],
})];

// Send exit fees to the to the fee recipient
if taxed_amount > Uint128::zero() {
Expand All @@ -235,7 +236,7 @@ pub fn execute_sell(deps: DepsMut<TokenFactoryQuery>, _env: Env, info: MessageIn
}))
}

Ok(Response::<TokenFactoryMsg>::new()
Ok(Response::new()
.add_messages(burn_msgs)
.add_messages(send_msgs)
.add_attribute("action", "burn")
Expand All @@ -246,7 +247,10 @@ pub fn execute_sell(deps: DepsMut<TokenFactoryQuery>, _env: Env, info: MessageIn
}

/// Calculate the exit taxation for the sell amount based on the phase
fn calculate_exit_fee(storage: &dyn Storage, sell_amount: Uint128) -> CwAbcResult<Uint128> {
fn calculate_exit_fee(
storage: &dyn Storage,
sell_amount: Uint128,
) -> Result<Uint128, ContractError> {
// Load the phase config and phase
let phase = PHASE.load(storage)?;
let phase_config = PHASE_CONFIG.load(storage)?;
Expand All @@ -270,7 +274,7 @@ fn calculate_exit_fee(storage: &dyn Storage, sell_amount: Uint128) -> CwAbcResul
}

/// Transitions the bonding curve to a closed phase where only sells are allowed
pub fn execute_close(deps: DepsMut<TokenFactoryQuery>, info: MessageInfo) -> CwAbcResult {
pub fn execute_close(deps: DepsMut, info: MessageInfo) -> Result<Response, ContractError> {
cw_ownable::assert_owner(deps.storage, &info.sender)?;

PHASE.save(deps.storage, &CommonsPhase::Closed)?;
Expand All @@ -280,10 +284,10 @@ pub fn execute_close(deps: DepsMut<TokenFactoryQuery>, info: MessageInfo) -> CwA

/// Send a donation to the funding pool
pub fn execute_donate(
deps: DepsMut<TokenFactoryQuery>,
deps: DepsMut,
_env: Env,
info: MessageInfo,
) -> CwAbcResult {
) -> Result<Response, ContractError> {
let mut curve_state = CURVE_STATE.load(deps.storage)?;

let payment = must_pay(&info, &curve_state.reserve_denom)?;
Expand Down Expand Up @@ -317,10 +321,10 @@ fn assert_allowlisted(storage: &dyn Storage, hatcher: &Addr) -> Result<(), Contr
/// Set the maxiumum supply (only callable by owner)
/// If `max_supply` is set to None there will be no limit.`
pub fn update_max_supply(
deps: DepsMut<TokenFactoryQuery>,
deps: DepsMut,
info: MessageInfo,
max_supply: Option<Uint128>,
) -> CwAbcResult {
) -> Result<Response, ContractError> {
cw_ownable::assert_owner(deps.storage, &info.sender)?;

match max_supply {
Expand All @@ -335,11 +339,11 @@ pub fn update_max_supply(

/// Add and remove addresses from the hatcher allowlist
pub fn update_hatch_allowlist(
deps: DepsMut<TokenFactoryQuery>,
deps: DepsMut,
info: MessageInfo,
to_add: Vec<String>,
to_remove: Vec<String>,
) -> CwAbcResult {
) -> Result<Response, ContractError> {
cw_ownable::assert_owner(deps.storage, &info.sender)?;
let mut allowlist = HATCHER_ALLOWLIST.may_load(deps.storage)?;

Expand Down Expand Up @@ -368,11 +372,11 @@ pub fn update_hatch_allowlist(

/// Update the configuration of a particular phase
pub fn update_phase_config(
deps: DepsMut<TokenFactoryQuery>,
deps: DepsMut,
_env: Env,
info: MessageInfo,
update_phase_config_msg: UpdatePhaseConfigMsg,
) -> CwAbcResult {
) -> Result<Response, ContractError> {
// Assert that the sender is the contract owner
cw_ownable::assert_owner(deps.storage, &info.sender)?;

Expand Down Expand Up @@ -442,10 +446,10 @@ pub fn update_phase_config(
/// NOTE: this changes the pricing. Use with caution.
/// TODO: what other limitations do we want to put on this?
pub fn update_curve(
deps: DepsMut<TokenFactoryQuery>,
deps: DepsMut,
info: MessageInfo,
curve_type: CurveType,
) -> CwAbcResult {
) -> Result<Response, ContractError> {
cw_ownable::assert_owner(deps.storage, &info.sender)?;

CURVE_TYPE.save(deps.storage, &curve_type)?;
Expand All @@ -455,11 +459,11 @@ pub fn update_curve(

/// Update the ownership of the contract
pub fn update_ownership(
deps: DepsMut<TokenFactoryQuery>,
deps: DepsMut,
env: &Env,
info: &MessageInfo,
action: cw_ownable::Action,
) -> Result<Response<TokenFactoryMsg>, ContractError> {
) -> Result<Response, ContractError> {
let ownership = cw_ownable::update_ownership(
DepsMut {
storage: deps.storage,
Expand Down Expand Up @@ -489,7 +493,7 @@ mod tests {

const TEST_DONOR: &str = "donor";

fn exec_donate(deps: DepsMut<TokenFactoryQuery>, donation_amount: u128) -> CwAbcResult {
fn exec_donate(deps: DepsMut, donation_amount: u128) -> Result<Response, ContractError> {
execute_donate(
deps,
mock_env(),
Expand All @@ -498,8 +502,8 @@ mod tests {
}

#[test]
fn should_fail_with_no_funds() -> CwAbcResult<()> {
let mut deps = mock_tf_dependencies();
fn should_fail_with_no_funds() -> Result<(), ContractError> {
let mut deps = mock_dependencies();
let curve_type = CurveType::Linear {
slope: Uint128::new(1),
scale: 1,
Expand All @@ -516,8 +520,8 @@ mod tests {
}

#[test]
fn should_fail_with_incorrect_denom() -> CwAbcResult<()> {
let mut deps = mock_tf_dependencies();
fn should_fail_with_incorrect_denom() -> Result<(), ContractError> {
let mut deps = mock_dependencies();
let curve_type = CurveType::Linear {
slope: Uint128::new(1),
scale: 1,
Expand All @@ -540,8 +544,8 @@ mod tests {
}

#[test]
fn should_add_to_funding_pool() -> CwAbcResult<()> {
let mut deps = mock_tf_dependencies();
fn should_add_to_funding_pool() -> Result<(), ContractError> {
let mut deps = mock_dependencies();
// this matches `linear_curve` test case from curves.rs
let curve_type = CurveType::SquareRoot {
slope: Uint128::new(1),
Expand Down
34 changes: 9 additions & 25 deletions contracts/external/cw-abc/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ use cw_tokenfactory_issuer::msg::{
};
use cw_utils::parse_reply_instantiate_data;
use std::collections::HashSet;
use token_bindings::{TokenFactoryMsg, TokenFactoryQuery};

use crate::abc::{CommonsPhase, CurveFn};
use crate::curves::DecimalPlaces;
Expand All @@ -28,15 +27,13 @@ const CONTRACT_VERSION: &str = env!("CARGO_PKG_VERSION");

const INSTANTIATE_TOKEN_FACTORY_ISSUER_REPLY_ID: u64 = 0;

pub type CwAbcResult<T = Response<TokenFactoryMsg>> = Result<T, ContractError>;

#[cfg_attr(not(feature = "library"), entry_point)]
pub fn instantiate(
deps: DepsMut<TokenFactoryQuery>,
deps: DepsMut,
_env: Env,
info: MessageInfo,
msg: InstantiateMsg,
) -> CwAbcResult {
) -> Result<Response, ContractError> {
set_contract_version(deps.storage, CONTRACT_NAME, CONTRACT_VERSION)?;

let InstantiateMsg {
Expand Down Expand Up @@ -110,11 +107,11 @@ pub fn instantiate(

#[cfg_attr(not(feature = "library"), entry_point)]
pub fn execute(
deps: DepsMut<TokenFactoryQuery>,
deps: DepsMut,
env: Env,
info: MessageInfo,
msg: ExecuteMsg,
) -> CwAbcResult {
) -> Result<Response, ContractError> {
match msg {
ExecuteMsg::Buy {} => commands::execute_buy(deps, env, info),
ExecuteMsg::Sell {} => commands::execute_sell(deps, env, info),
Expand All @@ -137,7 +134,7 @@ pub fn execute(
}

#[cfg_attr(not(feature = "library"), entry_point)]
pub fn query(deps: Deps<TokenFactoryQuery>, env: Env, msg: QueryMsg) -> StdResult<Binary> {
pub fn query(deps: Deps, env: Env, msg: QueryMsg) -> StdResult<Binary> {
// default implementation stores curve info as enum, you can do something else in a derived
// contract and just pass in your custom curve to do_execute
let curve_type = CURVE_TYPE.load(deps.storage)?;
Expand All @@ -148,12 +145,7 @@ pub fn query(deps: Deps<TokenFactoryQuery>, env: Env, msg: QueryMsg) -> StdResul
/// We pull out logic here, so we can import this from another contract and set a different Curve.
/// This contacts sets a curve with an enum in [`InstantiateMsg`] and stored in state, but you may want
/// to use custom math not included - make this easily reusable
pub fn do_query(
deps: Deps<TokenFactoryQuery>,
_env: Env,
msg: QueryMsg,
curve_fn: CurveFn,
) -> StdResult<Binary> {
pub fn do_query(deps: Deps, _env: Env, msg: QueryMsg, curve_fn: CurveFn) -> StdResult<Binary> {
match msg {
// custom queries
QueryMsg::CurveInfo {} => to_json_binary(&queries::query_curve_info(deps, curve_fn)?),
Expand All @@ -175,22 +167,14 @@ pub fn do_query(
}

#[cfg_attr(not(feature = "library"), entry_point)]
pub fn migrate(
deps: DepsMut<TokenFactoryQuery>,
_env: Env,
_msg: MigrateMsg,
) -> Result<Response<TokenFactoryMsg>, ContractError> {
pub fn migrate(deps: DepsMut, _env: Env, _msg: MigrateMsg) -> Result<Response, ContractError> {
// Set contract to version to latest
set_contract_version(deps.storage, CONTRACT_NAME, CONTRACT_VERSION)?;
Ok(Response::<TokenFactoryMsg>::default())
Ok(Response::default())
}

#[cfg_attr(not(feature = "library"), entry_point)]
pub fn reply(
deps: DepsMut<TokenFactoryQuery>,
env: Env,
msg: Reply,
) -> Result<Response<TokenFactoryMsg>, ContractError> {
pub fn reply(deps: DepsMut, env: Env, msg: Reply) -> Result<Response, ContractError> {
match msg.id {
INSTANTIATE_TOKEN_FACTORY_ISSUER_REPLY_ID => {
// Parse and save address of cw-tokenfactory-issuer
Expand Down
16 changes: 6 additions & 10 deletions contracts/external/cw-abc/src/queries.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,9 @@ use crate::state::{
};
use cosmwasm_std::{Deps, Order, QuerierWrapper, StdResult, Uint128};
use std::ops::Deref;
use token_bindings::TokenFactoryQuery;

/// Get the current state of the curve
pub fn query_curve_info(
deps: Deps<TokenFactoryQuery>,
curve_fn: CurveFn,
) -> StdResult<CurveInfoResponse> {
pub fn query_curve_info(deps: Deps, curve_fn: CurveFn) -> StdResult<CurveInfoResponse> {
let CurveState {
reserve,
supply,
Expand All @@ -37,13 +33,13 @@ pub fn query_curve_info(
}

/// Returns information about the supply Denom
pub fn get_denom(deps: Deps<TokenFactoryQuery>) -> StdResult<DenomResponse> {
pub fn get_denom(deps: Deps) -> StdResult<DenomResponse> {
let denom = SUPPLY_DENOM.load(deps.storage)?;
Ok(DenomResponse { denom })
}

pub fn query_donations(
deps: Deps<TokenFactoryQuery>,
deps: Deps,
start_aftor: Option<String>,
limit: Option<u32>,
) -> StdResult<DonationsResponse> {
Expand All @@ -67,7 +63,7 @@ pub fn query_donations(

/// Query hatchers who contributed during the hatch phase
pub fn query_hatchers(
deps: Deps<TokenFactoryQuery>,
deps: Deps,
start_aftor: Option<String>,
limit: Option<u32>,
) -> StdResult<HatchersResponse> {
Expand All @@ -90,13 +86,13 @@ pub fn query_hatchers(
}

/// Query the max supply of the supply token
pub fn query_max_supply(deps: Deps<TokenFactoryQuery>) -> StdResult<Uint128> {
pub fn query_max_supply(deps: Deps) -> StdResult<Uint128> {
let max_supply = MAX_SUPPLY.may_load(deps.storage)?;
Ok(max_supply.unwrap_or(Uint128::MAX))
}

/// Load and return the phase config
pub fn query_phase_config(deps: Deps<TokenFactoryQuery>) -> StdResult<CommonsPhaseConfigResponse> {
pub fn query_phase_config(deps: Deps) -> StdResult<CommonsPhaseConfigResponse> {
let phase = PHASE.load(deps.storage)?;
let phase_config = PHASE_CONFIG.load(deps.storage)?;
Ok(CommonsPhaseConfigResponse {
Expand Down
Loading

0 comments on commit 84811db

Please sign in to comment.