UNPKG

kleros-escrow-data-service

Version:

Data service for interacting with Kleros Escrow

115 lines (114 loc) 5.02 kB
import { BaseService } from "../base/BaseService"; /** * Service for writing transaction data to the Kleros Escrow contract */ export class TransactionActions extends BaseService { /** * Creates a new TransactionActions instance * @param config The Kleros Escrow configuration * @param signer A signer for write operations */ constructor(config, signer) { super(config, signer); /** * Creates a new escrow transaction * @param params Parameters for creating the transaction * @returns The transaction response and the transaction ID */ this.createTransaction = async (params) => { var _a, _b; this.ensureCanWrite(); let tx; // Check if this is a token transaction by looking for tokenAddress parameter if ('tokenAddress' in params) { // Token transaction tx = await this.escrowContract.createTransaction(params.amount, params.tokenAddress, params.timeoutPayment, params.receiver, params.metaEvidence); } else { // ETH transaction tx = await this.escrowContract.createTransaction(params.timeoutPayment, params.receiver, params.metaEvidence, { value: params.value } // Already in Wei ); } // Wait for the transaction to be mined const receipt = await tx.wait(); // Find the transaction ID from the event logs const events = receipt.events; const event = events === null || events === void 0 ? void 0 : events.find((e) => e.event === "MetaEvidence"); const transactionId = ((_b = (_a = event === null || event === void 0 ? void 0 : event.args) === null || _a === void 0 ? void 0 : _a._metaEvidenceID) === null || _b === void 0 ? void 0 : _b.toString()) || "0"; return { transactionResponse: tx, transactionId, }; }; /** * Pays the receiver (releases funds from escrow) * @param params Parameters for the payment * @param params.amount Amount in Wei * @returns The transaction response */ this.pay = async (params) => { this.ensureCanWrite(); const tx = await this.escrowContract.pay(params.transactionId, params.amount // Already in Wei ); return tx; }; /** * Reimburses the sender (returns funds from escrow) * @param params Parameters for the reimbursement * @param params.amount Amount in Wei * @returns The transaction response */ this.reimburse = async (params) => { this.ensureCanWrite(); const tx = await this.escrowContract.reimburse(params.transactionId, params.amount // Already in Wei ); return tx; }; /** * Executes a transaction after the timeout period * @param transactionId The ID of the transaction to execute * @returns The transaction response */ this.executeTransaction = async (transactionId) => { this.ensureCanWrite(); const tx = await this.escrowContract.executeTransaction(transactionId); return tx; }; /** * Times out the receiver for not paying arbitration fees * @param transactionId The ID of the transaction * @returns The transaction response */ this.timeOutBySender = async (transactionId) => { this.ensureCanWrite(); const tx = await this.escrowContract.timeOutBySender(transactionId); return tx; }; /** * Times out the sender for not paying arbitration fees * @param transactionId The ID of the transaction * @returns The transaction response */ this.timeOutByReceiver = async (transactionId) => { this.ensureCanWrite(); const tx = await this.escrowContract.timeOutByReceiver(transactionId); return tx; }; /** * Estimates gas for creating a transaction * @param params Parameters for creating the transaction * @returns The estimated gas */ this.estimateGasForCreateTransaction = async (params) => { // Check if this is a token transaction by looking for tokenAddress parameter if ('tokenAddress' in params) { // Token transaction return await this.escrowContract.estimateGas.createTransaction(params.amount, params.tokenAddress, params.timeoutPayment, params.receiver, params.metaEvidence); } else { // ETH transaction return await this.escrowContract.estimateGas.createTransaction(params.timeoutPayment, params.receiver, params.metaEvidence, { value: params.value }); } }; } }