22// SPDX-License-Identifier: Apache-2.0
33
44import type { Bytes , Vec } from '@polkadot/types' ;
5- import type { ChainProperties , ContractConstructorSpecLatest , ContractEventParamSpecLatest , ContractMessageParamSpecLatest , ContractMessageSpecLatest , ContractMetadata , ContractMetadataV4 , ContractMetadataV5 , ContractProjectInfo , ContractTypeSpec , EventRecord } from '@polkadot/types/interfaces' ;
5+ import type { ChainProperties , ContractConstructorSpecLatest , ContractEventParamSpecLatest , ContractMessageParamSpecLatest , ContractMessageSpecLatest , ContractMetadata , ContractMetadataV4 , ContractMetadataV5 , ContractMetadataV6 , ContractProjectInfo , ContractTypeSpec , EventRecord } from '@polkadot/types/interfaces' ;
66import type { Codec , Registry , TypeDef } from '@polkadot/types/types' ;
77import type { AbiConstructor , AbiEvent , AbiEventParam , AbiMessage , AbiMessageParam , AbiParam , DecodedEvent , DecodedMessage } from '../types.js' ;
88
@@ -19,12 +19,12 @@ interface AbiJson {
1919}
2020
2121type EventOf < M > = M extends { spec : { events : Vec < infer E > } } ? E : never
22- export type ContractMetadataSupported = ContractMetadataV4 | ContractMetadataV5 ;
22+ export type ContractMetadataSupported = ContractMetadataV4 | ContractMetadataV5 | ContractMetadataV6 ;
2323type ContractEventSupported = EventOf < ContractMetadataSupported > ;
2424
2525const l = logger ( 'Abi' ) ;
2626
27- const PRIMITIVE_ALWAYS = [ 'AccountId' , 'AccountIndex' , 'Address' , 'Balance' ] ;
27+ const PRIMITIVE_ALWAYS = [ 'AccountId' , 'AccountId20' , ' AccountIndex', 'Address' , 'Balance' ] ;
2828
2929function findMessage < T extends AbiMessage > ( list : T [ ] , messageOrId : T | string | number ) : T {
3030 const message = isNumber ( messageOrId )
@@ -66,9 +66,28 @@ function getMetadata (registry: Registry, json: AbiJson): ContractMetadataSuppor
6666 return upgradedMetadata ;
6767}
6868
69- function parseJson ( json : Record < string , unknown > , chainProperties ?: ChainProperties ) : [ Record < string , unknown > , Registry , ContractMetadataSupported , ContractProjectInfo ] {
69+ function isRevive ( json : Record < string , unknown > ) : boolean {
70+ const source = json [ 'source' ] ;
71+ const version = json [ 'version' ] ;
72+
73+ const hasContractBinary =
74+ typeof source === 'object' &&
75+ source !== null &&
76+ 'contract_binary' in source ;
77+
78+ const hasVersion =
79+ typeof version === 'number' && version >= 6 ;
80+
81+ return hasContractBinary || hasVersion ;
82+ }
83+
84+ function parseJson ( json : Record < string , unknown > , chainProperties ?: ChainProperties ) : [ Record < string , unknown > , Registry , ContractMetadataSupported , ContractProjectInfo , boolean ] {
7085 const registry = new TypeRegistry ( ) ;
71- const info = registry . createType ( 'ContractProjectInfo' , json ) as unknown as ContractProjectInfo ;
86+
87+ const revive = isRevive ( json ) ;
88+ const typeName = revive ? 'ContractReviveProjectInfo' : 'ContractProjectInfo' ;
89+
90+ const info = registry . createType ( typeName , json ) as unknown as ContractProjectInfo ;
7291 const metadata = getMetadata ( registry , json as unknown as AbiJson ) ;
7392 const lookup = registry . createType ( 'PortableRegistry' , { types : metadata . types } , true ) ;
7493
@@ -84,7 +103,7 @@ function parseJson (json: Record<string, unknown>, chainProperties?: ChainProper
84103 lookup . getTypeDef ( id )
85104 ) ;
86105
87- return [ json , registry , metadata , info ] ;
106+ return [ json , registry , metadata , info , revive ] ;
88107}
89108
90109/**
@@ -112,9 +131,10 @@ export class Abi {
112131 readonly metadata : ContractMetadataSupported ;
113132 readonly registry : Registry ;
114133 readonly environment = new Map < string , TypeDef | Codec > ( ) ;
134+ readonly isRevive : boolean ;
115135
116136 constructor ( abiJson : Record < string , unknown > | string , chainProperties ?: ChainProperties ) {
117- [ this . json , this . registry , this . metadata , this . info ] = parseJson (
137+ [ this . json , this . registry , this . metadata , this . info , this . isRevive ] = parseJson (
118138 isString ( abiJson )
119139 ? JSON . parse ( abiJson ) as Record < string , unknown >
120140 : abiJson ,
0 commit comments