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: String Storage ADO #512

Merged
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
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
15 changes: 15 additions & 0 deletions Cargo.lock

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

4 changes: 4 additions & 0 deletions contracts/data-storage/andromeda-string-storage/.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"
40 changes: 40 additions & 0 deletions contracts/data-storage/andromeda-string-storage/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
[package]
name = "andromeda-string-storage"
version = "1.0.0"
authors = ["Mitar Djakovic <[email protected]>"]
edition = "2021"
rust-version = "1.75.0"

exclude = [
# Those files are rust-optimizer artifacts. You might want to commit them for convenience but they should not be part of the source code publication.
"contract.wasm",
"hash.txt",
]

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[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 }
cw20 = { workspace = true }


andromeda-std = { workspace = true, features = ["rates"] }
andromeda-data-storage = { workspace = true }


[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
cw-multi-test = { workspace = true, optional = true }
andromeda-testing = { workspace = true, optional = true }
10 changes: 10 additions & 0 deletions contracts/data-storage/andromeda-string-storage/examples/schema.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
use andromeda_data_storage::string_storage::{ExecuteMsg, InstantiateMsg, QueryMsg};
use cosmwasm_schema::write_api;
fn main() {
write_api! {
instantiate: InstantiateMsg,
query: QueryMsg,
execute: ExecuteMsg,

}
}
74 changes: 74 additions & 0 deletions contracts/data-storage/andromeda-string-storage/src/contract.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
#[cfg(not(feature = "library"))]
use cosmwasm_std::entry_point;
use cosmwasm_std::{Binary, Deps, DepsMut, Env, MessageInfo, Response};

use andromeda_data_storage::string_storage::{ExecuteMsg, InstantiateMsg, QueryMsg};
use andromeda_std::{
ado_base::{InstantiateMsg as BaseInstantiateMsg, MigrateMsg},
ado_contract::ADOContract,
common::{context::ExecuteContext, encode_binary},
error::ContractError,
};
use crate::{
execute::handle_execute,
query::{get_value, get_data_owner},
state::RESTRICTION,
};

// version info for migration info
const CONTRACT_NAME: &str = "crates.io:andromeda-string-storage";
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,
},
)?;
RESTRICTION.save(deps.storage, &msg.restriction)?;
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),
}
}

#[cfg_attr(not(feature = "library"), entry_point)]
pub fn query(deps: Deps, env: Env, msg: QueryMsg) -> Result<Binary, ContractError> {
match msg {
QueryMsg::GetValue { } => encode_binary(&get_value(deps.storage)?),
QueryMsg::GetDataOwner { } => encode_binary(&get_data_owner(deps.storage)?),
_ => ADOContract::default().query(deps, env, msg),
}
}

#[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)
}
158 changes: 158 additions & 0 deletions contracts/data-storage/andromeda-string-storage/src/execute.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
use andromeda_data_storage::string_storage::{ExecuteMsg, StringStorage, StringStorageRestriction};
use andromeda_std::{
ado_base::rates::{Rate, RatesMessage},
ado_contract::ADOContract,
common::{
actions::call_action, call_action::get_action_name, context::ExecuteContext,
rates::get_tax_amount, Funds,
},
error::ContractError,
};
use cosmwasm_std::{
coin, ensure, BankMsg, Coin, CosmosMsg, Deps, MessageInfo, Response, SubMsg, Uint128,
};
use cw_utils::nonpayable;

use crate::{
query::has_permission,
state::{DATA, DATA_OWNER, RESTRICTION},
};
const CONTRACT_NAME: &str = "crates.io:andromeda-string-storage";

pub fn handle_execute(mut ctx: ExecuteContext, msg: ExecuteMsg) -> Result<Response, ContractError> {
let action = get_action_name(CONTRACT_NAME, msg.as_ref());
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 { value } => set_value(ctx, value, action),
ExecuteMsg::DeleteValue { } => delete_value(ctx),
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: StringStorageRestriction,
) -> 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 set_value(
ctx: ExecuteContext,
value: StringStorage,
action: String,
) -> Result<Response, ContractError> {
let sender = ctx.info.sender.clone();
ensure!(
has_permission(ctx.deps.storage, &sender)?,
ContractError::Unauthorized {}
);

value.validate()?;

let tax_response = tax_set_value(ctx.deps.as_ref(), &ctx.info, action)?;

DATA.save(ctx.deps.storage, &value.clone())?;
DATA_OWNER.save(ctx.deps.storage, &sender)?;

let mut response = Response::new()
.add_attribute("method", "set_value")
.add_attribute("sender", sender)
.add_attribute("value", format!("{value:?}"));

if let Some(tax_response) = tax_response {
response = response.add_submessages(tax_response.1);
let refund = tax_response.0.try_get_coin()?;
if !refund.amount.is_zero() {
return Ok(response.add_message(CosmosMsg::Bank(BankMsg::Send {
to_address: ctx.info.sender.into_string(),
amount: vec![refund],
})));
}
}

Ok(response)
}

pub fn delete_value(ctx: ExecuteContext) -> Result<Response, ContractError> {
nonpayable(&ctx.info)?;
let sender = ctx.info.sender;
ensure!(
has_permission(ctx.deps.storage, &sender)?,
ContractError::Unauthorized {}
);
DATA.remove(ctx.deps.storage);
DATA_OWNER.remove(ctx.deps.storage);
Ok(Response::new()
.add_attribute("method", "delete_value")
.add_attribute("sender", sender)
)
}

fn tax_set_value(
deps: Deps,
info: &MessageInfo,
action: String,
) -> Result<Option<(Funds, Vec<SubMsg>)>, ContractError> {
let default_coin = coin(0_u128, "uandr".to_string());
let sent_funds = info.funds.first().unwrap_or(&default_coin);

let transfer_response = ADOContract::default().query_deducted_funds(
deps,
action,
Funds::Native(sent_funds.clone()),
)?;

if let Some(transfer_response) = transfer_response {
let remaining_funds = transfer_response.leftover_funds.try_get_coin()?;
let tax_amount = get_tax_amount(
&transfer_response.msgs,
remaining_funds.amount,
remaining_funds.amount,
);

let refund = if sent_funds.amount > tax_amount {
sent_funds.amount.checked_sub(tax_amount)?
} else {
Uint128::zero()
};

let after_tax_payment = Coin {
denom: remaining_funds.denom,
amount: refund,
};
Ok(Some((
Funds::Native(after_tax_payment),
transfer_response.msgs,
)))
} else {
Ok(None)
}
}
9 changes: 9 additions & 0 deletions contracts/data-storage/andromeda-string-storage/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
pub mod contract;
mod execute;
#[cfg(all(not(target_arch = "wasm32"), feature = "testing"))]
pub mod mock;
mod query;
mod state;

#[cfg(test)]
mod testing;
Loading
Loading