Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
panindustrial-dev committed Aug 15, 2024
1 parent b3982e9 commit 9faa87a
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 2 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
#Changelog

## v0.4.1

- Fix indexing bug https://github.com/PanIndustrial-Org/icrc_nft.mo/issues/3
- Add tests for tokens_of
- Add maintenance method for reindexing owners

## v0.4.0

- Breaking change: Owner is no longer handled in the metadata
Expand Down
2 changes: 1 addition & 1 deletion mops.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "icrc7-mo"
version = "0.4.0"
version = "0.4.1"
description = "icrc7 for motoko"
repository = "https://github.com/PanIndustrial-Org/icrc7.mo"
keywords = [ "nft" ]
Expand Down
31 changes: 30 additions & 1 deletion src/lib.mo
Original file line number Diff line number Diff line change
Expand Up @@ -894,6 +894,16 @@ module {
/// Bool - A boolean value indicating if the owner was successfully unindexed from the token.
private func unindex_owner(token_id : Nat, account : Account) : Bool {

switch(Map.get(state.indexes.nft_to_owner, Map.nhash, token_id)){
case(null){}; //already reindexed
case(?val){
if(account_eq(val, account)){
ignore Map.remove(state.indexes.nft_to_owner, Map.nhash, token_id);
};
};
};


switch (Map.get(state.indexes.owner_to_nfts, ahash, account)) {
case (null) { return false };
case (?val) {
Expand Down Expand Up @@ -1002,7 +1012,7 @@ module {
Vec.add(trxtop, ("ts", #Nat(Int.abs(environment.get_time()))));

Vec.add(trxtop, ("from", accountToValue(current_owner)));
ignore unindex_owner(thisItem, current_owner);


let to = switch (state.ledger_info.burn_account) {
case (null) { null };
Expand Down Expand Up @@ -1881,6 +1891,25 @@ module {
return #ok(nft_value);
};

/// Indexes the owner of a token in the owner-to-token index.
/// Should only be used in emergencies or if indexes become corrupted.
public func maintenance_reindex_owners() : () {

Map.clear(state.indexes.nft_to_owner);
Map.clear(state.indexes.owner_to_nfts);
label proc for (thisItem in Map.entries(state.nfts)) {
let nft = thisItem.1;
let owner = switch (nft.owner) {
case (null) { {owner = environment.canister(); subaccount = null} };
case (?val) val;
};

ignore index_owner(thisItem.0, owner);
};
};

};



};
8 changes: 8 additions & 0 deletions test/lib.test.mo
Original file line number Diff line number Diff line change
Expand Up @@ -499,6 +499,14 @@ testsys<system>("Transfer a token to another account", func<system>() {
assert(ownerResponse.owner == toAccount.owner);
assert(ownerResponse.subaccount == toAccount.subaccount);

let tokens_of = icrc7.tokens_of(toAccount, null, null);

assert(tokens_of[0] == 1);

let tokens_of2 = icrc7.tokens_of(testOwnerAccount, null, null);

assert(tokens_of2.size() == 0);



});
Expand Down

0 comments on commit 9faa87a

Please sign in to comment.