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

Mint fix #199

Merged
merged 5 commits into from
Apr 11, 2022
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
5 changes: 1 addition & 4 deletions contractlib/mintlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,7 @@ def __init__(self, label, native_asset, oracle, treasury=None,
},
}
if treasury:
init_msg['treasury'] = {
'address': treasury.address,
'code_hash': treasury.code_hash,
}
init_msg['treasury'] = treasury.address

if asset_peg:
init_msg['peg'] = asset_peg
Expand Down
3 changes: 2 additions & 1 deletion contractlib/secretlib/secretlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

GAS_METRICS = []
STORE_GAS = '4000000'
GAS = '100000'
GAS = '4000000'


def run_command(command):
Expand Down Expand Up @@ -123,6 +123,7 @@ def run_command_compute_hash(command):

try:
txhash = json.loads(out)["txhash"]
# print(txhash)
except Exception as e:
# print(out)
raise e
Expand Down
79 changes: 38 additions & 41 deletions contracts/mint/src/handle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ pub fn try_burn<S: Storage, A: Api, Q: Querier>(
amount: Uint128,
msg: Option<Binary>,
) -> StdResult<HandleResponse> {

let config = config_r(&deps.storage).load()?;
// Check if contract enabled
if !config.activated {
Expand Down Expand Up @@ -67,7 +68,7 @@ pub fn try_burn<S: Storage, A: Api, Q: Querier>(
let mut input_amount = amount;
let mut messages = vec![];

if burn_asset.fee > Uint128(0) {
if burn_asset.fee > Uint128::zero() {
let fee_amount = calculate_portion(input_amount, burn_asset.fee);
// Reduce input by fee
input_amount = (input_amount - fee_amount)?;
Expand Down Expand Up @@ -106,10 +107,10 @@ pub fn try_burn<S: Storage, A: Api, Q: Querier>(
let mut burn_amount = input_amount;

// Ignore capture if the set capture is 0
if burn_asset.capture > Uint128(0) {
if burn_asset.capture > Uint128::zero() {
let capture_amount = calculate_portion(amount, burn_asset.capture);

// Commission to treasury
// Capture to treasury
messages.push(send_msg(
config.treasury,
capture_amount,
Expand All @@ -124,17 +125,30 @@ pub fn try_burn<S: Storage, A: Api, Q: Querier>(
burn_amount = (input_amount - capture_amount)?;
}

// Try to burn
if let Some(token_config) = &burn_asset.asset.token_config {
if token_config.burn_enabled {
messages.push(burn_msg(
burn_amount,
None,
None,
256,
burn_asset.asset.contract.code_hash.clone(),
burn_asset.asset.contract.address.clone(),
)?);
if burn_amount > Uint128::zero() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Duplicate code, can be reduced to

        // Try to burn
        if let Some(token_config) = &burn_asset.asset.token_config {
            if token_config.burn_enabled {
                messages.push(burn_msg(
                    burn_amount,
                    None,
                    None,
                    256,
                    burn_asset.asset.contract.code_hash.clone(),
                    burn_asset.asset.contract.address.clone(),
                )?);
            }
        } else if let Some(recipient) = config.secondary_burn {
            messages.push(send_msg(
                recipient,
                burn_amount,
                None,
                None,
                None,
                1,
                burn_asset.asset.contract.code_hash.clone(),
                burn_asset.asset.contract.address.clone(),
            )?);
        }
    }```

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It isn't duplicated, there are 3 cases to be accounted for.

if has_token_config() {
   if burn_enabled => do_burn()
   else => do_secondary_burn()
}
else if no_token_confg() {
    do_secondary_burn()
}

// Try to burn
if let Some(token_config) = &burn_asset.asset.token_config {
if token_config.burn_enabled {
messages.push(burn_msg(
burn_amount,
None,
None,
256,
burn_asset.asset.contract.code_hash.clone(),
burn_asset.asset.contract.address.clone(),
)?);
} else if let Some(recipient) = config.secondary_burn {
messages.push(send_msg(
recipient,
burn_amount,
None,
None,
None,
1,
burn_asset.asset.contract.code_hash.clone(),
burn_asset.asset.contract.address.clone(),
)?);
}
} else if let Some(recipient) = config.secondary_burn {
messages.push(send_msg(
recipient,
Expand All @@ -147,20 +161,8 @@ pub fn try_burn<S: Storage, A: Api, Q: Querier>(
burn_asset.asset.contract.address.clone(),
)?);
}
} else if let Some(recipient) = config.secondary_burn {
messages.push(send_msg(
recipient,
burn_amount,
None,
None,
None,
1,
burn_asset.asset.contract.code_hash.clone(),
burn_asset.asset.contract.address.clone(),
)?);
}

// Update burned amount
total_burned_w(&mut deps.storage).update(
burn_asset.asset.contract.address.to_string().as_bytes(),
|burned| match burned {
Expand All @@ -169,11 +171,6 @@ pub fn try_burn<S: Storage, A: Api, Q: Querier>(
},
)?;

let mint_asset = native_asset_r(&deps.storage).load()?;

// This will calculate the total mint value
let amount_to_mint: Uint128 = mint_amount(deps, input_amount, &burn_asset, &mint_asset)?;

if let Some(message) = msg {
let msg: MintMsgHook = from_binary(&message)?;

Expand All @@ -185,12 +182,6 @@ pub fn try_burn<S: Storage, A: Api, Q: Querier>(
}
};

debug_print!(
"Minting: {} {}",
amount_to_mint,
&mint_asset.token_info.symbol
);

messages.push(mint_msg(
from,
amount_to_mint,
Expand All @@ -215,8 +206,10 @@ pub fn try_limit_refresh<S: Storage, A: Api, Q: Querier>(
deps: &mut Extern<S, A, Q>,
env: Env,
limit: Limit,
) -> StdResult<()> {
) -> StdResult<Uint128> {

match DateTime::parse_from_rfc3339(&limit_refresh_r(&deps.storage).load()?) {

Ok(parsed) => {
let naive = NaiveDateTime::from_timestamp(env.block.time as i64, 0);
let now: DateTime<Utc> = DateTime::from_utc(naive, Utc);
Expand Down Expand Up @@ -276,16 +269,17 @@ pub fn try_limit_refresh<S: Storage, A: Api, Q: Querier>(

limit_w(&mut deps.storage).update(|state| {
// Stack with previous unminted limit
Ok((state - minted)? + fresh_amount)
fresh_amount = (state - minted)? + fresh_amount;
Ok(fresh_amount)
})?;
limit_refresh_w(&mut deps.storage).save(&now.to_rfc3339())?;
minted_w(&mut deps.storage).save(&Uint128(0))?;
}

Ok(fresh_amount)
}
Err(e) => return Err(StdError::generic_err("Failed to parse previous datetime")),
}

Ok(())
}

pub fn try_update_config<S: Storage, A: Api, Q: Querier>(
Expand Down Expand Up @@ -499,6 +493,9 @@ pub fn calculate_portion(amount: Uint128, portion: Uint128) -> Uint128 {
*
* return portion = amount * portion / 10^18
*/
if portion == Uint128::zero() {
return Uint128::zero()
}

amount.multiply_ratio(portion, 10u128.pow(18))
}
Expand Down
8 changes: 6 additions & 2 deletions contracts/mint/src/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,18 @@ pub fn mint<S: Storage, A: Api, Q: Querier>(
offer_asset: HumanAddr,
amount: Uint128,
) -> StdResult<QueryAnswer> {

let native_asset = native_asset_r(&deps.storage).load()?;

match assets_r(&deps.storage).may_load(offer_asset.to_string().as_bytes())? {
Some(asset) => {
let fee_amount = calculate_portion(amount, asset.fee);
Ok(QueryAnswer::Mint {
asset: native_asset.contract.clone(),
amount: mint_amount(deps, (amount - fee_amount)?, &asset, &native_asset)?,
amount: mint_amount(deps,
(amount - calculate_portion(amount, asset.fee))?,
&asset,
&native_asset,
)?,
})
}
None => {
Expand Down
13 changes: 7 additions & 6 deletions contracts/mint_router/src/handle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,24 +54,25 @@ pub fn receive<S: Storage, A: Api, Q: Querier>(
}
};

if output_asset.address != final_asset {
// ignore slippage until final asset
if output_asset.address == final_asset {
// Send with the msg for slippage
messages.push(send_msg(
mint.address.clone(),
input_amount,
None,
msg.clone(),
None,
None,
1,
input_asset.code_hash.clone(),
input_asset.address.clone(),
)?);
} else {
// Send with the OG msg, to maintain slippage reqs
}
else {
// ignore slippage for intermediate steps
messages.push(send_msg(
mint.address.clone(),
input_amount,
msg.clone(),
None,
None,
None,
1,
Expand Down
Loading