Skip to content

Commit

Permalink
Donate only into the funding pool
Browse files Browse the repository at this point in the history
  • Loading branch information
ismellike committed Apr 23, 2024
1 parent 1093819 commit 92a3f32
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 158 deletions.
45 changes: 1 addition & 44 deletions contracts/external/cw-abc/schema/cw-abc.json
Original file line number Diff line number Diff line change
Expand Up @@ -520,27 +520,14 @@
"additionalProperties": false
},
{
"description": "Donate will donate tokens to a pool. You must send only reserve tokens.",
"description": "Donate will donate tokens to the funding pool. You must send only reserve tokens.",
"type": "object",
"required": [
"donate"
],
"properties": {
"donate": {
"type": "object",
"properties": {
"pool": {
"description": "The pool to donate tokens into (defaults to funding pool)",
"anyOf": [
{
"$ref": "#/definitions/DonationPool"
},
{
"type": "null"
}
]
}
},
"additionalProperties": false
}
},
Expand Down Expand Up @@ -878,36 +865,6 @@
"description": "A fixed-point decimal value with 18 fractional digits, i.e. Decimal(1_000_000_000_000_000_000) == 1.0\n\nThe greatest possible value that can be represented is 340282366920938463463.374607431768211455 (which is (2^128 - 1) / 10^18)",
"type": "string"
},
"DonationPool": {
"oneOf": [
{
"type": "object",
"required": [
"funding"
],
"properties": {
"funding": {
"type": "object",
"additionalProperties": false
}
},
"additionalProperties": false
},
{
"type": "object",
"required": [
"reserve"
],
"properties": {
"reserve": {
"type": "object",
"additionalProperties": false
}
},
"additionalProperties": false
}
]
},
"Expiration": {
"description": "Expiration represents a point in time when some event happens. It can compare with a BlockInfo and will return is_expired() == true once the condition is hit (and for every block in the future)",
"oneOf": [
Expand Down
100 changes: 14 additions & 86 deletions contracts/external/cw-abc/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use cw_utils::must_pay;
use std::ops::Deref;

use crate::abc::{CommonsPhase, CurveType};
use crate::msg::{DonationPool, HatcherAllowlistEntry, UpdatePhaseConfigMsg};
use crate::msg::{HatcherAllowlistEntry, UpdatePhaseConfigMsg};
use crate::state::{
hatcher_allowlist, HatcherAllowlistConfigType, CURVE_STATE, CURVE_TYPE, DONATIONS,
FUNDING_POOL_FORWARDING, HATCHERS, IS_PAUSED, MAX_SUPPLY, PHASE, PHASE_CONFIG, SUPPLY_DENOM,
Expand Down Expand Up @@ -273,36 +273,24 @@ pub fn close(deps: DepsMut, info: MessageInfo) -> Result<Response, ContractError
}

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

let payment = must_pay(&info, &curve_state.reserve_denom)?;

let mut msgs = vec![];
match pool {
DonationPool::Funding {} => {
if let Some(funding_pool_forwarding) = FUNDING_POOL_FORWARDING.may_load(deps.storage)? {
msgs.push(CosmosMsg::Bank(BankMsg::Send {
to_address: funding_pool_forwarding.to_string(),
amount: info.funds,
}));
} else {
curve_state.funding += payment;

CURVE_STATE.save(deps.storage, &curve_state)?;
}
}
DonationPool::Reserve {} => {
curve_state.reserve += payment;
let msgs =
if let Some(funding_pool_forwarding) = FUNDING_POOL_FORWARDING.may_load(deps.storage)? {
vec![CosmosMsg::Bank(BankMsg::Send {
to_address: funding_pool_forwarding.to_string(),
amount: info.funds,
})]
} else {
curve_state.funding += payment;

CURVE_STATE.save(deps.storage, &curve_state)?;
}
};

vec![]
};

// No minting of tokens is necessary, the supply stays the same
let total_donation =
Expand All @@ -318,7 +306,6 @@ pub fn donate(
.add_attribute("action", "donate")
.add_attribute("donor", info.sender)
.add_attribute("amount", payment)
.add_attribute("pool", pool.to_string())
.add_attribute("total_donation", total_donation)
.add_messages(msgs))
}
Expand Down Expand Up @@ -621,7 +608,6 @@ mod tests {
deps,
mock_env(),
mock_info(TEST_DONOR, &[coin(donation_amount, TEST_RESERVE_DENOM)]),
DonationPool::Funding {},
)
}

Expand Down Expand Up @@ -657,7 +643,6 @@ mod tests {
deps.as_mut(),
mock_env(),
mock_info(TEST_DONOR, &[coin(1, "fake")]),
DonationPool::Funding {},
);
assert_that!(res)
.is_err()
Expand All @@ -669,64 +654,7 @@ mod tests {
}

#[test]
fn test_donation() -> 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),
scale: 1,
};
let init_msg = default_instantiate_msg(2, 8, curve_type);
mock_init(deps.as_mut(), init_msg)?;

let donation_amount = 5;
let _res = exec_donate(deps.as_mut(), donation_amount)?;

// check that the curve's funding has been increased while supply and reserve have not
let curve_state = CURVE_STATE.load(&deps.storage)?;
assert_that!(curve_state.funding).is_equal_to(Uint128::new(donation_amount));

// check that the donor is in the donations map
let donation = DONATIONS.load(&deps.storage, &Addr::unchecked(TEST_DONOR))?;
assert_that!(donation).is_equal_to(Uint128::new(donation_amount));

// check that the owner can withdraw
withdraw(
deps.as_mut(),
mock_env(),
mock_info(TEST_CREATOR, &[]),
None,
)?;

// check that a random can't withdraw
let res = withdraw(deps.as_mut(), mock_env(), mock_info("random", &[]), None);
assert_that!(res)
.is_err()
.is_equal_to(ContractError::Ownership(
cw_ownable::OwnershipError::NotOwner,
));

// execute donation to the reserve pool
donate(
deps.as_mut(),
mock_env(),
mock_info(TEST_DONOR, &[coin(donation_amount, TEST_RESERVE_DENOM)]),
DonationPool::Reserve {},
)?;

// check that the curve's reserve has been increased while supply and reserve have not
let curve_state = CURVE_STATE.load(&deps.storage)?;
assert_that!(curve_state.reserve).is_equal_to(Uint128::new(donation_amount));

// check that the donor is in the donations map
let donation = DONATIONS.load(&deps.storage, &Addr::unchecked(TEST_DONOR))?;
assert_that!(donation).is_equal_to(Uint128::new(donation_amount * 2));

Ok(())
}

#[test]
fn should_send_to_funding_pool_forwarding() -> Result<(), ContractError> {
fn should_donation_with_forwarding() -> Result<(), ContractError> {
let mut deps = mock_dependencies();
// this matches `linear_curve` test case from curves.rs
let curve_type = CurveType::SquareRoot {
Expand Down
2 changes: 1 addition & 1 deletion contracts/external/cw-abc/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ pub fn execute(
ExecuteMsg::Buy {} => commands::buy(deps, env, info),
ExecuteMsg::Sell {} => commands::sell(deps, env, info),
ExecuteMsg::Close {} => commands::close(deps, info),
ExecuteMsg::Donate { pool } => commands::donate(deps, env, info, pool.unwrap_or_default()),
ExecuteMsg::Donate {} => commands::donate(deps, env, info),
ExecuteMsg::Withdraw { amount } => commands::withdraw(deps, env, info, amount),
ExecuteMsg::UpdateFundingPoolForwarding { address } => {
commands::update_funding_pool_forwarding(deps, env, info, address)
Expand Down
29 changes: 2 additions & 27 deletions contracts/external/cw-abc/src/msg.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use cosmwasm_schema::{cw_serde, QueryResponses};
use cosmwasm_std::{Addr, Decimal as StdDecimal, Uint128};
use cw_address_like::AddressLike;
use std::fmt::{self, Display};

use crate::{
abc::{CommonsPhase, CommonsPhaseConfig, CurveType, MinMax, ReserveToken, SupplyToken},
Expand Down Expand Up @@ -65,12 +64,9 @@ pub enum ExecuteMsg {
/// Sell burns supply tokens in return for the reserve token.
/// You must send only supply tokens.
Sell {},
/// Donate will donate tokens to a pool.
/// Donate will donate tokens to the funding pool.
/// You must send only reserve tokens.
Donate {
/// The pool to donate tokens into (defaults to funding pool)
pool: Option<DonationPool>,
},
Donate {},
/// Withdraw will withdraw tokens from the funding pool.
Withdraw {
/// The amount to withdraw (defaults to full amount).
Expand Down Expand Up @@ -174,27 +170,6 @@ pub struct HatcherAllowlistEntry<T: AddressLike> {
pub config: HatcherAllowlistConfig,
}

#[cw_serde]
pub enum DonationPool {
Funding {},
Reserve {},
}

impl Default for DonationPool {
fn default() -> Self {
DonationPool::Funding {}
}
}

impl Display for DonationPool {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
DonationPool::Funding {} => write!(f, "Funding"),
DonationPool::Reserve {} => write!(f, "Reserve"),
}
}
}

#[cw_serde]
pub struct CurveInfoResponse {
/// How many reserve tokens have been received
Expand Down

0 comments on commit 92a3f32

Please sign in to comment.