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

remove zero Balance or empty NFTGallery #28

Merged
merged 5 commits into from
May 24, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
9 changes: 9 additions & 0 deletions integration-tests/account/remove_zero_balance.exp
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
processed 3 tasks

task 2 'run'. lines 5-24:
{
"gas_used": 0,
"status": {
"Discard": 14
pause125 marked this conversation as resolved.
Show resolved Hide resolved
}
}
24 changes: 24 additions & 0 deletions integration-tests/account/remove_zero_balance.move
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//# init -n dev

//# faucet --addr alice --amount 0
pause125 marked this conversation as resolved.
Show resolved Hide resolved

//# run --signers alice
script {
use StarcoinFramework::STC::{STC};
use StarcoinFramework::Signer;
use StarcoinFramework::Account;
use StarcoinFramework::Token;

fun main(account: signer) {
let addr: address = Signer::address_of(&account);

let coin = Token::zero<STC>();
Account::deposit_to_self<STC>(&account, coin);

Account::remove_zero_balance<STC>(&account);

Account::set_auto_accept_token(&account, false);
assert!(!Account::is_accept_token<STC>(addr), 101);
}
}
// check: EXECUTED
jolestar marked this conversation as resolved.
Show resolved Hide resolved
20 changes: 19 additions & 1 deletion integration-tests/nft/test_gallery.exp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
processed 16 tasks
processed 17 tasks

task 4 'run'. lines 70-78:
{
Expand Down Expand Up @@ -95,3 +95,21 @@ task 15 'run'. lines 236-250:
"Keep": "Executed"
}
}

task 16 'run'. lines 252-269:
{
"gas_used": 519398,
"status": {
"Keep": {
"MoveAbort": [
{
"Module": {
"address": "0x00000000000000000000000000000001",
"name": "Vector"
}
},
3
]
}
pause125 marked this conversation as resolved.
Show resolved Hide resolved
}
}
19 changes: 19 additions & 0 deletions integration-tests/nft/test_gallery.move
Original file line number Diff line number Diff line change
Expand Up @@ -247,4 +247,23 @@ fun main(sender: signer) {
}
}

// check: EXECUTED

//# run --signers bob
script {
use creator::AnyNFT::{AnyNFT, AnyNFTBody};
use StarcoinFramework::NFTGallery;
use StarcoinFramework::Signer;

fun main(sender: signer) {
let addr: address = Signer::address_of(&sender);

NFTGallery::accept<AnyNFT, AnyNFTBody>(&sender);
assert!(NFTGallery::is_accept<AnyNFT, AnyNFTBody>(addr), 1);

NFTGallery::remove_empty_gallery<AnyNFT, AnyNFTBody>(&sender);
pause125 marked this conversation as resolved.
Show resolved Hide resolved
assert!(!NFTGallery::is_accept<AnyNFT, AnyNFTBody>(addr), 2);
}
}

// check: EXECUTED
12 changes: 12 additions & 0 deletions sources/Account.move
Original file line number Diff line number Diff line change
Expand Up @@ -993,6 +993,18 @@ module Account {
aborts_if txn_gas_price * (txn_max_gas_units - gas_units_remaining) > 0 &&
global<TransactionFee::TransactionFee<TokenType>>(CoreAddresses::GENESIS_ADDRESS()).fee.value + txn_gas_price * (txn_max_gas_units - gas_units_remaining) > max_u128();
}

/// Remove zero Balance
public fun remove_zero_balance<TokenType: store>(account: &signer) acquires Balance {
let addr: address = Signer::address_of(account);
let Balance<TokenType> { token } = move_from<Balance<TokenType>>(addr);
Token::destroy_zero<TokenType>(token);
}

spec remove_zero_balance {
let addr = Signer::address_of(account);
aborts_if !exists<Balance<CoinType>>(addr);
pause125 marked this conversation as resolved.
Show resolved Hide resolved
}
}

}
5 changes: 5 additions & 0 deletions sources/AccountScripts.move
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,10 @@ module AccountScripts {
public(script) fun disable_auto_accept_token(account: signer) {
Account::set_auto_accept_token(&account, false);
}

/// Remove zero Balance
public(script) fun remove_zero_balance<TokenType: store>(account: signer) {
Account::remove_zero_balance<TokenType>(&account);
}
}
}
24 changes: 24 additions & 0 deletions sources/NFT.move
Original file line number Diff line number Diff line change
Expand Up @@ -879,6 +879,25 @@ module NFTGallery {
let gallery = borrow_global_mut<NFTGallery<NFTMeta, NFTBody>>(owner);
Vector::length(&gallery.items)
}

/// Remove empty NFTGallery<Meta,Body>.
public fun remove_empty_gallery<NFTMeta: copy + store + drop, NFTBody: store>(sender: &signer) acquires NFTGallery{
let sender_addr = Signer::address_of(sender);
let NFTGallery<NFTMeta, NFTBody> {withdraw_events, deposit_events, items} = move_from<NFTGallery<NFTMeta, NFTBody>>(sender_addr);

Event::destroy_handle<WithdrawEvent<NFTMeta>>(withdraw_events);
Event::destroy_handle<DepositEvent<NFTMeta>>(deposit_events);
Vector::destroy_empty<NFT<NFTMeta, NFTBody>>(items);
}

spec remove_empty_gallery {
let sender_addr = Signer::address_of(sender);
aborts_if !exists<NFTGallery<NFTMeta, NFTBody>>(sender_addr);

let gallery = global<NFTGallery<NFTMeta, NFTBody>>(sender_addr);
aborts_if Vector::length(&gallery) > 0;
}

}

module NFTGalleryScripts {
Expand All @@ -900,5 +919,10 @@ module NFTGalleryScripts {
) {
NFTGallery::transfer<NFTMeta, NFTBody>(&sender, id, receiver);
}

/// Remove empty NFTGallery<Meta,Body>.
public(script) fun remove_empty_gallery<NFTMeta: copy + store + drop, NFTBody: store>(sender: signer) {
NFTGallery::remove_empty_gallery<NFTMeta, NFTBody>(&sender);
}
}
}