Browse Source

hybrid guide - extend scripts with boilerplate (#291)

* extend scripts with boilerplate

* add overall section
MarkSackerberg 10 months ago
parent
commit
49b9b65971

+ 6 - 0
src/components/products/guides/index.js

@@ -187,6 +187,12 @@ export const guides = {
               created: '2024-09-20',
               updated: null, // null means it's never been updated
             },
+            {
+              title: 'MPL-Hybrid / MPL-404',
+              href: '/mpl-hybrid/guides',
+              created: '2025-01-06',
+              updated: null, // null means it's never been updated
+            },
           ],
         },
         {

+ 107 - 33
src/pages/mpl-hybrid/guides/create-your-first-hybrid-collection.md

@@ -248,20 +248,49 @@ console.log(`Escrow created! https://explorer.solana.com/tx/${signature}?cluster
 {% totem-accordion title="Send Assets to the Escrow" %}
 
 ```javascript
-import { transfer } from '@metaplex-foundation/mpl-core'
+import { keypairIdentity, publicKey } from "@metaplex-foundation/umi";
+import {
+  MPL_HYBRID_PROGRAM_ID,
+  mplHybrid,
+} from "@metaplex-foundation/mpl-hybrid";
+import { readFileSync } from "fs";
+import { mplTokenMetadata } from "@metaplex-foundation/mpl-token-metadata";
+import { createUmi } from "@metaplex-foundation/umi-bundle-defaults";
+import {
+  string,
+  publicKey as publicKeySerializer,
+} from "@metaplex-foundation/umi/serializers";
+import { transfer } from "@metaplex-foundation/mpl-core";
 
-// Derive the Escrow
-const escrow = umi.eddsa.findPda(MPL_HYBRID_PROGRAM_ID, [
-  string({ size: 'variable' }).serialize('escrow'),
-  publicKeySerializer().serialize(collection),
-])[0];
+(async () => {
+  const collection = publicKey("<COLLECTION>"); // The collection we are swapping to/from
+  const asset = publicKey("<NFT MINT>"); // Mint Address of the NFT you want to send
+
+  const umi = createUmi("<ENDPOINT>").use(mplHybrid()).use(mplTokenMetadata());
+
+  const wallet = "<path to wallet>"; // The path to your filesystem Wallet
+  const secretKey = JSON.parse(readFileSync(wallet, "utf-8"));
+
+  // Create a keypair from your private key
+  const keypair = umi.eddsa.createKeypairFromSecretKey(
+    new Uint8Array(secretKey)
+  );
+  umi.use(keypairIdentity(keypair));
+
+  // Derive the Escrow
+  const escrow = umi.eddsa.findPda(MPL_HYBRID_PROGRAM_ID, [
+    string({ size: "variable" }).serialize("escrow"),
+    publicKeySerializer().serialize(collection),
+  ])[0];
+
+  // Transfer Asset to it
+  const transferAssetTx = await transfer(umi, {
+    asset,
+    collection,
+    newOwner: escrow,
+  }).sendAndConfirm(umi);
+})();
 
-// Transfer Asset to it
-const transferAssetTx = await transfer(umi, {
-  asset,
-  collection,
-  newOwner: escrow
-}).sendAndConfirm(umi);
 ```
 
 {% /totem-accordion %}
@@ -269,28 +298,73 @@ const transferAssetTx = await transfer(umi, {
 {% totem-accordion title="Send Fungible Tokens to the Escrow" %}
 
 ```javascript
-import { transactionBuilder } from '@metaplex-foundation/umi'
-import { createTokenIfMissing, transferTokens } from '@metaplex-foundation/mpl-toolbox'
+import {
+  keypairIdentity,
+  publicKey,
+  transactionBuilder,
+} from "@metaplex-foundation/umi";
+import {
+  createTokenIfMissing,
+  findAssociatedTokenPda,
+  transferTokens,
+} from "@metaplex-foundation/mpl-toolbox";
+import {
+  MPL_HYBRID_PROGRAM_ID,
+  mplHybrid,
+} from "@metaplex-foundation/mpl-hybrid";
+import { readFileSync } from "fs";
+import { mplTokenMetadata } from "@metaplex-foundation/mpl-token-metadata";
+import { createUmi } from "@metaplex-foundation/umi-bundle-defaults";
+import {
+  string,
+  publicKey as publicKeySerializer,
+} from "@metaplex-foundation/umi/serializers";
+
+(async () => {
+  const collection = publicKey("<COLLECTION>"); // The collection we are swapping to/from
+  const token = publicKey("<TOKEN MINT>"); // The token we are swapping to/from
+
+  const umi = createUmi("<ENDPOINT>").use(mplHybrid()).use(mplTokenMetadata());
+
+  const wallet = "<path to wallet>"; // The path to your filesystem Wallet
+  const secretKey = JSON.parse(readFileSync(wallet, "utf-8"));
+
+  // Create a keypair from your private key
+  const keypair = umi.eddsa.createKeypairFromSecretKey(
+    new Uint8Array(secretKey)
+  );
+  umi.use(keypairIdentity(keypair));
+
+  // Derive the Escrow
+  const escrow = umi.eddsa.findPda(MPL_HYBRID_PROGRAM_ID, [
+    string({ size: "variable" }).serialize("escrow"),
+    publicKeySerializer().serialize(collection),
+  ])[0];
+
+  // Transfer Fungible Tokens to it (after creating the ATA if needed)
+  const transferTokenTx = await transactionBuilder()
+    .add(
+      createTokenIfMissing(umi, {
+        mint: token,
+        owner: escrow,
+      })
+    )
+    .add(
+      transferTokens(umi, {
+        source: findAssociatedTokenPda(umi, {
+          mint: token,
+          owner: umi.identity.publicKey,
+        }),
+        destination: findAssociatedTokenPda(umi, {
+          mint: token,
+          owner: escrow,
+        }),
+        amount: 300000000,
+      })
+    )
+    .sendAndConfirm(umi);
+})();
 
-// Derive the Escrow
-const escrow = umi.eddsa.findPda(MPL_HYBRID_PROGRAM_ID, [
-  string({ size: 'variable' }).serialize('escrow'),
-  publicKeySerializer().serialize(collection),
-])[0];
-
-// Transfer Fungible Tokens to it (after creating the ATA if needed)
-const transferTokenTx = await transactionBuilder().add(
-  createTokenIfMissing(umi, { 
-      mint: token, 
-      owner: escrow 
-  })
-).add(
-  transferTokens(umi, {
-      source: findAssociatedTokenPda(umi, { mint: token, owner: umi.identity.publicKey }),
-      destination: findAssociatedTokenPda(umi, { mint: token, owner: escrow }),
-      amount,
-  })
-).sendAndConfirm(umi)
 ```
 {% /totem-accordion %}