Skip to content

Commit

Permalink
[api] Add VaraApiV1000 and VaraApiV1010 classes (#1458)
Browse files Browse the repository at this point in the history
  • Loading branch information
osipov-mit authored Nov 28, 2023
1 parent 48a9abb commit e0c6a92
Show file tree
Hide file tree
Showing 20 changed files with 131 additions and 127 deletions.
4 changes: 3 additions & 1 deletion api/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,9 @@ const nodeVersion = await gearApi.nodeVersion();
const genesis = gearApi.genesisHash.toHex();
```

Since Vara and VaraTestnet can have different runtime versions, they can have different extrinsics signatures. If your application is running on the Vara Network it is more convinient to use the `VaraApi` class instead of `GearApi` and `VaraTestnetApi` for the Vara Testnet Network
Since different networks can have different runtime versions, they can have different extrinsics / rpc calls signatures.
In general you may use `GearApi` class to interact with the node, but it can be more convivnient to use classes based on a specific runtime version.
For example for interacting with the network that has runtime version more than `1010` you can use the `VaraApiV1010` class.
---

## Payloads and metadata
Expand Down
55 changes: 32 additions & 23 deletions api/src/Message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,12 @@ import { HexString } from '@polkadot/util/types';
import { ISubmittableResult } from '@polkadot/types/types';
import { ReplaySubject } from 'rxjs';

import {
IMessageSendOptions,
IMessageSendReplyOptions,
VaraMessageSendOptions,
VaraMessageSendReplyOptions,
} from './types';
import { MessageSendOptions, MessageSendReplyOptions } from './types';
import { SendMessageError, SendReplyError } from './errors';
import { encodePayload, getExtrinsic, validateGasLimit, validateMailboxItem, validateValue } from './utils';
import { GearTransaction } from './Transaction';
import { ProgramMetadata } from './metadata';
import { SPEC_VERSION } from './consts';
import { UserMessageSentData } from './events';

export class GearMessage extends GearTransaction {
Expand All @@ -21,7 +17,9 @@ export class GearMessage extends GearTransaction {
* @param args Message parameters.
* @param meta Program metadata obtained using `getProgramMetadata` function.
* @param typeIndex (optional) Index of type in the registry. If not specified the type index from `meta.handle.input` will be used instead.
* @returns Submittable result
* @returns Submitable result
*
* _Note that parameter `prepaid` is not supported starting from `1010` runtime version._
* @example
* ```javascript
* const programId = '0x..';
Expand All @@ -40,7 +38,7 @@ export class GearMessage extends GearTransaction {
* ```
*/
send(
args: IMessageSendOptions | VaraMessageSendOptions,
args: MessageSendOptions,
meta: ProgramMetadata,
typeIndex?: number,
): SubmittableExtrinsic<'promise', ISubmittableResult>;
Expand All @@ -50,7 +48,9 @@ export class GearMessage extends GearTransaction {
* @param args Message parameters
* @param hexRegistry Registry in hex format
* @param typeIndex Index of type in the registry.
* @returns Submitted result
* @returns Submitable result
*
* _Note that parameter `prepaid` is not supported starting from `1010` runtime version._
* @example
* ```javascript
* const programId = '0x..';
Expand All @@ -68,7 +68,7 @@ export class GearMessage extends GearTransaction {
* ```
*/
send(
args: IMessageSendOptions | VaraMessageSendOptions,
args: MessageSendOptions,
hexRegistry: HexString,
typeIndex: number,
): SubmittableExtrinsic<'promise', ISubmittableResult>;
Expand All @@ -78,7 +78,9 @@ export class GearMessage extends GearTransaction {
* @param args Message parameters
* @param metaOrHexRegistry (optional) Registry in hex format or ProgramMetadata
* @param typeName payload type (one of the default rust types if metadata or registry don't specified)
* @returns Submitted result
* @returns Submitable result
*
* _Note that parameter `prepaid` is not supported starting from `1010` runtime version._
* @example
* ```javascript
* const programId = '0x..';
Expand All @@ -95,7 +97,7 @@ export class GearMessage extends GearTransaction {
* ```
*/
send(
args: IMessageSendOptions | VaraMessageSendOptions,
args: MessageSendOptions,
metaOrHexRegistry?: ProgramMetadata | HexString,
typeName?: string,
): SubmittableExtrinsic<'promise', ISubmittableResult>;
Expand All @@ -105,10 +107,10 @@ export class GearMessage extends GearTransaction {
* @param message
* @param metaOrHexRegistry Metadata
* @param typeIndexOrTypeName type index in registry or type name
* @returns Submitted result
* @returns Submitable result
*/
send(
{ destination, value, gasLimit, payload, ...rest }: IMessageSendOptions | VaraMessageSendOptions,
{ destination, value, gasLimit, payload, ...rest }: MessageSendOptions,
metaOrHexRegistry?: ProgramMetadata | HexString,
typeIndexOrTypeName?: number | string,
): SubmittableExtrinsic<'promise', ISubmittableResult> {
Expand All @@ -120,7 +122,7 @@ export class GearMessage extends GearTransaction {
try {
const txArgs: any[] = [destination, _payload, gasLimit, value || 0];

if (this._api.specVersion >= 1010) {
if (this._api.specVersion >= SPEC_VERSION.V1010) {
txArgs.push('keepAlive' in rest ? rest.keepAlive : true);
} else {
txArgs.push('prepaid' in rest ? rest.prepaid : false);
Expand All @@ -138,7 +140,9 @@ export class GearMessage extends GearTransaction {
* @param args Message parameters
* @param meta Program metadata obtained using `ProgramMetadata.from` method.
* @param typeIndex (optional) Index of type in the registry. If not specified the type index from `meta.reply.input` will be used instead.
* @returns Submitted result
* @returns Submitable result
*
* _Note that parameter `prepaid` is not supported starting from `1010` runtime version._
* @example
* ```javascript
* const replyToMessage = '0x..';
Expand All @@ -157,7 +161,7 @@ export class GearMessage extends GearTransaction {
* ```
*/
sendReply(
args: IMessageSendReplyOptions | VaraMessageSendReplyOptions,
args: MessageSendReplyOptions,
meta?: ProgramMetadata,
typeIndex?: number,
): Promise<SubmittableExtrinsic<'promise', ISubmittableResult>>;
Expand All @@ -167,7 +171,9 @@ export class GearMessage extends GearTransaction {
* @param args Message parameters
* @param hexRegistry Registry in hex format
* @param typeIndex Index of type in the registry.
* @returns Submitted result
* @returns Submitable result
*
* _Note that parameter `prepaid` is not supported starting from `1010` runtime version._
* @example
* ```javascript
* const replyToMessage = '0x..';
Expand All @@ -185,7 +191,7 @@ export class GearMessage extends GearTransaction {
* ```
*/
sendReply(
args: IMessageSendReplyOptions | VaraMessageSendReplyOptions,
args: MessageSendReplyOptions,
hexRegistry: HexString,
typeIndex: number,
): Promise<SubmittableExtrinsic<'promise', ISubmittableResult>>;
Expand All @@ -195,7 +201,10 @@ export class GearMessage extends GearTransaction {
* @param args Message parameters
* @param metaOrHexRegistry (optional) Registry in hex format or ProgramMetadata
* @param typeName payload type (one of the default rust types if metadata or registry don't specified)
* @returns Submitted result
* @returns Submitable result
*
* _Note that parameter `prepaid` is not supported starting from `1010` runtime version._
* @example
* ```javascript
* const replyToMessage = '0x..';
* const hexRegistry = '0x...';
Expand All @@ -212,7 +221,7 @@ export class GearMessage extends GearTransaction {
* ```
*/
sendReply(
args: IMessageSendReplyOptions | VaraMessageSendReplyOptions,
args: MessageSendReplyOptions,
metaOrHexRegistry?: ProgramMetadata | HexString,
typeName?: string,
): Promise<SubmittableExtrinsic<'promise', ISubmittableResult>>;
Expand All @@ -226,7 +235,7 @@ export class GearMessage extends GearTransaction {
*/

async sendReply(
{ value, gasLimit, replyToId, payload, account, ...rest }: IMessageSendReplyOptions | VaraMessageSendReplyOptions,
{ value, gasLimit, replyToId, payload, account, ...rest }: MessageSendReplyOptions,
metaOrHexRegistry?: ProgramMetadata | HexString,
typeIndexOrTypeName?: number | string,
): Promise<SubmittableExtrinsic<'promise', ISubmittableResult>> {
Expand All @@ -242,7 +251,7 @@ export class GearMessage extends GearTransaction {
try {
const txArgs: any[] = [replyToId, _payload, gasLimit, value || 0];

if (this._api.specVersion >= 1010) {
if (this._api.specVersion >= SPEC_VERSION.V1010) {
txArgs.push('keepAlive' in rest ? rest.keepAlive : true);
} else {
txArgs.push('prepaid' in rest ? rest.prepaid : false);
Expand Down
43 changes: 13 additions & 30 deletions api/src/Program.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,10 @@ import { randomAsHex } from '@polkadot/util-crypto';

import {
GearCommonProgram,
IProgramCreateOptions,
IProgramCreateResult,
IProgramUploadOptions,
IProgramUploadResult,
VaraProgramCreateOptions,
VaraProgramUploadOptions,
ProgramCreateOptions,
ProgramUploadOptions,
} from './types';
import { ProgramDoesNotExistError, ProgramHasNoMetahash, SubmitProgramError } from './errors';
import {
Expand All @@ -30,6 +28,7 @@ import { GearGas } from './Gas';
import { GearResumeSession } from './ResumeSession';
import { GearTransaction } from './Transaction';
import { ProgramMetadata } from './metadata';
import { SPEC_VERSION } from './consts';

export class GearProgram extends GearTransaction {
public calculateGas: GearGas;
Expand Down Expand Up @@ -63,11 +62,7 @@ export class GearProgram extends GearTransaction {
* })
* ```
*/
upload(
args: IProgramUploadOptions | VaraProgramUploadOptions,
meta?: ProgramMetadata,
typeIndex?: number,
): IProgramUploadResult;
upload(args: ProgramUploadOptions, meta?: ProgramMetadata, typeIndex?: number): IProgramUploadResult;

/**
* ### Upload program with code using registry in hex format to encode payload
Expand All @@ -76,25 +71,21 @@ export class GearProgram extends GearTransaction {
* @param typeIndex Index of type in the registry.
* @returns Object containing program id, generated (or specified) salt, code id, prepared extrinsic
*/
upload(
args: IProgramUploadOptions | VaraProgramUploadOptions,
hexRegistry: HexString,
typeIndex: number,
): IProgramUploadResult;
upload(args: ProgramUploadOptions, hexRegistry: HexString, typeIndex: number): IProgramUploadResult;

/** ### Upload program with code using type name to encode payload
* @param args
* @param metaOrHexRegistry (optional) Metadata or hex registry
* @param typeName type name (one of the default rust types if metadata or registry don't specified)
*/
upload(
args: IProgramUploadOptions | VaraProgramUploadOptions,
args: ProgramUploadOptions,
metaOrHexRegistry?: ProgramMetadata | HexString,
typeName?: string,
): IProgramUploadResult;

upload(
args: IProgramUploadOptions | VaraProgramUploadOptions,
args: ProgramUploadOptions,
metaOrHexRegistry?: ProgramMetadata | HexString,
typeIndexOrTypeName?: number | string,
): IProgramUploadResult {
Expand All @@ -110,7 +101,7 @@ export class GearProgram extends GearTransaction {

try {
const txArgs: any[] = [code, salt, payload, args.gasLimit, args.value || 0];
if (this._api.specVersion >= 1010) {
if (this._api.specVersion >= SPEC_VERSION.V1010) {
txArgs.push('keepAlive' in args ? args.keepAlive : true);
}

Expand Down Expand Up @@ -145,11 +136,7 @@ export class GearProgram extends GearTransaction {
* })
* ```
*/
create(
args: IProgramCreateOptions | VaraProgramCreateOptions,
meta?: ProgramMetadata,
typeIndex?: number | null,
): IProgramCreateResult;
create(args: ProgramCreateOptions, meta?: ProgramMetadata, typeIndex?: number | null): IProgramCreateResult;

/**
* ### Create program from uploaded on chain code using program metadata to encode payload
Expand All @@ -158,25 +145,21 @@ export class GearProgram extends GearTransaction {
* @param typeIndex Index of type in the registry.
* @returns Object containing program id, generated (or specified) salt, prepared extrinsic
*/
create(
args: IProgramCreateOptions | VaraProgramCreateOptions,
hexRegistry: HexString,
typeIndex: number,
): IProgramCreateResult;
create(args: ProgramCreateOptions, hexRegistry: HexString, typeIndex: number): IProgramCreateResult;

/** ## Create program using existed codeId
* @param args
* @param metaOrHexRegistry (optional) Metadata or hex registry in hex format
* @param type name type name (one of the default rust types if metadata or registry don't specified)
*/
create(
args: IProgramCreateOptions | VaraProgramCreateOptions,
args: ProgramCreateOptions,
metaOrHexRegistry?: HexString | ProgramMetadata,
typeName?: number | string,
): IProgramCreateResult;

create(
{ codeId, initPayload, value, gasLimit, ...args }: IProgramCreateOptions | VaraProgramCreateOptions,
{ codeId, initPayload, value, gasLimit, ...args }: ProgramCreateOptions,
metaOrHexRegistry?: HexString | ProgramMetadata,
typeIndexOrTypeName?: number | string | null,
): IProgramCreateResult {
Expand All @@ -191,7 +174,7 @@ export class GearProgram extends GearTransaction {
try {
const txArgs: any[] = [codeId, salt, payload, gasLimit, value || 0];

if (this._api.specVersion >= 1010) {
if (this._api.specVersion >= SPEC_VERSION.V1010) {
txArgs.push('keepAlive' in args ? args.keepAlive : true);
}

Expand Down
5 changes: 3 additions & 2 deletions api/src/Storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
ProgramTerminatedError,
} from './errors';
import { GearApi } from './GearApi';
import { SPEC_VERSION } from './consts';

export class GearProgramStorage {
constructor(protected _api: GearApi) {}
Expand Down Expand Up @@ -48,11 +49,11 @@ export class GearProgramStorage {
async getProgramPages(programId: HexString, program: GearCommonActiveProgram, at?: HexString): Promise<IGearPages> {
const pages = {};
const query =
this._api.specVersion >= 1020
this._api.specVersion >= SPEC_VERSION.V1030
? this._api.query.gearProgram.memoryPages
: this._api.query.gearProgram.memoryPageStorage;

const args = this._api.specVersion >= 1020 ? [programId, program.memoryInfix] : [programId];
const args = this._api.specVersion >= SPEC_VERSION.V1030 ? [programId, program.memoryInfix] : [programId];

for (const page of program.pagesWithData) {
pages[page.toNumber()] = u8aToU8a(
Expand Down
4 changes: 2 additions & 2 deletions api/src/apis/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export { VaraApi } from './vara/api';
export { VaraTestnetApi } from './vara-testnet/api';
export { VaraApiV1000 } from './v1000/api';
export { VaraApiV1010 } from './v1010/api';
8 changes: 8 additions & 0 deletions api/src/apis/v1000/api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { GearApi } from '../../GearApi';
import { V1000Message } from './message';
import { V1000Program } from './program';

export class VaraApiV1000 extends GearApi {
public declare program: V1000Program;
public declare message: V1000Message;
}
16 changes: 8 additions & 8 deletions api/src/apis/vara/message.ts → api/src/apis/v1000/message.ts
Original file line number Diff line number Diff line change
@@ -1,39 +1,39 @@
import { ISubmittableResult } from '@polkadot/types/types';
import { SubmittableExtrinsic } from '@polkadot/api/types';

import { VaraMessageSendOptions, VaraMessageSendReplyOptions } from '../../types';
import { V1000MessageSendOptions, V1000MessageSendReplyOptions } from '../../types';
import { GearMessage } from '../../Message';
import { ProgramMetadata } from '../../metadata';

export declare class VaraMessage extends GearMessage {
export declare class V1000Message extends GearMessage {
send(
args: VaraMessageSendOptions,
args: V1000MessageSendOptions,
meta: ProgramMetadata,
typeIndex?: number,
): SubmittableExtrinsic<'promise', ISubmittableResult>;
send(
args: VaraMessageSendOptions,
args: V1000MessageSendOptions,
hexRegistry: `0x${string}`,
typeIndex: number,
): SubmittableExtrinsic<'promise', ISubmittableResult>;
send(
args: VaraMessageSendOptions,
args: V1000MessageSendOptions,
metaOrHexRegistry?: ProgramMetadata | `0x${string}`,
typeName?: string,
): SubmittableExtrinsic<'promise', ISubmittableResult>;

sendReply(
args: VaraMessageSendReplyOptions,
args: V1000MessageSendReplyOptions,
meta?: ProgramMetadata,
typeIndex?: number,
): Promise<SubmittableExtrinsic<'promise', ISubmittableResult>>;
sendReply(
args: VaraMessageSendReplyOptions,
args: V1000MessageSendReplyOptions,
hexRegistry: `0x${string}`,
typeIndex: number,
): Promise<SubmittableExtrinsic<'promise', ISubmittableResult>>;
sendReply(
args: VaraMessageSendReplyOptions,
args: V1000MessageSendReplyOptions,
metaOrHexRegistry?: ProgramMetadata | `0x${string}`,
typeName?: string,
): Promise<SubmittableExtrinsic<'promise', ISubmittableResult>>;
Expand Down
Loading

0 comments on commit e0c6a92

Please sign in to comment.