Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: curve ADO #515

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
7c5d2f2
feat: curve ADO
mdjakovic0920 Jul 15, 2024
ad27845
Merge branch 'andromedaprotocol:development' into mdjakovic/curve-ado
mdjakovic0920 Aug 5, 2024
32a18f2
fix: added call_action() to execute functions
mdjakovic0920 Aug 5, 2024
29b58b8
feat: added log to CHANGELOG.md
mdjakovic0920 Aug 5, 2024
81fde51
fix: removed unnecessary casting integer literal
mdjakovic0920 Aug 5, 2024
9c4e5ae
fix: removed unnecessary casting integer literal
mdjakovic0920 Aug 5, 2024
0fd8f95
fix: checked fmt
mdjakovic0920 Aug 5, 2024
6db3b1f
Merge branch 'andromedaprotocol:development' into mdjakovic/curve-ado
mdjakovic0920 Aug 15, 2024
504ca42
Merge branch 'development' into mdjakovic/curve-ado
crnbarr93 Sep 2, 2024
10666f9
Merge branch 'andromedaprotocol:development' into mdjakovic/curve-ado
mdjakovic0920 Sep 2, 2024
87ce171
fix: use interface instead of individual state values
mdjakovic0920 Sep 2, 2024
88adf67
fix: removed unused comments
mdjakovic0920 Sep 2, 2024
d25ac7c
feat: added rstest crate as a dependency
mdjakovic0920 Sep 2, 2024
14094ac
fix: use test_case crate for testing
mdjakovic0920 Sep 3, 2024
73e999a
feat: implemented the validation method for CurveConfig
mdjakovic0920 Sep 3, 2024
bbb2db7
fix: use __.is_err() instead of is_ok()
mdjakovic0920 Sep 3, 2024
b52dbf3
Merge branch 'development' into mdjakovic/curve-ado
mdjakovic0920 Sep 3, 2024
3345f19
Merge branch 'development' into mdjakovic/curve-ado
mdjakovic0920 Oct 11, 2024
fe63444
fix: just use action name as an attribute
mdjakovic0920 Oct 11, 2024
2370722
fix: just use action name as an attribute
mdjakovic0920 Oct 11, 2024
b33862b
fix: add nonpayable method to functions
mdjakovic0920 Oct 11, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Added Expiry Enum [(#419)](https://github.com/andromedaprotocol/andromeda-core/pull/419)
- Added Conditional Splitter [(#441)](https://github.com/andromedaprotocol/andromeda-core/pull/441)
- Validator Staking: Added the option to set an amount while unstaking [(#458)](https://github.com/andromedaprotocol/andromeda-core/pull/458)
- Added Curve ADO [(#515)](https://github.com/andromedaprotocol/andromeda-core/pull/515)
- Set Amount Splitter [(#507)](https://github.com/andromedaprotocol/andromeda-core/pull/507)
- Added String Storage ADO [(#512)](https://github.com/andromedaprotocol/andromeda-core/pull/512)
- Boolean Storage ADO [(#513)](https://github.com/andromedaprotocol/andromeda-core/pull/513)
Expand Down
49 changes: 49 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,4 @@ semver = "1.0.0"
enum-repr = "0.2.6"
cw-multi-test = { version = "1.0.0", features = ["cosmwasm_1_2"] }
serde = { version = "1.0.127" }
test-case = { version = "3.3.1" }
4 changes: 4 additions & 0 deletions contracts/modules/andromeda-curve/.cargo/config
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[alias]
wasm = "build --release --target wasm32-unknown-unknown"
unit-test = "test --lib"
schema = "run --example schema"
33 changes: 33 additions & 0 deletions contracts/modules/andromeda-curve/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
[package]
name = "andromeda-curve"
version = "1.0.0"
edition = "2021"
rust-version = "1.75.0"

[lib]
crate-type = ["cdylib", "rlib"]

[features]
# for more explicit tests, cargo test --features=backtraces
backtraces = ["cosmwasm-std/backtraces"]
# use library feature to disable all instantiate/execute/query exports
library = []
testing = ["cw-multi-test", "andromeda-testing"]


[dependencies]
cosmwasm-std = { workspace = true }
cosmwasm-schema = { workspace = true }
cw-storage-plus = { workspace = true }
cw-utils = { workspace = true }
test-case = { workspace = true }

andromeda-std = { workspace = true, features = [] }
andromeda-modules = { workspace = true }

[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
cw-multi-test = { workspace = true, optional = true }
andromeda-testing = { workspace = true, optional = true }

[dev-dependencies]
andromeda-app = { workspace = true }
10 changes: 10 additions & 0 deletions contracts/modules/andromeda-curve/examples/schema.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
use andromeda_modules::curve::{ExecuteMsg, InstantiateMsg, QueryMsg};
use cosmwasm_schema::write_api;
fn main() {
write_api! {
instantiate: InstantiateMsg,
query: QueryMsg,
execute: ExecuteMsg,

}
}
213 changes: 213 additions & 0 deletions contracts/modules/andromeda-curve/src/contract.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,213 @@
#[cfg(not(feature = "library"))]
use crate::state::{
CURVE_CONFIG, DEFAULT_CONSTANT_VALUE, DEFAULT_MULTIPLE_VARIABLE_VALUE, RESTRICTION,
};
use andromeda_modules::curve::{
CurveConfig, CurveId, CurveRestriction, ExecuteMsg, GetCurveConfigResponse,
GetPlotYFromXResponse, GetRestrictionResponse, InstantiateMsg, QueryMsg,
};
use andromeda_std::{
ado_base::{InstantiateMsg as BaseInstantiateMsg, MigrateMsg},
ado_contract::ADOContract,
common::{actions::call_action, context::ExecuteContext, encode_binary},
error::ContractError,
};

use cosmwasm_std::{
ensure, entry_point, Addr, Binary, Deps, DepsMut, Env, MessageInfo, Response, Storage,
};

use cw_utils::nonpayable;

// version info for migration info
const CONTRACT_NAME: &str = "crates.io:andromeda-curve";
const CONTRACT_VERSION: &str = env!("CARGO_PKG_VERSION");

#[cfg_attr(not(feature = "library"), entry_point)]
pub fn instantiate(
deps: DepsMut,
env: Env,
info: MessageInfo,
msg: InstantiateMsg,
) -> Result<Response, ContractError> {
let resp = ADOContract::default().instantiate(
deps.storage,
env,
deps.api,
&deps.querier,
info,
BaseInstantiateMsg {
ado_type: CONTRACT_NAME.to_string(),
ado_version: CONTRACT_VERSION.to_string(),
kernel_address: msg.kernel_address,
owner: msg.owner,
},
)?;

msg.curve_config.validate()?;

RESTRICTION.save(deps.storage, &msg.restriction)?;
CURVE_CONFIG.save(deps.storage, &msg.curve_config)?;

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),
}
}

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.clone() {
ExecuteMsg::UpdateCurveConfig { curve_config } => {
execute_update_curve_config(ctx, curve_config)
}
ExecuteMsg::UpdateRestriction { restriction } => {
execute_update_restriction(ctx, restriction)
}
ExecuteMsg::Reset {} => execute_reset(ctx),
_ => ADOContract::default().execute(ctx, msg),
}?;
mdjakovic0920 marked this conversation as resolved.
Show resolved Hide resolved

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

pub fn execute_update_curve_config(
mdjakovic0920 marked this conversation as resolved.
Show resolved Hide resolved
ctx: ExecuteContext,
curve_config: CurveConfig,
) -> Result<Response, ContractError> {
nonpayable(&ctx.info)?;
let sender = ctx.info.sender.clone();
ensure!(
has_permission(ctx.deps.storage, &sender)?,
ContractError::Unauthorized {}
);

mdjakovic0920 marked this conversation as resolved.
Show resolved Hide resolved
curve_config.validate()?;
CURVE_CONFIG.update(ctx.deps.storage, |_| {
Ok::<CurveConfig, ContractError>(curve_config)
})?;

Ok(Response::new()
.add_attribute("method", "update_curve_config")
.add_attribute("sender", sender))
}

pub fn execute_update_restriction(
ctx: ExecuteContext,
restriction: CurveRestriction,
) -> 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")
.add_attribute("sender", sender))
}

pub fn execute_reset(ctx: ExecuteContext) -> Result<Response, ContractError> {
nonpayable(&ctx.info)?;
let sender = ctx.info.sender.clone();
ensure!(
has_permission(ctx.deps.storage, &sender)?,
ContractError::Unauthorized {}
);

CURVE_CONFIG.remove(ctx.deps.storage);

Ok(Response::new().add_attribute("method", "reset"))
}

pub fn has_permission(storage: &dyn Storage, addr: &Addr) -> Result<bool, ContractError> {
let is_operator = ADOContract::default().is_owner_or_operator(storage, addr.as_str())?;
let allowed = match RESTRICTION.load(storage)? {
CurveRestriction::Private => is_operator,
CurveRestriction::Public => true,
};
Ok(is_operator || allowed)
}

#[cfg_attr(not(feature = "library"), entry_point)]
pub fn query(deps: Deps, env: Env, msg: QueryMsg) -> Result<Binary, ContractError> {
match msg {
QueryMsg::GetCurveConfig {} => encode_binary(&query_curve_config(deps.storage)?),
QueryMsg::GetRestriction {} => encode_binary(&query_restriction(deps.storage)?),
QueryMsg::GetPlotYFromX { x_value } => {
encode_binary(&query_plot_y_from_x(deps.storage, x_value)?)
}
_ => ADOContract::default().query(deps, env, msg),
}
}

pub fn query_curve_config(storage: &dyn Storage) -> Result<GetCurveConfigResponse, ContractError> {
let curve_config = CURVE_CONFIG.load(storage)?;
Ok(GetCurveConfigResponse { curve_config })
}

pub fn query_restriction(storage: &dyn Storage) -> Result<GetRestrictionResponse, ContractError> {
let restriction = RESTRICTION.load(storage)?;
Ok(GetRestrictionResponse { restriction })
}

pub fn query_plot_y_from_x(
storage: &dyn Storage,
x_value: f64,
) -> Result<GetPlotYFromXResponse, ContractError> {
let curve_config = CURVE_CONFIG.load(storage)?;

let y_value = match curve_config {
CurveConfig::ExpConfig {
curve_id,
base_value,
multiple_variable_value,
constant_value,
} => {
let curve_id_f64 = match curve_id {
CurveId::Growth => 1_f64,
CurveId::Decay => -1_f64,
};
let base_value_f64 = base_value as f64;
let constant_value_f64 = constant_value.unwrap_or(DEFAULT_CONSTANT_VALUE) as f64;
let multiple_variable_value_f64 =
multiple_variable_value.unwrap_or(DEFAULT_MULTIPLE_VARIABLE_VALUE) as f64;

(constant_value_f64
* base_value_f64.powf(curve_id_f64 * multiple_variable_value_f64 * x_value))
.to_string()
}
};
mdjakovic0920 marked this conversation as resolved.
Show resolved Hide resolved

Ok(GetPlotYFromXResponse { y_value })
}

#[cfg_attr(not(feature = "library"), entry_point)]
pub fn migrate(deps: DepsMut, _env: Env, _msg: MigrateMsg) -> Result<Response, ContractError> {
ADOContract::default().migrate(deps, CONTRACT_NAME, CONTRACT_VERSION)
}
7 changes: 7 additions & 0 deletions contracts/modules/andromeda-curve/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
pub mod contract;
pub mod state;
#[cfg(test)]
pub mod testing;

#[cfg(all(not(target_arch = "wasm32"), feature = "testing"))]
pub mod mock;
Loading
Loading