Skip to content

Commit

Permalink
V2 init
Browse files Browse the repository at this point in the history
  • Loading branch information
Rhaki committed Dec 12, 2023
1 parent 5415140 commit c5147a6
Show file tree
Hide file tree
Showing 27 changed files with 546 additions and 149 deletions.
38 changes: 21 additions & 17 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,24 +1,28 @@
[workspace]
members = ["package", "contracts/otcer", "tests", "scripts"]
members = ["package", "contracts/otcer", "tests", "scripts", "contracts/register", "contracts/vesting-account"]

[workspace.package]
version = "0.1.0"
edition = "2021"
authors = ["Rhaki"]
version = "0.1.0"
edition = "2021"
authors = ["Rhaki"]


[workspace.dependencies]
cosmwasm-schema = "1.3.0"
cosmwasm-std = "1.3.0"
cw-storage-plus = "1.1.0"
cw2 = "1.0.1"
cw20 = "1.1.2"
cw721 = "0.18.0"
schemars = "0.8.10"
serde = { version = "1.0.145", default-features = false, features = ["derive"] }
thiserror = "1.0.31"
rhaki-cw-plus = "0.6.12"
otcer-pkg = { path = "./package", version = "0.1.0"}
otcer = { path = "./contracts/otcer", version = "0.1.0"}
cosmwasm-schema = "1.3.0"
cosmwasm-std = "1.3.0"
cw-storage-plus = "1.1.0"
cw2 = "1.0.1"
cw20 = "1.1.2"
cw721 = "0.18.0"
rhaki-cw-plus = "0.7.0"
schemars = "0.8.10"
serde = { version = "1.0.145", default-features = false, features = ["derive"] }
thiserror = "1.0.31"
variable-provider-pkg = "0.2.2"

cw-multi-test = "0.16.5"
otcer-pkg = { path = "./package", version = "0.1.0"}
otcer-core = { path = "./contracts/otcer", version = "0.1.0"}
otcer-register = { path = "./contracts/register", version = "0.1.0"}
otcer-vesting-account = { path = "./contracts/vesting-account", version = "0.1.0"}

cw-multi-test = { version = "0.20.0", features = ["cosmwasm_1_2"]}
56 changes: 28 additions & 28 deletions contracts/otcer/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,38 +1,38 @@
[package]
name = "otcer"
version = { workspace = true }
authors = { workspace = true }
edition = { workspace = true }
exclude = ["contract.wasm", "hash.txt"]
name = "otcer-core"
version = { workspace = true }
authors = { workspace = true }
edition = { workspace = true }
exclude = ["contract.wasm", "hash.txt"]

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

[profile.release]
opt-level = 3
debug = false
rpath = false
lto = true
debug-assertions = false
codegen-units = 1
panic = 'abort'
incremental = false
overflow-checks = true
opt-level = 3
debug = false
rpath = false
lto = true
debug-assertions = false
codegen-units = 1
panic = 'abort'
incremental = false
overflow-checks = true

[features]
backtraces = ["cosmwasm-std/backtraces"]
library = []
backtraces = ["cosmwasm-std/backtraces"]
library = []

[dependencies]
cosmwasm-schema = { workspace = true }
cosmwasm-std = { workspace = true }
cw-storage-plus = { workspace = true }
cw2 = { workspace = true }
schemars = { workspace = true }
serde = { workspace = true }
thiserror = { workspace = true }
rhaki-cw-plus = { workspace = true }
otcer-pkg = { workspace = true }
cosmwasm-schema = { workspace = true }
cosmwasm-std = { workspace = true }
cw-storage-plus = { workspace = true }
cw2 = { workspace = true }
rhaki-cw-plus = { workspace = true }
schemars = { workspace = true }
serde = { workspace = true }
thiserror = { workspace = true }
variable-provider-pkg = { workspace = true }

otcer-pkg = { workspace = true }

[dev-dependencies]
cw-multi-test = { workspace = true }
93 changes: 85 additions & 8 deletions contracts/otcer/src/contract.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
use cosmwasm_std::{entry_point, Binary, Deps, DepsMut, Env, MessageInfo, Response, StdResult};
use cosmwasm_std::{
entry_point, Binary, Decimal, Deps, DepsMut, Env, MessageInfo, Response, StdResult, WasmMsg,
};

use otcer_pkg::otcer::{
definitions::Config,
msgs::{ExecuteMsg, InstantiateMsg, MigrateMsg, QueryMsg},
};
use rhaki_cw_plus::traits::{IntoAddr, IntoBinaryResult};
use rhaki_cw_plus::{
traits::{IntoAddr, IntoBinary, IntoBinaryResult},
wasm::WasmMsgBuilder,
};
use variable_provider_pkg::{definitions::Variable, msgs::RegisterVariableMsg};

use crate::{
execute::{run_cancel_otc, run_claim_otc, run_create_otc, run_execute_otc},
Expand All @@ -16,21 +22,92 @@ use crate::{
#[entry_point]
pub fn instantiate(
deps: DepsMut,
_env: Env,
env: Env,
_info: MessageInfo,
msg: InstantiateMsg,
) -> ContractResponse {
let config = Config::new(
// Asserts
if msg.performance_fee.is_zero() || msg.performance_fee > Decimal::one() {
return Err(crate::response::ContractError::InvalidPerformanceFee {
fee: msg.performance_fee,
});
}

// Init variable provider
let (msg_init_vp, variable_provider_addr) = rhaki_cw_plus::wasm::build_instantiate_2(
deps.as_ref(),
msg.owner.clone().into_addr(deps.api)?,
msg.fee,
msg.fee_collector.into_addr(deps.api)?,
&env.contract.address,
"variable_provider".to_string().into_binary()?,
Some(msg.owner.to_string()),
msg.code_id_variable_provider,
variable_provider_pkg::msgs::InstantiateMsg {
owners: vec![env.contract.address.to_string(), msg.owner.clone()],
},
vec![],
"Otcer variable provider".to_string(),
)?;

// Init register
let (msg_init_register, register_addr) = rhaki_cw_plus::wasm::build_instantiate_2(
deps.as_ref(),
&env.contract.address,
"register".to_string().into_binary()?,
Some(msg.owner.to_string()),
msg.code_id_register,
otcer_pkg::register::msgs::InstantiateMsg {},
vec![],
"Otcer register".to_string(),
)?;

// Register fee_collector
let msg_register_fee_collector = WasmMsg::build_execute(
&variable_provider_addr,
variable_provider_pkg::msgs::ExecuteMsg::RegisterVariable(RegisterVariableMsg {
key: otcer_pkg::variable_provider::KEY_FEE_COLLECTOR_ADDR.to_string(),
value: Variable::Addr(msg.fee_collector.clone().into_addr(deps.api)?),
}),
vec![],
)?;

// Register performance fee
let msg_register_fees = WasmMsg::build_execute(
&variable_provider_addr,
variable_provider_pkg::msgs::ExecuteMsg::RegisterVariable(RegisterVariableMsg {
key: otcer_pkg::variable_provider::KEY_PERFORMANCE_FEE.to_string(),
value: Variable::Decimal(msg.performance_fee),
}),
vec![],
)?;

// Register register
let msg_register_register = WasmMsg::build_execute(
&variable_provider_addr,
variable_provider_pkg::msgs::ExecuteMsg::RegisterVariable(RegisterVariableMsg {
key: otcer_pkg::variable_provider::KEY_REGISTER_ADDR.to_string(),
value: Variable::Addr(register_addr.clone()),
}),
vec![],
)?;

let config = Config::new(
msg.owner.clone().into_addr(deps.api)?,
variable_provider_addr.clone(),
);

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

Ok(Response::new()
.add_attribute("action", "instantiate")
.add_attribute("owner", msg.owner))
.add_attribute("owner", msg.owner)
.add_attribute("variable_provider", variable_provider_addr)
.add_attribute("register", register_addr)
.add_attribute("fee_collector", msg.fee_collector)
.add_attribute("performance_fee", msg.performance_fee.to_string())
.add_message(msg_init_vp)
.add_message(msg_init_register)
.add_message(msg_register_fee_collector)
.add_message(msg_register_fees)
.add_message(msg_register_register))
}

#[entry_point]
Expand Down
23 changes: 20 additions & 3 deletions contracts/otcer/src/execute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ pub fn run_create_otc(
let mut config = CONFIG.load(deps.storage)?;
config.counter_otc += 1;

let position = OtcPosition::from_create_otc_msg(
let mut position = OtcPosition::from_create_otc_msg(
deps.as_ref(),
&env,
msg,
Expand All @@ -32,7 +32,16 @@ pub fn run_create_otc(
let (msgs_deposit, remaining_coins) =
collect_otc_items(&env, &position.offer, info.sender, info.funds)?;

let msgs_fee = send_fee(&env, &config.fee, &config.fee_collector, remaining_coins)?;
if !remaining_coins.is_empty() {
return Err(ContractError::ExtraCoinReceived);
}

let msgs_fee = send_fee(
deps.as_ref(),
&env,
&mut position.offer,
&config.variable_provider,
)?;

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

Expand Down Expand Up @@ -65,7 +74,15 @@ pub fn run_execute_otc(
let (msgs_deposit, remaining_coins) =
collect_otc_items(&env, &position.ask, info.sender, info.funds)?;

let msgs_fee = send_fee(&env, &config.fee, &config.fee_collector, remaining_coins)?;
let msgs_fee = send_fee(
deps.as_ref(),
&env,
&mut position.ask,
&config.variable_provider,
)?;
if !remaining_coins.is_empty() {
return Err(ContractError::ExtraCoinReceived);
}

let msgs_to_owner = send_otc_items(&env, &mut position.ask, &position.status, &position.owner)?;
let msgs_to_executor = send_otc_items(
Expand Down
41 changes: 34 additions & 7 deletions contracts/otcer/src/functions.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
use cosmwasm_std::{
attr, Addr, Attribute, Coin, CosmosMsg, DepsMut, Env, StdError, StdResult, Uint128,
attr, Addr, Attribute, Coin, CosmosMsg, Decimal, Deps, DepsMut, Env, StdError, StdResult,
Uint128,
};
use otcer_pkg::{
otcer::definitions::{OtcItem, OtcItemInfo, OtcPosition, OtcPositionStatus},
variable_provider::vp_get_fee_and_collector,
};
use otcer_pkg::otcer::definitions::{OtcItem, OtcItemInfo, OtcPosition, OtcPositionStatus};

use crate::state::positions;

Expand Down Expand Up @@ -52,13 +56,36 @@ pub fn send_otc_items(
}

pub fn send_fee(
deps: Deps,
env: &Env,
items_info: &Vec<OtcItemInfo>,
fee_collector: &Addr,
funds: Vec<Coin>,
items_info: &mut Vec<OtcItem>,
vp_addr: &Addr,
) -> StdResult<Vec<CosmosMsg>> {
assert_received_funds(items_info, funds)?;
build_send_otc_info_items(env, items_info, fee_collector)
let (fee, collector) = vp_get_fee_and_collector(deps, vp_addr)?;

if fee > Decimal::zero() {
items_info
.iter_mut()
.filter_map(|val| {
let fee_amount = val.item_info.get_elegible_fee_amount() * fee;

if fee_amount > Uint128::zero() {
val.item_info.subtract_fee_amount(fee_amount).unwrap();

Some(val.item_info.build_send_msg(
env,
&env.contract.address,
&collector,
Some(fee_amount),
))
} else {
None
}
})
.collect()
} else {
Ok(vec![])
}
}

pub fn cancel_otc(env: &Env, position: &OtcPosition) -> StdResult<Vec<CosmosMsg>> {
Expand Down
10 changes: 8 additions & 2 deletions contracts/otcer/src/response.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use cosmwasm_std::{Response, StdError};
use cosmwasm_std::{Decimal, Response, StdError};
use thiserror::Error;

pub type ContractResponse = Result<Response, ContractError>;
Expand All @@ -9,5 +9,11 @@ pub enum ContractError {
Std(#[from] StdError),

#[error("Unauthorized")]
Unauthorized {},
Unauthorized,

#[error("Invalid performance fee: {fee}")]
InvalidPerformanceFee { fee: Decimal },

#[error("Wrong coins sent during creating or executing otc")]
ExtraCoinReceived
}
38 changes: 38 additions & 0 deletions contracts/register/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
[package]
name = "otcer-register"
version = { workspace = true }
authors = { workspace = true }
edition = { workspace = true }
exclude = ["contract.wasm", "hash.txt"]

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

[profile.release]
opt-level = 3
debug = false
rpath = false
lto = true
debug-assertions = false
codegen-units = 1
panic = 'abort'
incremental = false
overflow-checks = true

[features]
backtraces = ["cosmwasm-std/backtraces"]
library = []

[dependencies]
cosmwasm-schema = { workspace = true }
cosmwasm-std = { workspace = true }
cw-storage-plus = { workspace = true }
cw2 = { workspace = true }
schemars = { workspace = true }
serde = { workspace = true }
thiserror = { workspace = true }
rhaki-cw-plus = { workspace = true }
otcer-pkg = { workspace = true }

[dev-dependencies]
cw-multi-test = { workspace = true }
Loading

0 comments on commit c5147a6

Please sign in to comment.