xcm-lib-zkverify
Version:
XCM asset teleportation and remote EVM calls for ZKVerify
59 lines • 2.52 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.TransactionExecutor = void 0;
class TransactionExecutor {
/**
* Generic transaction executor that handles all cases
*/
static async executeTransaction(api, tx, keyPair, options = {}) {
const { errorPrefix = 'Transaction failed', skipNonce = false, returnSuccess = false } = options;
const nonce = skipNonce ? undefined : await api.rpc.system.accountNextIndex(keyPair.address);
const signOptions = nonce ? { nonce } : {};
return new Promise((resolve, reject) => {
tx.signAndSend(keyPair, signOptions, (result) => {
if (result.status.isInBlock || result.status.isFinalized) {
const failureEvent = this.getExtrinsicFailureEvent(result.events);
if (failureEvent) {
const errorMessage = this.extractErrorMessage(failureEvent);
reject(new Error(`${errorPrefix}: ${errorMessage}`));
return;
}
const txHash = result.txHash.toString();
// Return format based on options
if (returnSuccess) {
resolve({ success: true, txHash });
}
else {
resolve({ txHash, events: result.events });
}
}
else if (result.isError) {
reject(new Error('Transaction failed'));
}
}).catch(reject);
});
}
static getExtrinsicFailureEvent(events) {
return events.find(({ event }) => event.section === 'system' && event.method === 'ExtrinsicFailed');
}
static extractErrorMessage(failureEvent) {
try {
const error = failureEvent.event.data[0];
if (error?.isModule) {
return `Module error: ${error.asModule.section}.${error.asModule.name}`;
}
if (error?.isBadOrigin) {
return 'Bad origin (insufficient permissions)';
}
if (error?.isCannotLookup) {
return 'Cannot lookup account';
}
return error?.toString() || 'Unknown transaction error';
}
catch {
return 'Failed to extract error details';
}
}
}
exports.TransactionExecutor = TransactionExecutor;
//# sourceMappingURL=transaction-executor.js.map