{"path":"lib/sdk/src/client.ts","size":5394,"locked":false,"binary":false,"truncated":false,"content":"import * as anchor from \"@coral-xyz/anchor\";\nimport { BN, AnchorProvider, Program, Idl } from \"@coral-xyz/anchor\";\nimport { PublicKey, SystemProgram } from \"@solana/web3.js\";\nimport {\n  GigEscrowAccount,\n  FetchedEscrow,\n  InitializeJobParams,\n  ReleasePaymentParams,\n  CancelJobParams,\n} from \"./types.js\";\nimport { MAPPERS_PROGRAM_ID } from \"./constants.js\";\nimport { deriveEscrowPda, deriveVaultPda } from \"./pda.js\";\nimport { IDL } from \"./idl.js\";\n\n// ─── MAPPERS CLIENT ───────────────────────────────────────────────────────────\n\nexport class MappersClient {\n  readonly provider: AnchorProvider;\n  readonly programId: PublicKey;\n  // eslint-disable-next-line @typescript-eslint/no-explicit-any\n  private readonly program: Program<Idl>;\n\n  constructor(provider: AnchorProvider, programId: PublicKey = MAPPERS_PROGRAM_ID) {\n    this.provider  = provider;\n    this.programId = programId;\n\n    const idlWithAddress = { ...IDL, address: programId.toBase58() } as unknown as Idl;\n    this.program = new anchor.Program(idlWithAddress, provider);\n  }\n\n  // ─── PDA HELPERS ─────────────────────────────────────────────────────────\n\n  deriveEscrowPda(client: PublicKey, jobId: string): [PublicKey, number] {\n    return deriveEscrowPda(client, jobId, this.programId);\n  }\n\n  deriveVaultPda(client: PublicKey, jobId: string): [PublicKey, number] {\n    return deriveVaultPda(client, jobId, this.programId);\n  }\n\n  // ─── READS ───────────────────────────────────────────────────────────────\n\n  async fetchEscrow(escrowPubkey: PublicKey): Promise<GigEscrowAccount> {\n    // eslint-disable-next-line @typescript-eslint/no-explicit-any\n    return (this.program.account as any).gigEscrow.fetch(escrowPubkey) as Promise<GigEscrowAccount>;\n  }\n\n  async fetchEscrowByJobId(client: PublicKey, jobId: string): Promise<GigEscrowAccount> {\n    const [escrowPda] = this.deriveEscrowPda(client, jobId);\n    return this.fetchEscrow(escrowPda);\n  }\n\n  async fetchAllEscrows(): Promise<FetchedEscrow[]> {\n    // eslint-disable-next-line @typescript-eslint/no-explicit-any\n    return (this.program.account as any).gigEscrow.all() as Promise<FetchedEscrow[]>;\n  }\n\n  async fetchEscrowsByClient(client: PublicKey): Promise<FetchedEscrow[]> {\n    // eslint-disable-next-line @typescript-eslint/no-explicit-any\n    return (this.program.account as any).gigEscrow.all([\n      {\n        memcmp: {\n          offset: 8,\n          bytes: client.toBase58(),\n        },\n      },\n    ]) as Promise<FetchedEscrow[]>;\n  }\n\n  // ─── INSTRUCTIONS ─────────────────────────────────────────────────────────\n\n  async initializeJob(params: InitializeJobParams): Promise<string> {\n    const { jobId, amount, freelancer, oracle } = params;\n\n    if (jobId.length > 32) {\n      throw new Error(`Job ID exceeds 32-character maximum (got ${jobId.length}).`);\n    }\n\n    const signer   = this.provider.wallet.publicKey;\n    const bnAmount = amount instanceof BN ? amount : new BN(amount.toString());\n\n    const [escrowPda] = this.deriveEscrowPda(signer, jobId);\n    const [vaultPda]  = this.deriveVaultPda(signer, jobId);\n\n    // eslint-disable-next-line @typescript-eslint/no-explicit-any\n    const txSig = await (this.program.methods as any)\n      .initializeJob(jobId, bnAmount)\n      .accounts({\n        client:        signer,\n        freelancer,\n        oracle,\n        escrowAccount: escrowPda,\n        vaultAccount:  vaultPda,\n        systemProgram: SystemProgram.programId,\n      })\n      .rpc({ commitment: \"confirmed\" });\n\n    return txSig as string;\n  }\n\n  async releasePayment(params: ReleasePaymentParams): Promise<string> {\n    const { escrowPubkey, escrow } = params;\n    const authority = this.provider.wallet.publicKey;\n    const [vaultPda] = deriveVaultPda(escrow.client, escrow.jobId, this.programId);\n\n    // eslint-disable-next-line @typescript-eslint/no-explicit-any\n    const txSig = await (this.program.methods as any)\n      .releasePayment()\n      .accounts({\n        authority,\n        freelancer:    escrow.freelancer,\n        client:        escrow.client,\n        escrowAccount: escrowPubkey,\n        vaultAccount:  vaultPda,\n        systemProgram: SystemProgram.programId,\n      })\n      .rpc({ commitment: \"confirmed\" });\n\n    return txSig as string;\n  }\n\n  async cancelJob(params: CancelJobParams): Promise<string> {\n    const { escrowPubkey, escrow } = params;\n    const oracle     = this.provider.wallet.publicKey;\n    const [vaultPda] = deriveVaultPda(escrow.client, escrow.jobId, this.programId);\n\n    // eslint-disable-next-line @typescript-eslint/no-explicit-any\n    const txSig = await (this.program.methods as any)\n      .cancelJob()\n      .accounts({\n        oracle,\n        client:        escrow.client,\n        escrowAccount: escrowPubkey,\n        vaultAccount:  vaultPda,\n        systemProgram: SystemProgram.programId,\n      })\n      .rpc({ commitment: \"confirmed\" });\n\n    return txSig as string;\n  }\n}\n"}