@gear-js/api
Version:
A JavaScript library that provides functionality to connect GEAR Component APIs.
42 lines (39 loc) • 1.72 kB
JavaScript
import '@polkadot/util';
import { ClaimValueError } from '../errors/claim.errors.js';
import { GearTransaction } from './Transaction.js';
class GearMailbox extends GearTransaction {
async read(accountId, messageIdOrNumberOfMessages) {
const [messageId, numberOfMessages] = typeof messageIdOrNumberOfMessages === 'string'
? [messageIdOrNumberOfMessages, undefined]
: [undefined, messageIdOrNumberOfMessages || 1000];
if (messageId) {
const mailbox = await this._api.query.gearMessenger.mailbox(accountId, messageId);
const typedMailbox = this._api.createType('Option<(UserStoredMessage, GearCommonStoragePrimitivesInterval)>', mailbox);
return typedMailbox.unwrapOr(null);
}
const keyPrefixes = this._api.query.gearMessenger.mailbox.keyPrefix(accountId);
const keysPaged = await this._api.rpc.state.getKeysPaged(keyPrefixes, numberOfMessages, keyPrefixes);
if (keysPaged.length === 0) {
return [];
}
const mailbox = (await this._api.rpc.state.queryStorageAt(keysPaged));
return mailbox.map((item) => {
const typedItem = this._api.createType('Option<(UserStoredMessage, GearCommonStoragePrimitivesInterval)>', item);
return typedItem.unwrapOr(null);
});
}
/**
* ## Create `claimValue` extrinsic
* @param messageId MessageId with value to be claimed
*/
claimValue(messageId) {
try {
this.extrinsic = this._api.tx.gear.claimValue(messageId);
return this.extrinsic;
}
catch (_) {
throw new ClaimValueError();
}
}
}
export { GearMailbox };