2
2
// SPDX-License-Identifier: Apache-2.0
3
3
4
4
import 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' ;
6
6
import type { Codec , Registry , TypeDef } from '@polkadot/types/types' ;
7
7
import type { AbiConstructor , AbiEvent , AbiEventParam , AbiMessage , AbiMessageParam , AbiParam , DecodedEvent , DecodedMessage } from '../types.js' ;
8
8
@@ -19,12 +19,12 @@ interface AbiJson {
19
19
}
20
20
21
21
type EventOf < M > = M extends { spec : { events : Vec < infer E > } } ? E : never
22
- export type ContractMetadataSupported = ContractMetadataV4 | ContractMetadataV5 ;
22
+ export type ContractMetadataSupported = ContractMetadataV4 | ContractMetadataV5 | ContractMetadataV6 ;
23
23
type ContractEventSupported = EventOf < ContractMetadataSupported > ;
24
24
25
25
const l = logger ( 'Abi' ) ;
26
26
27
- const PRIMITIVE_ALWAYS = [ 'AccountId' , 'AccountIndex' , 'Address' , 'Balance' ] ;
27
+ const PRIMITIVE_ALWAYS = [ 'AccountId' , 'AccountId20' , ' AccountIndex', 'Address' , 'Balance' ] ;
28
28
29
29
function findMessage < T extends AbiMessage > ( list : T [ ] , messageOrId : T | string | number ) : T {
30
30
const message = isNumber ( messageOrId )
@@ -66,9 +66,28 @@ function getMetadata (registry: Registry, json: AbiJson): ContractMetadataSuppor
66
66
return upgradedMetadata ;
67
67
}
68
68
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 ] {
70
85
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 ;
72
91
const metadata = getMetadata ( registry , json as unknown as AbiJson ) ;
73
92
const lookup = registry . createType ( 'PortableRegistry' , { types : metadata . types } , true ) ;
74
93
@@ -84,7 +103,7 @@ function parseJson (json: Record<string, unknown>, chainProperties?: ChainProper
84
103
lookup . getTypeDef ( id )
85
104
) ;
86
105
87
- return [ json , registry , metadata , info ] ;
106
+ return [ json , registry , metadata , info , revive ] ;
88
107
}
89
108
90
109
/**
@@ -112,9 +131,10 @@ export class Abi {
112
131
readonly metadata : ContractMetadataSupported ;
113
132
readonly registry : Registry ;
114
133
readonly environment = new Map < string , TypeDef | Codec > ( ) ;
134
+ readonly isRevive : boolean ;
115
135
116
136
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 (
118
138
isString ( abiJson )
119
139
? JSON . parse ( abiJson ) as Record < string , unknown >
120
140
: abiJson ,
0 commit comments