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

[Feature] Identifier nft add borrow function #34

Merged
merged 9 commits into from
Jun 13, 2022
Merged
Changes from 5 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
53 changes: 52 additions & 1 deletion sources/NFT.move
Original file line number Diff line number Diff line change
Expand Up @@ -556,13 +556,14 @@ module NFT {
/// The NFT can not been transfer by owner.
module IdentifierNFT {
use StarcoinFramework::Option::{Self, Option};
use StarcoinFramework::NFT::{Self, NFT, MintCapability, BurnCapability};
use StarcoinFramework::NFT::{Self, NFT, MintCapability, BurnCapability, UpdateCapability};
use StarcoinFramework::Signer;
use StarcoinFramework::Errors;

const ERR_NFT_EXISTS: u64 = 101;
const ERR_NFT_NOT_EXISTS: u64 = 102;
const ERR_NFT_NOT_ACCEPT: u64 = 103;
const ERR_BORROW_ADDR_NOT_SAME: u64 = 104;

spec module {
pragma verify = false;
Expand All @@ -572,6 +573,11 @@ module IdentifierNFT {
nft: Option<NFT<NFTMeta, NFTBody>>,
}

struct BorrowIdentifierNFT<NFTMeta: copy + store + drop, NFTBody: store> has key {
jolestar marked this conversation as resolved.
Show resolved Hide resolved
nft: NFT<NFTMeta, NFTBody>,
addr:address
}

/// Check the `owner` is prepared with IdentifierNFT for accept the NFT<NFTMeta, NFTBody>
public fun is_accept<NFTMeta: copy + store + drop, NFTBody: store>(owner: address): bool {
exists<IdentifierNFT<NFTMeta, NFTBody>>(owner)
Expand Down Expand Up @@ -632,6 +638,51 @@ module IdentifierNFT {
Option::destroy_some(nft)
}

/// borrow_mut the NFT<NFTMeta, NFTBody> from owner.
public fun borrow_out<NFTMeta: copy + store + drop, NFTBody: store>(
_cap: &mut UpdateCapability<NFTMeta>,
owner: address
): BorrowIdentifierNFT<NFTMeta, NFTBody> acquires IdentifierNFT {
assert!(exists<IdentifierNFT<NFTMeta, NFTBody>>(owner), Errors::not_published(ERR_NFT_NOT_EXISTS));

let id_nft = borrow_global_mut<IdentifierNFT<NFTMeta, NFTBody>>(owner);
assert!(Option::is_some(&id_nft.nft), Errors::not_published(ERR_NFT_NOT_EXISTS));
jolestar marked this conversation as resolved.
Show resolved Hide resolved

let nft = Option::extract(&mut id_nft.nft);

BorrowIdentifierNFT{
nft : nft,
addr: owner
}
}

/// borrow_mut the NFT<NFTMeta, NFTBody> back to owner.
public fun borrow_back<NFTMeta: copy + store + drop, NFTBody: store>(
jolestar marked this conversation as resolved.
Show resolved Hide resolved
borrownft: BorrowIdentifierNFT<NFTMeta, NFTBody>,
) acquires IdentifierNFT {

let BorrowIdentifierNFT{
nft: nft,
addr: owner
} = borrownft ;
assert!(exists<IdentifierNFT<NFTMeta, NFTBody>>(owner), Errors::not_published(ERR_NFT_NOT_EXISTS));
let id_nft = borrow_global_mut<IdentifierNFT<NFTMeta, NFTBody>>(owner);

Option::fill(&mut id_nft.nft , nft)
}

public fun borrow_nft<NFTMeta: copy + store + drop, NFTBody: store>(
borrownft:&BorrowIdentifierNFT<NFTMeta, NFTBody>
) : & NFT<NFTMeta, NFTBody> {
& borrownft.nft
}

public fun borrow_nft_mut <NFTMeta: copy + store + drop, NFTBody: store>(
borrownft:&mut BorrowIdentifierNFT<NFTMeta, NFTBody>
) : &mut NFT<NFTMeta, NFTBody> {
&mut borrownft.nft
}

/// Check `owner` is owns the IdentifierNFT<NFTMeta, NFTBody>
public fun owns<NFTMeta: copy + store + drop, NFTBody: store>(owner: address): bool acquires IdentifierNFT {
if (!exists<IdentifierNFT<NFTMeta, NFTBody>>(owner)) {
Expand Down