@polkadot/types/types#AnyTuple TypeScript Examples
The following examples show how to use
@polkadot/types/types#AnyTuple.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: substrate-rpc.ts From moonbeam with GNU General Public License v3.0 | 6 votes |
createBlockWithExtrinsicParachain = async <
Call extends SubmittableExtrinsic<ApiType>,
ApiType extends ApiTypes
>(
api: ApiPromise,
sender: AddressOrPair,
polkadotCall: Call
): Promise<{ extrinsic: GenericExtrinsic<AnyTuple>; events: Event[] }> => {
console.log("-------------- EXTRINSIC CALL -------------------------------");
// This should return a Uint8Array
const extrinsicHash = (await polkadotCall.signAndSend(sender)) as unknown as Uint8Array;
// We create the block which is containing the extrinsic
//const blockResult = await context.createBlock();
return await tryLookingForEvents(api, extrinsicHash);
}
Example #2
Source File: Waitlist.ts From gear-js with GNU General Public License v3.0 | 6 votes |
private transformWaitlist(option: StoredMessage, keys?: StorageKey<AnyTuple>): WaitlistItem {
if (option === null) {
return null;
}
const [storedDispatched, blockNumber] = this.api.createType('(GearCoreMessageStoredStoredDispatch, u32)', option);
let result = {
blockNumber: blockNumber.toNumber(),
storedDispatch: storedDispatched.toHuman() as unknown as StoredDispatch,
};
if (keys) {
const [programId, messageId] = keys.toHuman() as [ProgramId, MessageId];
result['programId'] = programId;
result['messageId'] = messageId;
}
return result;
}
Example #3
Source File: transaction.ts From interbtc-api with Apache License 2.0 | 6 votes |
async sendLogged<T extends AnyTuple>(
transaction: SubmittableExtrinsic<"promise">,
successEventType?: AugmentedEvent<ApiTypes, T>,
onlyInBlock?: boolean
): Promise<ISubmittableResult> {
if (this.account === undefined) {
return Promise.reject(new Error(ACCOUNT_NOT_SET_ERROR_MESSAGE));
}
return DefaultTransactionAPI.sendLogged(this.api, this.account, transaction, successEventType, onlyInBlock);
}
Example #4
Source File: transaction.ts From interbtc-api with Apache License 2.0 | 6 votes |
static async waitForEvent<T extends AnyTuple>(
api: ApiPromise,
event: AugmentedEvent<ApiTypes, T>,
timeoutMs: number
): Promise<boolean> {
// Use this function with a timeout.
// Unless the awaited event occurs, this Promise will never resolve.
let timeoutHandle: NodeJS.Timeout;
const timeoutPromise = new Promise((_, reject) => {
timeoutHandle = setTimeout(() => reject(), timeoutMs);
});
await Promise.race([
new Promise<void>((resolve, _reject) => {
api.query.system.events((eventsVec) => {
const events = eventsVec.toArray();
if (this.doesArrayContainEvent(events, event)) {
resolve();
}
});
}),
timeoutPromise,
]).then((_) => {
clearTimeout(timeoutHandle);
});
return true;
}
Example #5
Source File: transaction.ts From interbtc-api with Apache License 2.0 | 6 votes |
static doesArrayContainEvent<T extends AnyTuple>(
events: EventRecord[],
eventType: AugmentedEvent<ApiTypes, T>
): boolean {
for (const { event } of events) {
if (eventType.is(event)) {
return true;
}
}
return false;
}
Example #6
Source File: issueRedeem.ts From interbtc-api with Apache License 2.0 | 6 votes |
/**
* @param events The EventRecord array returned after sending a transaction
* @param methodToCheck The name of the event method whose existence to check
* @returns The id associated with the transaction. If the EventRecord array does not
* contain required events, the function throws an error.
*/
export function getRequestIdsFromEvents(
events: EventRecord[],
eventToFind: AugmentedEvent<ApiTypes, AnyTuple>,
api: ApiPromise
): Hash[] {
const ids = new Array<Hash>();
for (const { event } of events) {
if (eventToFind.is(event)) {
// the redeem id has type H256 and is the first item of the event data array
const id = api.createType("Hash", event.data[0]);
ids.push(id);
}
}
if (ids.length > 0) return ids;
throw new Error("Transaction failed");
}
Example #7
Source File: substrate-rpc.ts From polkadot-launch with MIT License | 6 votes |
async function tryLookingForEvents(
api: ApiPromise,
extrinsicHash: Uint8Array
): Promise<{ extrinsic: GenericExtrinsic<AnyTuple>; events: Event[] }> {
await waitOneBlock(api);
let { extrinsic, events } = await lookForExtrinsicAndEvents(
api,
extrinsicHash
);
if (events.length > 0) {
return {
extrinsic,
events,
};
} else {
return await tryLookingForEvents(api, extrinsicHash);
}
}
Example #8
Source File: substrate-rpc.ts From polkadot-launch with MIT License | 6 votes |
createBlockWithExtrinsicParachain = async <
Call extends SubmittableExtrinsic<ApiType>,
ApiType extends ApiTypes
>(
api: ApiPromise,
sender: AddressOrPair,
polkadotCall: Call
): Promise<{ extrinsic: GenericExtrinsic<AnyTuple>; events: Event[] }> => {
console.log("-------------- EXTRINSIC CALL -------------------------------");
// This should return a Uint8Array
const extrinsicHash = (await polkadotCall.signAndSend(
sender
)) as unknown as Uint8Array;
// We create the block which is containing the extrinsic
//const blockResult = await context.createBlock();
return await tryLookingForEvents(api, extrinsicHash);
}
Example #9
Source File: Blocks.ts From gear-js with GNU General Public License v3.0 | 5 votes |
/**
* Get all extrinsic of particular block
* @param blockHash hash of particular block
* @returns Vec of extrinsics
*/
async getExtrinsics(blockHash: `0x${string}` | Uint8Array): Promise<Vec<GenericExtrinsic<AnyTuple>>> {
return (await this.get(blockHash)).block.extrinsics;
}
Example #10
Source File: redeem.ts From interbtc-api with Apache License 2.0 | 5 votes |
private getRedeemIdsFromEvents(events: EventRecord[], event: AugmentedEvent<ApiTypes, AnyTuple>): Hash[] {
return getRequestIdsFromEvents(events, event, this.api);
}
Example #11
Source File: transaction.ts From interbtc-api with Apache License 2.0 | 5 votes |
static async sendLogged<T extends AnyTuple>(
api: ApiPromise,
account: AddressOrPair,
transaction: SubmittableExtrinsic<"promise">,
successEventType?: AugmentedEvent<ApiTypes, T>,
onlyInBlock?: boolean
): Promise<ISubmittableResult> {
const { unsubscribe, result } = await new Promise((resolve, reject) => {
let unsubscribe: () => void;
// When passing { nonce: -1 } to signAndSend the API will use system.accountNextIndex to determine the nonce
transaction
.signAndSend(account, { nonce: -1 }, (result: ISubmittableResult) => callback({ unsubscribe, result }))
.then((u: () => void) => (unsubscribe = u))
.catch((error) => reject(error));
function callback(callbackObject: { unsubscribe: () => void; result: ISubmittableResult }): void {
const status = callbackObject.result.status;
if (onlyInBlock) {
if (status.isInBlock) {
resolve(callbackObject);
}
} else {
if (status.isFinalized) {
resolve(callbackObject);
}
}
}
});
if (onlyInBlock) {
console.log(`Transaction included at blockHash ${result.status.asInBlock}`);
} else {
console.log(`Transaction finalized at blockHash ${result.status.asFinalized}`);
}
unsubscribe(result);
// Print all events for debugging
DefaultTransactionAPI.printEvents(api, result.events);
const dispatchError = result.dispatchError;
if (dispatchError) {
// Construct error message
let message = "The transaction failed.";
// Runtime error in one of the parachain modules
if (dispatchError.isModule) {
// for module errors, we have the section indexed, lookup
const decoded = api.registry.findMetaError(dispatchError.asModule);
const { docs, name, section } = decoded;
message = message.concat(` The error code is ${section}.${name}. ${docs.join(" ")}`);
// Bad origin
} else if (dispatchError.isBadOrigin) {
message = message.concat(` The error is caused by using an incorrect account.
The error code is BadOrigin ${dispatchError}.`);
}
// Other, CannotLookup, no extra info
else {
message = message.concat(` The error is ${dispatchError}.`);
}
console.log(message);
return Promise.reject(new Error(message));
}
return result;
}