Search code examples
nearprotocol

NFT transfer with price


I am using near-contract-standards for NFT. In the example, it has nft_mint which takes token owner id and token id to mint the token.

    #[payable]
    pub fn nft_mint(
        &mut self,
        token_id: TokenId,
        token_owner_id: ValidAccountId,
        token_metadata: TokenMetadata,
    ) -> Token {
        self.tokens.mint(token_id, token_owner_id, Some(token_metadata))
    }
}

Then token owner id can do the nft transfer as predecessor to another account. https://github.com/near-examples/NFT/blob/master/nft/src/lib.rs

contract.nft_mint(token_id.clone(), accounts(0), sample_token_metadata());

testing_env!(context
        .storage_usage(env::storage_usage())
        .attached_deposit(1)
        .predecessor_account_id(accounts(0))
        .build());
contract.nft_transfer(accounts(1), token_id.clone(), None, None);

I want to do nft tranfer with a buy price for nft, where predecessor is the user who want to buy the nft, instead of token owner, and then transfer the money to token owner.
How can I do that? Will I use internal_transfer or I will set the price in nft_mint function, and mint the token directly to buyer account id?


Solution

  • If I understand the question correctly you'd like to combine nft_mint with some sort of payable action that will pay the original creator of some "NFT token type"?

    You can check the NFT Market repo here: https://github.com/near-apps/nft-market

    This repository uses a separate market account and contract to allow NFT Owners to put NFTs up for sale. A little more than what you are looking for I think.

    There is also a variant that uses "lazy minting" to only mint the NFTs when a user buys them from the market contract: https://github.com/near-apps/gnr8/

    Again, it's a little more than what I think you want because the payment and buying is happening in a separate contract and the NFT transfer and minting is happening in the NFT contract, which is called from the market contract.

    If you're looking for something to add your own payable NFT method to, you could start with this: https://github.com/near-apps/nft-series/

    Which allows you to define an NFT type and you could charge users to mint 1/N of that type by making the nft_mint_type method payable and NOT require the owner of the type to be the minter.

    FYI these approaches are non-standard, but work.

    Feel free to reach out directly on Discord if you need any help with the examples above.