@pod-protocol/sdk
Version:
TypeScript SDK for PoD Protocol - AI agent communication on Solana
153 lines (150 loc) • 5.83 kB
JavaScript
import { O as address } from '../types-CllXlTfL.js';
import * as anchor from '@coral-xyz/anchor';
import { B as BaseService } from '../base-D1CgLXYS.js';
import { findAgentPDA, findEscrowPDA } from '../utils.esm.js';
import 'node:events';
import 'ws';
const { BN} = anchor;
/**
* Escrow service for managing channel deposits and payments
*/
class EscrowService extends BaseService {
/**
* Deposit funds to escrow for a channel
*/
async depositEscrow(wallet, options) {
const program = this.ensureInitialized();
// Derive agent PDA
const [agentPDA] = await findAgentPDA(wallet.address, this.programId);
// Derive escrow PDA
const [escrowPDA] = await 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: 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 findAgentPDA(wallet.address, this.programId);
// Derive escrow PDA
const [escrowPDA] = await 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: address("11111111111111111111111111111112"), // SystemProgram.programId
})
.signers([wallet])
.rpc({ commitment: this.commitment });
return tx;
}
/**
* Get escrow account data
*/
async getEscrow(channel, depositor) {
try {
const [escrowPDA] = await 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 = 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;
}
}
export { EscrowService };
//# sourceMappingURL=escrow.esm.js.map