@kippurocks/libticketto-papi
Version:
A Kippu implementation of The Ticketto Protocol with Polkadot-API
108 lines (107 loc) • 4.47 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); }
};
import { inject, injectable } from "inversify";
import { TOKEN } from "./types.js";
import { Binary } from "polkadot-api";
import { Bytes } from '@polkadot-api/substrate-bindings';
let TransactionSubmitter = class TransactionSubmitter {
client;
api;
accountProvider;
constructor(client, api, accountProvider) {
this.client = client;
this.api = api;
this.accountProvider = accountProvider;
}
signTx(tx) {
return this.accountProvider.sign(tx);
}
async quickSubmit(tx) {
// Note that the signer returns a `Vec<u8>` that
// contains the encoded extrinsic, and not the
// encoded extrinsic itself. We need to decode it
// to pass it to the validator method.
const extrinsicBytes = Bytes.dec()(tx.asBytes());
let result;
try {
result = await this.api.apis
.BlockBuilder.apply_extrinsic(Binary.fromBytes(extrinsicBytes));
}
catch (cause) {
throw new Error("ClientError", { cause });
}
if (!result.success) {
throw new Error("InvalidTransaction");
}
if (!result.value.success) {
throw result.value.value;
}
const { resolve, reject, promise } = Promise.withResolvers();
this.client.submitAndWatch(tx.asHex()).subscribe({
next(value) {
switch (value.type) {
case "broadcasted":
return resolve();
}
},
error(err) {
return reject(err);
},
});
return promise;
}
async signAndSubmit(tx) {
const extrinsic = Binary.fromBytes(await this.accountProvider.sign(tx));
// const queue = this.queue;
const { resolve, reject, promise } = Promise.withResolvers();
this.client.submitAndWatch(extrinsic.asHex()).subscribe({
next(value) {
switch (value.type) {
case "txBestBlocksState":
// TODO: Handle passing events to the client queue.
// // to get the events and deposit them on the events queue
// for (const event of value.events) {
// switch (event.type) {
// case "Contracts":
// switch (event.value.type) {
// case "ContractEmitted":
// queue.depositEvent({
// type: "AttendanceMarked",
// id: BigInt(0),
// issuer: 0,
// owner: "",
// time: BigInt(Date.now()),
// });
// break;
// }
// break;
// }
// }
return resolve();
}
},
error(err) {
return reject(err);
},
});
return promise;
}
};
TransactionSubmitter = __decorate([
injectable(),
__param(0, inject(TOKEN.POLKADOT_CLIENT)),
__param(1, inject(TOKEN.KREIVO_API)),
__param(2, inject(TOKEN.ACCUNT_PROVIDER)),
__metadata("design:paramtypes", [Object, Object, Object])
], TransactionSubmitter);
export { TransactionSubmitter };