import { Connection } from '@solana/web3.js'; import { TransactionInstruction, PublicKey, Transaction } from '@solana/web3.js'; import { ASSOCIATED_TOKEN_PROGRAM_ID, Token, TOKEN_PROGRAM_ID } from '@solana/spl-token'; import { BinaryWriter } from '@dao-xyz/borsh'; import { Vesting } from './state'; export * from './state'; export const VESTING_PROGRAM_ID = new PublicKey('8xetCZkhmg6NfWm4fYVwfjoDQFZ7LGi13fzw6xANtawj'); export async function getAllUserVesting(connection: Connection, receiver: PublicKey) { // magic:8 // mint:32 // sender:32 // receiver:32 const RECEIVER_OFFSET = 8 + 32 + 32; const accounts = await connection.getProgramAccounts(VESTING_PROGRAM_ID, { filters: [ { memcmp: { offset: RECEIVER_OFFSET, bytes: receiver.toBase58(), }, }, ], }); const vestings = accounts.map((a) => { const vesting = Vesting.fromBytes(a.account.data); return { ...a, vesting, }; }); return vestings; } /** * * @param {import("@solana/wallet-adapter-react").WalletContextState} wallet * @param {Vesting} vesting * @returns */ export async function claimTransaction(wallet, vesting) { const mint = new PublicKey(vesting.mint); const recipientTokenAccount = await Token.getAssociatedTokenAddress( ASSOCIATED_TOKEN_PROGRAM_ID, TOKEN_PROGRAM_ID, mint, wallet.publicKey, ); const [metadataAccount] = await PublicKey.findProgramAddress( [recipientTokenAccount.toBytes()], VESTING_PROGRAM_ID, ); const transaction = new Transaction(); transaction.add( claimInstruction( wallet.publicKey, new PublicKey(vesting.sender), new PublicKey(vesting.recipient), new PublicKey(vesting.recipientTokens), metadataAccount, new PublicKey(vesting.escrowTokens), mint, 0, ), ); return transaction; } export function claimInstruction( withdrawAuthority: PublicKey, sender: PublicKey, recipient: PublicKey, recipientToken: PublicKey, metadataAccount: PublicKey, escrowAccount: PublicKey, mint: PublicKey, amount: number, ) { const writer = new BinaryWriter(); // 0x1 is instrucition::Withdraw writer.u8(0x01); writer.u64(amount); const data = writer.finalize(); return new TransactionInstruction({ programId: VESTING_PROGRAM_ID, keys: [ { pubkey: withdrawAuthority, isSigner: true, isWritable: false }, { pubkey: sender, isSigner: false, isWritable: false }, { pubkey: recipient, isSigner: false, isWritable: true }, { pubkey: recipientToken, isSigner: false, isWritable: true }, { pubkey: metadataAccount, isSigner: false, isWritable: true }, { pubkey: escrowAccount, isSigner: false, isWritable: true }, { pubkey: mint, isSigner: false, isWritable: false }, { pubkey: TOKEN_PROGRAM_ID, isSigner: false, isWritable: false }, ], data: Buffer.from(data), }); }