Solana SDK


Installlation

npm i metapass-sdk

Create a host

To create an event you first need a host

const [hostPDA, hostBump] = await PublicKey.findProgramAddress(
  [
    anchor.utils.bytes.utf8.encode('event-host-key'),
    wallet.publicKey.toBuffer(),
  ],
  PROGRAM_ID
)
console.log('host', hostPDA.toString())
const accounts1: InitializeHostInstructionAccounts = {
  eventHostAccount: hostPDA,
  authority: wallet.publicKey,
}
const args: InitializeHostInstructionArgs = {
  displayName: event.displayName as string,
  profileImg: event.profileImage as string,
}

const transactionInstruction1 = createInitializeHostInstruction(accounts1, args)

const transaction1 = new Transaction().add(transactionInstruction1)
const { blockhash } = await connection.getLatestBlockhash()
transaction1.recentBlockhash = blockhash
transaction1.feePayer = wallet.publicKey
if (wallet.signTransaction) {
  const signedTx = await wallet.signTransaction(transaction1)
  const txid = await connection.sendRawTransaction(signedTx.serialize())
  console.log('Host created', `https://solscan.io/tx/${txid}?cluster=devnet`)
}

You can then fetch the host using this

const hostAccount = await program.account.eventHostAccount.fetch(hostPDA)

Create Event

const accounts1: InitializeHostInstructionAccounts = {
  eventHostAccount: hostPDA,
  authority: wallet.publicKey,
}
const args: InitializeHostInstructionArgs = {
  displayName: event.displayName as string,
  profileImg: event.profileImage as string,
}

const transactionInstruction1 = createInitializeHostInstruction(accounts1, args)

const transaction1 = new Transaction().add(transactionInstruction1)
const { blockhash } = await connection.getLatestBlockhash()
transaction1.recentBlockhash = blockhash
transaction1.feePayer = wallet.publicKey
if (wallet.signTransaction) {
  const signedTx = await wallet.signTransaction(transaction1)
  const txid = await connection.sendRawTransaction(signedTx.serialize())
  console.log('Host created', `https://solscan.io/tx/${txid}?cluster=devnet`)
} else {
  throw Error('signTransaction is undefined, line 205 create/index.tsx')
}

let nonce = '0' // if new host is created obviously the nonce is zero
const [eventPDA, _] = await PublicKey.findProgramAddress(
  [
    anchor.utils.bytes.utf8.encode('event_account'),
    wallet.publicKey.toBuffer(),
    new anchor.BN(nonce).toArrayLike(Buffer),
  ],
  PROGRAM_ID
)

console.log(hostPDA.toString(), nonce, eventPDA.toString())

const accounts = {
  eventAccount: eventPDA,
  authority: wallet.publicKey,
  eventHostAccount: hostPDA,
}

const transactionData = {
  createEventInfo: {
    title: event.title,
    description: JSON.stringify(event.description),
    uri: 'https://arweave.net/AhzTMchxDglQbfLWzQHOOGV9BA-CeEy36C-DJwFfqUc', // event.solanaUri as string,
    link: event.link as string,
    fee: new anchor.BN((event.fee as number) * LAMPORTS_PER_SOL),
    seats: new anchor.BN(event.seats as number),
    date: event.date,
    venue: event.type,
    isCutPayedByCreator: true,
    isCustomSplToken: true,
    customSplToken: new anchor.web3.PublicKey(
      '7gjQaUHVdP8m7BvrFWyPkM7L3H9p4umwm3F56q1qyLk1'
    ),
  },
}
const txnInstruction = createInitializeEventInstruction(
  accounts,
  transactionData
)
const transaction = new Transaction().add(txnInstruction)
const signature = await wallet.sendTransaction(transaction, connection)

Mint a ticket

const TOKEN_METADATA_PROGRAM_ID = new web3.PublicKey(
  'metaqbxxUerdq28cj1RbAWkYQm3ybzjb6a8bt518x1s'
)
const SPL_ASSOCIATED_TOKEN_ACCOUNT_PROGRAM_ID = new web3.PublicKey(
  'ATokenGPvbdGVxr1b2hvZbsiqW5xWH25efTNsLJA8knL'
)
const customSPL = new web3.PublicKey(
  '7gjQaUHVdP8m7BvrFWyPkM7L3H9p4umwm3F56q1qyLk1'
)
const getMetadata = async (mint: web3.PublicKey): Promise<web3.PublicKey> => {
  return (
    await web3.PublicKey.findProgramAddress(
      [
        Buffer.from('metadata'),
        TOKEN_METADATA_PROGRAM_ID.toBuffer(),
        mint.toBuffer(),
      ],
      TOKEN_METADATA_PROGRAM_ID
    )
  )[0]
}

const getMasterEdition = async (
  mint: web3.PublicKey
): Promise<web3.PublicKey> => {
  return (
    await web3.PublicKey.findProgramAddress(
      [
        Buffer.from('metadata'),
        TOKEN_METADATA_PROGRAM_ID.toBuffer(),
        mint.toBuffer(),
        Buffer.from('edition'),
      ],
      TOKEN_METADATA_PROGRAM_ID
    )
  )[0]
}

const mint = web3.Keypair.generate()
const metadataAddress = await getMetadata(mint.publicKey)
const masterEdition = await getMasterEdition(mint.publicKey)
const NftTokenAccount: web3.PublicKey = await getAssociatedTokenAddress(
  mint.publicKey,
  wallet.publicKey as web3.PublicKey
)
const hostPDA: web3.PublicKey = await getHostPDA(
  new web3.PublicKey(event.eventHost)
)
const adminPDA: web3.PublicKey = await getAdminPDA()

const hostCustomSplTokenAta = await getAssociatedTokenAddress(
  customSPL,
  new web3.PublicKey(event.eventHost) // the receiver
)
const adminCustomSplTokenATA = await getAssociatedTokenAddress(
  customSPL,
  new web3.PublicKey('B641ooUCSG8ToLRki3YuxWMiNj6BS5c4eSM1rWcSazeV')
)
const senderCustomTokenATA: web3.PublicKey = await getAssociatedTokenAddress(
  customSPL,
  wallet.publicKey as web3.PublicKey
)
const accounts: MintTicketInstructionAccounts = {
  mintAuthority: wallet.publicKey as web3.PublicKey,
  eventAccount: new web3.PublicKey(event.childAddress),
  mint: mint.publicKey,
  metadata: metadataAddress,
  tokenAccount: NftTokenAccount,
  tokenMetadataProgram: TOKEN_METADATA_PROGRAM_ID,
  payer: wallet.publicKey as web3.PublicKey,
  masterEdition: masterEdition,
  eventHost: hostPDA,
  eventHostKey: new web3.PublicKey(event.eventHost),
  adminAccount: adminPDA,
  adminKey: new web3.PublicKey('B641ooUCSG8ToLRki3YuxWMiNj6BS5c4eSM1rWcSazeV'),
  customSplToken: customSPL,
  customSplTokenProgram: TOKEN_PROGRAM_ID,
  senderCustomSplTokenAta: senderCustomTokenATA,
  hostCustomSplTokenAta: hostCustomSplTokenAta,
  adminCustomTokenAta: adminCustomSplTokenATA,
  associatedTokenProgram: ASSOCIATED_TOKEN_PROGRAM_ID,
}

const transactionInstruction = createMintTicketInstruction(accounts)

const transaction = new web3.Transaction().add(transactionInstruction)
setIsLoading(true)
const { blockhash } = await connection.getLatestBlockhash()
transaction.recentBlockhash = blockhash
transaction.feePayer = wallet.publicKey as web3.PublicKey
if (wallet.signTransaction) {
  const signedTx = await wallet.signTransaction(transaction)
  const txid = await connection.sendRawTransaction(signedTx.serialize(), {
    preflightCommitment: 'recent',
  })
}