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
Show file tree
Hide file tree
Changes from 7 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
2 changes: 1 addition & 1 deletion build/StarcoinFramework/BuildInfo.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ compiled_package_info:
? address: "0x00000000000000000000000000000001"
name: YieldFarmingV2
: StarcoinFramework
source_digest: E9144252A3E50D7727A1E1A0DD76494580D66DD68C46C9625CC1042087A4D5BE
source_digest: DE30999B6994235FABB3E58555F30F36E075D52A82667727901436D922F75D57
build_flags:
dev_mode: false
test_mode: false
Expand Down
Binary file modified build/StarcoinFramework/bytecode_modules/IdentifierNFT.mv
Binary file not shown.
Binary file modified build/StarcoinFramework/source_maps/IdentifierNFT.mvsm
Binary file not shown.
Binary file modified build/StarcoinFramework/source_maps/IdentifierNFTScripts.mvsm
Binary file not shown.
Binary file modified build/StarcoinFramework/source_maps/NFT.mvsm
Binary file not shown.
Binary file modified build/StarcoinFramework/source_maps/NFTGallery.mvsm
Binary file not shown.
Binary file modified build/StarcoinFramework/source_maps/NFTGalleryScripts.mvsm
Binary file not shown.
18 changes: 13 additions & 5 deletions integration-tests/nft/identifier_nft.exp
Original file line number Diff line number Diff line change
@@ -1,30 +1,38 @@
processed 10 tasks
processed 11 tasks

task 5 'run'. lines 110-118:
task 5 'run'. lines 127-135:
{
"gas_used": 151177,
"status": {
"Keep": "Executed"
}
}

task 6 'run'. lines 120-128:
task 6 'run'. lines 137-145:
{
"gas_used": 176281,
"status": {
"Keep": "Executed"
}
}

task 7 'run'. lines 132-140:
task 7 'run'. lines 147-155:
{
"gas_used": 136416,
"status": {
"Keep": "Executed"
}
}

task 8 'run'. lines 158-166:
{
"gas_used": 96545,
"status": {
"Keep": "Executed"
}
}

task 9 'run'. lines 145-153:
task 10 'run'. lines 171-179:
{
"gas_used": 193000,
"status": {
Expand Down
26 changes: 26 additions & 0 deletions integration-tests/nft/identifier_nft.move
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,23 @@ module creator::XMembership {
//do other membership jobs
}
}

//check borrow
public fun add(sender: &signer, fee: u128) acquires XMembershipUpdateCapability, XMembershipInfo{
let cap = borrow_global_mut<XMembershipUpdateCapability>(@creator);
let addr = Signer::address_of(sender);
let borrow_nft = IdentifierNFT::borrow_out<XMembership, XMembershipBody>(&mut cap.cap, addr);
let nft = IdentifierNFT::borrow_nft_mut<XMembership, XMembershipBody>(&mut borrow_nft);
let nft_base_meta = *NFT::get_base_meta(nft);
let nft_meta = *NFT::get_type_meta(nft);
let nft_body = NFT::borrow_body_mut_with_cap(&mut cap.cap, nft);
Token::deposit<STC>(&mut nft_body.fee, Account::withdraw<STC>(sender, fee));
let info = borrow_global<XMembershipInfo>(@creator);
nft_meta.end_time = nft_meta.join_time + ((Token::value(&nft_body.fee)/info.price_per_millis) as u64);

NFT::update_meta_with_cap<XMembership, XMembershipBody>(&mut cap.cap, nft ,nft_base_meta, nft_meta);
IdentifierNFT::return_back(borrow_nft);
}
}

// check: EXECUTED
Expand All @@ -127,6 +144,15 @@ script {

// check: EXECUTED

//# run --signers bob
script {
use creator::XMembership;
fun main(sender: signer) {
XMembership::add(&sender, 100000);
}
}

// check: EXECUTED


//# run --signers bob
Expand Down
54 changes: 53 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 @@ -571,6 +572,12 @@ module IdentifierNFT {
struct IdentifierNFT<NFTMeta: copy + store + drop, NFTBody: store> has key {
nft: Option<NFT<NFTMeta, NFTBody>>,
}

//Used when lending or returning NFT, note: there is no drop ability, it must be returned after lending
jolestar marked this conversation as resolved.
Show resolved Hide resolved
struct BorrowNFT<NFTMeta: copy + store + drop, NFTBody: store> {
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 {
Expand Down Expand Up @@ -632,6 +639,51 @@ module IdentifierNFT {
Option::destroy_some(nft)
}

/// borrow_out the NFT<NFTMeta, NFTBody> from owner.
public fun borrow_out<NFTMeta: copy + store + drop, NFTBody: store>(
_cap: &mut UpdateCapability<NFTMeta>,
owner: address
): BorrowNFT<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);

BorrowNFT{
nft : nft,
addr: owner
}
}

/// return_back the NFT<NFTMeta, NFTBody> to owner.
public fun return_back<NFTMeta: copy + store + drop, NFTBody: store>(
borrownft: BorrowNFT<NFTMeta, NFTBody>,
) acquires IdentifierNFT {

let BorrowNFT{
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:&BorrowNFT<NFTMeta, NFTBody>
) : & NFT<NFTMeta, NFTBody> {
& borrownft.nft
}

public fun borrow_nft_mut <NFTMeta: copy + store + drop, NFTBody: store>(
borrownft:&mut BorrowNFT<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