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

contract metadata v5 #5594

Closed
Closed
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
3 changes: 3 additions & 0 deletions packages/api-contract/src/Abi/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ export class Abi {
this.constructors = this.metadata.spec.constructors.map((spec: ContractConstructorSpecLatest, index) =>
this.#createMessage(spec, index, {
isConstructor: true,
isDefault: spec.default.isTrue,
isPayable: spec.payable.isTrue
})
);
Expand All @@ -103,6 +104,7 @@ export class Abi {
const typeSpec = spec.returnType.unwrapOr(null);

return this.#createMessage(spec, index, {
isDefault: spec.default.isTrue,
isMutating: spec.mutates.isTrue,
isPayable: spec.payable.isTrue,
returnType: typeSpec
Expand Down Expand Up @@ -208,6 +210,7 @@ export class Abi {
const message = {
...add,
args,
default: spec.default,
docs: spec.docs.map((d) => d.toString()),
fromU8a: (data: Uint8Array): DecodedMessage => ({
args: this.#decodeArgs(args, data),
Expand Down
26 changes: 25 additions & 1 deletion packages/api-contract/src/Abi/toLatest.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import { TypeRegistry } from '@polkadot/types';

import abis from '../test/contracts/index.js';
import { v0ToLatest, v1ToLatest, v2ToLatest, v3ToLatest, v4ToLatest } from './toLatest.js';
import { v0ToLatest, v1ToLatest, v2ToLatest, v3ToLatest, v4ToLatest, v5ToLatest } from './toLatest.js';

describe('v0ToLatest', (): void => {
const registry = new TypeRegistry();
Expand Down Expand Up @@ -137,3 +137,27 @@ describe('v4ToLatest', (): void => {
).toEqual(false);
});
});

describe('v5ToLatest', (): void => {
const registry = new TypeRegistry();
const contract = registry.createType('ContractMetadata', { V5: abis.ink_v5_flipperContract });
const latest = v5ToLatest(registry, contract.asV5);

it('has the correct constructor flags', (): void => {
expect(
latest.spec.constructors[0].default.isFalse
).toEqual(true);
expect(
latest.spec.constructors[1].default.isTrue
).toEqual(true);
});

it('has the correct message flags', (): void => {
expect(
latest.spec.messages[0].default.isTrue
).toEqual(true);
expect(
latest.spec.messages[1].default.isFalse
).toEqual(true);
});
});
11 changes: 7 additions & 4 deletions packages/api-contract/src/Abi/toLatest.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
// Copyright 2017-2023 @polkadot/api-contract authors & contributors
// SPDX-License-Identifier: Apache-2.0

import type { ContractMetadataLatest, ContractMetadataV4 } from '@polkadot/types/interfaces';
import type { ContractMetadataLatest, ContractMetadataV5 } from '@polkadot/types/interfaces';
import type { Registry } from '@polkadot/types/types';

import { v0ToV1 } from './toV1.js';
import { v1ToV2 } from './toV2.js';
import { v2ToV3 } from './toV3.js';
import { v3ToV4 } from './toV4.js';
import { v4ToV5 } from './toV5.js';

// The versions where an enum is used, aka V0 is missing
// (Order from newest, i.e. we expect more on newest vs oldest)
export const enumVersions = ['V4', 'V3', 'V2', 'V1'] as const;
export const enumVersions = ['V5', 'V4', 'V3', 'V2', 'V1'] as const;

type Versions = typeof enumVersions[number] | 'V0';

Expand All @@ -23,16 +24,18 @@ function createConverter <I, O> (next: (registry: Registry, input: O) => Contrac
next(registry, step(registry, input));
}

export function v4ToLatest (_registry: Registry, v4: ContractMetadataV4): ContractMetadataLatest {
return v4;
export function v5ToLatest (_registry: Registry, v5: ContractMetadataV5): ContractMetadataLatest {
return v5;
}

export const v4ToLatest = /*#__PURE__*/ createConverter(v5ToLatest, v4ToV5);
export const v3ToLatest = /*#__PURE__*/ createConverter(v4ToLatest, v3ToV4);
export const v2ToLatest = /*#__PURE__*/ createConverter(v3ToLatest, v2ToV3);
export const v1ToLatest = /*#__PURE__*/ createConverter(v2ToLatest, v1ToV2);
export const v0ToLatest = /*#__PURE__*/ createConverter(v1ToLatest, v0ToV1);

export const convertVersions: [Versions, Converter][] = [
['V5', v5ToLatest],
['V4', v4ToLatest],
['V3', v3ToLatest],
['V2', v2ToLatest],
Expand Down
22 changes: 22 additions & 0 deletions packages/api-contract/src/Abi/toV5.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Copyright 2017-2023 @polkadot/api-contract authors & contributors
// SPDX-License-Identifier: Apache-2.0

import type { ContractMetadataV4, ContractMetadataV5 } from '@polkadot/types/interfaces';
import type { Registry } from '@polkadot/types/types';

import { objectSpread } from '@polkadot/util';

export function v4ToV5 (registry: Registry, v4: ContractMetadataV4): ContractMetadataV5 {
return registry.createType('ContractMetadataV5', objectSpread({}, v4, {
spec: objectSpread({}, v4.spec, {
constructors: v4.spec.constructors.map((c) =>
// V5 introduces the default flag on constructors, for <V5, it is always false
registry.createType('ContractConstructorSpecV4', objectSpread({}, c, { default: false }))
),
messages: v4.spec.messages.map((m) =>
// V5 introduces the default flag on messages, for <V5, it is always false
registry.createType('ContractMessageSpecV3', objectSpread({}, m, { default: false }))
)
})
}));
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
[
{
"info": "Plain",
"lookupIndex": 0,
"type": "bool",
"docs": [],
"namespace": ""
},
{
"info": "Result",
"lookupIndex": 1,
"type": "Result<Null, InkPrimitivesLangError>",
"docs": [],
"namespace": "Result",
"sub": [
{
"name": "Ok",
"info": "Null",
"lookupIndex": 2,
"type": "Null",
"docs": [],
"namespace": ""
},
{
"name": "Error",
"docs": [],
"info": "Si",
"lookupIndex": 3,
"lookupName": "InkPrimitivesLangError",
"type": "Lookup3"
}
]
},
{
"info": "Null",
"lookupIndex": 2,
"type": "Null",
"docs": [],
"namespace": ""
},
{
"info": "Enum",
"lookupIndex": 3,
"lookupName": "InkPrimitivesLangError",
"type": "{\"_enum\":[\"__Unused0\",\"CouldNotReadInput\"]}",
"docs": [],
"namespace": "ink_primitives::LangError",
"sub": [
{
"index": 0,
"info": "Null",
"name": "__Unused0",
"type": "Null"
},
{
"info": "Null",
"type": "Null",
"index": 1,
"name": "CouldNotReadInput"
}
]
},
{
"info": "Result",
"lookupIndex": 4,
"type": "Result<bool, InkPrimitivesLangError>",
"docs": [],
"namespace": "Result",
"sub": [
{
"name": "Ok",
"info": "Plain",
"lookupIndex": 0,
"type": "bool",
"docs": [],
"namespace": ""
},
{
"name": "Error",
"docs": [],
"info": "Si",
"lookupIndex": 3,
"lookupName": "InkPrimitivesLangError",
"type": "Lookup3"
}
]
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
[
{
"info": "Plain",
"lookupIndex": 0,
"type": "bool",
"docs": [],
"namespace": ""
},
{
"info": "Result",
"lookupIndex": 1,
"type": "Result<Null, InkPrimitivesLangError>",
"docs": [],
"namespace": "Result",
"sub": [
{
"name": "Ok",
"info": "Null",
"lookupIndex": 2,
"type": "Null",
"docs": [],
"namespace": ""
},
{
"name": "Error",
"docs": [],
"info": "Si",
"lookupIndex": 3,
"lookupName": "InkPrimitivesLangError",
"type": "Lookup3"
}
]
},
{
"info": "Null",
"lookupIndex": 2,
"type": "Null",
"docs": [],
"namespace": ""
},
{
"info": "Enum",
"lookupIndex": 3,
"lookupName": "InkPrimitivesLangError",
"type": "{\"_enum\":[\"__Unused0\",\"CouldNotReadInput\"]}",
"docs": [],
"namespace": "ink_primitives::LangError",
"sub": [
{
"index": 0,
"info": "Null",
"name": "__Unused0",
"type": "Null"
},
{
"info": "Null",
"type": "Null",
"index": 1,
"name": "CouldNotReadInput"
}
]
},
{
"info": "Result",
"lookupIndex": 4,
"type": "Result<bool, InkPrimitivesLangError>",
"docs": [],
"namespace": "Result",
"sub": [
{
"name": "Ok",
"info": "Plain",
"lookupIndex": 0,
"type": "bool",
"docs": [],
"namespace": ""
},
{
"name": "Error",
"docs": [],
"info": "Si",
"lookupIndex": 3,
"lookupName": "InkPrimitivesLangError",
"type": "Lookup3"
}
]
}
]
3 changes: 2 additions & 1 deletion packages/api-contract/src/test/contracts/ink/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ import * as v1 from './v1/index.js';
import * as v2 from './v2/index.js';
import * as v3 from './v3/index.js';
import * as v4 from './v4/index.js';
import * as v5 from './v5/index.js';

export default createVersionedExport({ v0, v1, v2, v3, v4 });
export default createVersionedExport({ v0, v1, v2, v3, v4, v5 });

Large diffs are not rendered by default.

Loading