Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

hybrid guide - extend scripts with boilerplate #291

Merged
merged 2 commits into from
Jan 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/components/products/guides/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,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