Skip to content

Commit

Permalink
chore(api): remove supporting old runtime versions (#1584)
Browse files Browse the repository at this point in the history
  • Loading branch information
osipov-mit authored Aug 2, 2024
1 parent aa9b9e4 commit 5dc89e3
Show file tree
Hide file tree
Showing 27 changed files with 1,786 additions and 1,496 deletions.
45 changes: 0 additions & 45 deletions api/.eslintrc

This file was deleted.

10 changes: 10 additions & 0 deletions api/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
## 0.38.2
_08/01/2024_

_Compatible with [1.4.2](https://github.com/gear-tech/gear/releases/tag/v1.4.2) release_

### Changes
https://github.com/gear-tech/gear-js/pull/1584
- Remove supporting old runtime versions
- Bump dependencies

## 0.38.1

_05/22/2024_
Expand Down
63 changes: 63 additions & 0 deletions api/eslint.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import globals from 'globals';
import tsParser from '@typescript-eslint/parser';
import typescriptEslint from '@typescript-eslint/eslint-plugin';

export default {
extends: ['eslint:recommended', 'plugin:@typescript-eslint/recommended'],
plugins: {
'@typescript-eslint': typescriptEslint,
},
languageOptions: {
globals: {
...globals.browser,
...globals.node,
},
parser: tsParser,
ecmaVersion: 'latest',
sourceType: 'module',
},
rules: {
semi: ['error', 'always'],
'object-curly-spacing': ['error', 'always'],
indent: [
'warn',
2,
{
SwitchCase: 1,
flatTernaryExpressions: false,
offsetTernaryExpressions: true,
},
],
'linebreak-style': ['error', 'unix'],
quotes: [
'warn',
'single',
{
avoidEscape: true,
},
],
'@typescript-eslint/no-empty-function': 0,
'@typescript-eslint/no-explicit-any': 'off',
'no-case-declarations': 0,
'eol-last': 'error',
'max-len': [
'error',
{
code: 120,
tabWidth: 2,
ignoreStrings: true,
ignoreComments: true,
},
],
'sort-imports': [
'error',
{
ignoreCase: false,
ignoreDeclarationSort: false,
ignoreMemberSort: false,
memberSyntaxSortOrder: ['none', 'all', 'multiple', 'single'],
allowSeparatedGroups: true,
},
],
},
};
28 changes: 14 additions & 14 deletions api/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@gear-js/api",
"version": "0.38.1",
"version": "0.38.2",
"description": "A JavaScript library that provides functionality to connect GEAR Component APIs.",
"main": "cjs/index.js",
"module": "index.js",
Expand Down Expand Up @@ -48,38 +48,38 @@
},
"license": "GPL-3.0",
"peerDependencies": {
"@polkadot/api": "11.2.1",
"@polkadot/api": "12.0.1",
"@polkadot/wasm-crypto": "7.3.2",
"rxjs": "7.8.1"
},
"devDependencies": {
"@babel/plugin-transform-typescript": "7.24.7",
"@babel/preset-env": "7.24.7",
"@babel/plugin-transform-typescript": "7.25.2",
"@babel/preset-env": "7.25.3",
"@babel/preset-typescript": "7.24.7",
"@polkadot/api": "10.13.1",
"@polkadot/typegen": "10.13.1",
"@polkadot/types": "10.13.1",
"@polkadot/types-codec": "10.13.1",
"@polkadot/api": "12.2.3",
"@polkadot/typegen": "12.2.3",
"@polkadot/types": "12.2.3",
"@polkadot/types-codec": "12.2.3",
"@polkadot/wasm-crypto": "7.3.2",
"@rollup/plugin-commonjs": "25.0.8",
"@rollup/plugin-commonjs": "26.0.1",
"@rollup/plugin-json": "6.1.0",
"@rollup/plugin-node-resolve": "15.2.3",
"@rollup/plugin-typescript": "11.1.6",
"@types/jest": "29.5.12",
"@types/rollup-plugin-peer-deps-external": "2.2.5",
"@typescript-eslint/parser": "5.62.0",
"@typescript-eslint/parser": "8.0.0",
"babel-cli": "6.26.0",
"babel-core": "6.26.3",
"babel-jest": "29.7.0",
"eslint": "8.57.0",
"eslint": "9.8.0",
"jest": "29.7.0",
"rollup": "3.29.4",
"rollup": "4.19.2",
"rollup-plugin-peer-deps-external": "2.2.4",
"rollup-plugin-typescript2": "0.36.0",
"rxjs": "7.8.1",
"ts-jest": "29.1.4",
"ts-jest": "29.2.4",
"ts-node": "10.9.2",
"typescript": "5.4.5"
"typescript": "5.5.4"
},
"lint-staged": {
"*.ts": [
Expand Down
4 changes: 2 additions & 2 deletions api/src/Code.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Bytes, Option } from '@polkadot/types';
import { HexString } from '@polkadot/util/types';
import { u8aToHex } from '@polkadot/util';

import { CodeUploadResult, GearCommonCodeMetadata, GearCoreCodeInstrumentedCode } from './types';
import { CodeUploadResult, GearCommonCodeMetadata, GearCoreCodeInstrumentedInstrumentedCode } from './types';
import { generateCodeHash, getIdsFromKeys, validateCodeId } from './utils';
import { CodeDoesNotExistError } from './errors';
import { GearTransaction } from './Transaction';
Expand Down Expand Up @@ -36,7 +36,7 @@ export class GearCode extends GearTransaction {
* ### Get code storage
* @param codeId
*/
async storage(codeId: HexString): Promise<Option<GearCoreCodeInstrumentedCode>> {
async storage(codeId: HexString): Promise<Option<GearCoreCodeInstrumentedInstrumentedCode>> {
return this._api.query.gearProgram.codeStorage(codeId);
}

Expand Down
1 change: 1 addition & 0 deletions api/src/GearApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ export class GearApi extends ApiPromise {
version: 1030,
},
],
GearBuiltinApi: [{ methods: {}, version: 1 }],
},
signedExtensions: {
VoucherLegitimate: { extrinsic: {}, payload: {} },
Expand Down
21 changes: 4 additions & 17 deletions api/src/Message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import {
} from './utils';
import { GearTransaction } from './Transaction';
import { ProgramMetadata } from './metadata';
import { SPEC_VERSION } from './consts';

export class GearMessage extends GearTransaction {
/**
Expand Down Expand Up @@ -117,7 +116,7 @@ export class GearMessage extends GearTransaction {
* @returns Submitable result
*/
send(
{ destination, value, gasLimit, payload, ...rest }: MessageSendOptions,
{ destination, value, gasLimit, payload, keepAlive }: MessageSendOptions,
metaOrHexRegistry?: ProgramMetadata | HexString,
typeIndexOrTypeName?: number | string,
): SubmittableExtrinsic<'promise', ISubmittableResult> {
Expand All @@ -127,13 +126,7 @@ export class GearMessage extends GearTransaction {
const _payload = encodePayload(payload, metaOrHexRegistry, 'handle', typeIndexOrTypeName);

try {
const txArgs: any[] = [destination, _payload, gasLimit, value || 0];

if (this._api.specVersion >= SPEC_VERSION.V1010) {
txArgs.push('keepAlive' in rest ? rest.keepAlive : true);
} else {
txArgs.push('prepaid' in rest ? rest.prepaid : false);
}
const txArgs: any[] = [destination, _payload, gasLimit, value || 0, keepAlive || true];

this.extrinsic = getExtrinsic(this._api, 'gear', 'sendMessage', txArgs);
return this.extrinsic;
Expand Down Expand Up @@ -242,7 +235,7 @@ export class GearMessage extends GearTransaction {
*/

async sendReply(
{ value, gasLimit, replyToId, payload, account, ...rest }: MessageSendReplyOptions,
{ value, gasLimit, replyToId, payload, account, keepAlive }: MessageSendReplyOptions,
metaOrHexRegistry?: ProgramMetadata | HexString,
typeIndexOrTypeName?: number | string,
): Promise<SubmittableExtrinsic<'promise', ISubmittableResult>> {
Expand All @@ -256,13 +249,7 @@ export class GearMessage extends GearTransaction {
const _payload = encodePayload(payload, metaOrHexRegistry, 'reply', typeIndexOrTypeName);

try {
const txArgs: any[] = [replyToId, _payload, gasLimit, value || 0];

if (this._api.specVersion >= SPEC_VERSION.V1010) {
txArgs.push('keepAlive' in rest ? rest.keepAlive : true);
} else {
txArgs.push('prepaid' in rest ? rest.prepaid : false);
}
const txArgs: any[] = [replyToId, _payload, gasLimit, value || 0, keepAlive || true];

this.extrinsic = getExtrinsic(this._api, 'gear', 'sendReply', txArgs);
return this.extrinsic;
Expand Down
18 changes: 5 additions & 13 deletions api/src/Program.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { Bytes, Option, u128, u32 } from '@polkadot/types';
import { Option, u128, u32 } from '@polkadot/types';
import { H256 } from '@polkadot/types/interfaces';
import { HexString } from '@polkadot/util/types';
import { ISubmittableResult } from '@polkadot/types/types';
import { SubmittableExtrinsic } from '@polkadot/api/types';
import { randomAsHex } from '@polkadot/util-crypto';

import {
GearCommonProgram,
GearCoreProgram,
IProgramCreateResult,
IProgramUploadResult,
ProgramCreateOptions,
Expand All @@ -27,7 +27,6 @@ import { GearApi } from './GearApi';
import { GearGas } from './Gas';
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 @@ -99,10 +98,7 @@ export class GearProgram extends GearTransaction {
const programId = generateProgramId(this._api, codeId, salt);

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

this.extrinsic = getExtrinsic(this._api, 'gear', 'uploadProgram', txArgs);

Expand Down Expand Up @@ -171,11 +167,7 @@ export class GearProgram extends GearTransaction {
const programId = generateProgramId(this._api, codeId, salt);

try {
const txArgs: any[] = [codeId, salt, payload, gasLimit, value || 0];

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

this.extrinsic = getExtrinsic(this._api, 'gear', 'createProgram', txArgs);

Expand Down Expand Up @@ -247,7 +239,7 @@ export class GearProgram extends GearTransaction {
* @returns `true` if address belongs to program, and `false` otherwise
*/
async exists(id: HexString): Promise<boolean> {
const program = (await this._api.query.gearProgram.programStorage(id)) as Option<GearCommonProgram>;
const program = (await this._api.query.gearProgram.programStorage(id)) as Option<GearCoreProgram>;
return program.isSome;
}

Expand Down
41 changes: 25 additions & 16 deletions api/src/Storage.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
import { Option, u32 } from '@polkadot/types';
import { Bytes, Option, u32 } from '@polkadot/types';
import { u8aToNumber, u8aToU8a } from '@polkadot/util';
import { H256 } from '@polkadot/types/interfaces';
import { HexString } from '@polkadot/util/types';
import { ITuple } from '@polkadot/types-codec/types';
import { u8aToU8a } from '@polkadot/util';

import { GearCommonActiveProgram, GearCommonProgram, IGearPages, PausedProgramBlockAndHash } from './types';
import { GearCoreProgram, GearCoreProgramActiveProgram, IGearPages, PausedProgramBlockAndHash } from './types';
import {
PausedProgramDoesNotExistError,
ProgramDoesNotExistError,
ProgramExitedError,
ProgramTerminatedError,
} from './errors';
import { GearApi } from './GearApi';
import { SPEC_VERSION } from './consts';

export class GearProgramStorage {
constructor(protected _api: GearApi) {}
Expand All @@ -23,9 +22,9 @@ export class GearProgramStorage {
* @param at _(optional)_ Hash of block to query at
* @returns
*/
async getProgram(id: HexString, at?: HexString): Promise<GearCommonActiveProgram> {
async getProgram(id: HexString, at?: HexString): Promise<GearCoreProgramActiveProgram> {
const api = at ? await this._api.at(at) : this._api;
const programOption = (await api.query.gearProgram.programStorage(id)) as Option<GearCommonProgram>;
const programOption = (await api.query.gearProgram.programStorage(id)) as Option<GearCoreProgram>;

if (programOption.isNone) {
throw new ProgramDoesNotExistError(id);
Expand All @@ -46,20 +45,30 @@ export class GearProgramStorage {
* @param gProg
* @returns
*/
async getProgramPages(programId: HexString, program: GearCommonActiveProgram, at?: HexString): Promise<IGearPages> {
async getProgramPages(
programId: HexString,
program: GearCoreProgramActiveProgram,
at?: HexString,
): Promise<IGearPages> {
const pages = {};
const query =
this._api.specVersion >= SPEC_VERSION.V1100
? this._api.query.gearProgram.memoryPages
: this._api.query.gearProgram.memoryPageStorage;

const args = this._api.specVersion >= SPEC_VERSION.V1100 ? [programId, program.memoryInfix] : [programId];
const blockAt = at || (await this._api.rpc.chain.getHeader()).hash.toHex();

for (const [start, end] of program.pagesWithData.inner) {
for (let page = start.toNumber(); page <= end.toNumber(); page++) {
pages[page] = u8aToU8a(await this._api.provider.send('state_getStorage', [query.key(...args, page), at]));
}
const apiAt = await this._api.at(blockAt);

const keys = await apiAt.query.gearProgram.memoryPages.keys(programId, program.memoryInfix);

const pageNumbers = keys.map((k) => {
const u8aAddr = k.toU8a();
const page = u8aAddr.slice(u8aAddr.length - 4);
return u8aToNumber(page, { isLe: true });
});

for (let i = 0; i < pageNumbers.length; i++) {
const page = pageNumbers[i];
pages[page] = (await this._api.rpc.state.getStorage<Bytes>(keys[i].toHex(), blockAt)).toU8a();
}

return pages;
}

Expand Down
Loading

0 comments on commit 5dc89e3

Please sign in to comment.