-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* fix identity verification for the whitelisted_postfix related interfaces. * add donation functions and mft_unregister * update mft_unregister * update donation functions * add comments to the donation functions.
- Loading branch information
1 parent
9d853b0
commit 4bf8df2
Showing
18 changed files
with
317 additions
and
7 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
[package] | ||
name = "ref-exchange" | ||
version = "1.9.6" | ||
version = "1.9.7" | ||
authors = ["Illia Polosukhin <[email protected]>"] | ||
edition = "2018" | ||
publish = false | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
use crate::*; | ||
|
||
#[near_bindgen] | ||
impl Contract { | ||
/// The user donates the shares they hold to the protocol. | ||
/// | ||
/// # Arguments | ||
/// | ||
/// * `pool_id` - The pool id where the shares are located. | ||
/// * `amount` - The donation amount; if it's None, the entire amount will be donated. | ||
/// * `unregister` - If `Some(true)`, Will attempt to unregister the shares and refund the user’s storage fees. | ||
/// The storage fee will be refunded to the user's internal account first; | ||
/// if there is no internal account, a transfer will be initiated. | ||
#[payable] | ||
pub fn donation_share(&mut self, pool_id: u64, amount: Option<U128>, unregister: Option<bool>) { | ||
assert_one_yocto(); | ||
self.assert_contract_running(); | ||
let account_id = env::predecessor_account_id(); | ||
let prev_storage = env::storage_usage(); | ||
let mut pool = self.pools.get(pool_id).expect(ERR85_NO_POOL); | ||
let donation_amount = amount.map(|v| v.0).unwrap_or(pool.share_balances(&account_id)); | ||
assert!(donation_amount > 0, "Invalid amount"); | ||
pool.share_transfer(&account_id, &env::current_account_id(), donation_amount); | ||
if unregister == Some(true) { | ||
pool.share_unregister(&account_id); | ||
} | ||
self.pools.replace(pool_id, &pool); | ||
if prev_storage > env::storage_usage() { | ||
let refund = (prev_storage - env::storage_usage()) as Balance * env::storage_byte_cost(); | ||
if let Some(mut account) = self.internal_get_account(&account_id) { | ||
account.near_amount += refund; | ||
self.internal_save_account(&account_id, account); | ||
} else { | ||
Promise::new(account_id.clone()).transfer(refund); | ||
} | ||
} | ||
event::Event::DonationShare { account_id: &account_id, pool_id, amount: U128(donation_amount) }.emit(); | ||
} | ||
|
||
/// The user donates the tokens they hold to the owner. | ||
/// | ||
/// # Arguments | ||
/// | ||
/// * `token_id` - The id of the donated token. | ||
/// * `amount` - The donation amount; if it's None, the entire amount will be donated. | ||
/// * `unregister` - If `Some(true)`, Will attempt to unregister the tokens. | ||
#[payable] | ||
pub fn donation_token(&mut self, token_id: ValidAccountId, amount: Option<U128>, unregister: Option<bool>) { | ||
assert_one_yocto(); | ||
self.assert_contract_running(); | ||
let account_id = env::predecessor_account_id(); | ||
let mut account = self.internal_unwrap_account(&account_id); | ||
let donation_amount = amount.map(|v| v.0).unwrap_or(account.get_balance(token_id.as_ref()).expect("Invalid token_id")); | ||
assert!(donation_amount > 0, "Invalid amount"); | ||
account.withdraw(token_id.as_ref(), donation_amount); | ||
if unregister == Some(true) { | ||
account.unregister(token_id.as_ref()); | ||
} | ||
self.internal_save_account(&account_id, account); | ||
let mut owner_account = self.internal_unwrap_account(&self.owner_id); | ||
owner_account.deposit(token_id.as_ref(), donation_amount); | ||
self.accounts.insert(&self.owner_id, &owner_account.into()); | ||
event::Event::DonationToken { account_id: &account_id, token_id: token_id.as_ref(), amount: U128(donation_amount) }.emit(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
|
||
use crate::*; | ||
use near_sdk::serde_json::json; | ||
|
||
const EVENT_STANDARD: &str = "exchange.ref"; | ||
const EVENT_STANDARD_VERSION: &str = "1.0.0"; | ||
|
||
#[derive(Serialize, Debug, Clone)] | ||
#[serde(crate = "near_sdk::serde")] | ||
#[serde(tag = "event", content = "data")] | ||
#[serde(rename_all = "snake_case")] | ||
#[must_use = "Don't forget to `.emit()` this event"] | ||
pub enum Event<'a> { | ||
DonationShare { | ||
account_id: &'a AccountId, | ||
pool_id: u64, | ||
amount: U128, | ||
}, | ||
DonationToken { | ||
account_id: &'a AccountId, | ||
token_id: &'a AccountId, | ||
amount: U128, | ||
} | ||
} | ||
|
||
impl Event<'_> { | ||
pub fn emit(&self) { | ||
let data = json!(self); | ||
let event_json = json!({ | ||
"standard": EVENT_STANDARD, | ||
"version": EVENT_STANDARD_VERSION, | ||
"event": data["event"], | ||
"data": [data["data"]] | ||
}) | ||
.to_string(); | ||
log!("EVENT_JSON:{}", event_json); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.