|
1 | 1 | module SimpleNFT::nft { |
2 | | - use std::signer; |
3 | | - use std::vector; |
4 | | - use std::string; |
5 | | - |
6 | | - /// Minimal NFT (educational) |
| 2 | + /// Minimal NFT without strings/vectors/signers |
7 | 3 | struct NFT has store { |
8 | 4 | id: u64, |
9 | | - name: string::String, |
10 | 5 | owner: address, |
11 | 6 | } |
12 | 7 |
|
13 | | - struct Registry has store { |
14 | | - next_id: u64, |
15 | | - items: vector<NFT>, |
16 | | - } |
17 | | - |
18 | | - public fun new_registry(_admin: &signer): Registry { |
19 | | - Registry { next_id: 0, items: vector::empty<NFT>() } |
20 | | - } |
21 | | - |
22 | | - public fun mint(admin: &signer, r: &mut Registry, to: address, name: string::String) { |
23 | | - let id = r.next_id; |
24 | | - r.next_id = id + 1; |
25 | | - let nft = NFT { id, name, owner: to }; |
26 | | - vector::push_back(&mut r.items, nft); |
27 | | - } |
28 | | - |
29 | | - public fun transfer(_caller: &signer, r: &mut Registry, id: u64, to: address) { |
30 | | - let i = find_index(&r.items, id); |
31 | | - let mut nft = vector::borrow_mut(&mut r.items, i); |
32 | | - nft.owner = to; |
| 8 | + /// Mint an NFT to owner with id |
| 9 | + public fun mint(id: u64, owner: address): NFT { |
| 10 | + NFT { id, owner } |
33 | 11 | } |
34 | 12 |
|
35 | | - public fun get_owner(r: &Registry, id: u64): address { |
36 | | - let i = find_index(&r.items, id); |
37 | | - let nft = vector::borrow(&r.items, i); |
38 | | - nft.owner |
| 13 | + /// Transfer ownership |
| 14 | + public fun transfer(n: &mut NFT, to: address) { |
| 15 | + n.owner = to; |
39 | 16 | } |
40 | 17 |
|
41 | | - fun find_index(items: &vector<NFT>, id: u64): u64 { |
42 | | - let mut i = 0; |
43 | | - while (i < vector::length(items)) { |
44 | | - let nft_ref = vector::borrow(items, i); |
45 | | - if (nft_ref.id == id) return i; |
46 | | - i = i + 1; |
47 | | - }; |
48 | | - 0 |
| 18 | + /// Read owner |
| 19 | + public fun owner_of(n: &NFT): address { |
| 20 | + n.owner |
49 | 21 | } |
50 | 22 | } |
0 commit comments