Created
April 22, 2025 09:57
-
-
Save goodylili/c746fe1eedc66f08a98323bd124f5afa to your computer and use it in GitHub Desktop.
NFT with Account
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module impatient::goodylili{ | |
use std::string::{Self, String}; | |
use sui::url::{Self, Url}; | |
use sui::event; | |
use sui::balance::{Self, Balance}; | |
use sui::coin::{Self, Coin}; | |
use std::string::{utf8}; | |
use sui::display; | |
use sui::package; | |
use sui::sui::SUI; | |
public struct GOODYLILI_NFT<phantom T> has key, store { | |
id: UID, | |
name: String, | |
description: String, | |
url: Url, | |
balance: Balance<T> | |
} | |
public struct NFTMinted has copy, drop { | |
nft_name: String, | |
description: String, | |
url: Url, | |
} | |
public struct GOODYLILI has drop {} | |
fun init(otw: GOODYLILI, ctx: &mut TxContext) { | |
let keys = vector[ | |
utf8(b"name"), | |
utf8(b"description"), | |
utf8(b"image_url"), | |
utf8(b"rarity"), | |
]; | |
let values = vector[ | |
utf8(b"name"), | |
utf8(b"description"), | |
utf8(b"image_url"), | |
utf8(b"rarity"), | |
]; | |
let publisher = package::claim(otw, ctx); | |
let mut display = display::new_with_fields<GOODYLILI_NFT<SUI>> ( | |
&publisher, keys, values, ctx | |
); | |
display.update_version(); | |
transfer::public_transfer(publisher, tx_context::sender(ctx)); | |
transfer::public_transfer(display, tx_context::sender(ctx)); | |
} | |
#[allow(lint(self_transfer))] | |
public fun mint_to_sender<T: store>( | |
name: vector<u8>, | |
description: vector<u8>, | |
url: vector<u8>, | |
ctx: &mut TxContext, | |
) { | |
let sender = ctx.sender(); | |
let nft = GOODYLILI_NFT<T> { | |
id: object::new(ctx), | |
name: string::utf8(name), | |
description: string::utf8(description), | |
url: url::new_unsafe_from_bytes(url), | |
balance: balance::zero(), // Initialize with zero balance | |
}; | |
event::emit(NFTMinted { | |
nft_name: string::utf8(name), | |
description: string::utf8(description), | |
url: url::new_unsafe_from_bytes(url), | |
}); | |
transfer::public_transfer(nft, sender); | |
} | |
public entry fun add_balance<T: store>( | |
nft: &mut GOODYLILI_NFT<T>, | |
amount: u64, | |
payment: &mut Coin<T> | |
){ | |
let coin_balance = coin::balance_mut(payment); | |
let paid = balance::split(coin_balance, amount); | |
balance::join(&mut nft.balance, paid); | |
} | |
public entry fun withdraw_balance<T: store>( | |
nft: &mut GOODYLILI_NFT<T>, | |
amount: u64, | |
ctx: &mut TxContext | |
) { | |
let withdrawn = coin::from_balance( | |
balance::split(&mut nft.balance, amount), | |
ctx | |
); | |
transfer::public_transfer(withdrawn, tx_context::sender(ctx)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment