Skip to content

Commit

Permalink
Merge branch 'main' into cw20-kernel-routing
Browse files Browse the repository at this point in the history
  • Loading branch information
joemonem authored Dec 16, 2024
2 parents c354fbc + 60083f8 commit 6df4bed
Show file tree
Hide file tree
Showing 34 changed files with 2,993 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Added Distance ADO [(#570)](https://github.com/andromedaprotocol/andromeda-core/pull/570)
- Rates: Handle cross-chain recipients [(#671)](https://github.com/andromedaprotocol/andromeda-core/pull/671)
- Permissions: Permissioned Actors in AndromedaQuery [(#717)](https://github.com/andromedaprotocol/andromeda-core/pull/717)
- Added Schema and Form ADOs [(#591)](https://github.com/andromedaprotocol/andromeda-core/pull/591)
- CW20 support for kernel routing [(#723)](https://github.com/andromedaprotocol/andromeda-core/pull/723)

### Changed
Expand Down
39 changes: 39 additions & 0 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,5 @@ cw-multi-test = { version = "1.0.0", features = ["cosmwasm_1_2"] }
serde = { version = "1.0.215" }
test-case = { version = "3.3.1" }
cw-orch = "=0.24.1"
jsonschema-valid = { version = "0.5.2"}
serde_json = { version = "1.0.128" }
4 changes: 4 additions & 0 deletions contracts/data-storage/andromeda-form/.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"
45 changes: 45 additions & 0 deletions contracts/data-storage/andromeda-form/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
[package]
name = "andromeda-form"
version = "0.1.0-beta"
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 }
cw-json = { git = "https://github.com/SlayerAnsh/cw-json.git" }
serde_json = { workspace = true }
serde = { workspace = true }
test-case = { workspace = true }

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


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

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

use andromeda_data_storage::form::{ExecuteMsg, InstantiateMsg, QueryMsg};
use andromeda_std::{
ado_base::{
permissioning::{LocalPermission, Permission},
InstantiateMsg as BaseInstantiateMsg, MigrateMsg,
},
ado_contract::ADOContract,
common::{
context::ExecuteContext, encode_binary, expiration::get_and_validate_start_time,
Milliseconds,
},
error::ContractError,
};

use crate::execute::{handle_execute, milliseconds_from_expiration};
use crate::query::{
get_all_submissions, get_form_status, get_schema, get_submission, get_submission_ids,
};
use crate::state::{Config, CONFIG, SCHEMA_ADO_ADDRESS, SUBMISSION_ID};

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

pub const SUBMIT_FORM_ACTION: &str = "submit_form";

#[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.clone(),
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,
},
)?;

let schema_ado_address = msg.schema_ado_address;
schema_ado_address.validate(deps.api)?;

SCHEMA_ADO_ADDRESS.save(deps.storage, &schema_ado_address)?;
SUBMISSION_ID.save(deps.storage, &Uint64::zero())?;

let start_time = match msg.form_config.start_time {
Some(start_time) => Some(milliseconds_from_expiration(
get_and_validate_start_time(&env.clone(), Some(start_time))?.0,
)?),
None => None,
};
let end_time = match msg.form_config.end_time {
Some(end_time) => {
let time_res = get_and_validate_start_time(&env.clone(), Some(end_time));
if time_res.is_ok() {
Some(milliseconds_from_expiration(time_res.unwrap().0)?)
} else {
let current_time = Milliseconds::from_nanos(env.block.time.nanos()).milliseconds();
let current_height = env.block.height;
return Err(ContractError::CustomError {
msg: format!(
"End time in the past. current_time {:?}, current_block {:?}",
current_time, current_height
),
});
}
}
None => None,
};

if let (Some(start_time), Some(end_time)) = (start_time, end_time) {
ensure!(
end_time.gt(&start_time),
ContractError::StartTimeAfterEndTime {}
);
}

let allow_multiple_submissions = msg.form_config.allow_multiple_submissions;
let allow_edit_submission = msg.form_config.allow_edit_submission;

let config = Config {
start_time,
end_time,
allow_multiple_submissions,
allow_edit_submission,
};

CONFIG.save(deps.storage, &config)?;

if let Some(authorized_addresses_for_submission) = msg.authorized_addresses_for_submission {
if !authorized_addresses_for_submission.is_empty() {
ADOContract::default().permission_action(SUBMIT_FORM_ACTION, deps.storage)?;
}

for address in authorized_addresses_for_submission {
let addr = address.get_raw_address(&deps.as_ref())?;
ADOContract::set_permission(
deps.storage,
SUBMIT_FORM_ACTION,
addr,
Permission::Local(LocalPermission::Whitelisted(None)),
)?;
}
}

let mut response = resp.add_event(Event::new("form_instantiated"));

if let Some(custom_key) = msg.custom_key_for_notifications {
response = response.add_event(
cosmwasm_std::Event::new("custom_key")
.add_attribute("custom_key", custom_key)
.add_attribute("notification_service", "Telegram"),
);
}

Ok(response)
}

#[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::GetSchema {} => encode_binary(&get_schema(deps)?),
QueryMsg::GetAllSubmissions {} => encode_binary(&get_all_submissions(deps.storage)?),
QueryMsg::GetSubmission {
submission_id,
wallet_address,
} => encode_binary(&get_submission(deps, submission_id, wallet_address)?),
QueryMsg::GetSubmissionIds { wallet_address } => {
encode_binary(&get_submission_ids(deps, wallet_address)?)
}
QueryMsg::GetFormStatus {} => encode_binary(&get_form_status(deps.storage, env)?),
_ => 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)
}

#[cfg_attr(not(feature = "library"), entry_point)]
pub fn reply(_deps: DepsMut, _env: Env, msg: Reply) -> Result<Response, ContractError> {
if msg.result.is_err() {
return Err(ContractError::Std(StdError::generic_err(
msg.result.unwrap_err(),
)));
}

Ok(Response::default())
}
Loading

0 comments on commit 6df4bed

Please sign in to comment.