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

Refactor threshold #105

Merged
merged 11 commits into from
Dec 6, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 2 additions & 0 deletions artifacts/checksums.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
2f392abdc3919f347459b76d7ce3ae3be8fd71e344f56371e5d8c0a4f32bb6dd streamswap_controller.wasm
defca0f7926f953d7d64bd71cc925f98f1faeb767dcc74544571781614238447 streamswap_stream.wasm
Binary file added artifacts/streamswap_controller.wasm
Binary file not shown.
Binary file added artifacts/streamswap_stream.wasm
Binary file not shown.
156 changes: 17 additions & 139 deletions contracts/stream/src/circuit_ops.rs
Original file line number Diff line number Diff line change
@@ -1,76 +1,11 @@
use crate::helpers::build_u128_bank_send_msg;
use crate::pool::pool_refund;
use crate::state::{CONTROLLER_PARAMS, POSITIONS, POST_STREAM, STREAM_INFO, STREAM_STATE};
use crate::state::{CONTROLLER_PARAMS, POST_STREAM, STREAM_INFO, STREAM_STATE};
use crate::stream::{sync_stream, sync_stream_status};
use crate::ContractError;
use cosmwasm_std::{attr, BankMsg, CosmosMsg, DepsMut, Env, MessageInfo, Response, Timestamp};
use cosmwasm_std::{BankMsg, CosmosMsg, DepsMut, Env, MessageInfo, Response};
use cw_utils::NativeBalance;
use streamswap_types::controller::Params;
use streamswap_types::stream::ThresholdState;
use streamswap_types::stream::{Status, ThresholdError};

pub fn execute_exit_cancelled(
deps: DepsMut,
env: Env,
info: MessageInfo,
) -> Result<Response, ContractError> {
let mut stream = STREAM_STATE.load(deps.storage)?;

let mut position = POSITIONS.load(deps.storage, &info.sender)?;
if position.owner != info.sender {
return Err(ContractError::Unauthorized {});
}
if position.exit_date != Timestamp::from_seconds(0) {
return Err(ContractError::SubscriberAlreadyExited {});
}

sync_stream_status(&mut stream, env.block.time);

// This execution requires the stream to be cancelled or
// the stream to be ended and the threshold not reached.
// If any of other condition fails return not cancelled error.
if !stream.is_cancelled() {
let threshold_state = ThresholdState::new();
// Threshold should be set
let is_set = threshold_state.check_if_threshold_set(deps.storage)?;
if !is_set {
return Err(ContractError::StreamNotCancelled {});
}

// Stream should be ended
if !stream.is_ended() {
return Err(ContractError::StreamNotCancelled {});
}
// Update stream before checking threshold
sync_stream(&mut stream, env.block.time);
threshold_state.error_if_reached(deps.storage, &stream)?;
}

// no need to sync position here, we just need to return total balance
let total_balance = position.in_balance + position.spent;
// sync position exit date
position.exit_date = env.block.time;
position.last_updated = env.block.time;
POSITIONS.save(deps.storage, &position.owner, &position)?;

let send_msg = build_u128_bank_send_msg(
stream.in_denom.clone(),
info.sender.to_string(),
total_balance,
)?;
let attributes = vec![
attr("action", "exit_cancelled"),
attr("to_address", info.sender.to_string()),
attr("total_balance", total_balance),
attr("exit_date", position.exit_date.to_string()),
attr("last_updated", position.last_updated.to_string()),
];
// send funds to the sender
let res = Response::new()
.add_message(send_msg)
.add_attributes(attributes);

Ok(res)
}
use streamswap_types::stream::Status;

pub fn execute_cancel_stream(
deps: DepsMut,
Expand All @@ -96,7 +31,7 @@ pub fn execute_cancel_stream(
STREAM_STATE.save(deps.storage, &stream)?;

// Refund all out tokens to stream creator(treasury)
let mut refund_coins = vec![stream.out_asset.clone()];
let mut refund_coins = NativeBalance::default() + stream.out_asset.clone();

// refund pool creation if any
let post_stream_ops = POST_STREAM.may_load(deps.storage)?;
Expand All @@ -106,11 +41,15 @@ pub fn execute_cancel_stream(
post_stream_ops.pool_config,
stream.out_asset.denom.clone(),
)?;
refund_coins.extend(pool_refund_coins);
for coin in pool_refund_coins {
refund_coins += coin;
}
}

refund_coins.normalize();
let stream_info = STREAM_INFO.load(deps.storage)?;
let funds_msgs: Vec<CosmosMsg> = refund_coins
.into_vec()
.iter()
.map(|coin| {
CosmosMsg::Bank(BankMsg::Send {
Expand All @@ -125,72 +64,6 @@ pub fn execute_cancel_stream(
.add_attribute("status", "cancelled")
.add_messages(funds_msgs))
}

pub fn execute_cancel_stream_with_threshold(
deps: DepsMut,
env: Env,
info: MessageInfo,
) -> Result<Response, ContractError> {
let mut stream = STREAM_STATE.load(deps.storage)?;
let stream_info = STREAM_INFO.load(deps.storage)?;
// Only stream creator can cancel the stream with threshold not reached
if info.sender != stream_info.stream_admin {
return Err(ContractError::Unauthorized {});
}
sync_stream_status(&mut stream, env.block.time);
// Stream should not be cancelled of finalized, should be ended.
// Creator should not able to finalize the stream with threshold not reached but only cancel it.
if !stream.is_ended() {
return Err(ContractError::OperationNotAllowed {
current_status: stream.status_info.status.to_string(),
});
}

sync_stream(&mut stream, env.block.time);

let threshold_state = ThresholdState::new();

if !threshold_state.check_if_threshold_set(deps.storage)? {
return Err(ContractError::ThresholdError(
ThresholdError::ThresholdNotSet {},
));
}
// Threshold should not be reached
threshold_state.error_if_reached(deps.storage, &stream)?;

stream.status_info.status = Status::Cancelled;

STREAM_STATE.save(deps.storage, &stream)?;

// Refund all out tokens to stream creator(treasury)
let mut refund_coins = vec![stream.out_asset.clone()];

// refund pool creation if any
let post_stream_ops = POST_STREAM.may_load(deps.storage)?;
if let Some(post_stream_ops) = post_stream_ops {
let pool_refund_coins = pool_refund(
&deps,
post_stream_ops.pool_config,
stream.out_asset.denom.clone(),
)?;
refund_coins.extend(pool_refund_coins);
}

let funds_msgs: Vec<CosmosMsg> = refund_coins
.iter()
.map(|coin| {
CosmosMsg::Bank(BankMsg::Send {
to_address: stream_info.treasury.to_string(),
amount: vec![coin.clone()],
})
})
.collect();

Ok(Response::new()
.add_attribute("action", "cancel_stream")
.add_messages(funds_msgs)
.add_attribute("status", "cancelled"))
}
pub fn execute_stream_admin_cancel(
deps: DepsMut,
env: Env,
Expand All @@ -216,7 +89,7 @@ pub fn execute_stream_admin_cancel(
STREAM_STATE.save(deps.storage, &stream)?;

// Refund all out tokens to stream creator(treasury)
let mut refund_coins = vec![stream.out_asset.clone()];
let mut refund_coins = NativeBalance::default() + stream.out_asset.clone();

// refund pool creation if any
let post_stream_ops = POST_STREAM.may_load(deps.storage)?;
Expand All @@ -226,10 +99,15 @@ pub fn execute_stream_admin_cancel(
post_stream_ops.pool_config,
stream.out_asset.denom.clone(),
)?;
refund_coins.extend(pool_refund_coins);
for coin in pool_refund_coins {
refund_coins += coin;
}
}

refund_coins.normalize();

let funds_msgs: Vec<CosmosMsg> = refund_coins
.into_vec()
.iter()
.map(|coin| {
CosmosMsg::Bank(BankMsg::Send {
Expand Down
Loading
Loading