import type { AugmentedProvider, TransactionEnvelope, } from "@saberhq/solana-contrib"; import type { TokenAmount, u64 } from "@saberhq/token-utils"; import { getOrCreateATA, TOKEN_PROGRAM_ID } from "@saberhq/token-utils"; import type { AccountInfo, PublicKey } from "@solana/web3.js"; import { Keypair, SystemProgram } from "@solana/web3.js"; import invariant from "tiny-invariant"; import type { TransfererData, TransferWrapperData, TransferWrapperProgram, } from "../../programs/transferWrapper"; import type { QuarrySDK } from "../../sdk"; import { findTransfererAddress, findTransferWrapperAddress } from "./pda"; export class TransferWrapper { readonly program: TransferWrapperProgram; constructor(readonly sdk: QuarrySDK) { this.program = sdk.programs.TransferWrapper; } get provider(): AugmentedProvider { return this.sdk.provider; } async newWrapper({ tokenMint, baseKP = Keypair.generate(), tokenProgram = TOKEN_PROGRAM_ID, admin = this.provider.wallet.publicKey, payer = this.provider.wallet.publicKey, }: { tokenMint: PublicKey; baseKP?: Keypair; tokenProgram?: PublicKey; admin?: PublicKey; payer?: PublicKey; }): Promise { const [transferWrapper, nonce] = await findTransferWrapperAddress( baseKP.publicKey, this.program.programId ); const result = await getOrCreateATA({ provider: this.provider, mint: tokenMint, owner: transferWrapper, }); invariant( result.instruction !== null, "getOrCreateATA instruction is null" ); const tx = this.provider.newTX( [ this.program.instruction.newWrapper(nonce, { accounts: { base: baseKP.publicKey, transferWrapper, admin, tokenMint, sourceTokenAccount: result.address, tokenProgram, payer, systemProgram: SystemProgram.programId, }, }), ], [baseKP] ); tx.instructions.unshift(result.instruction); return tx; } /** * Fetches info on a Transfer Wrapper. * @param wrapper * @returns */ async fetchTransferWrapper( wrapper: PublicKey ): Promise { const accountInfo = await this.program.provider.connection.getAccountInfo( wrapper ); if (!accountInfo) { return null; } return this.program.coder.accounts.decode( "TransferWrapper", accountInfo.data ); } /** * Fetches info on a transferer. * @param wrapper * @returns */ async fetchTransferer( wrapper: PublicKey, authority: PublicKey ): Promise { const [transfererAddress] = await findTransfererAddress( wrapper, authority, this.program.programId ); const accountInfo = await this.program.provider.connection.getAccountInfo( transfererAddress ); if (!accountInfo) { return null; } return this.program.coder.accounts.decode( "Transferer", accountInfo.data ); } async newTransferer( wrapper: PublicKey, authority: PublicKey ): Promise { const [transferer, bump] = await findTransfererAddress( wrapper, authority, this.program.programId ); return this.provider.newTX([ this.program.instruction.newTransferer(bump, { accounts: { auth: { transferWrapper: wrapper, admin: this.provider.wallet.publicKey, }, transferAuthority: authority, transferer, payer: this.provider.wallet.publicKey, systemProgram: SystemProgram.programId, }, }), ]); } /** * Updates a transferer's allowance. * @param wrapper * @param allowance * @returns */ async transfererUpdate( wrapper: PublicKey, authority: PublicKey, allowance: u64 ): Promise { const [transferer] = await findTransfererAddress( wrapper, authority, this.program.programId ); const transferWrapper = await this.fetchTransferWrapper(wrapper); invariant(transferWrapper !== null, "transfer wrapper is null"); return this.provider.newTX([ this.program.instruction.transferUpdate(allowance, { accounts: { auth: { transferWrapper: wrapper, admin: this.provider.wallet.publicKey, }, transferer, sourceTokenAccount: transferWrapper.sourceTokenAccount, }, }), ]); } /** * Updates a transferer's cap. * @param wrapper * @param cap * @returns */ async updateCap(wrapper: PublicKey, cap: u64): Promise { const transferWrapper = await this.fetchTransferWrapper(wrapper); invariant(transferWrapper !== null, "transfer wrapper is null"); return this.provider.newTX([ this.program.instruction.updateCap(cap, { accounts: { auth: { transferWrapper: wrapper, admin: this.provider.wallet.publicKey, }, sourceTokenAccount: transferWrapper.sourceTokenAccount, }, }), ]); } /** * withdraw token * @param wrapper * @param dest * @returns */ async withdraw( wrapper: PublicKey, dest: PublicKey ): Promise { const transferWrapper = await this.fetchTransferWrapper(wrapper); invariant(transferWrapper !== null, "transfer wrapper is null"); return this.provider.newTX([ this.program.instruction.withdraw({ accounts: { auth: { transferWrapper: wrapper, admin: this.provider.wallet.publicKey, }, sourceTokenAccount: transferWrapper.sourceTokenAccount, destTokenAccount: dest, tokenProgram: TOKEN_PROGRAM_ID, }, }), ]); } /** * Creates a new Transferer with an allowance. * @param wrapper * @param authority * @param allowance * @returns */ async newTransfererWithAllowance( wrapper: PublicKey, authority: PublicKey, allowance: u64 ): Promise { const newMinter = await this.newTransferer(wrapper, authority); const updateAllowance = await this.transfererUpdate( wrapper, authority, allowance ); return newMinter.combine(updateAllowance); } transferAdmin(wrapper: PublicKey, nextAdmin: PublicKey): TransactionEnvelope { return this.provider.newTX([ this.program.instruction.transferAdmin({ accounts: { transferWrapper: wrapper, admin: this.provider.wallet.publicKey, nextAdmin, }, }), ]); } acceptAdmin(wrapper: PublicKey): TransactionEnvelope { return this.provider.newTX([ this.program.instruction.acceptAdmin({ accounts: { transferWrapper: wrapper, pendingAdmin: this.provider.wallet.publicKey, }, }), ]); } /** * Transfer tokens to an address as a Transferer on the Transfer Wrapper. */ async performTransferTo({ amount, transferWrapper, transfererAuthority = this.provider.wallet.publicKey, destOwner = this.provider.wallet.publicKey, }: { amount: TokenAmount; transferWrapper: PublicKey; transfererAuthority?: PublicKey; destOwner?: PublicKey; }): Promise { const ata = await getOrCreateATA({ provider: this.provider, mint: amount.token.mintAccount, owner: destOwner, }); const transfer_wrapper_acc = await this.fetchTransferWrapper( transferWrapper ); invariant( transfer_wrapper_acc !== null, `The transfer wrapper:${transferWrapper.toBase58()} not found` ); const [transferer] = await findTransfererAddress( transferWrapper, transfererAuthority ); return this.sdk.provider.newTX([ ata.instruction, this.program.instruction.performTransfer(amount.toU64(), { accounts: { transferWrapper, transfererAuthority, tokenMint: amount.token.mintAccount, destination: ata.address, transferer, sourceTokenAccount: transfer_wrapper_acc.sourceTokenAccount, tokenProgram: TOKEN_PROGRAM_ID, }, }), ]); } /** * Performs a transfer of tokens to an account. * @returns */ async performTransfer({ amount, transferWrapper, transferer, }: { amount: TokenAmount; transferWrapper: PublicKey; transferer: { accountId: PublicKey; accountInfo: AccountInfo; }; }): Promise { const transferData = transferer.accountInfo.data; const ata = await getOrCreateATA({ provider: this.provider, mint: amount.token.mintAccount, owner: this.provider.wallet.publicKey, }); const transfer_wrapper_acc = await this.fetchTransferWrapper( transferWrapper ); invariant( transfer_wrapper_acc !== null, `The transfer wrapper:${transferWrapper.toBase58()} not found` ); return this.provider.newTX([ ata.instruction, this.program.instruction.performTransfer(amount.toU64(), { accounts: { transferWrapper: transferData.transferWrapper, transfererAuthority: transferData.transferAuthority, tokenMint: amount.token.mintAccount, destination: ata.address, transferer: transferer.accountId, sourceTokenAccount: transfer_wrapper_acc.sourceTokenAccount, tokenProgram: TOKEN_PROGRAM_ID, }, }), ]); } }