-
Notifications
You must be signed in to change notification settings - Fork 352
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use Si V1 in contracts (w/ PortableRegistry usage) (#3988)
* Use Si V1 in contracts (w/ PortableRegistry usage) * Type id lookups * Fix build & linting * Remove extra .toNumber() (VSCode has gone crazy) * v1 tests * Cleanup parsing * Explicit type cast for v1 types * Unneeded toNumber * Align events exposure with messages/constructors * dedupe * Re-add typedef extraction tests * with typedefs * Override for always primitives * displayName improvements * Versioned project * New structure * Fixes * Cleanups * Ensure API registry is the default used * Adjust arg & return encoding/decoding * Recursive contract * DoNotConstruct to Si type
- Loading branch information
Showing
83 changed files
with
2,695 additions
and
2,496 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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,102 @@ | ||
// Copyright 2017-2021 @polkadot/api-contract authors & contributors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
import type { Registry } from '@polkadot/types/types'; | ||
|
||
import fs from 'fs'; | ||
import path from 'path'; | ||
|
||
import { TypeDefInfo } from '@polkadot/types/types'; | ||
import { blake2AsHex } from '@polkadot/util-crypto'; | ||
|
||
import abis from '../../test/contracts'; | ||
import { Abi } from '.'; | ||
|
||
interface SpecDef { | ||
messages: { | ||
name: string[] | string | ||
}[] | ||
} | ||
|
||
interface JSONAbi { | ||
source: { | ||
compiler: string, | ||
hash: string, | ||
language: string, | ||
wasm: string | ||
}, | ||
spec: SpecDef; | ||
V1: { | ||
spec: SpecDef; | ||
} | ||
} | ||
|
||
function stringifyInfo (key: string, value: unknown): unknown { | ||
return key === 'info' | ||
? TypeDefInfo[value as number] | ||
: value; | ||
} | ||
|
||
function stringifyJson (registry: Registry): string { | ||
const defs = registry.lookup.types.map(({ id }) => | ||
registry.lookup.getTypeDef(id) | ||
); | ||
|
||
return JSON.stringify(defs, stringifyInfo, 2); | ||
} | ||
|
||
describe('Abi', (): void => { | ||
describe('ABI', (): void => { | ||
Object.entries(abis).forEach(([abiName, abi]: [string, JSONAbi]) => { | ||
it(`initializes from a contract ABI (${abiName})`, (): void => { | ||
try { | ||
const messageIds = (abi.V1 ? abi.V1 : abi).spec.messages.map(({ name }) => Array.isArray(name) ? name[0] : name); | ||
const inkAbi = new Abi(abis[abiName]); | ||
|
||
expect(inkAbi.messages.map(({ identifier }) => identifier)).toEqual(messageIds); | ||
} catch (error) { | ||
console.error(error); | ||
|
||
throw error; | ||
} | ||
}); | ||
}); | ||
}); | ||
|
||
describe('TypeDef', (): void => { | ||
Object.keys(abis).forEach((abiName) => { | ||
it(`initializes from a contract ABI (${abiName})`, (): void => { | ||
const abi = new Abi(abis[abiName]); | ||
const json = stringifyJson(abi.registry); | ||
const cmpPath = path.join(__dirname, `../../test/compare/${abiName}.test.json`); | ||
|
||
try { | ||
// eslint-disable-next-line @typescript-eslint/no-var-requires | ||
expect(JSON.parse(json)).toEqual(require(cmpPath)); | ||
} catch (error) { | ||
if (process.env.GITHUB_REPOSITORY) { | ||
console.error(json); | ||
|
||
throw error; | ||
} | ||
|
||
fs.writeFileSync(cmpPath, json, { flag: 'w' }); | ||
} | ||
}); | ||
}); | ||
}); | ||
|
||
it('has the correct hash for the source', (): void => { | ||
const bundle = abis.ink_v0_flipperBundle as JSONAbi; | ||
const abi = new Abi(bundle as any); | ||
|
||
// manual | ||
expect(bundle.source.hash).toEqual(blake2AsHex(bundle.source.wasm)); | ||
|
||
// the Codec hash | ||
expect(bundle.source.hash).toEqual(abi.info.source.wasm.hash.toHex()); | ||
|
||
// the hash as per the actual Abi | ||
expect(bundle.source.hash).toEqual(abi.info.source.wasmHash.toHex()); | ||
}); | ||
}); |
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 |
---|---|---|
@@ -0,0 +1,14 @@ | ||
// Copyright 2017-2021 @polkadot/api-contract authors & contributors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
import type { ContractMetadataLatest, ContractMetadataV0 } from '@polkadot/types/interfaces'; | ||
import type { Registry } from '@polkadot/types/types'; | ||
|
||
import { convertSiV0toV1 } from '@polkadot/types/generic/PortableRegistry'; | ||
|
||
export function toLatest (registry: Registry, v0: ContractMetadataV0): ContractMetadataLatest { | ||
return registry.createType('ContractMetadataLatest', { | ||
...v0, | ||
types: convertSiV0toV1(registry, v0.types) | ||
}); | ||
} |
Oops, something went wrong.