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

Address comments #1

Merged
merged 2 commits into from
Sep 29, 2023
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
1 change: 0 additions & 1 deletion examples/multi-token/mt/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ impl ExampleMTContract {

#[init]
pub fn new(owner_id: AccountId, metadata: MtContractMetadata) -> Self {
require!(!env::state_exists(), "Already initialized");
metadata.assert_valid();

Self {
Expand Down
18 changes: 6 additions & 12 deletions near-contract-standards/src/multi_token/approval/approval_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,20 +153,14 @@ impl MultiTokenApproval for MultiToken {
for (idx, (token_id, amount)) in token_ids.iter().zip(amounts).enumerate() {
let by_owner = by_token.get(token_id).unwrap_or_default();

let grantee_to_approval = match by_owner.get(&owner_id) {
Some(grantee_to_approval) => grantee_to_approval,
None => return false,
let approval = match by_owner
.get(&owner_id)
.and_then(|grantee_to_approval| grantee_to_approval.get(&approved_account_id))
{
Some(approval) if approval.amount.eq(&amount.into()) => approval,
_ => return false,
};

let approval = match grantee_to_approval.get(&approved_account_id) {
Some(approval) => approval,
None => return false,
};

if !approval.amount.eq(&amount.into()) {
return false;
}

if let Some(given_approval) = approval_ids.get(idx) {
if !approval.approval_id.eq(given_approval) {
return false;
Expand Down
6 changes: 3 additions & 3 deletions near-contract-standards/src/multi_token/core/core_impl.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use near_sdk::borsh::{self, BorshDeserialize, BorshSerialize};
use near_sdk::collections::{LookupMap, TreeMap, UnorderedMap, UnorderedSet};
use near_sdk::collections::{LookupMap, UnorderedMap, UnorderedSet};
use near_sdk::json_types::U128;
use near_sdk::{
assert_one_yocto, env, log, require, AccountId, Balance, BorshStorageKey, CryptoHash, Gas,
Expand Down Expand Up @@ -49,7 +49,7 @@ pub struct MultiToken {
pub extra_storage_in_bytes_per_emission: StorageUsage,

/// Owner of each token
pub owner_by_id: TreeMap<TokenId, AccountId>,
pub owner_by_id: UnorderedMap<TokenId, AccountId>,

/// Total supply for each token
pub total_supply: LookupMap<TokenId, Balance>,
Expand Down Expand Up @@ -124,7 +124,7 @@ impl MultiToken {
let mut this = Self {
owner_id,
extra_storage_in_bytes_per_emission: 0,
owner_by_id: TreeMap::new(owner_by_id_prefix),
owner_by_id: UnorderedMap::new(owner_by_id_prefix),
total_supply: LookupMap::new(StorageKey::TotalSupply { supply: 0 }),
token_metadata_by_id: token_metadata_prefix.map(LookupMap::new),
tokens_per_owner: enumeration_prefix.map(LookupMap::new),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ impl MultiTokenEnumeration for MultiToken {
self.owner_by_id.len() as u128 >= start_index,
"Out of bounds, please use a smaller from_index."
);
let limit = limit.map(|v| v as usize).unwrap_or(usize::MAX);
let limit = limit.unwrap_or(u64::MAX);
require!(limit != 0, "Limit cannot be 0");

self.owner_by_id
Expand Down
13 changes: 10 additions & 3 deletions near-contract-standards/src/multi_token/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,16 @@ pub struct Approval {
// How Approvals are stored in the contract
pub type ApprovalContainer = LookupMap<TokenId, HashMap<AccountId, HashMap<AccountId, Approval>>>;

// Represents a temporary record of an Approval
// that was removed from the ApprovalContainer but may be restored in case of rollback in XCC.
// Values are (owner_id, approval_id, amount)
// Represents a record of an Approval that has been temporarily added or removed
// from the ApprovalContainer during cross-contract calls (XCC).
// This data is stored to facilitate possible rollback scenarios where the
// approval needs to be restored.
//
// The tuple contains the following elements:
// - `AccountId`: The Account ID of the owner who initially granted the approval.
// - `Approval`: A struct containing:
// - `amount`: The number of tokens that were initially approved for transfer.
// - `approval_id`: A unique identifier assigned to this specific approval.
pub type ClearedApproval = (AccountId, Approval);

/// Info on individual token
Expand Down