Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update reply field in data of UserMessageSent event #848

Merged
merged 7 commits into from
Jul 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 10 additions & 21 deletions api/.eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,16 @@
"es2021": true,
"node": true
},
"extends": [
"eslint:recommended",
"plugin:@typescript-eslint/recommended"
],
"extends": ["eslint:recommended", "plugin:@typescript-eslint/recommended"],
"parser": "@typescript-eslint/parser",
"parserOptions": {
"ecmaVersion": "latest",
"sourceType": "module"
},
"plugins": [
"@typescript-eslint"
],
"plugins": ["@typescript-eslint"],
"rules": {
"semi": ["error", "always"],
"object-curly-spacing": ["error", "always"],
"indent": [
"warn",
2,
Expand All @@ -26,19 +23,11 @@
"flatTernaryExpressions": false
}
],
"linebreak-style": [
"error",
"unix"
],
"quotes": [
"warn",
"single"
],
"semi": [
"error",
"always"
],
"linebreak-style": ["error", "unix"],
"quotes": ["warn", "single"],
"@typescript-eslint/no-empty-function": 0,
"no-case-declarations": 0
"no-case-declarations": 0,
"eol-last": "error",
"max-len": ["error", { "code": 120, "tabWidth": 2, "ignoreStrings": true }]
}
}
}
16 changes: 16 additions & 0 deletions api/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
## 0.24.0

_07/19/2022_

https://github.com/gear-tech/gear-js/pull/848

### Changes

- Update `reply` field of `Message` type according to https://github.com/gear-tech/gear/pull/1198
_Type of `reply` field is Struct with fields `replyTo` and `exitCode` instead of Tuple from now on_
- Update type of waitlist and mailbox item according to https://github.com/gear-tech/gear/pull/1197
_From this moment the types are Tuple with 2 elements, the first one remained the same and the 2nd one is object with `start` and `finish` fields that show block's number when message gets into **mailbox / waitlist** and last block's number when message gets out of **mailbox / waitlist**_
- Also `waitlist.read` and `mailbox.read` methods now return Codec types without keys.


---
## 0.23.6

_07/19/2022_
Expand Down
4 changes: 2 additions & 2 deletions api/examples/src/message/claimValue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ const main = async () => {

const mailbox = await api.mailbox.read(GearKeyring.decodeAddress(alice.address));

const messageWithValue = mailbox.find(([_, message]) => parseInt(message.value) > 0);
const messageWithValue = mailbox.find((item) => item[0].value.gtn(0));

console.log(messageWithValue);

const messageId = messageWithValue[0][1];
const messageId = messageWithValue[0].id.toHex();

api.mailbox.claimValue.submit(messageId);
try {
Expand Down
2 changes: 1 addition & 1 deletion api/examples/src/message/waitForReply.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export function waitForReply(api: GearApi, programId: string): (messageId: strin
},
} = event as UserMessageSent;
if (source.eq(programId) && reply.isSome) {
messages[reply.unwrap()[0].toHex()] = event as UserMessageSent;
messages[reply.unwrap().replyTo.toHex()] = event as UserMessageSent;
}
}
});
Expand Down
2 changes: 1 addition & 1 deletion api/jest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ const config: Config.InitialOptions = {
testTimeout: 15000,
};

export default config;
export default config;
4 changes: 2 additions & 2 deletions api/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions api/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@gear-js/api",
"version": "0.23.6",
"version": "0.24.0",
"description": "A JavaScript library that provides functionality to connect GEAR Component APIs.",
"main": "cjs/index.js",
"module": "index.js",
Expand All @@ -24,8 +24,8 @@
"test": "jest --runInBand",
"prebuild": "rm -rf lib",
"build": "rollup --config rollup.config.js",
"lint": "eslint ./src --ext .ts",
"lint:fix": "eslint --fix ./src --ext .ts"
"lint": "eslint ./src/**/*.ts ./test/**/*.ts ./examples/**/*.ts",
"lint:fix": "eslint --fix ./src/**/*.ts ./test/**/*.ts ./examples/**/*.ts"
},
"exports": {
"./cjs/package.json": "./cjs/package.json",
Expand All @@ -40,7 +40,7 @@
}
},
"license": "GPL-3.0",
"dependencies": {
"peerDependencies": {
"@polkadot/api": "^8.11.3",
"rxjs": "^7.5.5"
},
Expand Down
68 changes: 49 additions & 19 deletions api/src/Mailbox.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { AccountId32, H256 } from '@polkadot/types/interfaces';
import { AccountId32 } from '@polkadot/types/interfaces';
import { UnsubscribePromise } from '@polkadot/api/types';
import { Option } from '@polkadot/types';

import { MailboxRecord, Hex, StoredMessage } from './types';
import { Hex, MailboxItem } from './types';
import { GearClaimValue } from './Claim';
import { GearApi } from './GearApi';

Expand All @@ -17,33 +17,63 @@ export class GearMailbox {
}

/**
* Read mailbox
* ## Read mailbox connected with account
* @param accountId
* @returns
* @example
* @param numberOfMessages _(default 1000)_ number of messages that will be read from mailbox
* ```javascript
* const alice = '0xd43593c715fdd31c61141abd04a99fd6822c8558854ccde39a5684e7a56da27d'
* const api = await GearApi.create();
* const mailbox = await api.mailbox.read('5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY');
* console.log(mailbox);
* const mailbox = await api.mailbox.read(alice);
* console.log(mailbox.map(item => item.toHuman()));
* ```
*/
async read(accountId: Hex | AccountId32 | string, messageId?: Hex | H256): Promise<MailboxRecord[]> {
async read(accountId: Hex, numberOfMessages?: number): Promise<MailboxItem[]>;

/**
* ## Get particular message from mailbox
* @param accountId
* @param messageId
* ```javascript
* const api = await GearApi.create();
*
* const alice = '0xd43593c715fdd31c61141abd04a99fd6822c8558854ccde39a5684e7a56da27d'
* const messageId = '0xe9f3b99f23203d0c032868d3bd0349c8e243119626a8af98a2f4ac5ea6c78947'
* const mailbox = await api.mailbox.read(alice, messageId);
* if (mailbox !== null) {
* console.log(mailbox.toHuman());
* }
* ```
*/
async read(accountId: Hex, messageId: Hex): Promise<MailboxItem>;

async read(
accountId: Hex | AccountId32 | string,
messageIdOrNumberOfMessages?: Hex | number,
): Promise<MailboxItem[] | MailboxItem> {
const [messageId, numberOfMessages] =
typeof messageIdOrNumberOfMessages === 'string'
? [messageIdOrNumberOfMessages, undefined]
: [undefined, messageIdOrNumberOfMessages || 1000];
if (messageId) {
const mailbox = await this.api.query.gearMessenger.mailbox(accountId, messageId);
return mailbox.toHuman() as MailboxRecord[];
const typedMailbox = this.api.createType(
'Option<(GearCoreMessageStoredStoredMessage, GearCommonStoragePrimitivesInterval)>',
mailbox,
) as Option<MailboxItem>;
return typedMailbox.unwrapOr(null);
} else {
const keys = await this.api.query.gearMessenger.mailbox.keys(accountId);
if (keys.length === 0) {
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 keyPrefixes = this.api.query.gearMessenger.mailbox.keyPrefix(accountId);
const keysPaged = await this.api.rpc.state.getKeysPaged(keyPrefixes, 1000, keyPrefixes);
const mailbox = (await this.api.rpc.state.queryStorageAt(keysPaged)) as Option<StoredMessage>[];
return mailbox.map((option, index) => {
return [
keys[index].toHuman() as [Hex, Hex],
this.api.createType('GearCoreMessageStoredStoredMessage', option.unwrap()).toHuman(),
] as MailboxRecord;
const mailbox = (await this.api.rpc.state.queryStorageAt(keysPaged)) as Option<MailboxItem>[];
return mailbox.map((item) => {
const typedItem = this.api.createType(
'Option<(GearCoreMessageStoredStoredMessage, GearCommonStoragePrimitivesInterval)>',
item,
) as Option<MailboxItem>;
return typedItem.unwrapOr(null);
});
}
}
Expand Down
79 changes: 44 additions & 35 deletions api/src/Waitlist.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { Option, StorageKey } from '@polkadot/types';
import { AnyTuple } from '@polkadot/types/types';
import { Option } from '@polkadot/types';

import { Hex, HumanStoredDispatch, WaitlistItem, StoredMessage } from './types';
import { Hex, WaitlistItem } from './types';
import { GearApi } from './GearApi';

export class GearWaitlist {
Expand All @@ -11,51 +10,61 @@ export class GearWaitlist {
this.api = gearApi;
}

async read(programId: Hex): Promise<WaitlistItem[]>;
async read(programId: Hex, messageId: Hex): Promise<WaitlistItem>;
/**
* ## _Read program's waitlist_
* @param programId
* @param numberOfMessages _(default 1000)_ number of messages that will be read from program's waitlist
* @example
* ```javascript
* const api = await GearApi.create();
* const waitlist = await api.waitlist.read('0xe0c6997d0bd83269ec108474494e2bd6ed156b30de599b9f2c91e82bb6ad04e8');
* console.log(waitlist.map(item => item.toHuman()));
* ```
*/
async read(programId: Hex, numberOfMessages?: number): Promise<WaitlistItem[]>;

/**
*
* ## _Get particular message from program's waitlist_
* @param programId
* @param messageId
* @returns Waitlist of particular program if messageId was not specified
* @example
* ```javascript
* const api = await GearApi.create();
* const waitlist = await api.waitlist.read('0xe0c6997d0bd83269ec108474494e2bd6ed156b30de599b9f2c91e82bb6ad04e8');
* console.log(waitlist);
* const programId = '0xe0c6997d0bd83269ec108474494e2bd6ed156b30de599b9f2c91e82bb6ad04e8'
* const messageId = '0xe9f3b99f23203d0c032868d3bd0349c8e243119626a8af98a2f4ac5ea6c78947'
* const waitlist = await api.waitlist.read(programId, messageId);
* console.log(waitlist.toHuman());
* ```
*/
async read(programId: Hex, messageId?: Hex): Promise<WaitlistItem[] | WaitlistItem> {
async read(programId: Hex, messageId: Hex): Promise<WaitlistItem>;

async read(programId: Hex, messageIdOrNumberOfMessages?: Hex | number): Promise<WaitlistItem[] | WaitlistItem> {
const [messageId, numberOfMessages] =
typeof messageIdOrNumberOfMessages === 'string'
? [messageIdOrNumberOfMessages, undefined]
: [undefined, messageIdOrNumberOfMessages || 1000];

if (messageId) {
const waitlist = (await this.api.query.gearMessenger.waitlist(programId, messageId)) as Option<StoredMessage>;
return this.transformWaitlist(waitlist.unwrapOr(null));
const waitlist = await this.api.query.gearMessenger.waitlist(programId, messageId);
const typedWaitlist = this.api.createType(
'Option<(GearCoreMessageStoredStoredDispatch, GearCommonStoragePrimitivesInterval)>',
waitlist,
) as Option<WaitlistItem>;
return typedWaitlist.unwrapOr(null);
} else {
const keys = await this.api.query.gearMessenger.waitlist.keys(programId);
if (keys.length === 0) {
const keyPrefix = this.api.query.gearMessenger.waitlist.keyPrefix(programId);
const keysPaged = await this.api.rpc.state.getKeysPaged(keyPrefix, numberOfMessages, keyPrefix);
if (keysPaged.length === 0) {
return [];
}
const keyPrefixes = this.api.query.gearMessenger.waitlist.keyPrefix(programId);
const keysPaged = await this.api.rpc.state.getKeysPaged(keyPrefixes, 1000, keyPrefixes);
const waitlist = (await this.api.rpc.state.queryStorageAt(keysPaged)) as Option<StoredMessage>[];
return waitlist.map((option, index) => this.transformWaitlist(option.unwrap(), keys[index]));
}
}

private transformWaitlist(option: StoredMessage, keys?: StorageKey<AnyTuple>): WaitlistItem {
if (option === null) {
return null;
}
const [storedDispatched, blockNumber] = this.api.createType('(GearCoreMessageStoredStoredDispatch, u32)', option);
const result = {
blockNumber: blockNumber.toNumber(),
storedDispatch: storedDispatched.toHuman() as unknown as HumanStoredDispatch,
};
if (keys) {
const [programId, messageId] = keys.toHuman() as [Hex, Hex];
result['programId'] = programId;
result['messageId'] = messageId;
const waitlist = (await this.api.rpc.state.queryStorageAt(keysPaged)) as Option<WaitlistItem>[];
return waitlist.map((item) => {
const typedItem = this.api.createType(
'Option<(GearCoreMessageStoredStoredDispatch, GearCommonStoragePrimitivesInterval)>',
item,
);
return typedItem.unwrapOr(null);
}) as WaitlistItem[];
}
return result;
}
}
4 changes: 2 additions & 2 deletions api/src/errors/message.errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ export class SendMessageError extends Error {
name = 'SendMessageError';

constructor(message?: string) {
super(message || "Can't send message. Params are invalid");
super(message || 'Can\'t send message. Params are invalid');
}
}

export class SendReplyError extends Error {
name = 'SendReplyError';
constructor(message?: string) {
super(message || "Can't send reply. Params are invalid");
super(message || 'Can\'t send reply. Params are invalid');
}
}
2 changes: 1 addition & 1 deletion api/src/errors/program.errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,4 @@ export class ProgramTerminatedError extends Error {
constructor() {
super('Program terminated');
}
}
}
2 changes: 0 additions & 2 deletions api/src/types/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
export * from './common';
export * from './payload';
export * from './maibox';
export * from './waitlist';
export * from './interfaces';
Loading