-
Notifications
You must be signed in to change notification settings - Fork 349
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support for ink! metadata v3 (#4432)
* Support for ink! metadata v3 * Flatten check for V1-V3 * DRY-er is/as conversions * Test for v2 conversion * Remove stray console.log * Generic converter * Cleanups
- Loading branch information
Showing
7 changed files
with
124 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,38 @@ | ||
// Copyright 2017-2022 @polkadot/api-contract authors & contributors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
import type { ContractMetadataLatest, ContractMetadataV0, ContractMetadataV1 } from '@polkadot/types/interfaces'; | ||
import type { ContractMetadataLatest, ContractMetadataV3 } from '@polkadot/types/interfaces'; | ||
import type { Registry } from '@polkadot/types/types'; | ||
|
||
import { v0ToV1 } from './toV1'; | ||
import { v1ToV2 } from './toV2'; | ||
import { v2ToV3 } from './toV3'; | ||
|
||
export function v1ToLatest (registry: Registry, v1: ContractMetadataV1): ContractMetadataLatest { | ||
return v1ToV2(registry, v1); | ||
type Versions = 'V3' | 'V2' | 'V1' | 'V0'; | ||
|
||
type Converter = (registry: Registry, vx: any) => ContractMetadataLatest; | ||
|
||
// Helper to convert metadata from one step to the next | ||
function createConverter <I, O> (next: (registry: Registry, input: O) => ContractMetadataLatest, step: (registry: Registry, input: I) => O): (registry: Registry, input: I) => ContractMetadataLatest { | ||
return (registry: Registry, input: I): ContractMetadataLatest => | ||
next(registry, step(registry, input)); | ||
} | ||
|
||
export function v0ToLatest (registry: Registry, v0: ContractMetadataV0): ContractMetadataLatest { | ||
return v1ToLatest(registry, v0ToV1(registry, v0)); | ||
export function v3ToLatest (registry: Registry, v3: ContractMetadataV3): ContractMetadataLatest { | ||
return v3; | ||
} | ||
|
||
export const v2ToLatest = createConverter(v3ToLatest, v2ToV3); | ||
export const v1ToLatest = createConverter(v2ToLatest, v1ToV2); | ||
export const v0ToLatest = createConverter(v1ToLatest, v0ToV1); | ||
|
||
// 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 = ['V3', 'V2', 'V1']; | ||
|
||
export const convertVersions: [Versions, Converter][] = [ | ||
['V3', v3ToLatest], | ||
['V2', v2ToLatest], | ||
['V1', v1ToLatest], | ||
['V0', v0ToLatest] | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
// Copyright 2017-2022 @polkadot/api-contract authors & contributors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
import type { ContractMetadataV2, ContractMetadataV3 } from '@polkadot/types/interfaces'; | ||
import type { Registry } from '@polkadot/types/types'; | ||
|
||
import { objectSpread } from '@polkadot/util'; | ||
|
||
export function v2ToV3 (registry: Registry, v2: ContractMetadataV2): ContractMetadataV3 { | ||
return registry.createType('ContractMetadataV3', objectSpread({}, v2, { | ||
spec: objectSpread({}, v2.spec, { | ||
constructors: v2.spec.constructors.map((c) => | ||
// V3 introduces the payable flag on constructors, for <V3, it is always true | ||
registry.createType('ContractConstructorSpecV3', objectSpread({}, c, { payable: true })) | ||
) | ||
}) | ||
})); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters