Skip to content
This repository has been archived by the owner on Dec 29, 2023. It is now read-only.

Commit

Permalink
Feat/import financial (#58)
Browse files Browse the repository at this point in the history
* chore: add execution functions

* chore: fix cargo conflict

* chore: fix cargo conflict

* chore: add execution function financial smart contract

* chore: add broker address param

* chore: fix conflict with main

* chore: fix conflict

* chore: fix conflict

* chore: fix conflict with main

* fix: address in close margin position

* chore: update elys binding reference

* chore: update cargo lock

* Update Cargo.toml

---------

Co-authored-by: Cosmic Vagabond <[email protected]>
  • Loading branch information
kenta-elys and cosmic-vagabond authored Dec 3, 2023
1 parent f50b3cc commit 0301b08
Show file tree
Hide file tree
Showing 15 changed files with 267 additions and 12 deletions.
12 changes: 6 additions & 6 deletions Cargo.lock

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

8 changes: 4 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "trade_shield_contract"
version = "0.8.0"
version = "0.9.0"
edition = "2021"

[lib]
Expand All @@ -14,13 +14,13 @@ thiserror = "1"
schemars = "0.8.1"
cosmwasm-schema = "1.1.4"
cw-utils = "0.13"
elys-bindings = { version = "0.5.0", git = "https://github.com/elys-network/bindings", tag = "v0.5.0" }
elys-bindings = { version = "0.6.0", git = "https://github.com/elys-network/bindings", tag = "v0.6.0" }

[dev-dependencies]

cw-multi-test = "0.13.4"
serde_json = "1.0.107"
elys-bindings = { version = "0.5.0", git = "https://github.com/elys-network/bindings", tag = "v0.5.0", features = [
elys-bindings = { version = "0.6.0", git = "https://github.com/elys-network/bindings", tag = "v0.6.0", features = [
"testing",
] }
elys-bindings-test = { version = "0.5.0", git = "https://github.com/elys-network/bindings", tag = "v0.5.0" }
elys-bindings-test = { version = "0.6.0", git = "https://github.com/elys-network/bindings", tag = "v0.6.0" }
19 changes: 19 additions & 0 deletions src/action/execute/claim_rewards_request.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
use super::*;
use elys_bindings::types::EarnType;

pub fn claim_rewards_request(
env: Env,
info: MessageInfo,
_deps: DepsMut<ElysQuery>,
withdraw_type: EarnType,
) -> Result<Response<ElysMsg>, ContractError> {
let msg = ElysMsg::withdraw_rewards(
env.contract.address.into_string(),
info.sender.into_string(),
withdraw_type,
);

let resp = Response::new().add_message(msg);

Ok(resp)
}
18 changes: 18 additions & 0 deletions src/action/execute/claim_validator_commission_request.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
use super::*;
// delegator_address, validator_address, denom
pub fn claim_validator_commission_request(
env: Env,
info: MessageInfo,
_deps: DepsMut<ElysQuery>,
validator_address: String,
) -> Result<Response<ElysMsg>, ContractError> {
let msg = ElysMsg::withdraw_validator_commissions(
env.contract.address.into_string(),
info.sender.into_string(),
validator_address,
);

let resp = Response::new().add_message(msg);

Ok(resp)
}
2 changes: 1 addition & 1 deletion src/action/execute/close_margin_position.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ pub fn close_margin_position(
env: Env,
id: u64,
) -> Result<Response<ElysMsg>, ContractError> {
let msg = ElysMsg::margin_broker_close_position(info.sender, id, env.contract.address);
let msg = ElysMsg::margin_broker_close_position(env.contract.address, id, info.sender);

let resp = Response::new().add_message(CosmosMsg::Custom(msg));

Expand Down
20 changes: 20 additions & 0 deletions src/action/execute/eden_cancel_vest_request.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
use super::*;
use cosmwasm_std::Int128;

pub fn eden_cancel_vest_request(
env: Env,
info: MessageInfo,
_deps: DepsMut<ElysQuery>,
amount: u64,
) -> Result<Response<ElysMsg>, ContractError> {
let msg = ElysMsg::eden_cancel_vesting(
env.contract.address.into_string(),
info.sender.into_string(),
Int128::from(amount),
"ueden".to_string(),
);

let resp = Response::new().add_message(msg);

Ok(resp)
}
20 changes: 20 additions & 0 deletions src/action/execute/eden_vest_request.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
use super::*;
use cosmwasm_std::Int128;

pub fn eden_vest_request(
env: Env,
info: MessageInfo,
_deps: DepsMut<ElysQuery>,
amount: u64,
) -> Result<Response<ElysMsg>, ContractError> {
let msg: ElysMsg = ElysMsg::eden_vesting(
env.contract.address.into_string(),
info.sender.into_string(),
Int128::from(amount),
"ueden".to_string(),
);

let resp = Response::new().add_message(msg);

Ok(resp)
}
23 changes: 23 additions & 0 deletions src/action/execute/elys_cancel_unstake_request.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
use super::*;
use cosmwasm_std::Coin;

pub fn elys_cancel_unstake_request(
env: Env,
info: MessageInfo,
_deps: DepsMut<ElysQuery>,
validator_address: String,
amount: Coin,
creation_height: i64,
) -> Result<Response<ElysMsg>, ContractError> {
let msg = ElysMsg::cancel_unbonding(
env.contract.address.into_string(),
info.sender.into_string(),
validator_address,
amount,
creation_height,
);

let resp = Response::new().add_message(msg);

Ok(resp)
}
27 changes: 27 additions & 0 deletions src/action/execute/elys_redelegation_request.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
use super::*;
use cosmwasm_std::Coin;

pub fn elys_redelegation_request(
env: Env,
info: MessageInfo,
_deps: DepsMut<ElysQuery>,
// the amount to be staked in base denomination.
validator_src_address: String,
// The asset to be staked
validator_dst_address: String,
// The validator Address is required only if the staked asset is
// uelys.
amount: Coin
) -> Result<Response<ElysMsg>, ContractError> {
let msg = ElysMsg::begin_redelegate(
env.contract.address.into_string(),
info.sender.into_string(),
validator_src_address,
validator_dst_address,
amount,
);

let resp = Response::new().add_message(msg);

Ok(resp)
}
35 changes: 35 additions & 0 deletions src/action/execute/stake_request.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
use super::*;
use cosmwasm_std::Int128;

pub fn stake_request(
env: Env,
info: MessageInfo,
deps: DepsMut<ElysQuery>,
// the amount to be staked in base denomination.
amount: u64,
// The asset to be staked
asset: String,
// The validator Address is required only if the staked asset is
// uelys.
validator_address: Option<String>
) -> Result<Response<ElysMsg>, ContractError> {
let querier = ElysQuerier::new(&deps.querier);
let address = info.sender.into_string();
let balance = querier.get_balance(address.to_owned(), asset.to_owned())?;
let token_amount: u128 = balance.amount.into();
if token_amount < amount as u128 {
return Err(ContractError::InsufficientBalanceError { balance: balance.amount.into(), amount: amount });
}

let msg = ElysMsg::stake_token(
env.contract.address.into_string(),
address,
Int128::from(amount),
asset,
validator_address,
);

let resp = Response::new().add_message(msg);

Ok(resp)
}
27 changes: 27 additions & 0 deletions src/action/execute/unstake_request.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
use super::*;
use cosmwasm_std::Int128;

pub fn unstake_request(
env: Env,
info: MessageInfo,
_deps: DepsMut<ElysQuery>,
// the amount to be staked in base denomination.
amount: u64,
// The asset to be staked
asset: String,
// The validator Address is required only if the staked asset is
// uelys.
validator_address: Option<String>
) -> Result<Response<ElysMsg>, ContractError> {
let msg = ElysMsg::unstake_token(
env.contract.address.into_string(),
info.sender.into_string(),
Int128::from(amount),
asset,
validator_address,
);

let resp = Response::new().add_message(msg);

Ok(resp)
}
20 changes: 20 additions & 0 deletions src/action/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,17 @@ pub mod execute {
mod close_margin_position;
mod create_margin_order;
mod create_spot_order;

mod stake_request;
mod unstake_request;

mod claim_rewards_request;
mod claim_validator_commission_request;
mod eden_cancel_vest_request;
mod eden_vest_request;
mod elys_cancel_unstake_request;
mod elys_redelegation_request;

use super::*;

pub use cancel_margin_order::cancel_margin_order;
Expand All @@ -48,6 +59,15 @@ pub mod execute {
pub use close_margin_position::close_margin_position;
pub use create_margin_order::create_margin_order;
pub use create_spot_order::create_spot_order;

pub use stake_request::stake_request;
pub use unstake_request::unstake_request;
pub use claim_rewards_request::claim_rewards_request;
pub use claim_validator_commission_request::claim_validator_commission_request;
pub use eden_cancel_vest_request::eden_cancel_vest_request;
pub use eden_vest_request::eden_vest_request;
pub use elys_cancel_unstake_request::elys_cancel_unstake_request;
pub use elys_redelegation_request::elys_redelegation_request;
}

pub mod reply {
Expand Down
9 changes: 9 additions & 0 deletions src/entry_point/execute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,5 +60,14 @@ pub fn execute(
order_type,
} => cancel_margin_orders(info, deps, order_ids, owner_address, order_type),
CloseMarginPosition { id } => close_margin_position(info, env, id),

StakeRequest { amount, asset, validator_address } => stake_request(env, info, deps, amount, asset, validator_address),
UnstakeRequest { amount, asset, validator_address } => unstake_request(env, info, deps, amount, asset, validator_address),
ElysRedelegateRequest { validator_src_address, validator_dst_address, amount} => elys_redelegation_request(env, info, deps, validator_src_address, validator_dst_address, amount),
ElysCancelUnstakeRequest { validator_address, amount, creation_height } => elys_cancel_unstake_request(env, info, deps, validator_address, amount, creation_height),
EdenVestRequest { amount} => eden_vest_request(env, info, deps, amount),
EdenCancelVestRequest { amount } => eden_cancel_vest_request(env, info, deps, amount),
ClaimRewardsRequest { withdraw_type } => claim_rewards_request(env, info, deps, withdraw_type),
ClaimValidatorCommissionRequest { validator_address } => claim_validator_commission_request(env, info, deps, validator_address),
}
}
2 changes: 2 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,6 @@ pub enum ContractError {
Leverage,
#[error("cannot cancel order: {order_id}, status: {status:?}")]
CancelStatusError { order_id: u64, status: Status },
#[error("{balance} is smaller than {amount}")]
InsufficientBalanceError {balance: u128, amount: u64},
}
37 changes: 36 additions & 1 deletion src/msg/execute_msg.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::types::{MarginOrderType, MarginPosition, OrderPrice, SpotOrderType};
use cosmwasm_schema::cw_serde;
use cosmwasm_std::Decimal;
use cosmwasm_std::{Decimal, Coin};
use elys_bindings::types::EarnType;

#[cw_serde]
pub enum ExecuteMsg {
Expand Down Expand Up @@ -40,4 +41,38 @@ pub enum ExecuteMsg {
CloseMarginPosition {
id: u64,
},
StakeRequest {
amount: u64,
asset: String,
validator_address: Option<String>,
},
UnstakeRequest {
amount: u64,
asset: String,
validator_address: Option<String>,
},
ElysRedelegateRequest {
validator_src_address: String,
validator_dst_address: String,
amount: Coin,
},
ElysCancelUnstakeRequest {
validator_address: String,
// amount is always less than or equal to unbonding delegation entry balance
amount: Coin,
// creation_height is the height which the unbonding took place.
creation_height: i64,
},
EdenVestRequest {
amount: u64,
},
EdenCancelVestRequest {
amount: u64,
},
ClaimRewardsRequest {
withdraw_type: EarnType,
},
ClaimValidatorCommissionRequest {
validator_address: String,
}
}

0 comments on commit 0301b08

Please sign in to comment.