### Install Hydra SDK (JavaScript) Source: https://developers.metaplex.com/hydra/quick-start Installs the Hydra SDK package for JavaScript projects using npm or yarn. ```bash yarn add @glasseaters/hydra-sdk ``` -------------------------------- ### Initialize and Use Hydra Wallet (JavaScript) Source: https://developers.metaplex.com/hydra/quick-start Demonstrates how to initialize a Hydra wallet, retrieve its details, add members, and distribute funds using the Hydra JavaScript SDK. It covers setting up the connection, generating wallets, initializing the fanout, fetching account data, and sending distribution transactions. ```javascript import { Connection, Keypair, LAMPORTS_PER_SOL } from "@solana/web3.js"; import { ASSOCIATED_TOKEN_PROGRAM_ID, Token, TOKEN_PROGRAM_ID } from "@solana/spl-token"; import { airdrop } from "@metaplex-foundation/amman"; import { Fanout, FanoutClient, FanoutMembershipMintVoucher, FanoutMembershipVoucher, FanoutMint, MembershipModel } from "@glasseaters/hydra-sdk"; const connection = new Connection("devnet", "confirmed"); const authorityWallet = Keypair.generate(); await airdrop(connection, authorityWallet.publicKey, LAMPORTS_PER_SOL * 2); const fanoutSdk = new FanoutClient( connection, newNodeWallet(new Account(authorityWallet.secretKey)) ); // Initialize the Hydra Wallet const { fanout, nativeAccount } = await fanoutSdk.initializeFanout({ totalShares: 100, name: `Your Globally Unique Wallet Name`, membershipModel: MembershipModel.Wallet, }); // fanout is your fanout config address // nativeAccount is your account address // Retrieve the onchain Hydra Wallet const fanoutAccount = await fanoutSdk.fetch( fanout, Fanout ); console.log(fanoutAccount); // Shows you all the parameters in your Hydra // This is your Hydra Wallet Address let HydraAccountKey = fanoutAccount.accountKey; // this is the same thing as nativeAccount above // If you only know the Hydra name, this is how you can retrieve the account key let name = `Your Globally Unique Wallet Name`; let [key, bump] = await fanoutSdk.fanoutKey(name); let [holdingAccount, bump] = await fanoutSdk.nativeAccount(key); // Add members const member1 = new Keypair(); const { membershipAccount1 } = await fanoutSdk.addMemberWallet({ fanout: init.fanout, fanoutNativeAccount: init.nativeAccount, membershipKey: member1.publicKey, shares: 10, }); // Repeat for all members until sum(shares) == totalShares from initialization... // Send some Sol to the Hydra Wallet so you can distribute await airdrop(connection, HydraAccountKey, 2); // Generate the distribution instructions let distMember1 = await fanoutSdk.distributeWalletMemberInstructions({ distributeForMint: false, member: member1.wallet.publicKey, fanout: fanout, payer: authorityWallet.publicKey, // This can be changed to whoever sends the tx }); // Send the distribution instructions const tx = await fanoutSdk.sendInstructions([...distMember1.instructions], [authorityWallet], authorityWallet.publicKey); if (!!tx.RpcResponseAndContext.value.err) { const txdetails = await connection.getConfirmedTransaction(tx.TransactionSignature); console.log(txdetails, tx.RpcResponseAndContext.value.err); } // Member1 Should have 0.2 more sol in their wallet ``` -------------------------------- ### Initialize Hydra Wallet Source: https://developers.metaplex.com/hydra/index Initializes a new Hydra wallet with a specified total number of shares, a unique name, and a membership model. Requires a connection to the Solana network and an authority wallet. The creation process costs approximately XXX SOL. ```javascript const connection =newConnection('devnet','confirmed') let fanoutSdk: FanoutClient authorityWallet = Keypair.generate() fanoutSdk =newFanoutClient( connection, newNodeWallet(newAccount(authorityWallet.secretKey)) ) const init =await fanoutSdk.initializeFanout({ totalShares:100, name:`Test${Date.now()}`, membershipModel: MembershipModel.Wallet, }) ``` -------------------------------- ### Initialize Fanout with Token Model and Stake Tokens Source: https://developers.metaplex.com/hydra/index Initializes a Hydra fanout with the Token membership model and demonstrates staking tokens for members. This model associates membership with staked ownership of a specified SPL Token. Distributions are calculated based on staked tokens. ```javascript const membershipMintPublicKey ='SPL-TokenPublicKey' const{ fanout }=await fanoutSdk.initializeFanout({ totalShares:0, name:`Test${Date.now()}`, membershipModel: MembershipModel.Token, mint: membershipMintPublicKey, }) // Staking tokens const ixs =await fanoutSdk.stakeTokenMemberInstructions({ shares: supply *0.1, fanout: fanout, membershipMintTokenAccount: tokenAcctMember, membershipMint: membershipMint.publicKey, member: member.publicKey, payer: member.publicKey, }) const tx =await fanoutSdk.sendInstructions( ixs.instructions,[member], member.publicKey ) if(!!tx.RpcResponseAndContext.value.err){ const txdetails =await connection.getConfirmedTransaction( tx.TransactionSignature ) console.log(txdetails, tx.RpcResponseAndContext.value.err) } const stake =await membershipMint.getAccountInfo(ixs.output.stakeAccount) ``` -------------------------------- ### Stake Tokens on Behalf of Member Source: https://developers.metaplex.com/hydra/index Demonstrates staking membership tokens on behalf of a member using `stakeForTokenMemberInstructions`. This is useful for use cases like airdropping membership tokens. The `membershipMintTokenAccount` should be the ATA of the Authority, not the member. ```javascript // Example of setting up a Hydra with a in Memory keypair. let authorityWallet = Keypair.generate(); let fanoutSdk =newFanoutClient( connection, newNodeWallet(newAccount(authorityWallet.secretKey)) ); // Setup a Hydra -> Since you configured the SDK with the authority Wallet as the wallet you dont need to pass the signer into the init. const{ fanout }=await fanoutSdk.initializeFanout({ totalShares:0, name:`Test${Date.now()}`, membershipModel: MembershipModel.Token, mint: membershipMint.publicKey }); ... const ixs =await fanoutSdk.stakeForTokenMemberInstructions({ shares: supply *.1, fanout: fanout, membershipMintTokenAccount: tokenAcct, membershipMint: membershipMint.publicKey, fanoutAuthority: authorityWallet.publicKey, member: member.publicKey, payer: authorityWallet.publicKey }); ``` -------------------------------- ### Sign NFT Metadata as Creator Source: https://developers.metaplex.com/hydra/index Enables the Hydra Wallet authority to sign NFT metadata, verifying the wallet as a creator. This is useful for setting up royalty shares. It requires the metadata account, the holding account, and the fanout details. ```typescript // Create Hydra as above. // Set Royalties const allCreators = [{ creator: authorityWallet.publicKey, share: 0 }, { creator: init.fanout, publicKey, share: 100, }] // CREATE NFT Code Adding allCreators as Creator for the NFT const instructions: TransactionInstruction[] = [] instructions.push( /// Create NFT Instructions /// Sign the nft... fanoutSdk.signMetadataInstructions({ metadata: metadataAccount, holdingAccount: init.nativeAccount, fanout: init.fanout, }).instructions ) ///....send instructions to solana ``` -------------------------------- ### Hydra Program IDs Source: https://developers.metaplex.com/hydra/index Provides the Program IDs for the Hydra wallet on different Solana networks. These IDs are essential for interacting with the Hydra program on-chain. ```APIDOC Mainnet: `hyDQ4Nz1eYyegS6JfenyKwKzYxRsCWCriYSAjtzP4Vg` Devnet: `hyDQ4Nz1eYyegS6JfenyKwKzYxRsCWCriYSAjtzP4Vg` ``` -------------------------------- ### Add SPL Token Support to Hydra Wallet Source: https://developers.metaplex.com/hydra/index Updates an existing Hydra wallet to accept a specific SPL token. This involves providing the public key of the SPL token mint. The function returns the updated fanout configuration for the mint and the associated token account. ```javascript const mintPublicKey ='SPL-Token-Public-Key' const{ fanoutForMint, tokenAccount }=await fanoutSdk.initializeFanoutForMint({ fanout, mint: mintPublicKey, }) ``` -------------------------------- ### Distribute SPL Tokens to Wallet Member Source: https://developers.metaplex.com/hydra/index Distributes specified SPL tokens to a wallet member. This is an extension of the wallet distribution, allowing for the distribution of specific SPL tokens rather than just SOL. It requires the mint address of the token to distribute, the member's public key, fanout details, and a payer. ```javascript const mint .publicKey = "SPL-Token-To-Distribute-PublicKey"; let distributeToMember1 = await fanoutSdk.distributeWalletMemberInstructions({ distributeForMint: true, member: member1.publicKey, fanout: builtFanout.fanout, payer: distributionBot.publicKey, fanoutMint: mint.publicKey },); // Transaction sending logic would follow here, similar to the wallet example. ``` -------------------------------- ### Add Wallet Member Source: https://developers.metaplex.com/hydra/index Adds a member to a Hydra fanout using the Wallet membership model. This model associates membership with a public address and a number of shares. The sum of all member shares must equal the total shares specified during fanout initialization. ```javascript const member =newKeypair(); const{ membershipAccount }=await fanoutSdk.addMemberWallet({ fanout: init.fanout, fanoutNativeAccount: init.nativeAccount, membershipKey: member.publicKey, shares:10 }); // Add members until sum of shares = totalShares... ``` -------------------------------- ### Add NFT Member Source: https://developers.metaplex.com/hydra/index Adds a member to a Hydra fanout using the NFT membership model. Membership is tied to an NFT mint address, allowing for varying quantities of shares per NFT. This model facilitates the transfer of distribution rights through NFT ownership. ```javascript const nftMintPublicKey ="nftMintPublicKey"; const init =await fanoutSdk.initializeFanout({ totalShares:100, name:`Test${Date.now()}`, membershipModel: MembershipModel.NFT, }); const{ membershipAccount }=await fanoutSdk.addMemberNft({ fanout: init.fanout, fanoutNativeAccount: init.nativeAccount, membershipKey: nftMintPublicKey, shares:10 }); // Add members until sum of shares = totalShares... ``` -------------------------------- ### Distribute Funds to Wallet Member Source: https://developers.metaplex.com/hydra/index Distributes funds to a specific wallet member within a Hydra fanout. This method is used when the membership model is a standard wallet address. It requires the member's public key, the fanout details, and a payer for the transaction. ```javascript const member1 .publicKey ="Member1.publicKey"; const distributionBot =new Keypair(); // This is the caller of the Distribute method, it can be a bot or a user, they just need enough funds to pay for the transaction fee. If you're using // this code, airdrop a sol to distributionBot. let distributeToMember1 = await fanoutSdk.distributeWalletMemberInstructions({ distributeForMint: false, member: member1.publicKey, fanout: fanout, // From initialization payer: distributionBot.publicKey, }); const tx = await fanoutSdk.sendInstructions([...distMember1.instructions], [distributionBot], distributionBot.publicKey ); if (!!tx.RpcResponseAndContext.value.err) { const txdetails = await connection.getConfirmedTransaction(tx.TransactionSignature); console.log(txdetails, tx.RpcResponseAndContext.value.err); } ``` -------------------------------- ### Distribute Funds to NFT Member Source: https://developers.metaplex.com/hydra/index Distributes funds to a member associated with an NFT. This method is used when the membership is tied to an NFT's mint address. It requires the member's public key, the NFT's mint address, the fanout details, and a payer. ```javascript const member1 .mint = "NFT Mint for Member 1"; let distributeToMember1 = await fanoutSdk.distributeNftMemberInstructions({ distributeForMint: false, member: member1.publicKey, membershipKey: member1.mint, fanout: fanout, payer: distributionBot.publicKey, }); // Transaction sending logic would follow here, similar to the wallet example. ``` -------------------------------- ### Distribute Funds to Token Member Source: https://developers.metaplex.com/hydra/index Distributes funds to a member associated with an SPL token. This method is used when membership is determined by holding a specific SPL token. It requires the member's public key, the SPL token mint address, the fanout details, and a payer. ```javascript const membershipMint .publicKey = "SPL-Token-PublicKey"; let distributeToMember1 = await fanoutSdk.distributeTokenMemberInstructions({ distributeForMint: false, membershipMint: membershipMint.publicKey, fanout: fanout, member: member1.publicKey, payer: distributionBot.publicKey, }); // Transaction sending logic would follow here, similar to the wallet example. ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.