From 149d27fcc6d4536c4ab881149311a311dbc5a6e6 Mon Sep 17 00:00:00 2001 From: CanvasL <746591811@qq.com> Date: Sun, 15 Sep 2024 10:15:54 +0800 Subject: [PATCH] fix: metaplex lib --- extensions/puppet/client/index.ts | 98 ++++++++++------- extensions/puppet/keypair.json | 1 + extensions/puppet/package.json | 8 +- extensions/puppet/pnpm-lock.yaml | 171 ++++++++++++++++++++++++++++++ 4 files changed, 234 insertions(+), 44 deletions(-) create mode 100644 extensions/puppet/keypair.json diff --git a/extensions/puppet/client/index.ts b/extensions/puppet/client/index.ts index 59990da..dbc730e 100644 --- a/extensions/puppet/client/index.ts +++ b/extensions/puppet/client/index.ts @@ -1,25 +1,37 @@ import yargs from "yargs"; import { hideBin } from "yargs/helpers"; import * as anchor from "@coral-xyz/anchor"; -import { Keypair, PublicKey } from "@solana/web3.js"; +import { PublicKey } from "@solana/web3.js"; import { Puppet } from "../target/types/puppet"; import { Program } from "@coral-xyz/anchor"; import { - Metaplex, - irysStorage, - keypairIdentity, -} from "@metaplex-foundation/js"; -import { Connection, clusterApiUrl } from "@solana/web3.js"; + generateSigner, + percentAmount, + createSignerFromKeypair, + signerIdentity, +} from "@metaplex-foundation/umi"; +import { createUmi } from '@metaplex-foundation/umi-bundle-defaults' +import { + mplTokenMetadata, + createNft, + fetchDigitalAsset, +} from "@metaplex-foundation/mpl-token-metadata"; import { getAssociatedTokenAddress, TOKEN_PROGRAM_ID, ASSOCIATED_TOKEN_PROGRAM_ID, } from "@solana/spl-token"; +import fs from "fs"; +import path from "path"; const DEPHY_ID_PROGRAM = new PublicKey( "ATokenGPvbdGVxr1b2hvZbsiqW5xWH25efTNsLJA8knL" ); -const DEV_RPC = "https://api.apr.dev"; +const DEV_RPC = "https://api.devnet.solana.com"; +const SECRET_KEY = fs.readFileSync( + path.join(__dirname, '../keypair.json'), + "utf-8" +) yargs(hideBin(process.argv)) .command( @@ -28,26 +40,29 @@ yargs(hideBin(process.argv)) { url: { type: "string", demandOption: true }, name: { type: "string", demandOption: true }, - privatekey: { type: "string", demandOption: true }, }, async (args) => { - const payer = Keypair.fromSecretKey( - new Uint8Array(JSON.parse(args.privatekey)) - ); - console.log("pubkey:", payer.publicKey); - const connection = new Connection(clusterApiUrl("devnet")); - const metaplex = Metaplex.make(connection) - .use(keypairIdentity(payer)) - .use(irysStorage()); + const umi = createUmi(DEV_RPC); + + const keypair = umi.eddsa.createKeypairFromSecretKey(new Uint8Array(JSON.parse(SECRET_KEY))); - const { nft: collectionNft } = await metaplex.nfts().create({ - uri: args.url, - name: args.name, - sellerFeeBasisPoints: 500, // Represents 5.00%. - isCollection: true, - }); + console.log(keypair.publicKey) - console.log("mplCollection:", collectionNft.address.toBase58()); + const signer = createSignerFromKeypair(umi, keypair); + + umi.use(signerIdentity(signer)) + umi.use(mplTokenMetadata()) + const mint = generateSigner(umi) + await createNft(umi, { + mint, + name: args.name, + uri: args.url, + sellerFeeBasisPoints: percentAmount(5.5), + isCollection: true + }).sendAndConfirm(umi) + + const asset = await fetchDigitalAsset(umi, mint.publicKey) + console.log("mplCollection:", asset.mint.publicKey); } ) .command( @@ -60,27 +75,30 @@ yargs(hideBin(process.argv)) privatekey: { type: "string", demandOption: true }, }, async (args) => { - const payer = Keypair.fromSecretKey( - new Uint8Array(JSON.parse(args.privatekey)) - ); - const connection = new Connection(clusterApiUrl("devnet")); - const metaplex = Metaplex.make(connection) - .use(keypairIdentity(payer)) - .use(irysStorage()); + const umi = createUmi(DEV_RPC); + + const keypair = umi.eddsa.createKeypairFromSecretKey(new Uint8Array(JSON.parse(SECRET_KEY))); - // Create an NFT under the specified collection. - const { nft } = await metaplex.nfts().create({ - uri: args.url, - name: args.name, - sellerFeeBasisPoints: 500, // Represents 5.00%. - collection: new PublicKey(args.collection), - }); + const signer = createSignerFromKeypair(umi, keypair); - console.log("mpl_mint:", nft.address.toBase58()); + umi.use(signerIdentity(signer)) + umi.use(mplTokenMetadata()) + const mint = generateSigner(umi) + await createNft(umi, { + mint, + name: args.name, + uri: args.url, + sellerFeeBasisPoints: percentAmount(5.5), + isCollection: true + }).sendAndConfirm(umi) + + const asset = await fetchDigitalAsset(umi, mint.publicKey) + console.log("asset:", asset.publicKey) + console.log("mpl_mint:", asset.mint.publicKey); const mplAta = await getAssociatedTokenAddress( - nft.address, // The mint address of the created NFT - payer.publicKey, // The wallet address of the NFT owner + new anchor.web3.PublicKey(asset.mint.publicKey), // The mint address of the created NFT + new anchor.web3.PublicKey(keypair.publicKey), // The wallet address of the NFT owner false, TOKEN_PROGRAM_ID, ASSOCIATED_TOKEN_PROGRAM_ID diff --git a/extensions/puppet/keypair.json b/extensions/puppet/keypair.json new file mode 100644 index 0000000..46e5710 --- /dev/null +++ b/extensions/puppet/keypair.json @@ -0,0 +1 @@ +[3,98,129,50,166,110,222,238,8,122,218,16,146,91,239,253,219,217,78,248,136,253,20,247,228,232,255,72,51,52,152,232,59,56,47,66,135,204,197,194,83,150,196,82,143,128,67,241,163,111,202,98,165,17,242,25,253,86,24,192,84,74,103,132] \ No newline at end of file diff --git a/extensions/puppet/package.json b/extensions/puppet/package.json index d5cb732..b62a2ec 100644 --- a/extensions/puppet/package.json +++ b/extensions/puppet/package.json @@ -6,11 +6,11 @@ "test": "ts-mocha -p ./tsconfig.json -t 1000000 tests/**/*.ts", "cli": "ts-node ./client/index.ts" }, - "dependencies": { - "@coral-xyz/anchor": "^0.30.1" - }, "devDependencies": { - "@metaplex-foundation/js": "^0.20.1", + "@coral-xyz/anchor": "^0.30.1", + "@metaplex-foundation/umi": "^0.9.2", + "@metaplex-foundation/umi-bundle-defaults": "^0.9.2", + "@metaplex-foundation/mpl-token-metadata": "^3.2.1", "@solana/spl-token": "^0.4.8", "@solana/web3.js": "^1.95.3", "@types/bn.js": "^5.1.0", diff --git a/extensions/puppet/pnpm-lock.yaml b/extensions/puppet/pnpm-lock.yaml index 61f100b..ac6f501 100644 --- a/extensions/puppet/pnpm-lock.yaml +++ b/extensions/puppet/pnpm-lock.yaml @@ -8,11 +8,20 @@ dependencies: '@coral-xyz/anchor': specifier: ^0.30.1 version: 0.30.1 + '@metaplex-foundation/umi': + specifier: ^0.9.2 + version: 0.9.2 + '@metaplex-foundation/umi-bundle-defaults': + specifier: ^0.9.2 + version: 0.9.2(@metaplex-foundation/umi@0.9.2)(@solana/web3.js@1.95.3) devDependencies: '@metaplex-foundation/js': specifier: ^0.20.1 version: 0.20.1(arweave@1.15.1)(fastestsmallesttextencoderdecoder@1.0.22)(typescript@4.3.5) + '@metaplex-foundation/mpl-token-metadata': + specifier: ^3.2.1 + version: 3.2.1(@metaplex-foundation/umi@0.9.2) '@solana/spl-token': specifier: ^0.4.8 version: 0.4.8(@solana/web3.js@1.95.3)(fastestsmallesttextencoderdecoder@1.0.22)(typescript@4.3.5) @@ -716,6 +725,168 @@ packages: - utf-8-validate dev: true + /@metaplex-foundation/mpl-token-metadata@3.2.1(@metaplex-foundation/umi@0.9.2): + resolution: {integrity: sha512-26W1NhQwDWmLOg/pBRYut7x/vEs/5kFS2sWVEY5/X0f2jJOLhnd4NaZQcq+5u+XZsXvm1jq2AtrRGPNK43oqWQ==} + peerDependencies: + '@metaplex-foundation/umi': '>= 0.8.2 < 1' + dependencies: + '@metaplex-foundation/mpl-toolbox': 0.9.4(@metaplex-foundation/umi@0.9.2) + '@metaplex-foundation/umi': 0.9.2 + dev: true + + /@metaplex-foundation/mpl-toolbox@0.9.4(@metaplex-foundation/umi@0.9.2): + resolution: {integrity: sha512-fd6JxfoLbj/MM8FG2x91KYVy1U6AjBQw4qjt7+Da3trzQaWnSaYHDcYRG/53xqfvZ9qofY1T2t53GXPlD87lnQ==} + peerDependencies: + '@metaplex-foundation/umi': '>= 0.8.2 < 1' + dependencies: + '@metaplex-foundation/umi': 0.9.2 + dev: true + + /@metaplex-foundation/umi-bundle-defaults@0.9.2(@metaplex-foundation/umi@0.9.2)(@solana/web3.js@1.95.3): + resolution: {integrity: sha512-kV3tfvgvRjVP1p9OFOtH+ibOtN9omVJSwKr0We4/9r45e5LTj+32su0V/rixZUkG1EZzzOYBsxhtIE0kIw/Hrw==} + peerDependencies: + '@metaplex-foundation/umi': ^0.9.2 + '@solana/web3.js': ^1.72.0 + dependencies: + '@metaplex-foundation/umi': 0.9.2 + '@metaplex-foundation/umi-downloader-http': 0.9.2(@metaplex-foundation/umi@0.9.2) + '@metaplex-foundation/umi-eddsa-web3js': 0.9.2(@metaplex-foundation/umi@0.9.2)(@solana/web3.js@1.95.3) + '@metaplex-foundation/umi-http-fetch': 0.9.2(@metaplex-foundation/umi@0.9.2) + '@metaplex-foundation/umi-program-repository': 0.9.2(@metaplex-foundation/umi@0.9.2) + '@metaplex-foundation/umi-rpc-chunk-get-accounts': 0.9.2(@metaplex-foundation/umi@0.9.2) + '@metaplex-foundation/umi-rpc-web3js': 0.9.2(@metaplex-foundation/umi@0.9.2)(@solana/web3.js@1.95.3) + '@metaplex-foundation/umi-serializer-data-view': 0.9.2(@metaplex-foundation/umi@0.9.2) + '@metaplex-foundation/umi-transaction-factory-web3js': 0.9.2(@metaplex-foundation/umi@0.9.2)(@solana/web3.js@1.95.3) + '@solana/web3.js': 1.95.3 + transitivePeerDependencies: + - encoding + dev: false + + /@metaplex-foundation/umi-downloader-http@0.9.2(@metaplex-foundation/umi@0.9.2): + resolution: {integrity: sha512-tzPT9hBwenzTzAQg07rmsrqZfgguAXELbcJrsYMoASp5VqWFXYIP00g94KET6XLjWUXH4P1J2zoa6hGennPXHA==} + peerDependencies: + '@metaplex-foundation/umi': ^0.9.2 + dependencies: + '@metaplex-foundation/umi': 0.9.2 + dev: false + + /@metaplex-foundation/umi-eddsa-web3js@0.9.2(@metaplex-foundation/umi@0.9.2)(@solana/web3.js@1.95.3): + resolution: {integrity: sha512-hhPCxXbYIp4BC4z9gK78sXpWLkNSrfv4ndhF5ruAkdIp7GcRVYKj0QnOUO6lGYGiIkNlw20yoTwOe1CT//OfTQ==} + peerDependencies: + '@metaplex-foundation/umi': ^0.9.2 + '@solana/web3.js': ^1.72.0 + dependencies: + '@metaplex-foundation/umi': 0.9.2 + '@metaplex-foundation/umi-web3js-adapters': 0.9.2(@metaplex-foundation/umi@0.9.2)(@solana/web3.js@1.95.3) + '@noble/curves': 1.6.0 + '@solana/web3.js': 1.95.3 + dev: false + + /@metaplex-foundation/umi-http-fetch@0.9.2(@metaplex-foundation/umi@0.9.2): + resolution: {integrity: sha512-YCZuBu24T9ZzEDe4+w12LEZm/fO9pkyViZufGgASC5NX93814Lvf6Ssjn/hZzjfA7CvZbvLFbmujc6CV3Q/m9Q==} + peerDependencies: + '@metaplex-foundation/umi': ^0.9.2 + dependencies: + '@metaplex-foundation/umi': 0.9.2 + node-fetch: 2.7.0 + transitivePeerDependencies: + - encoding + dev: false + + /@metaplex-foundation/umi-options@0.8.9: + resolution: {integrity: sha512-jSQ61sZMPSAk/TXn8v8fPqtz3x8d0/blVZXLLbpVbo2/T5XobiI6/MfmlUosAjAUaQl6bHRF8aIIqZEFkJiy4A==} + + /@metaplex-foundation/umi-program-repository@0.9.2(@metaplex-foundation/umi@0.9.2): + resolution: {integrity: sha512-g3+FPqXEmYsBa8eETtUE2gb2Oe3mqac0z3/Ur1TvAg5TtIy3mzRzOy/nza+sgzejnfcxcVg835rmpBaxpBnjDA==} + peerDependencies: + '@metaplex-foundation/umi': ^0.9.2 + dependencies: + '@metaplex-foundation/umi': 0.9.2 + dev: false + + /@metaplex-foundation/umi-public-keys@0.8.9: + resolution: {integrity: sha512-CxMzN7dgVGOq9OcNCJe2casKUpJ3RmTVoOvDFyeoTQuK+vkZ1YSSahbqC1iGuHEtKTLSjtWjKvUU6O7zWFTw3Q==} + dependencies: + '@metaplex-foundation/umi-serializers-encodings': 0.8.9 + + /@metaplex-foundation/umi-rpc-chunk-get-accounts@0.9.2(@metaplex-foundation/umi@0.9.2): + resolution: {integrity: sha512-YRwVf6xH0jPBAUgMhEPi+UbjioAeqTXmjsN2TnmQCPAmHbrHrMRj0rlWYwFLWAgkmoxazYrXP9lqOFRrfOGAEA==} + peerDependencies: + '@metaplex-foundation/umi': ^0.9.2 + dependencies: + '@metaplex-foundation/umi': 0.9.2 + dev: false + + /@metaplex-foundation/umi-rpc-web3js@0.9.2(@metaplex-foundation/umi@0.9.2)(@solana/web3.js@1.95.3): + resolution: {integrity: sha512-MqcsBz8B4wGl6jxsf2Jo/rAEpYReU9VCSR15QSjhvADHMmdFxCIZCCAgE+gDE2Vuanfl437VhOcP3g5Uw8C16Q==} + peerDependencies: + '@metaplex-foundation/umi': ^0.9.2 + '@solana/web3.js': ^1.72.0 + dependencies: + '@metaplex-foundation/umi': 0.9.2 + '@metaplex-foundation/umi-web3js-adapters': 0.9.2(@metaplex-foundation/umi@0.9.2)(@solana/web3.js@1.95.3) + '@solana/web3.js': 1.95.3 + dev: false + + /@metaplex-foundation/umi-serializer-data-view@0.9.2(@metaplex-foundation/umi@0.9.2): + resolution: {integrity: sha512-5vGptadJxUxvUcyrwFZxXlEc6Q7AYySBesizCtrBFUY8w8PnF2vzmS45CP1MLySEATNH6T9mD4Rs0tLb87iQyA==} + peerDependencies: + '@metaplex-foundation/umi': ^0.9.2 + dependencies: + '@metaplex-foundation/umi': 0.9.2 + dev: false + + /@metaplex-foundation/umi-serializers-core@0.8.9: + resolution: {integrity: sha512-WT82tkiYJ0Qmscp7uTj1Hz6aWQPETwaKLAENAUN5DeWghkuBKtuxyBKVvEOuoXerJSdhiAk0e8DWA4cxcTTQ/w==} + + /@metaplex-foundation/umi-serializers-encodings@0.8.9: + resolution: {integrity: sha512-N3VWLDTJ0bzzMKcJDL08U3FaqRmwlN79FyE4BHj6bbAaJ9LEHjDQ9RJijZyWqTm0jE7I750fU7Ow5EZL38Xi6Q==} + dependencies: + '@metaplex-foundation/umi-serializers-core': 0.8.9 + + /@metaplex-foundation/umi-serializers-numbers@0.8.9: + resolution: {integrity: sha512-NtBf1fnVNQJHFQjLFzRu2i9GGnigb9hOm/Gfrk628d0q0tRJB7BOM3bs5C61VAs7kJs4yd+pDNVAERJkknQ7Lg==} + dependencies: + '@metaplex-foundation/umi-serializers-core': 0.8.9 + + /@metaplex-foundation/umi-serializers@0.9.0: + resolution: {integrity: sha512-hAOW9Djl4w4ioKeR4erDZl5IG4iJdP0xA19ZomdaCbMhYAAmG/FEs5khh0uT2mq53/MnzWcXSUPoO8WBN4Q+Vg==} + dependencies: + '@metaplex-foundation/umi-options': 0.8.9 + '@metaplex-foundation/umi-public-keys': 0.8.9 + '@metaplex-foundation/umi-serializers-core': 0.8.9 + '@metaplex-foundation/umi-serializers-encodings': 0.8.9 + '@metaplex-foundation/umi-serializers-numbers': 0.8.9 + + /@metaplex-foundation/umi-transaction-factory-web3js@0.9.2(@metaplex-foundation/umi@0.9.2)(@solana/web3.js@1.95.3): + resolution: {integrity: sha512-fR1Kf21uylMFd1Smkltmj4jTNxhqSWf416owsJ+T+cvJi2VCOcOwq/3UFzOrpz78fA0RhsajKYKj0HYsRnQI1g==} + peerDependencies: + '@metaplex-foundation/umi': ^0.9.2 + '@solana/web3.js': ^1.72.0 + dependencies: + '@metaplex-foundation/umi': 0.9.2 + '@metaplex-foundation/umi-web3js-adapters': 0.9.2(@metaplex-foundation/umi@0.9.2)(@solana/web3.js@1.95.3) + '@solana/web3.js': 1.95.3 + dev: false + + /@metaplex-foundation/umi-web3js-adapters@0.9.2(@metaplex-foundation/umi@0.9.2)(@solana/web3.js@1.95.3): + resolution: {integrity: sha512-RQqUTtHYY9fmEMnq7s3Hiv/81flGaoI0ZVVoafnFVaQLnxU6QBKxtboRZHk43XtD9CiFh5f9izrMJX7iK7KlOA==} + peerDependencies: + '@metaplex-foundation/umi': ^0.9.2 + '@solana/web3.js': ^1.72.0 + dependencies: + '@metaplex-foundation/umi': 0.9.2 + '@solana/web3.js': 1.95.3 + buffer: 6.0.3 + dev: false + + /@metaplex-foundation/umi@0.9.2: + resolution: {integrity: sha512-9i4Acm4pruQfJcpRrc2EauPBwkfDN0I9QTvJyZocIlKgoZwD6A6wH0PViH1AjOVG5CQCd1YI3tJd5XjYE1ElBw==} + dependencies: + '@metaplex-foundation/umi-options': 0.8.9 + '@metaplex-foundation/umi-public-keys': 0.8.9 + '@metaplex-foundation/umi-serializers': 0.9.0 + /@near-js/crypto@0.0.3: resolution: {integrity: sha512-3WC2A1a1cH8Cqrx+0iDjp1ASEEhxN/KHEMENYb0KZH6Hp5bXIY7Akt4quC7JlgJS5ESvEiLa40tS5h0zAhBWGw==} dependencies: