UNPKG

@pod-protocol/sdk

Version:

TypeScript SDK for PoD Protocol - AI agent communication on Solana

174 lines (168 loc) 6.42 kB
'use strict'; var types = require('../types-OQd1rGtn.js'); var anchor = require('@coral-xyz/anchor'); var base = require('../base-4VR-G3Dc.js'); var utils = require('../utils.js'); require('node:events'); require('ws'); function _interopNamespaceDefault(e) { var n = Object.create(null); if (e) { Object.keys(e).forEach(function (k) { if (k !== 'default') { var d = Object.getOwnPropertyDescriptor(e, k); Object.defineProperty(n, k, d.get ? d : { enumerable: true, get: function () { return e[k]; } }); } }); } n.default = e; return Object.freeze(n); } var anchor__namespace = /*#__PURE__*/_interopNamespaceDefault(anchor); const { BN} = anchor__namespace; /** * Escrow service for managing channel deposits and payments */ class EscrowService extends base.BaseService { /** * Deposit funds to escrow for a channel */ async depositEscrow(wallet, options) { const program = this.ensureInitialized(); // Derive agent PDA const [agentPDA] = await utils.findAgentPDA(wallet.address, this.programId); // Derive escrow PDA const [escrowPDA] = await utils.findEscrowPDA(options.channel, wallet.address, this.programId); const tx = await program.methods .depositEscrow(new BN(options.amount)) .accounts({ escrowAccount: escrowPDA, channelAccount: options.channel, depositorAgent: agentPDA, depositor: wallet.address, systemProgram: types.address("11111111111111111111111111111112"), // SystemProgram.programId }) .signers([wallet]) .rpc({ commitment: this.commitment }); return tx; } /** * Withdraw funds from escrow */ async withdrawEscrow(wallet, options) { const program = this.ensureInitialized(); // Derive agent PDA const [agentPDA] = await utils.findAgentPDA(wallet.address, this.programId); // Derive escrow PDA const [escrowPDA] = await utils.findEscrowPDA(options.channel, wallet.address, this.programId); const tx = await program.methods .withdrawEscrow(new BN(options.amount)) .accounts({ escrowAccount: escrowPDA, channelAccount: options.channel, depositorAgent: agentPDA, depositor: wallet.address, systemProgram: types.address("11111111111111111111111111111112"), // SystemProgram.programId }) .signers([wallet]) .rpc({ commitment: this.commitment }); return tx; } /** * Get escrow account data */ async getEscrow(channel, depositor) { try { const [escrowPDA] = await utils.findEscrowPDA(channel, depositor, this.programId); const escrowAccount = this.getAccount("escrowAccount"); const account = await escrowAccount.fetch(escrowPDA); return this.convertEscrowAccountFromProgram(account); } catch (error) { console.error("Error fetching escrow details:", error); return null; } } /** * Get all escrow accounts by depositor */ async getEscrowsByDepositor(depositor, limit = 50) { try { const escrowAccount = this.getAccount("escrowAccount"); const filters = [ { memcmp: { offset: 8 + 32, // After discriminator and channel pubkey bytes: depositor, // Address can be used directly in memcmp }, }, ]; const accounts = await escrowAccount.all(filters); return accounts .slice(0, limit) .map((acc) => this.convertEscrowAccountFromProgram(acc.account)); } catch (error) { console.warn("Error fetching escrows by depositor:", error); return []; } } // ============================================================================ // Helper Methods // ============================================================================ convertEscrowAccountFromProgram(account) { return { channel: account.channel, depositor: account.depositor, balance: account.balance?.toNumber() || 0, amount: account.balance?.toNumber() || 0, // alias for compatibility createdAt: account.createdAt?.toNumber() || Date.now(), lastUpdated: account.lastUpdated?.toNumber() || Date.now(), bump: account.bump, }; } // ============================================================================ // MCP Server Compatibility Methods // ============================================================================ /** * Create escrow method for MCP server compatibility */ async create(options) { // Real implementation using depositEscrow if (!this.wallet) { throw new Error('Wallet not configured for escrow service'); } const channelAddress = types.address(options.channelId); const signature = await this.depositEscrow(this.wallet, { channel: channelAddress, amount: options.amount }); // Generate escrow ID from signature const escrowId = `esc_${signature.slice(0, 16)}`; return { escrowId, signature }; } /** * Release escrow method for MCP server compatibility */ async release(escrowId, amount) { // Real implementation using withdrawEscrow if (!this.wallet) { throw new Error('Wallet not configured for escrow service'); } // For release, we need the channel address and amount // Since we only have escrowId, we'll need to fetch the escrow details first // This is a simplified implementation - in practice you'd parse the escrowId or maintain a mapping throw new Error('Release functionality requires channel address and amount. Use withdrawEscrow directly.'); } setWallet(wallet) { this.wallet = wallet; } } exports.EscrowService = EscrowService; //# sourceMappingURL=escrow.js.map