UNPKG

@kippurocks/libticketto-papi

Version:

A Kippu implementation of The Ticketto Protocol with Polkadot-API

77 lines (76 loc) 3.61 kB
import { EventQueue, WebStubEventSubscribtion } from "./subscriptions.js"; import { TOKEN } from "./types.js"; import { KippuDirectoryCalls, KippuDirectoryStorage } from "./directory.js"; import { KippuEventsCalls, KippuEventsStorage } from "./events.js"; import { KippuTicketsCalls, KippuTicketsStorage } from "./tickets.js"; import { contracts, kreivo } from "@polkadot-api/descriptors"; import { Container } from "inversify"; import { TickettoModelConverter } from "./tickettoModel.js"; import { TransactionSubmitter } from "./submitter.js"; import { createInkSdk } from "@polkadot-api/sdk-ink"; export { isKreivoTx, } from "./types.js"; export class KippuPAPIConsumer { container = new Container(); constructor() { this.container.bind(TOKEN.QUEUE).toConstantValue(new EventQueue()); this.container.bind(TOKEN.SUBMITTER).to(TransactionSubmitter); this.container.bind(KippuDirectoryCalls).toSelf(); this.container.bind(KippuDirectoryStorage).toSelf(); this.container.bind(KippuEventsCalls).toSelf(); this.container.bind(KippuEventsStorage).toSelf(); this.container.bind(KippuTicketsCalls).toSelf(); this.container.bind(KippuTicketsStorage).toSelf(); this.container.bind(WebStubEventSubscribtion).toSelf(); } async build(config) { if (config === undefined) { throw new TypeError("A valid `KippuConfig` is required to initialize the `KippuPAPIConsumer`"); } this.container .bind(TOKEN.ACCUNT_PROVIDER) .toConstantValue(config.accountProvider); this.container .bind(TOKEN.SETTINGS) .toConstantValue(config.consumerSettings); const consumerSettings = config.consumerSettings; this.container .bind(TOKEN.MERCHANT_ID) .toConstantValue(consumerSettings.merchantId ?? 3); this.container .bind(TOKEN.POLKADOT_CLIENT) .toConstantValue(consumerSettings.client); // Initialize Kreivo API const kreivoApi = consumerSettings.client.getTypedApi(kreivo); this.container.bind(TOKEN.KREIVO_API).toConstantValue(kreivoApi); this.container.bind(TickettoModelConverter).toSelf(); // Initialize Events contract const eventsSdk = createInkSdk(kreivoApi, contracts.tickettoEvents); this.container .bind(TOKEN.EVENTS_CONTRACT) .toConstantValue(eventsSdk.getContract(consumerSettings.eventsContractAddress)); this.container .bind(TOKEN.EVENTS_CONTRACT_ADDRESS) .toConstantValue(consumerSettings.eventsContractAddress); // Initialize Tickets contract const ticketsSdk = createInkSdk(kreivoApi, contracts.tickettoTickets); this.container .bind(TOKEN.TICKETS_CONTRACT) .toConstantValue(ticketsSdk.getContract(consumerSettings.ticketsContractAddress)); return { accountProvider: this.container.get(TOKEN.ACCUNT_PROVIDER), directory: { calls: this.container.get(KippuDirectoryCalls), query: this.container.get(KippuDirectoryStorage), }, events: { calls: this.container.get(KippuEventsCalls), query: this.container.get(KippuEventsStorage), }, tickets: { calls: this.container.get(KippuTicketsCalls), query: this.container.get(KippuTicketsStorage), }, systemEvents: this.container.get(WebStubEventSubscribtion), }; } }