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

Commission renamed to capture #101

Merged
merged 3 commits into from
Sep 28, 2021
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
30 changes: 6 additions & 24 deletions contractlib/micro_mintlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,26 +36,6 @@ def __init__(self, label, native_asset, oracle, treasury=None,
super().__init__(contract, init_msg, label, admin, uploader, backend,
instantiated_contract=instantiated_contract, code_id=code_id)

# def migrate(self, label, code_id, code_hash):
# """
# Instantiate another mint contract and migrate this contracts info into that one
# :param label: Label name of the contract
# :param code_id: Code id of the contract
# :param code_hash: Code hash
# :return: new Mint
# """
# msg = json.dumps(
# {"migrate": {"label": label, "code_id": code_id, "code_hash": code_hash}})
#
# new_mint = copy.deepcopy(self)
# for attribute in self.execute(msg, compute=False)["logs"][0]["events"][0]["attributes"]:
# if attribute["key"] == "contract_address":
# new_mint.address = attribute["value"]
# break
# new_mint.contract_id = code_id
# new_mint.code_hash = code_hash
# return new_mint

def update_config(self, owner=None, native_asset=None, oracle=None):
"""
Updates the minting contract's config
Expand All @@ -67,12 +47,14 @@ def update_config(self, owner=None, native_asset=None, oracle=None):
raw_msg = {"update_config": {}}
if owner is not None:
raw_msg["update_config"]["owner"] = owner

if native_asset is not None:
contract = {
"address": native_asset.address,
"code_hash": native_asset.code_hash
}
raw_msg["update_config"]["native_asset"] = contract

if oracle is not None:
contract = {
"address": oracle.address,
Expand All @@ -83,17 +65,17 @@ def update_config(self, owner=None, native_asset=None, oracle=None):
msg = json.dumps(raw_msg)
return self.execute(msg)

def register_asset(self, snip20, commission=None):
def register_asset(self, snip20, capture=None):
"""
Registers a SNIP20 asset
:param snip20: SNIP20 object to add
:param commission: Comission for the SNIP20
:param capture: Comission for the SNIP20
:return: Result
"""
msg = {"register_asset": {"contract": {"address": snip20.address, "code_hash": snip20.code_hash}}}

if commission:
msg['register_asset']['commission'] = str(commission)
if capture:
msg['register_asset']['capture'] = str(capture)

return self.execute(json.dumps(msg))

Expand Down
4 changes: 2 additions & 2 deletions contracts/micro_mint/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,8 @@ pub fn handle<S: Storage, A: Api, Q: Querier>(
} => handle::try_update_limit(deps, env, start_epoch, epoch_frequency, epoch_limit),
HandleMsg::RegisterAsset {
contract,
commission,
} => handle::try_register_asset(deps, &env, &contract, commission),
capture,
} => handle::try_register_asset(deps, &env, &contract, capture),
HandleMsg::RemoveAsset {
address
} => handle::try_remove_asset(deps, &env, address),
Expand Down
42 changes: 25 additions & 17 deletions contracts/micro_mint/src/handle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,15 @@ use shade_protocol::{
generic_response::ResponseStatus,
};

use crate::state::{config_w, config_r, native_asset_r, asset_peg_r, assets_w, assets_r, asset_list_w, total_burned_w, limit_w, limit_r};
use crate::state::{
config_w, config_r,
native_asset_r,
asset_peg_r,
assets_w, assets_r,
asset_list_w,
total_burned_w,
limit_w
};

pub fn try_burn<S: Storage, A: Api, Q: Querier>(
deps: &mut Extern<S, A, Q>,
Expand Down Expand Up @@ -70,20 +78,20 @@ pub fn try_burn<S: Storage, A: Api, Q: Querier>(

let mut burn_amount = amount;
if let Some(treasury) = config.treasury {
// Ignore commission if the set commission is 0
if burn_asset.commission != Uint128(0) {
let commission_amount = calculate_commission(amount, burn_asset.commission);
// Ignore capture if the set capture is 0
if burn_asset.capture != Uint128(0) {
let capture_amount = calculate_capture(amount, burn_asset.capture);

// Commission to treasury
messages.push(send_msg(treasury.address,
commission_amount,
capture_amount,
None,
None,
1,
burn_asset.asset.contract.code_hash.clone(),
burn_asset.asset.contract.address.clone())?);

burn_amount = (amount - commission_amount)?;
burn_amount = (amount - capture_amount)?;
}
}

Expand Down Expand Up @@ -154,7 +162,7 @@ pub fn try_burn<S: Storage, A: Api, Q: Querier>(

limit.total_minted = new_total;

limit_storage.save(&limit);
limit_storage.save(&limit)?;
}

debug_print!("Minting: {} {}", amount_to_mint, &mint_asset.token_info.symbol);
Expand Down Expand Up @@ -275,7 +283,7 @@ pub fn try_register_asset<S: Storage, A: Api, Q: Querier>(
deps: &mut Extern<S, A, Q>,
env: &Env,
contract: &Contract,
commission: Option<Uint128>
capture: Option<Uint128>
) -> StdResult<HandleResponse> {

let config = config_r(&deps.storage).load()?;
Expand Down Expand Up @@ -308,8 +316,8 @@ pub fn try_register_asset<S: Storage, A: Api, Q: Querier>(
token_info: asset_info,
token_config: asset_config,
},
// If commission is not set then default to 0
commission: match commission {
// If capture is not set then default to 0
capture: match capture {
None => Uint128(0),
Some(value) => value
}
Expand Down Expand Up @@ -340,7 +348,7 @@ pub fn try_register_asset<S: Storage, A: Api, Q: Querier>(

pub fn try_remove_asset<S: Storage, A: Api, Q: Querier>(
deps: &mut Extern<S, A, Q>,
env: &Env,
_env: &Env,
address: HumanAddr
) -> StdResult<HandleResponse> {

Expand Down Expand Up @@ -440,16 +448,16 @@ pub fn calculate_mint(burn_price: Uint128, burn_amount: Uint128, burn_decimals:
}
}

pub fn calculate_commission(
amount: Uint128, commission: Uint128
pub fn calculate_capture(
amount: Uint128, capture: Uint128
) -> Uint128 {
/* amount: total amount sent to burn (uSSCRT/uSILK/uSHD)
* commission: commission_percent * 10,000 e.g. 532 = 5.32% = .0532
* capture: capture_percent * 10,000 e.g. 532 = 5.32% = .0532
*
* commission_amount = amount * commission / 10000
* capture_amount = amount * capture / 10000
*/

return amount.multiply_ratio(commission, 10000u128);
return amount.multiply_ratio(capture, 10000u128);
}

fn oracle<S: Storage, A: Api, Q: Querier>(
Expand All @@ -464,4 +472,4 @@ fn oracle<S: Storage, A: Api, Q: Querier>(
config.oracle.code_hash,
config.oracle.address)?;
Ok(answer.rate)
}
}
2 changes: 1 addition & 1 deletion contracts/micro_mint/src/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,4 @@ pub fn config<S: Storage, A: Api, Q: Querier>
pub fn limit<S: Storage, A: Api, Q: Querier>
(deps: &Extern<S, A, Q>) -> StdResult<QueryAnswer> {
Ok(QueryAnswer::MintLimit { limit: limit_r(&deps.storage).load()? })
}
}
22 changes: 11 additions & 11 deletions contracts/micro_mint/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ pub mod tests {
init, handle, query,
},
handle::{
calculate_commission,
calculate_capture,
calculate_mint,
try_burn,
},
Expand Down Expand Up @@ -60,7 +60,7 @@ pub mod tests {
}
}

fn dummy_init(admin: String, native_asset: Contract, oracle: Contract, peg: Option<String>, treasury: Option<Contract>, commission: Option<Uint128>) -> Extern<MockStorage, MockApi, MockQuerier> {
fn dummy_init(admin: String, native_asset: Contract, oracle: Contract, peg: Option<String>, treasury: Option<Contract>, capture: Option<Uint128>) -> Extern<MockStorage, MockApi, MockQuerier> {
let mut deps = mock_dependencies(20, &[]);
let msg = InitMsg {
admin: None,
Expand Down Expand Up @@ -90,7 +90,7 @@ pub mod tests {
peg: Option::from("TKN".to_string()),
treasury: Option::from(create_contract("", "")),
// 1%
commission: Option::from(Uint128(100)),
capture: Option::from(Uint128(100)),
};
let env = mock_env("creator", &coins(1000, "earth"));

Expand All @@ -106,28 +106,28 @@ pub mod tests {
let native_asset = create_contract("snip20", "hash");
let oracle = create_contract("oracle", "hash");
let treasury = create_contract("treasury", "hash");
let commission = Uint128(100);
let capture = Uint128(100);

let admin_env = mock_env("admin", &coins(1000, "earth"));
let mut deps = dummy_init("admin".to_string(),
native_asset,
oracle,
None,
Option::from(treasury),
Option::from(commission));
Option::from(capture));

// new config vars
let new_oracle = Option::from(create_contract("new_oracle", "hash"));
let new_treasury = Option::from(create_contract("new_treasury", "hash"));
let new_commission = Option::from(Uint128(200));
let new_capture = Option::from(Uint128(200));

// Update config
let update_msg = HandleMsg::UpdateConfig {
owner: None,
oracle: new_oracle.clone(),
treasury: new_treasury.clone(),
// 2%
commission: new_commission.clone(),
capture: new_capture.clone(),
};
let update_res = handle(&mut deps, admin_env, update_msg);

Expand All @@ -137,7 +137,7 @@ pub mod tests {
QueryAnswer::Config { config } => {
assert_eq!(config.oracle, new_oracle.unwrap());
assert_eq!(config.treasury, new_treasury);
assert_eq!(config.commission, new_commission);
assert_eq!(config.capture, new_capture);
}
_ => { panic!("Received wrong answer") }
}
Expand Down Expand Up @@ -371,12 +371,12 @@ pub mod tests {
*/

#[test]
fn commission_calc() {
fn capture_calc() {
let amount = Uint128(1_000_000_000_000_000_000);
//10%
let commission = Uint128(1000);
let capture = Uint128(1000);
let expected = Uint128(100_000_000_000_000_000);
let value = calculate_commission(amount, commission);
let value = calculate_capture(amount, capture);
assert_eq!(value, expected);
}
#[test]
Expand Down
5 changes: 1 addition & 4 deletions contracts/oracle/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,7 @@ use shade_protocol::{
band::ReferenceData,
};
use crate::{
state::{
config_w, config_r,
hard_coded_r, hard_coded_w,
},
state::{ config_w, hard_coded_w },
query, handle,
};

Expand Down
6 changes: 1 addition & 5 deletions contracts/oracle/src/handle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,7 @@ use shade_protocol::{
},
asset::Contract,
generic_response::ResponseStatus,
snip20::{
Snip20Asset,
token_config_query,
TokenConfig,
},
snip20::Snip20Asset,
secretswap::{
PairQuery,
PairResponse,
Expand Down
3 changes: 1 addition & 2 deletions contracts/oracle/src/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,14 @@ use cosmwasm_std::{
use secret_toolkit::utils::Query;
use shade_protocol::{
oracle::{
QueryMsg, QueryAnswer, SswapPair
QueryAnswer, SswapPair
},
band::{
BandQuery, ReferenceData,
},
secretswap::{
PairQuery,
SimulationResponse,
Simulation,
Asset,
AssetInfo,
Token,
Expand Down
3 changes: 1 addition & 2 deletions contracts/oracle/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
mod tests {
use crate::query;
use cosmwasm_std::testing::{mock_dependencies, mock_env, MockStorage, MockApi, MockQuerier};
use cosmwasm_std::{coins, from_binary, Uint128};
use shade_protocol::asset::Contract;
use cosmwasm_std::{Uint128};

macro_rules! normalize_price_tests {
($($name:ident: $value:expr,)*) => {
Expand Down
3 changes: 1 addition & 2 deletions packages/secretcli/src/secretcli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ fn vec_str_to_vec_string (str_in: Vec<&str>) -> Vec<String> {
str_out
}

///
/// Will run any scretcli command and return its output
///
/// # Arguments
Expand Down Expand Up @@ -329,4 +328,4 @@ pub trait TestQuery<Response: serde::de::DeserializeOwned>: serde::Serialize {
fn t_query(&self, contract: &NetContract) -> Result<Response> {
query_contract(contract, self)
}
}
}
4 changes: 2 additions & 2 deletions packages/shade_protocol/src/band.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use cosmwasm_std::{Uint128};
use serde::{Deserialize, Serialize};
use schemars::JsonSchema;
use secret_toolkit::utils::{Query, InitCallback};
use secretcli::secretcli::{TestInit, TestHandle, TestQuery};
//use secretcli::secretcli::TestInit;

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
pub struct InitMsg { }
Expand All @@ -11,7 +11,7 @@ impl InitCallback for InitMsg {
const BLOCK_SIZE: usize = 256;
}

impl TestInit for InitMsg {}
//impl TestInit for InitMsg {}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
#[serde(rename_all = "snake_case")]
Expand Down
6 changes: 3 additions & 3 deletions packages/shade_protocol/src/micro_mint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ pub struct Config {
pub struct SupportedAsset {
pub asset: Snip20Asset,
// Commission percentage * 100 e.g. 5 == .05 == 5%
pub commission: Uint128,
pub capture: Uint128,
}

// Used to keep track of the cap
Expand All @@ -44,7 +44,7 @@ pub struct InitMsg {
pub oracle: Contract,
//Symbol to peg to, default to snip20 symbol
pub peg: Option<String>,
// Both treasury & commission must be set to function
// Both treasury & capture must be set to function
pub treasury: Option<Contract>,
// This is where the non-burnable assets will go, if not defined they will stay in this contract
pub secondary_burn: Option<HumanAddr>,
Expand Down Expand Up @@ -77,7 +77,7 @@ pub enum HandleMsg {
RegisterAsset {
contract: Contract,
// Commission * 100 e.g. 5 == .05 == 5%
commission: Option<Uint128>,
capture: Option<Uint128>,
},
RemoveAsset {
address: HumanAddr,
Expand Down
Loading