@kippurocks/libticketto-papi
Version:
A Kippu implementation of The Ticketto Protocol with Polkadot-API
84 lines (83 loc) • 3.63 kB
JavaScript
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
var __param = (this && this.__param) || function (paramIndex, decorator) {
return function (target, key) { decorator(target, key, paramIndex); }
};
var KippuAttendanceStorage_1;
import { inject, injectable } from "inversify";
import { ATTRIBUTE_KEYS, TOKEN, } from "./types.js";
import { Binary } from "polkadot-api";
import { Tuple, Vector, compact, u64 } from "scale-ts";
let KippuAttendanceCalls = class KippuAttendanceCalls {
settings;
kreivo;
constructor(settings, kreivo) {
this.settings = settings;
this.kreivo = kreivo;
}
async submit(xt) {
// Optimistic attempt. It tries to execute the attendance extrinsic. Within that,
// it'll attempt executing the contract.
const extrinsic = Binary.fromBytes(xt);
const { success } = await this.kreivo.apis.BlockBuilder.apply_extrinsic(extrinsic);
if (!success) {
throw new Error("Invalid submission");
}
await new Promise((resolve, reject) => {
let sent = false;
this.settings.client.submitAndWatch(extrinsic.asHex()).subscribe({
next(value) {
switch (value.type) {
case "broadcasted":
sent = true;
return resolve();
}
},
complete() {
if (!sent) {
resolve();
}
},
error(err) {
reject(err);
},
});
});
}
};
KippuAttendanceCalls = __decorate([
injectable(),
__param(0, inject(TOKEN.SETTINGS)),
__param(1, inject(TOKEN.KREIVO_API)),
__metadata("design:paramtypes", [Object, Object])
], KippuAttendanceCalls);
export { KippuAttendanceCalls };
let KippuAttendanceStorage = class KippuAttendanceStorage {
static { KippuAttendanceStorage_1 = this; }
settings;
kreivo;
static attendancesCodec = Tuple(compact, Vector(u64));
constructor(settings, kreivo) {
this.settings = settings;
this.kreivo = kreivo;
}
async attendances(id, ticketId) {
const [attendancesForTicket] = (await this.kreivo.query.ListingsCatalog.Attribute.getValue([this.settings.storeId ?? 2, id], BigInt(ticketId), { type: "Pallet", value: undefined }, ATTRIBUTE_KEYS.TICKETS.ATTENDANCES)) ?? [Binary.fromHex("0x00")];
const [, attendances] = KippuAttendanceStorage_1.attendancesCodec.dec(attendancesForTicket.asBytes());
return attendances;
}
};
KippuAttendanceStorage = KippuAttendanceStorage_1 = __decorate([
injectable(),
__param(0, inject(TOKEN.SETTINGS)),
__param(1, inject(TOKEN.KREIVO_API)),
__metadata("design:paramtypes", [Object, Object])
], KippuAttendanceStorage);
export { KippuAttendanceStorage };