Skip to content

Commit

Permalink
hybrid guide - extend scripts with boilerplate (#291)
Browse files Browse the repository at this point in the history
* extend scripts with boilerplate

* add overall section
  • Loading branch information
MarkSackerberg authored Jan 14, 2025
1 parent 6587c61 commit 49b9b65
Show file tree
Hide file tree
Showing 2 changed files with 113 additions and 33 deletions.
6 changes: 6 additions & 0 deletions src/components/products/guides/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
},
],
},
{
Expand Down
140 changes: 107 additions & 33 deletions src/pages/mpl-hybrid/guides/create-your-first-hybrid-collection.md
Original file line number Diff line number Diff line change
Expand Up @@ -248,49 +248,123 @@ 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 %}

{% 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 %}

Expand Down

0 comments on commit 49b9b65

Please sign in to comment.