neo-devpack-solidity 0.22.0

Production-focused Solidity-to-NeoVM compilation system
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
use super::*;

#[test]
fn single_file_compilation_resolves_relative_imports() {
    let temp = tempfile::tempdir().expect("tempdir");
    let base_path = temp.path().join("Base.sol");
    let derived_path = temp.path().join("Derived.sol");

    fs::write(
        &base_path,
        r#"
        // SPDX-License-Identifier: MIT
        pragma solidity ^0.8.19;

        contract Base {
            function foo() public pure returns (uint256) {
                return 1;
            }
        }
        "#,
    )
    .expect("write Base.sol");

    fs::write(
        &derived_path,
        r#"
        // SPDX-License-Identifier: MIT
        pragma solidity ^0.8.19;
        import "./Base.sol";

        contract Derived is Base {}
        "#,
    )
    .expect("write Derived.sol");

    let resolved =
        resolve_solidity_sources_with_imports(&derived_path, &[]).expect("resolve imports");
    assert_eq!(resolved.files.len(), 2, "expected Base + Derived sources");

    let artifacts = compile_contracts(&resolved.combined_source, false, 2)
        .expect("compilation with imports should succeed");
    let names: Vec<_> = artifacts.iter().map(|a| a.metadata.name.as_str()).collect();
    assert!(names.contains(&"Base"));
    assert!(names.contains(&"Derived"));
}

#[test]
fn single_file_compilation_resolves_imports_via_include_paths() {
    let temp = tempfile::tempdir().expect("tempdir");
    let lib_dir = temp.path().join("lib");
    fs::create_dir_all(&lib_dir).expect("create lib dir");

    let lib_path = lib_dir.join("Lib.sol");
    let entry_path = temp.path().join("Entry.sol");

    fs::write(
        &lib_path,
        r#"
        // SPDX-License-Identifier: MIT
        pragma solidity ^0.8.19;

        library Lib {
            function add(uint256 a, uint256 b) internal pure returns (uint256) {
                return a + b;
            }
        }
        "#,
    )
    .expect("write Lib.sol");

    fs::write(
        &entry_path,
        r#"
        // SPDX-License-Identifier: MIT
        pragma solidity ^0.8.19;
        import "Lib.sol";

        contract Entry {
            function sum(uint256 a, uint256 b) public pure returns (uint256) {
                return Lib.add(a, b);
            }
        }
        "#,
    )
    .expect("write Entry.sol");

    let resolved = resolve_solidity_sources_with_imports(&entry_path, &[lib_dir])
        .expect("resolve imports via include path");
    assert_eq!(resolved.files.len(), 2, "expected Lib + Entry sources");

    let artifacts = compile_contracts(&resolved.combined_source, false, 2)
        .expect("compilation with include path imports should succeed");
    let names: Vec<_> = artifacts.iter().map(|a| a.metadata.name.as_str()).collect();
    assert!(names.contains(&"Entry"));
}

#[test]
fn single_file_compilation_accepts_import_aliasing_for_dependency_resolution() {
    let temp = tempfile::tempdir().expect("tempdir");
    let lib_path = temp.path().join("Lib.sol");
    let entry_path = temp.path().join("Entry.sol");

    fs::write(
        &lib_path,
        r#"
        pragma solidity ^0.8.19;

        library Lib {
            function add(uint256 a, uint256 b) internal pure returns (uint256) {
                return a + b;
            }
        }
        "#,
    )
    .expect("write Lib.sol");

    fs::write(
        &entry_path,
        r#"
        pragma solidity ^0.8.19;
        import { Lib as MathLib } from "./Lib.sol";

        contract Entry {
            function sum(uint256 a, uint256 b) public pure returns (uint256) {
                return Lib.add(a, b);
            }
        }
        "#,
    )
    .expect("write Entry.sol");

    let resolved = resolve_solidity_sources_with_imports(&entry_path, &[])
        .expect("alias import should resolve");
    assert_eq!(resolved.files.len(), 2, "expected Lib + Entry sources");

    let artifacts = compile_contracts(&resolved.combined_source, false, 2)
        .expect("compilation with aliased import syntax should succeed");
    let names: Vec<_> = artifacts.iter().map(|a| a.metadata.name.as_str()).collect();
    assert!(names.contains(&"Entry"));
}

#[test]
fn single_file_compilation_accepts_global_symbol_imports_for_dependency_resolution() {
    let temp = tempfile::tempdir().expect("tempdir");
    let lib_path = temp.path().join("Lib.sol");
    let entry_path = temp.path().join("Entry.sol");

    fs::write(
        &lib_path,
        r#"
        pragma solidity ^0.8.19;

        library Lib {
            function add(uint256 a, uint256 b) internal pure returns (uint256) {
                return a + b;
            }
        }
        "#,
    )
    .expect("write Lib.sol");

    fs::write(
        &entry_path,
        r#"
        pragma solidity ^0.8.19;
        import * as LibNS from "./Lib.sol";

        contract Entry {
            function sum(uint256 a, uint256 b) public pure returns (uint256) {
                return Lib.add(a, b);
            }
        }
        "#,
    )
    .expect("write Entry.sol");

    let resolved = resolve_solidity_sources_with_imports(&entry_path, &[])
        .expect("global-symbol import should resolve");
    assert_eq!(resolved.files.len(), 2, "expected Lib + Entry sources");

    let artifacts = compile_contracts(&resolved.combined_source, false, 2)
        .expect("compilation with wildcard import syntax should succeed");
    let names: Vec<_> = artifacts.iter().map(|a| a.metadata.name.as_str()).collect();
    assert!(names.contains(&"Entry"));
}

#[test]
fn single_file_compilation_resolves_aliased_symbol_binding() {
    let temp = tempfile::tempdir().expect("tempdir");
    let lib_path = temp.path().join("Lib.sol");
    let entry_path = temp.path().join("Entry.sol");

    fs::write(
        &lib_path,
        r#"
        pragma solidity ^0.8.19;

        library Lib {
            function add(uint256 a, uint256 b) internal pure returns (uint256) {
                return a + b;
            }
        }
        "#,
    )
    .expect("write Lib.sol");

    fs::write(
        &entry_path,
        r#"
        pragma solidity ^0.8.19;
        import { Lib as MathLib } from "./Lib.sol";

        contract Entry {
            function sum(uint256 a, uint256 b) public pure returns (uint256) {
                return MathLib.add(a, b);
            }
        }
        "#,
    )
    .expect("write Entry.sol");

    let resolved = resolve_solidity_sources_with_imports(&entry_path, &[])
        .expect("alias import should resolve");
    let artifacts = compile_contracts(&resolved.combined_source, false, 2)
        .expect("aliased symbol binding should compile");
    let names: Vec<_> = artifacts.iter().map(|a| a.metadata.name.as_str()).collect();
    assert!(names.contains(&"Entry"));
}

#[test]
fn single_file_compilation_resolves_wildcard_namespace_binding() {
    let temp = tempfile::tempdir().expect("tempdir");
    let lib_path = temp.path().join("Lib.sol");
    let entry_path = temp.path().join("Entry.sol");

    fs::write(
        &lib_path,
        r#"
        pragma solidity ^0.8.19;

        library Lib {
            function add(uint256 a, uint256 b) internal pure returns (uint256) {
                return a + b;
            }
        }
        "#,
    )
    .expect("write Lib.sol");

    fs::write(
        &entry_path,
        r#"
        pragma solidity ^0.8.19;
        import * as LibNS from "./Lib.sol";

        contract Entry {
            function sum(uint256 a, uint256 b) public pure returns (uint256) {
                return LibNS.Lib.add(a, b);
            }
        }
        "#,
    )
    .expect("write Entry.sol");

    let resolved = resolve_solidity_sources_with_imports(&entry_path, &[])
        .expect("global-symbol import should resolve");
    let artifacts = compile_contracts(&resolved.combined_source, false, 2)
        .expect("wildcard namespace binding should compile");
    let names: Vec<_> = artifacts.iter().map(|a| a.metadata.name.as_str()).collect();
    assert!(names.contains(&"Entry"));
}

#[test]
fn single_file_compilation_resolves_wildcard_namespace_type_cast_binding() {
    let temp = tempfile::tempdir().expect("tempdir");
    let iface_path = temp.path().join("IFoo.sol");
    let entry_path = temp.path().join("Entry.sol");

    fs::write(
        &iface_path,
        r#"
        pragma solidity ^0.8.19;

        interface IFoo {
            function foo() external;
        }
        "#,
    )
    .expect("write IFoo.sol");

    fs::write(
        &entry_path,
        r#"
        pragma solidity ^0.8.19;
        import * as LibNS from "./IFoo.sol";

        contract Entry {
            function ping(address target) public {
                LibNS.IFoo(target).foo();
            }
        }
        "#,
    )
    .expect("write Entry.sol");

    let resolved = resolve_solidity_sources_with_imports(&entry_path, &[])
        .expect("global-symbol import should resolve");
    let artifacts = compile_contracts(&resolved.combined_source, false, 2)
        .expect("wildcard namespace type-cast binding should compile");
    let names: Vec<_> = artifacts.iter().map(|a| a.metadata.name.as_str()).collect();
    assert!(names.contains(&"Entry"));
}

#[test]
fn single_file_compilation_resolves_wildcard_namespace_selector_binding() {
    let temp = tempfile::tempdir().expect("tempdir");
    let iface_path = temp.path().join("IFoo.sol");
    let entry_path = temp.path().join("Entry.sol");

    fs::write(
        &iface_path,
        r#"
        pragma solidity ^0.8.19;

        interface IFoo {
            function foo() external;
        }
        "#,
    )
    .expect("write IFoo.sol");

    fs::write(
        &entry_path,
        r#"
        pragma solidity ^0.8.19;
        import * as LibNS from "./IFoo.sol";

        contract Entry {
            function selectorOfFoo() public pure returns (bytes4) {
                return LibNS.IFoo.foo.selector;
            }
        }
        "#,
    )
    .expect("write Entry.sol");

    let resolved = resolve_solidity_sources_with_imports(&entry_path, &[])
        .expect("global-symbol import should resolve");
    let artifacts = compile_contracts(&resolved.combined_source, false, 2)
        .expect("wildcard namespace selector binding should compile");
    let names: Vec<_> = artifacts.iter().map(|a| a.metadata.name.as_str()).collect();
    assert!(names.contains(&"Entry"));
}

#[test]
fn single_file_compilation_resolves_aliased_encode_call_function_reference() {
    let temp = tempfile::tempdir().expect("tempdir");
    let iface_path = temp.path().join("IFoo.sol");
    let entry_path = temp.path().join("Entry.sol");

    fs::write(
        &iface_path,
        r#"
        pragma solidity ^0.8.34;

        interface IFoo {
            function bar(uint256 x) external;
        }
        "#,
    )
    .expect("write IFoo.sol");

    fs::write(
        &entry_path,
        r#"
        pragma solidity ^0.8.34;
        import { IFoo as FooIface } from "./IFoo.sol";

        contract Entry {
            function payload() public pure returns (bytes memory) {
                return abi.encodeCall(FooIface.bar, (7));
            }
        }
        "#,
    )
    .expect("write Entry.sol");

    let resolved = resolve_solidity_sources_with_imports(&entry_path, &[])
        .expect("alias import should resolve");
    let artifacts = compile_contracts(&resolved.combined_source, false, 2)
        .expect("aliased encodeCall function reference should compile");
    let names: Vec<_> = artifacts.iter().map(|a| a.metadata.name.as_str()).collect();
    assert!(names.contains(&"Entry"));
}

#[test]
fn single_file_compilation_resolves_wildcard_namespace_encode_call_function_reference() {
    let temp = tempfile::tempdir().expect("tempdir");
    let iface_path = temp.path().join("IFoo.sol");
    let entry_path = temp.path().join("Entry.sol");

    fs::write(
        &iface_path,
        r#"
        pragma solidity ^0.8.34;

        interface IFoo {
            function bar(uint256 x) external;
        }
        "#,
    )
    .expect("write IFoo.sol");

    fs::write(
        &entry_path,
        r#"
        pragma solidity ^0.8.34;
        import * as FooNS from "./IFoo.sol";

        contract Entry {
            function payload() public pure returns (bytes memory) {
                return abi.encodeCall(FooNS.IFoo.bar, (7));
            }
        }
        "#,
    )
    .expect("write Entry.sol");

    let resolved = resolve_solidity_sources_with_imports(&entry_path, &[])
        .expect("wildcard import should resolve");
    let artifacts = compile_contracts(&resolved.combined_source, false, 2)
        .expect("wildcard namespace encodeCall function reference should compile");
    let names: Vec<_> = artifacts.iter().map(|a| a.metadata.name.as_str()).collect();
    assert!(names.contains(&"Entry"));
}

#[test]
fn auto_discovers_node_modules_for_npm_style_imports() {
    // Regression: bare imports like `@openzeppelin/contracts/...` should
    // resolve against a sibling `node_modules/` without the user needing to
    // pass `--include-path` explicitly. solc / hardhat / foundry all do this
    // for npm-style imports; we mirror their behaviour by climbing parents of
    // the entry file looking for `node_modules`.
    let temp = tempfile::tempdir().expect("tempdir");

    let project_dir = temp.path().join("project");
    let contracts_dir = project_dir.join("contracts");
    fs::create_dir_all(&contracts_dir).expect("project layout");

    let node_modules = project_dir.join("node_modules");
    let pkg_dir = node_modules.join("@scope").join("pkg");
    fs::create_dir_all(&pkg_dir).expect("create pkg dir");
    fs::write(
        pkg_dir.join("Lib.sol"),
        r#"
        // SPDX-License-Identifier: MIT
        pragma solidity ^0.8.20;

        library Lib {
            function add(uint256 a, uint256 b) internal pure returns (uint256) {
                return a + b;
            }
        }
        "#,
    )
    .expect("write Lib.sol");

    let entry_path = contracts_dir.join("Entry.sol");
    fs::write(
        &entry_path,
        r#"
        // SPDX-License-Identifier: MIT
        pragma solidity ^0.8.20;
        import "@scope/pkg/Lib.sol";

        contract Entry {
            function sum(uint256 a, uint256 b) public pure returns (uint256) {
                return Lib.add(a, b);
            }
        }
        "#,
    )
    .expect("write Entry.sol");

    let resolved = resolve_solidity_sources_with_imports(&entry_path, &[])
        .expect("node_modules import should resolve without explicit include-path");
    assert_eq!(resolved.files.len(), 2, "expected Lib + Entry sources");

    let artifacts = compile_contracts(&resolved.combined_source, false, 2)
        .expect("auto-discovered node_modules import should compile");
    let names: Vec<_> = artifacts.iter().map(|a| a.metadata.name.as_str()).collect();
    assert!(names.contains(&"Entry"));
}

#[test]
fn remap_flag_rewrites_import_prefix() {
    // Foundry-style remap: any import beginning with `@oz/` is rewritten to
    // start with the supplied replacement path. Mirrors solc's
    // `--allow-paths` + remappings feature and matches the Foundry default.
    let temp = tempfile::tempdir().expect("tempdir");
    let oz_layout = temp.path().join("oz_install");
    fs::create_dir_all(&oz_layout).expect("create oz dir");
    fs::write(
        oz_layout.join("Lib.sol"),
        r#"
        // SPDX-License-Identifier: MIT
        pragma solidity ^0.8.20;
        library Lib {
            function add(uint256 a, uint256 b) internal pure returns (uint256) {
                return a + b;
            }
        }
        "#,
    )
    .expect("write Lib.sol");

    let entry_path = temp.path().join("Entry.sol");
    fs::write(
        &entry_path,
        r#"
        // SPDX-License-Identifier: MIT
        pragma solidity ^0.8.20;
        import "@oz/Lib.sol";
        contract Entry {
            function sum(uint256 a, uint256 b) public pure returns (uint256) {
                return Lib.add(a, b);
            }
        }
        "#,
    )
    .expect("write Entry.sol");

    let remap =
        parse_remapping(&format!("@oz/={}/", oz_layout.display())).expect("parse remapping");
    let resolved = resolve_solidity_sources_with_options(&entry_path, &[], &[remap])
        .expect("remap should let bare-prefix import resolve");
    assert_eq!(resolved.files.len(), 2, "expected Lib + Entry sources");
    let artifacts = compile_contracts(&resolved.combined_source, false, 2)
        .expect("remapped imports should compile");
    let names: Vec<_> = artifacts.iter().map(|a| a.metadata.name.as_str()).collect();
    assert!(names.contains(&"Entry"));
}

#[test]
fn remappings_file_is_parsed_with_comments_and_blank_lines_ignored() {
    let temp = tempfile::tempdir().expect("tempdir");
    let target = temp.path().join("packages").join("foo");
    fs::create_dir_all(&target).expect("create dir");
    let remap_path = temp.path().join("remappings.txt");
    fs::write(
        &remap_path,
        format!(
            "# foundry-style remappings.txt\n\n@foo/={}/\n# trailing comment\n",
            target.display()
        ),
    )
    .expect("write remappings.txt");

    let remaps = load_remappings_file(&remap_path).expect("parse remappings");
    assert_eq!(remaps.len(), 1, "comments + blanks should be skipped");
    assert_eq!(remaps[0].prefix, "@foo/");
}

#[test]
fn cross_package_relative_imports_resolve_via_virtual_path() {
    // Regression: when a vendored file at `vendor/<pkg>/.../A.sol` does
    // `import "./B.sol"`, but `B.sol` was not vendored alongside it, the
    // resolver should fall back to resolving the relative import against the
    // file's virtual package path under any other include path that contains
    // a copy of the same package (e.g. an installed node_modules).
    let temp = tempfile::tempdir().expect("tempdir");

    // Layout 1: vendored copy that only contains A.sol (no B.sol next to it).
    let vendor_pkg = temp.path().join("vendor").join("@scope").join("pkg");
    fs::create_dir_all(&vendor_pkg).expect("vendor layout");
    fs::write(
        vendor_pkg.join("A.sol"),
        r#"
        // SPDX-License-Identifier: MIT
        pragma solidity ^0.8.20;
        import "./B.sol";

        contract A is B {}
        "#,
    )
    .expect("write A.sol");

    // Layout 2: fully populated install that has B.sol.
    let install_pkg = temp.path().join("install").join("@scope").join("pkg");
    fs::create_dir_all(&install_pkg).expect("install layout");
    fs::write(
        install_pkg.join("B.sol"),
        r#"
        // SPDX-License-Identifier: MIT
        pragma solidity ^0.8.20;

        contract B {
            function ping() public pure returns (uint256) { return 1; }
        }
        "#,
    )
    .expect("write B.sol");

    // The entry file is the vendored A.sol. The include path points at the
    // *install* root, which gives us a virtual layout of @scope/pkg/B.sol —
    // the resolver should bridge across.
    let resolved = resolve_solidity_sources_with_imports(
        &vendor_pkg.join("A.sol"),
        &[temp.path().join("install"), temp.path().join("vendor")],
    )
    .expect("virtual-path resolution should bridge to install copy");

    assert_eq!(resolved.files.len(), 2, "expected A + B sources");

    let artifacts = compile_contracts(&resolved.combined_source, false, 2)
        .expect("compilation across virtual-path-resolved import should succeed");
    let names: Vec<_> = artifacts.iter().map(|a| a.metadata.name.as_str()).collect();
    assert!(names.contains(&"A"));
    assert!(names.contains(&"B"));
}