@@ -17,7 +17,7 @@ import SOLIDITY_VERSION from './solidity-version.json';
17
17
import { inferTranspiled } from './infer-transpiled' ;
18
18
import { compatibleContractsSemver } from './utils/version' ;
19
19
import { stringifyUnicodeSafe } from './utils/sanitize' ;
20
- import { importsCommunityContracts } from './utils/imports-libraries' ;
20
+ import { importsLibrary } from './utils/imports-libraries' ;
21
21
import { getCommunityContractsGitCommit } from './utils/community-contracts-git-commit' ;
22
22
23
23
export function printContract ( contract : Contract , opts ?: Options ) : string {
@@ -31,7 +31,7 @@ export function printContract(contract: Contract, opts?: Options): string {
31
31
...spaceBetween (
32
32
[
33
33
`// SPDX-License-Identifier: ${ contract . license } ` ,
34
- printCompatibleLibraryVersions ( contract ) ,
34
+ printCompatibleLibraryVersions ( contract , opts ) ,
35
35
`pragma solidity ^${ SOLIDITY_VERSION } ;` ,
36
36
] ,
37
37
@@ -42,6 +42,7 @@ export function printContract(contract: Contract, opts?: Options): string {
42
42
[ `contract ${ contract . name } ` , ...printInheritance ( contract , helpers ) , '{' ] . join ( ' ' ) ,
43
43
44
44
spaceBetween (
45
+ printLibraries ( contract , helpers ) ,
45
46
contract . variables ,
46
47
printConstructor ( contract , helpers ) ,
47
48
...fns . code ,
@@ -56,17 +57,30 @@ export function printContract(contract: Contract, opts?: Options): string {
56
57
) ;
57
58
}
58
59
59
- function printCompatibleLibraryVersions ( contract : Contract ) : string {
60
- let result = `// Compatible with OpenZeppelin Contracts ${ compatibleContractsSemver } ` ;
61
- if ( importsCommunityContracts ( contract ) ) {
60
+ function printCompatibleLibraryVersions ( contract : Contract , opts ?: Options ) : string {
61
+ const libraries : string [ ] = [ ] ;
62
+ if ( importsLibrary ( contract , '@openzeppelin/contracts' ) ) {
63
+ libraries . push ( `OpenZeppelin Contracts ${ compatibleContractsSemver } ` ) ;
64
+ }
65
+ if ( importsLibrary ( contract , '@openzeppelin/community-contracts' ) ) {
62
66
try {
63
67
const commit = getCommunityContractsGitCommit ( ) ;
64
- result += ` and Community Contracts commit ${ commit } `;
68
+ libraries . push ( ` Community Contracts commit ${ commit } `) ;
65
69
} catch ( e ) {
66
70
console . error ( e ) ;
67
71
}
68
72
}
69
- return result ;
73
+ if ( opts ?. additionalCompatibleLibraries ) {
74
+ for ( const library of opts . additionalCompatibleLibraries ) {
75
+ if ( importsLibrary ( contract , library . path ) ) {
76
+ libraries . push ( `${ library . name } ${ library . version } ` ) ;
77
+ }
78
+ }
79
+ }
80
+
81
+ if ( libraries . length === 0 ) return '' ;
82
+ if ( libraries . length === 1 ) return `// Compatible with ${ libraries [ 0 ] } ` ;
83
+ return `// Compatible with ${ libraries . slice ( 0 , - 1 ) . join ( ', ' ) } and ${ libraries . slice ( - 1 ) } ` ;
70
84
}
71
85
72
86
function printInheritance ( contract : Contract , { transformName } : Helpers ) : [ ] | [ string ] {
@@ -87,9 +101,9 @@ function printConstructor(contract: Contract, helpers: Helpers): Lines[] {
87
101
const args = contract . constructorArgs . map ( a => printArgument ( a , helpers ) ) ;
88
102
const body = helpers . upgradeable
89
103
? spaceBetween (
90
- parents . map ( p => p + ';' ) ,
91
- contract . constructorCode ,
92
- )
104
+ parents . map ( p => p + ';' ) ,
105
+ contract . constructorCode ,
106
+ )
93
107
: contract . constructorCode ;
94
108
const head = helpers . upgradeable ? 'function initialize' : 'constructor' ;
95
109
const constructor = printFunction2 ( [ ] , head , args , modifiers , body ) ;
@@ -275,3 +289,14 @@ function printImports(imports: ImportContract[], helpers: Helpers): string[] {
275
289
276
290
return lines ;
277
291
}
292
+
293
+ function printLibraries ( contract : Contract , { transformName } : Helpers ) : string [ ] {
294
+ if ( ! contract . libraries || contract . libraries . length === 0 ) return [ ] ;
295
+
296
+ return contract . libraries
297
+ . sort ( ( a , b ) => a . library . name . localeCompare ( b . library . name ) ) // Sort by library name
298
+ . map ( lib => {
299
+ const sortedTypes = Array . from ( lib . usingFor ) . sort ( ( a , b ) => a . localeCompare ( b ) ) ; // Sort types
300
+ return `using ${ transformName ( lib . library ) } for ${ sortedTypes . join ( ', ' ) } ;` ;
301
+ } ) ;
302
+ }
0 commit comments