GithubHelp home page GithubHelp logo

Comments (7)

Woody4618 avatar Woody4618 commented on June 27, 2024

Hey, yeah sorry i didnt get to upgrade it to the new unity sdk version. You already found out you dont need to change the private key in fromAccount anymore and that metadatav2 doesnt exist anymore. 👍
Maybe check the meta data if its correct or try metadata v3. Also check if your name and symbol are not too long. You could also check if puffmetadata is still needed.

from solplay_unity_sdk.

BlackDemonZyT avatar BlackDemonZyT commented on June 27, 2024

Hi! How can i specify to use metadatav3 in my code? I do not understand 😅

from solplay_unity_sdk.

BlackDemonZyT avatar BlackDemonZyT commented on June 27, 2024

Hello, do you have Whatsapp, telegram or Discord in order to comunicate quickly? My project is pretty big and i need urgent help.

from solplay_unity_sdk.

Woody4618 avatar Woody4618 commented on June 27, 2024

Hey did you check the name and symbol length? There is a max limit on name length.

In general its probably better to ask in the magic block sdk repo now, since i dont have much time to maintain the solplay sdk and its behind in the sdk version.

This code here I just tested and it works using magic block unity sdk version 0.1.0:
Its not creating a master edition. But not sure if you need it. Depends on what you plan to do.

public async Task<string> MintNftWithMetaData(string metaDataUri, string name, string symbol, Action<bool> mintDone = null)
        {
            var walletHolderService = ServiceFactory.Resolve<WalletHolderService>();
            var wallet = walletHolderService.BaseWallet;
            var rpcClient = walletHolderService.BaseWallet.ActiveRpcClient;

            Account mintAccount = new Account();
            Account tokenAccount = new Account();

            var fromAccount = walletHolderService.BaseWallet.Account;

            RequestResult<ResponseValue<ulong>> balance =
                await rpcClient.GetBalanceAsync(wallet.Account.PublicKey, Commitment.Confirmed);

            if (balance.Result != null && balance.Result.Value < SolanaUtils.SolToLamports / 10)
            {
                LoggingService.Log("Sol balance is low. Minting may fail", true);
            }

            Debug.Log($"Balance: {balance.Result.Value} ");
            Debug.Log($"Mint key : {mintAccount.PublicKey} ");

            var blockHash = await rpcClient.GetRecentBlockHashAsync();
            var rentMint = await rpcClient.GetMinimumBalanceForRentExemptionAsync(
                TokenProgram.MintAccountDataSize,
                Commitment.Confirmed
            );
            var rentToken = await rpcClient.GetMinimumBalanceForRentExemptionAsync(
                TokenProgram.TokenAccountDataSize,
                Commitment.Confirmed
            );

            Debug.Log($"Token key : {tokenAccount.PublicKey} ");

            //2. create a mint and a token
            var createMintAccount = SystemProgram.CreateAccount(
                fromAccount,
                mintAccount,
                rentMint.Result,
                TokenProgram.MintAccountDataSize,
                TokenProgram.ProgramIdKey
            );
            var initializeMint = TokenProgram.InitializeMint(
                mintAccount.PublicKey,
                0,
                fromAccount.PublicKey,
                fromAccount.PublicKey
            );
            var createTokenAccount = SystemProgram.CreateAccount(
                fromAccount,
                tokenAccount,
                rentToken.Result,
                TokenProgram.TokenAccountDataSize,
                TokenProgram.ProgramIdKey
            );
            var initializeMintAccount = TokenProgram.InitializeAccount(
                tokenAccount.PublicKey,
                mintAccount.PublicKey,
                fromAccount.PublicKey
            );

            var mintTo = TokenProgram.MintTo(
                mintAccount.PublicKey,
                tokenAccount,
                1,
                fromAccount.PublicKey
            );

            // If you freeze the account the users will not be able to transfer the NFTs anywhere or burn them
            /*var freezeAccount = TokenProgram.FreezeAccount(
                tokenAccount,
                mintAccount,
                fromAccount,
                TokenProgram.ProgramIdKey
            );*/

            // PDA Metadata
            PublicKey metadataAddressPDA;
            byte nonce;
            PublicKey.TryFindProgramAddress(
                new List<byte[]>()
                {
                    Encoding.UTF8.GetBytes("metadata"),
                    MetadataProgram.ProgramIdKey,
                    mintAccount.PublicKey
                },
                MetadataProgram.ProgramIdKey,
                out metadataAddressPDA,
                out nonce
            );

            Console.WriteLine($"PDA METADATA: {metadataAddressPDA}");

            // PDA master edition (Makes sure there can only be one minted) 
            PublicKey masterEditionAddress;

            PublicKey.TryFindProgramAddress(
                new List<byte[]>()
                {
                    Encoding.UTF8.GetBytes("metadata"),
                    MetadataProgram.ProgramIdKey,
                    mintAccount.PublicKey,
                    Encoding.UTF8.GetBytes("edition"),
                },
                MetadataProgram.ProgramIdKey,
                out masterEditionAddress,
                out nonce
            );
            Console.WriteLine($"PDA MASTER: {masterEditionAddress}");

            // Craetors
            var creator1 = new Creator(fromAccount.PublicKey, 100, false);

            // Meta Data
            var data = new Metadata()
            {
                name = name,
                symbol = symbol,
                uri = metaDataUri,
                creators = new List<Creator>() {creator1},
                sellerFeeBasisPoints = 77
            };

            var signers = new List<Account> {fromAccount, mintAccount, tokenAccount};
            var transactionBuilder = new TransactionBuilder()
                .SetRecentBlockHash(blockHash.Result.Value.Blockhash)
                .SetFeePayer(fromAccount)
                .AddInstruction(createMintAccount)
                .AddInstruction(initializeMint)
                .AddInstruction(createTokenAccount)
                .AddInstruction(initializeMintAccount)
                .AddInstruction(mintTo)
                //.AddInstruction(freezeAccount)
                .AddInstruction(
                    MetadataProgram.CreateMetadataAccount(
                        metadataAddressPDA, // PDA
                        mintAccount,
                        fromAccount.PublicKey,
                        fromAccount.PublicKey,
                        fromAccount.PublicKey, // update Authority 
                        data, // DATA
                        TokenStandard.NonFungible,
                        true,
                        true, // ISMUTABLE,
                        masterEditionKey: null,
                        1,
                        0UL,
                        MetadataVersion.V3
                    )
                )
                .AddInstruction(
                    MetadataProgram.SignMetadata(
                        metadataAddressPDA,
                        creator1.key
                    )
                )
               .AddInstruction(
                    MetadataProgram.PuffMetada(
                        metadataAddressPDA
                    )
                )
                /*.AddInstruction(
                    MetadataProgram.CreateMasterEdition(
                        1,
                        masterEditionAddress,
                        mintAccount,
                        fromAccount.PublicKey,
                        fromAccount.PublicKey,
                        fromAccount.PublicKey,
                        metadataAddressPDA
                    )
                )*/;

            byte[] transaction = transactionBuilder.Build(signers);
            Transaction deserializedTransaction = Transaction.Deserialize(transaction);
            Transaction signedTransaction =
                await walletHolderService.BaseWallet.SignTransaction(deserializedTransaction);

            var transactionSignature =
                await walletHolderService.BaseWallet.ActiveRpcClient.SendTransactionAsync(
                    Convert.ToBase64String(signedTransaction.Serialize()), true, Commitment.Finalized);

            if (!transactionSignature.WasSuccessful)
            {
                mintDone?.Invoke(false);
                LoggingService
                    .Log("Mint was not successfull: " + transactionSignature.Reason, true);
            }
            else
            {
                ServiceFactory.Resolve<TransactionService>().CheckSignatureStatus(transactionSignature.Result,
                    success =>
                    {
                        mintDone?.Invoke(success);
                        LoggingService.Log("Mint Successfull! Woop woop!", true);
                        MessageRouter.RaiseMessage(new NftMintFinishedMessage());
                    }, null, TransactionService.TransactionResult.confirmed);
            }

            Debug.Log(transactionSignature.Reason);
            Debug.Log(transactionSignature.Result);
            return transactionSignature.Result;
        }`

from solplay_unity_sdk.

Woody4618 avatar Woody4618 commented on June 27, 2024

Doesnt seem to work with wallet adapter though for some reason only with ingame wallet. Ill write the magic block devs.

from solplay_unity_sdk.

BlackDemonZyT avatar BlackDemonZyT commented on June 27, 2024

Alright, i appreciate your help, i hope it gets fixed soon, there are people waiting for me xD

from solplay_unity_sdk.

Woody4618 avatar Woody4618 commented on June 27, 2024

Hey, its fixed in Unity SDK version 0.1.1 please update and this is the updated mint code:

public async Task<string> MintNftWithMetaData(string metaDataUri, string name, string symbol, Action<bool> mintDone = null)
        {
            var walletHolderService = ServiceFactory.Resolve<WalletHolderService>();
            var wallet = walletHolderService.BaseWallet;
            var rpcClient = walletHolderService.BaseWallet.ActiveRpcClient;

            Account mint = new Account();
            var associatedTokenAccount = AssociatedTokenAccountProgram
                .DeriveAssociatedTokenAccount(wallet.Account, mint.PublicKey);
            
            var fromAccount = walletHolderService.BaseWallet.Account;

            RequestResult<ResponseValue<ulong>> balance =
                await rpcClient.GetBalanceAsync(wallet.Account.PublicKey, Commitment.Confirmed);

            if (balance.Result != null && balance.Result.Value < SolanaUtils.SolToLamports / 10)
            {
                LoggingService.Log("Sol balance is low. Minting may fail", true);
            }

            Debug.Log($"Balance: {balance.Result.Value} ");
            Debug.Log($"Mint key : {mint.PublicKey} ");

            var blockHash = await rpcClient.GetRecentBlockHashAsync();
            var rentMint = await rpcClient.GetMinimumBalanceForRentExemptionAsync(
                TokenProgram.MintAccountDataSize,
                Commitment.Confirmed
            );
            var rentToken = await rpcClient.GetMinimumBalanceForRentExemptionAsync(
                TokenProgram.TokenAccountDataSize,
                Commitment.Confirmed
            );


            //2. create a mint and a token
            var createMintAccount = SystemProgram.CreateAccount(
                fromAccount,
                mint,
                rentMint.Result,
                TokenProgram.MintAccountDataSize,
                TokenProgram.ProgramIdKey
            );
            var initializeMint = TokenProgram.InitializeMint(
                mint.PublicKey,
                0,
                fromAccount.PublicKey,
                fromAccount.PublicKey
            );
            var createTokenAccount = AssociatedTokenAccountProgram.CreateAssociatedTokenAccount(
                fromAccount,
                fromAccount,
                mint.PublicKey);

            var mintTo = TokenProgram.MintTo(
                mint.PublicKey,
                associatedTokenAccount,
                1,
                fromAccount.PublicKey
            );

            // If you freeze the account the users will not be able to transfer the NFTs anywhere or burn them
            /*var freezeAccount = TokenProgram.FreezeAccount(
                tokenAccount,
                mintAccount,
                fromAccount,
                TokenProgram.ProgramIdKey
            );*/

            // PDA Metadata
            PublicKey metadataAddressPDA;
            byte nonce;
            PublicKey.TryFindProgramAddress(
                new List<byte[]>()
                {
                    Encoding.UTF8.GetBytes("metadata"),
                    MetadataProgram.ProgramIdKey,
                    mint.PublicKey
                },
                MetadataProgram.ProgramIdKey,
                out metadataAddressPDA,
                out nonce
            );

            Console.WriteLine($"PDA METADATA: {metadataAddressPDA}");

            // PDA master edition (Makes sure there can only be one minted) 
            PublicKey masterEditionAddress;

            PublicKey.TryFindProgramAddress(
                new List<byte[]>()
                {
                    Encoding.UTF8.GetBytes("metadata"),
                    MetadataProgram.ProgramIdKey,
                    mint.PublicKey,
                    Encoding.UTF8.GetBytes("edition"),
                },
                MetadataProgram.ProgramIdKey,
                out masterEditionAddress,
                out nonce
            );
            Console.WriteLine($"PDA MASTER: {masterEditionAddress}");

            // Craetors
            var creator1 = new Creator(fromAccount.PublicKey, 100, false);

            // Meta Data
            var data = new Metadata()
            {
                name = name,
                symbol = symbol,
                uri = metaDataUri,
                creators = new List<Creator>() {creator1},
                sellerFeeBasisPoints = 77
            };

            var signers = new List<Account> {fromAccount, mint};
            var transactionBuilder = new TransactionBuilder()
                .SetRecentBlockHash(blockHash.Result.Value.Blockhash)
                .SetFeePayer(fromAccount)
                .AddInstruction(createMintAccount)
                .AddInstruction(initializeMint)
                .AddInstruction(createTokenAccount)
                .AddInstruction(mintTo)
                //.AddInstruction(freezeAccount)
                .AddInstruction(
                    MetadataProgram.CreateMetadataAccount(
                        metadataAddressPDA, // PDA
                        mint,
                        fromAccount.PublicKey,
                        fromAccount.PublicKey,
                        fromAccount.PublicKey, // update Authority 
                        data, // DATA
                        TokenStandard.NonFungible,
                        true,
                        true, // ISMUTABLE,
                        masterEditionKey: null,
                        1,
                        0UL,
                        MetadataVersion.V3
                    )
                )
                .AddInstruction(
                    MetadataProgram.SignMetadata(
                        metadataAddressPDA,
                        creator1.key
                    )
                )
               .AddInstruction(
                    MetadataProgram.PuffMetada(
                        metadataAddressPDA
                    )
                )
                /*.AddInstruction(
                    MetadataProgram.CreateMasterEdition(
                        1,
                        masterEditionAddress,
                        mintAccount,
                        fromAccount.PublicKey,
                        fromAccount.PublicKey,
                        fromAccount.PublicKey,
                        metadataAddressPDA
                    )
                )*/;

            var tx = Transaction.Deserialize(transactionBuilder.Build(new List<Account> {fromAccount, mint}));
            var res = await walletHolderService.BaseWallet.SignAndSendTransaction(tx, true, Commitment.Confirmed);
            Debug.Log(res.Result);

            if (!res.WasSuccessful)
            {
                mintDone?.Invoke(false);
                LoggingService
                    .Log("Mint was not successfull: " + res.Reason, true);
            }
            else
            {
                ServiceFactory.Resolve<TransactionService>().CheckSignatureStatus(res.Result,
                    success =>
                    {
                        mintDone?.Invoke(success);
                        LoggingService.Log("Mint Successfull! Woop woop!", true);
                        MessageRouter.RaiseMessage(new NftMintFinishedMessage());
                    }, null, TransactionService.TransactionResult.confirmed);
            }

            return res.Result;
        }

from solplay_unity_sdk.

Related Issues (5)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.