UNPKG

@stellar/stellar-sdk

Version:

A library for working with the Stellar network, including communication with the Horizon and Soroban RPC servers.

165 lines (161 loc) 5.95 kB
'use strict'; require('../node_modules/.pnpm/@stellar_js-xdr@4.0.0/node_modules/@stellar/js-xdr/src/int.js'); require('../node_modules/.pnpm/@stellar_js-xdr@4.0.0/node_modules/@stellar/js-xdr/src/hyper.js'); require('../node_modules/.pnpm/@stellar_js-xdr@4.0.0/node_modules/@stellar/js-xdr/src/unsigned-int.js'); require('../node_modules/.pnpm/@stellar_js-xdr@4.0.0/node_modules/@stellar/js-xdr/src/unsigned-hyper.js'); require('../node_modules/.pnpm/@stellar_js-xdr@4.0.0/node_modules/@stellar/js-xdr/src/xdr-type.js'); require('buffer'); var curr_generated = require('../base/generated/curr_generated.js'); require('@noble/hashes/sha2.js'); require('../base/signing.js'); require('../base/keypair.js'); require('base32.js'); require('../base/util/continued_fraction.js'); require('../base/util/bignumber.js'); require('../base/transaction_builder.js'); require('../base/muxed_account.js'); require('../base/scval.js'); require('../base/numbers/uint128.js'); require('../base/numbers/uint256.js'); require('../base/numbers/int128.js'); require('../base/numbers/int256.js'); function events(entries) { return entries.filter( (entry) => entry.switch().value === curr_generated.default.ScSpecEntryKind.scSpecEntryEventV0().value ).map((entry) => entry.eventV0()); } function findEvent(entries, name, occurrence = 0) { if (!Number.isInteger(occurrence) || occurrence < 0) { throw new Error( `invalid occurrence for event ${name}: ${occurrence} (expected a non-negative integer)` ); } return events(entries).filter((e) => e.name().toString() === name)[occurrence]; } function topicListParams(event) { return event.params().filter( (p) => p.location().value === curr_generated.default.ScSpecEventParamLocationV0.scSpecEventParamLocationTopicList().value ); } function dataParams(event) { return event.params().filter( (p) => p.location().value === curr_generated.default.ScSpecEventParamLocationV0.scSpecEventParamLocationData().value ); } function prefixTopicText(topic) { switch (topic.switch().value) { case curr_generated.default.ScValType.scvSymbol().value: return topic.sym().toString(); case curr_generated.default.ScValType.scvString().value: return topic.str().toString(); default: return void 0; } } function matchesTopics(event, topics) { const prefixTopics = event.prefixTopics(); const tlParams = topicListParams(event); if (topics.length < prefixTopics.length + tlParams.length) { return void 0; } for (let i = 0; i < prefixTopics.length; i++) { if (prefixTopicText(topics[i]) !== prefixTopics[i].toString()) { return void 0; } } return tlParams; } function parseEvent(spec, entries, topics, data) { let topicVals; let dataVal; try { topicVals = topics.map( (t) => typeof t === "string" ? curr_generated.default.ScVal.fromXDR(t, "base64") : t ); dataVal = typeof data === "string" ? curr_generated.default.ScVal.fromXDR(data, "base64") : data; } catch { return void 0; } const specEvents = events(entries); for (const event of specEvents) { const tlParams = matchesTopics(event, topicVals); if (!tlParams) { continue; } try { const prefixLen = event.prefixTopics().length; const dataOut = /* @__PURE__ */ Object.create(null); tlParams.forEach((param, i) => { const val = topicVals[prefixLen + i]; dataOut[param.name().toString()] = spec.scValToNative( val, param.type() ); }); const dParams = dataParams(event); const format = event.dataFormat().value; if (format === curr_generated.default.ScSpecEventDataFormat.scSpecEventDataFormatSingleValue().value) { const param = dParams[0]; if (param) { dataOut[param.name().toString()] = spec.scValToNative( dataVal, param.type() ); } } else if (format === curr_generated.default.ScSpecEventDataFormat.scSpecEventDataFormatVec().value) { const vec = dataVal.vec() ?? []; if (vec.length < dParams.length) { continue; } dParams.forEach((param, i) => { dataOut[param.name().toString()] = spec.scValToNative( vec[i], param.type() ); }); } else if (format === curr_generated.default.ScSpecEventDataFormat.scSpecEventDataFormatMap().value) { const map = dataVal.map() ?? []; dParams.forEach((param) => { const name = param.name().toString(); const entry = map.find( (e) => e.key().switch().value === curr_generated.default.ScValType.scvSymbol().value && e.key().sym().toString() === name ); if (entry) { dataOut[name] = spec.scValToNative(entry.val(), param.type()); } }); } return { name: event.name().toString(), data: dataOut }; } catch { continue; } } return void 0; } function eventTopicFilter(spec, entries, name, topicValues, occurrence = 0) { const event = findEvent(entries, name, occurrence); if (!event) { throw new Error( occurrence > 0 ? `no such event: ${name} (occurrence ${occurrence})` : `no such event: ${name}` ); } const filter = event.prefixTopics().map((topic) => curr_generated.default.ScVal.scvSymbol(topic.toString()).toXDR("base64")); topicListParams(event).forEach((param) => { const paramName = param.name().toString(); if (topicValues && Object.prototype.hasOwnProperty.call(topicValues, paramName)) { const scVal = spec.nativeToScVal(topicValues[paramName], param.type()); filter.push(scVal.toXDR("base64")); } else { filter.push("*"); } }); return filter; } exports.eventTopicFilter = eventTopicFilter; exports.events = events; exports.findEvent = findEvent; exports.parseEvent = parseEvent; //# sourceMappingURL=event_spec.js.map