Skip to content

Commit

Permalink
Merge pull request #11 from octopus-network/v1.1.2
Browse files Browse the repository at this point in the history
Upgrade to v1.1.2
  • Loading branch information
riversyang authored Mar 7, 2022
2 parents 6de9dac + 4db0151 commit 3e5da77
Show file tree
Hide file tree
Showing 8 changed files with 141 additions and 217 deletions.
2 changes: 1 addition & 1 deletion appchain-anchor/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "appchain-anchor"
version = "1.1.1"
version = "1.1.2"
authors = ["Octopus Network"]
edition = "2018"

Expand Down
16 changes: 12 additions & 4 deletions appchain-anchor/src/assets/near_fungible_tokens.rs
Original file line number Diff line number Diff line change
Expand Up @@ -252,21 +252,29 @@ impl AppchainAnchor {
receiver_id_in_appchain: receiver_id_in_appchain.clone(),
amount,
});
self.internal_append_appchain_notification(
let appchain_notification_history = self.internal_append_appchain_notification(
AppchainNotification::NearFungibleTokenLocked {
contract_account: near_fungible_token.contract_account,
contract_account: near_fungible_token.contract_account.clone(),
sender_id_in_near: sender_id.clone(),
receiver_id_in_appchain: receiver_id_in_appchain.clone(),
amount,
},
);
log!(
"Received fungible token in contract '{}' from '{}'. Start transfer to '{}' of appchain. Amount: '{}', Crosschain notification index: '{}'.",
&near_fungible_token.contract_account,
&sender_id.clone(),
&receiver_id_in_appchain,
&amount.0,
&appchain_notification_history.index.0
);
return PromiseOrValue::Value(0.into());
}
}
}
panic!(
"Invalid deposit '{}' of unknown NEP-141 asset from '{}' received. Return deposit.",
amount.0, sender_id,
"Received invalid deposit '{}' in contract '{}' from '{}'. Return deposit.",
&amount.0, &predecessor_account_id, &sender_id,
);
}
//
Expand Down
105 changes: 45 additions & 60 deletions appchain-anchor/src/assets/wrapped_appchain_token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ impl Default for WrappedAppchainToken {
premined_balance: U128::from(0),
changed_balance: I128::from(0),
price_in_usd: U128::from(0),
total_supply: U128::from(0),
}
}
}
Expand Down Expand Up @@ -76,6 +77,7 @@ impl WrappedAppchainTokenManager for AppchainAnchor {
wrapped_appchain_token.metadata = metadata;
wrapped_appchain_token.premined_beneficiary = premined_beneficiary;
wrapped_appchain_token.premined_balance = premined_balance;
wrapped_appchain_token.total_supply = premined_balance;
self.wrapped_appchain_token.set(&wrapped_appchain_token);
}
//
Expand All @@ -86,6 +88,13 @@ impl WrappedAppchainTokenManager for AppchainAnchor {
self.wrapped_appchain_token.set(&wrapped_appchain_token);
}
//
fn set_total_supply_of_wrapped_appchain_token(&mut self, total_supply: U128) {
self.assert_owner();
let mut wrapped_appchain_token = self.wrapped_appchain_token.get().unwrap();
wrapped_appchain_token.total_supply = total_supply;
self.wrapped_appchain_token.set(&wrapped_appchain_token);
}
//
fn set_price_of_wrapped_appchain_token(&mut self, price: U128) {
let anchor_settings = self.anchor_settings.get().unwrap();
assert_eq!(
Expand Down Expand Up @@ -132,7 +141,6 @@ impl AppchainAnchor {
appchain_message_nonce: u32,
) -> AppchainMessageProcessingResult {
let wrapped_appchain_token = self.wrapped_appchain_token.get().unwrap();
let protocol_settings = self.protocol_settings.get().unwrap();
if let Some(sender_id) = &sender_id {
if !AccountIdInAppchain::new(Some(sender_id.clone())).is_valid() {
let result = AppchainMessageProcessingResult::Error {
Expand All @@ -143,52 +151,28 @@ impl AppchainAnchor {
return result;
}
}
if wrapped_appchain_token.total_market_value()
+ wrapped_appchain_token.get_market_value_of(amount.0)
> self.get_market_value_of_staked_oct_token().0
* u128::from(
protocol_settings.maximum_market_value_percent_of_wrapped_appchain_token,
)
/ 100
{
let message = format!("Too much wrapped appchain token to mint.");
self.internal_append_anchor_event(AnchorEvent::FailedToMintWrappedAppchainToken {
sender_id_in_appchain: sender_id,
receiver_id_in_near: receiver_id,
amount,
appchain_message_nonce,
reason: message.clone(),
});
let result = AppchainMessageProcessingResult::Error {
nonce: appchain_message_nonce,
message,
};
self.record_appchain_message_processing_result(&result);
result
} else {
ext_fungible_token::mint(
receiver_id.clone(),
amount,
&wrapped_appchain_token.contract_account,
STORAGE_DEPOSIT_FOR_NEP141_TOEKN,
GAS_FOR_MINT_FUNGIBLE_TOKEN,
)
.then(ext_self::resolve_wrapped_appchain_token_minting(
sender_id.clone(),
receiver_id.clone(),
amount,
appchain_message_nonce,
&env::current_account_id(),
0,
GAS_FOR_RESOLVER_FUNCTION,
));
AppchainMessageProcessingResult::Ok {
nonce: appchain_message_nonce,
message: Some(format!(
"Need to confirm result of 'mint' on account '{}'.",
wrapped_appchain_token.contract_account
)),
}
ext_fungible_token::mint(
receiver_id.clone(),
amount,
&wrapped_appchain_token.contract_account,
STORAGE_DEPOSIT_FOR_NEP141_TOEKN,
GAS_FOR_MINT_FUNGIBLE_TOKEN,
)
.then(ext_self::resolve_wrapped_appchain_token_minting(
sender_id.clone(),
receiver_id.clone(),
amount,
appchain_message_nonce,
&env::current_account_id(),
0,
GAS_FOR_RESOLVER_FUNCTION,
));
AppchainMessageProcessingResult::Ok {
nonce: appchain_message_nonce,
message: Some(format!(
"Need to confirm result of 'mint' on account '{}'.",
wrapped_appchain_token.contract_account
)),
}
}
}
Expand All @@ -203,10 +187,10 @@ impl WrappedAppchainTokenContractResolver for AppchainAnchor {
amount: U128,
) {
assert_self();
let mut wrapped_appchain_token = self.wrapped_appchain_token.get().unwrap();
match env::promise_result(0) {
PromiseResult::NotReady => unreachable!(),
PromiseResult::Successful(_) => {
let mut wrapped_appchain_token = self.wrapped_appchain_token.get().unwrap();
wrapped_appchain_token.changed_balance = I128::from(
wrapped_appchain_token.changed_balance.0 - i128::try_from(amount.0).unwrap(),
);
Expand All @@ -223,21 +207,22 @@ impl WrappedAppchainTokenContractResolver for AppchainAnchor {
amount: U128::from(amount),
},
);
env::log(
format!(
"Wrapped appchain token burnt by '{}' for '{}' of appchain. Amount: '{}', Crosschain notification index: '{}'.",
&sender_id_in_near, &receiver_id_in_appchain, &amount.0, &appchain_notification_history.index.0
)
.as_bytes(),
log!(
"Wrapped appchain token burnt in contract '{}' by '{}' for '{}' of appchain. Amount: '{}', Crosschain notification index: '{}'.",
&wrapped_appchain_token.contract_account,
&sender_id_in_near,
&receiver_id_in_appchain,
&amount.0,
&appchain_notification_history.index.0
);
}
PromiseResult::Failed => {
env::log(
format!(
"Failed to burn wrapped appchain token owned by '{}' for '{}' in appchain. Amount: '{}'",
&sender_id_in_near, &receiver_id_in_appchain, &amount.0
)
.as_bytes(),
log!(
"Failed to burn wrapped appchain token in contract '{}' by '{}' for '{}' in appchain. Amount: '{}'",
&wrapped_appchain_token.contract_account,
&sender_id_in_near,
&receiver_id_in_appchain,
&amount.0
);
self.internal_append_anchor_event(AnchorEvent::FailedToBurnWrappedAppchainToken {
sender_id_in_near: sender_id_in_near.clone(),
Expand Down
2 changes: 2 additions & 0 deletions appchain-anchor/src/interfaces.rs
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,8 @@ pub trait WrappedAppchainTokenManager {
///
fn set_account_of_wrapped_appchain_token(&mut self, contract_account: AccountId);
///
fn set_total_supply_of_wrapped_appchain_token(&mut self, total_supply: U128);
///
fn set_price_of_wrapped_appchain_token(&mut self, price: U128);
///
fn burn_wrapped_appchain_token(&self, receiver_id: String, amount: U128);
Expand Down
25 changes: 24 additions & 1 deletion appchain-anchor/src/permissionless_actions/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ mod switching_era;

use crate::*;
use crate::{interfaces::PermissionlessActions, message_decoder::AppchainMessage};
use core::convert::TryInto;
use core::convert::{TryFrom, TryInto};

#[derive(Serialize, Deserialize, Clone)]
#[serde(crate = "near_sdk::serde")]
Expand Down Expand Up @@ -234,6 +234,29 @@ impl AppchainAnchor {
self.record_appchain_message_processing_result(&result);
return result;
}
let wrapped_appchain_token = self.wrapped_appchain_token.get().unwrap();
if i128::try_from(wrapped_appchain_token.premined_balance.0).unwrap()
+ wrapped_appchain_token.changed_balance.0
+ i128::try_from(amount.0).unwrap()
> i128::try_from(wrapped_appchain_token.total_supply.0).unwrap()
{
let message = format!("Too much wrapped appchain token to mint.");
self.internal_append_anchor_event(
AnchorEvent::FailedToMintWrappedAppchainToken {
sender_id_in_appchain: Some(owner_id_in_appchain),
receiver_id_in_near,
amount,
appchain_message_nonce: appchain_message.nonce,
reason: message.clone(),
},
);
let result = AppchainMessageProcessingResult::Error {
nonce: appchain_message.nonce,
message,
};
self.record_appchain_message_processing_result(&result);
return result;
}
self.internal_mint_wrapped_appchain_token(
Some(owner_id_in_appchain),
receiver_id_in_near,
Expand Down
Loading

0 comments on commit 3e5da77

Please sign in to comment.