How to split coin to transfer Sui

505 views Asked by At

Here is what I'm trying:

public entry fun purchase_nft(
        marketplace: &mut Marketplace,
        item_id: ID,
        coin: Coin<SUI>,
        ctx: &mut TxContext
    ) {
        let sender = tx_context::sender(ctx);
        let listing = dynamic_object_field::borrow_mut<ID, List<BidData>>(&mut marketplace.id, item_id);

        let pay_amount = option::extract(&mut listing.bid_data).highest_price;
        let owner_cut = pay_amount * marketplace.owner_cut / 10000;
        
        let owner_cut_coin = coin::split(&mut coin, owner_cut, ctx);
        let pay_coin = coin::split(&mut coin, pay_amount - owner_cut, ctx);

        transfer::transfer(owner_cut_coin, marketplace.owner);
        transfer::transfer(pay_coin, listing.seller);

        transfer::transfer(listing.token, option::extract(&mut listing.bid_data).highest_bidder);
    }

But the compiler return error: " The parameter 'coin' still contains a value. The value does not have the 'drop' ability and must be consumed before the function returns". How can I fix this?

1

There are 1 answers

0
CodeNinja On

I met this error in previous project. Coin doesn't have the 'drop' ability So you need to send coin to any address(for example, your wallet). Please add this code at the bottom

transfer::transfer(coin, tx_context::sender(ctx));