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

Schema and Form ADO #591

Open
wants to merge 20 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 7 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Include ADOBase Version in Schema [(#574)](https://github.com/andromedaprotocol/andromeda-core/pull/574)
- Added Kernel ICS20 Transfer with Optional ExecuteMsg [(#577)](https://github.com/andromedaprotocol/andromeda-core/pull/577)
- Added IBC Denom Wrap/Unwrap [(#579)](https://github.com/andromedaprotocol/andromeda-core/pull/579)
- Schema and Form ADO [(#591)](https://github.com/andromedaprotocol/andromeda-core/pull/591)

### Changed

Expand Down
83 changes: 81 additions & 2 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 @@ -58,3 +58,5 @@ cw-multi-test = { version = "1.0.0", features = ["cosmwasm_1_2"] }
serde = { version = "1.0.127" }
test-case = { version = "3.3.1" }
cw-orch = "=0.24.1"
jsonschema-valid = { version = "0.5.2"}
serde_json = { version = "1.0.128" }
mdjakovic0920 marked this conversation as resolved.
Show resolved Hide resolved
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"
44 changes: 44 additions & 0 deletions contracts/data-storage/andromeda-form/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
[package]
name = "andromeda-form"
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 }
cw-json = { git = "https://github.com/SlayerAnsh/cw-json.git" }
mdjakovic0920 marked this conversation as resolved.
Show resolved Hide resolved
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-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,

}
}
162 changes: 162 additions & 0 deletions contracts/data-storage/andromeda-form/src/contract.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
#[cfg(not(feature = "library"))]
use cosmwasm_std::entry_point;
use cosmwasm_std::{ensure, Binary, Deps, DepsMut, Env, Event, MessageInfo, Response, 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};
use crate::state::{
ALLOW_EDIT_SUBMISSION, ALLOW_MULTIPLE_SUBMISSIONS, END_TIME, SCHEMA_ADO_ADDRESS, START_TIME,
SUBMISSION_ID,
};

// version info for migration info
const CONTRACT_NAME: &str = "crates.io:andromeda-form";
const CONTRACT_VERSION: &str = env!("CARGO_PKG_VERSION");
mdjakovic0920 marked this conversation as resolved.
Show resolved Hide resolved

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(),
mdjakovic0920 marked this conversation as resolved.
Show resolved Hide resolved
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
),
});
}
mdjakovic0920 marked this conversation as resolved.
Show resolved Hide resolved
}
None => None,
};
mdjakovic0920 marked this conversation as resolved.
Show resolved Hide resolved

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

START_TIME.save(deps.storage, &start_time)?;
END_TIME.save(deps.storage, &end_time)?;

let allow_multiple_submissions = msg.form_config.allow_multiple_submissions;
ALLOW_MULTIPLE_SUBMISSIONS.save(deps.storage, &allow_multiple_submissions)?;

let allow_edit_submission = msg.form_config.allow_edit_submission;
ALLOW_EDIT_SUBMISSION.save(deps.storage, &allow_edit_submission)?;

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::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)
}
Loading
Loading