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
/// ERC → NEP pattern adaptation diagnostics.
///
/// Detects Ethereum-style patterns in Solidity contracts and emits warnings
/// with actionable guidance for migrating to Neo N3 equivalents.
///
/// Checked patterns:
/// - ERC-20 `transfer(to, amount)` → NEP-17 `transfer(from, to, amount, data)`
/// - ERC-20 `approve`/`allowance`/`transferFrom` → not in NEP-17 spec
/// - ERC-721 `transferFrom(from, to, tokenId)` → NEP-11 `transfer(to, tokenId, data)`
/// - `receive()` / `fallback()` → `onNEP17Payment()` callback
/// - `supportsInterface(bytes4)` → manifest `supportedstandards`
fn validate_erc_nep_patterns(metadata: &ContractMetadata, diagnostics: &mut Vec<Diagnostic>) {
    let public_methods: Vec<&FunctionMetadata> = metadata
        .methods
        .iter()
        .filter(|m| {
            !matches!(m.kind, FunctionKind::Constructor)
                && matches!(
                    m.visibility,
                    VisibilityKind::Public | VisibilityKind::External
                )
        })
        .collect();

    let names_lower: std::collections::HashSet<String> = public_methods
        .iter()
        .map(|m| m.name.to_ascii_lowercase())
        .collect();

    check_erc20_transfer_pattern(&public_methods, &names_lower, diagnostics);
    check_erc20_approve_pattern(&public_methods, &names_lower, diagnostics);
    check_erc721_transfer_from_pattern(&public_methods, &names_lower, diagnostics);
    check_receive_fallback_pattern(&metadata.methods, diagnostics);
    check_supports_interface_pattern(&public_methods, diagnostics);
    check_bn254_precompile_usage(&public_methods, diagnostics);
    check_erc1155_pattern(&public_methods, diagnostics);
    check_erc2612_permit_pattern(&public_methods, diagnostics);
    check_erc4626_vault_pattern(&public_methods, &names_lower, diagnostics);
    check_nep14_multitoken_pattern(&public_methods, &names_lower, diagnostics);
    check_payment_callback(
        &public_methods,
        &names_lower,
        &metadata.methods,
        diagnostics,
    );
    check_nft_payment_callback(
        &public_methods,
        &names_lower,
        &metadata.methods,
        diagnostics,
    );
    check_payable_modifier(&public_methods, diagnostics);
    check_block_timestamp_dependency(&public_methods, diagnostics);
}

/// Detect ERC-20 style `transfer(address, uint256)` and suggest NEP-17 4-param form.
fn check_erc20_transfer_pattern(
    public_methods: &[&FunctionMetadata],
    names: &std::collections::HashSet<String>,
    diagnostics: &mut Vec<Diagnostic>,
) {
    let has_ownerof = names.contains("ownerof");
    // Only flag for fungible-token-like contracts (no ownerOf → not NFT)
    if has_ownerof {
        return;
    }

    if let Some(transfer) = public_methods
        .iter()
        .find(|m| m.name.eq_ignore_ascii_case("transfer"))
    {
        let param_count = transfer.parameters.len();
        if param_count == 2 {
            // Classic ERC-20: transfer(address to, uint256 amount)
            diagnostics.push(Diagnostic::warning(
                "function 'transfer' has 2 parameters (ERC-20 pattern). \
                 NEP-17 requires 4 parameters: transfer(from, to, amount, data). \
                 The `from` address is verified via Runtime.checkWitness() and \
                 `data` (type Any) is forwarded to the recipient's onNEP17Payment callback."
            ).with_code("W101")
             .with_suggestion("Add `from` and `data` parameters: `transfer(address from, address to, uint256 amount, bytes data)`"));
        } else if param_count == 3 {
            // Partial migration: transfer(from, to, amount) — missing `data`
            diagnostics.push(Diagnostic::warning(
                "function 'transfer' has 3 parameters, but NEP-17 requires 4: \
                 transfer(from, to, amount, data). The `data` parameter (type Any) \
                 is forwarded to the recipient's onNEP17Payment callback."
            ).with_code("W102")
             .with_suggestion("Add `data` parameter for NEP-17 compliance: `transfer(address from, address to, uint256 amount, bytes data)`"));
        }
    }
}

/// Detect ERC-20 approve/allowance/transferFrom and note they are not in NEP-17.
fn check_erc20_approve_pattern(
    public_methods: &[&FunctionMetadata],
    names: &std::collections::HashSet<String>,
    diagnostics: &mut Vec<Diagnostic>,
) {
    let erc20_extras: Vec<&str> = ["approve", "allowance", "transferfrom"]
        .iter()
        .filter(|n| names.contains(**n))
        .copied()
        .collect();

    if !erc20_extras.is_empty() {
        // Only warn if this still looks like an ERC-20-style token surface.
        // If a contract already exposes canonical NEP-17 transfer(from,to,amount,data)
        // or is NFT-shaped (`ownerOf`), treat approve/allowance/transferFrom as
        // compatibility extensions instead of migration warnings.
        let has_token_signal = names.contains("balanceof") || names.contains("transfer");
        let has_ownerof = names.contains("ownerof");
        let has_nep17_transfer = public_methods
            .iter()
            .any(|m| m.name.eq_ignore_ascii_case("transfer") && m.parameters.len() == 4);

        if has_token_signal && !has_ownerof && !has_nep17_transfer {
            diagnostics.push(Diagnostic::warning(format!(
                "ERC-20 method(s) [{}] detected. These are not part of the NEP-17 spec; \
                 Neo uses Runtime.checkWitness() for authorization instead of the \
                 approve/allowance pattern. You may keep them as extensions, but they \
                 will not contribute to NEP-17 standard detection.",
                erc20_extras.join(", ")
            )).with_code("W103")
             .with_suggestion("Remove approve/allowance or keep as optional extension alongside NEP-17 transfer"));
        }
    }
}

/// Detect ERC-721 `transferFrom(from, to, tokenId)` and suggest NEP-11 `transfer(to, tokenId, data)`.
fn check_erc721_transfer_from_pattern(
    public_methods: &[&FunctionMetadata],
    names: &std::collections::HashSet<String>,
    diagnostics: &mut Vec<Diagnostic>,
) {
    let has_ownerof = names.contains("ownerof");
    if !has_ownerof {
        return; // Not an NFT contract
    }

    let has_nep11_transfer = public_methods
        .iter()
        .any(|m| m.name.eq_ignore_ascii_case("transfer") && m.parameters.len() == 3);

    if has_nep11_transfer {
        return;
    }

    if let Some(xfer_from) = public_methods
        .iter()
        .find(|m| m.name.eq_ignore_ascii_case("transferfrom"))
    {
        let param_count = xfer_from.parameters.len();
        if param_count == 3 {
            diagnostics.push(
                Diagnostic::warning(
                    "function 'transferFrom' with 3 parameters (ERC-721 pattern) detected. \
                 NEP-11 uses transfer(to, tokenId, data) with 3 parameters instead. \
                 Authorization is via Runtime.checkWitness(owner), not msg.sender.",
                )
                .with_code("W104")
                .with_suggestion(
                    "Replace `transferFrom(from, to, id)` with `transfer(to, id, data)`",
                ),
            );
        }
    }
}

/// Detect `receive()` / `fallback()` and suggest `onNEP17Payment()`.
fn check_receive_fallback_pattern(
    all_methods: &[FunctionMetadata],
    diagnostics: &mut Vec<Diagnostic>,
) {
    // Distinguishing a user-written onNEP17Payment from the compiler-generated
    // one: the convert phase remaps a source `receive()` into an
    // `onNEP17Payment` ONLY when no explicit onNEP17Payment exists; when one
    // DOES exist, the `receive()` is kept verbatim (see
    // `src/solidity/convert/functions.rs`). So after convert:
    //   - `receive` survives in `all_methods` ⟺ there's a user-written
    //     onNEP17Payment (otherwise the receive would have been remapped into
    //     it).
    // The fallback case is trickier — fallback is never auto-remapped, so a
    // surviving `fallback` + an `onNEP17Payment` could be the remapped receive.
    // To stay sound we only treat the (receive + onNEP17Payment) coexist case
    // as the hard error, and leave fallback+onNEP17Payment at a warning.
    let has_onnep17 = all_methods
        .iter()
        .any(|m| m.name.eq_ignore_ascii_case("onnep17payment"));
    // A surviving `receive` method means convert kept it because an explicit
    // onNEP17Payment already existed — the dead-code trap is real.
    let has_surviving_receive = all_methods
        .iter()
        .any(|m| m.name.eq_ignore_ascii_case("receive"));

    for method in all_methods {
        let name_lower = method.name.to_ascii_lowercase();
        if name_lower == "receive" || name_lower == "fallback" {
            // M-FE1 hard error: only when `receive()` specifically survived
            // convert alongside an explicit onNEP17Payment (the unambiguous
            // dead-code case). fallback() stays a warning — its coexist
            // ambiguity is documented in the audit but less dangerous.
            let is_hard_error =
                name_lower == "receive" && has_onnep17 && has_surviving_receive;
            if is_hard_error {
                diagnostics.push(
                    Diagnostic::error(format!(
                        "function '{}' is dead code on Neo N3: the contract already defines \
                         onNEP17Payment, which is the ONLY callback Neo N3 invokes for incoming \
                         NEP-17 transfers. The '{}' body would never execute — remove it and \
                         consolidate the deposit-handling logic into onNEP17Payment.",
                        method.name, method.name
                    ))
                    .with_code("E105")
                    .with_suggestion(
                        "Remove the receive()/fallback() and move its logic into \
                         onNEP17Payment(address from, uint256 amount, bytes data)",
                    ),
                );
            } else if has_onnep17 {
                diagnostics.push(
                    Diagnostic::warning(format!(
                        "function '{}' has no effect on Neo N3. The contract already defines \
                         onNEP17Payment which is the correct Neo callback for receiving tokens.",
                        method.name
                    ))
                    .with_code("W105")
                    .with_suggestion(
                        "Remove — the existing onNEP17Payment handler is sufficient",
                    ),
                );
            } else {
                diagnostics.push(Diagnostic::warning(format!(
                    "function '{}' has no effect on Neo N3. Use onNEP17Payment(address from, \
                     uint256 amount, bytes data) to handle incoming token payments.",
                    method.name
                )).with_code("W105")
                 .with_suggestion("Replace with `function onNEP17Payment(address from, uint256 amount, bytes data)`"));
            }
        }
    }
}

/// Detect `supportsInterface(bytes4)` and note that Neo uses manifest instead.
fn check_supports_interface_pattern(
    public_methods: &[&FunctionMetadata],
    diagnostics: &mut Vec<Diagnostic>,
) {
    if let Some(si) = public_methods
        .iter()
        .find(|m| m.name == "supportsInterface")
    {
        if si.parameters.len() == 1 {
            diagnostics.push(
                Diagnostic::warning(
                    "function 'supportsInterface' (EIP-165) is unnecessary on Neo N3. \
                 Neo uses the manifest 'supportedstandards' array for interface \
                 detection, which the compiler populates automatically.",
                )
                .with_code("W106")
                .with_suggestion("Remove — Neo N3 uses manifest-based interface discovery"),
            );
        }
    }
}

/// Detect ERC-1155 multi-token pattern and note Neo N3 has no direct equivalent.
fn check_erc1155_pattern(public_methods: &[&FunctionMetadata], diagnostics: &mut Vec<Diagnostic>) {
    let has_safe_transfer = public_methods
        .iter()
        .any(|m| m.name.eq_ignore_ascii_case("safeTransferFrom") && m.parameters.len() == 5);
    let has_batch_transfer = public_methods
        .iter()
        .any(|m| m.name.eq_ignore_ascii_case("safeBatchTransferFrom") && m.parameters.len() == 5);

    if has_safe_transfer || has_batch_transfer {
        diagnostics.push(
            Diagnostic::warning(
                "ERC-1155 multi-token pattern detected. Neo N3 does not have a direct \
             NEP equivalent for multi-token contracts.",
            )
            .with_code("W107")
            .with_suggestion(
                "Split into separate NEP-17 (fungible) and NEP-11 (non-fungible) contracts",
            ),
        );
    }
}

/// Detect ERC-2612 permit pattern and note Neo uses checkWitness instead.
fn check_erc2612_permit_pattern(
    public_methods: &[&FunctionMetadata],
    diagnostics: &mut Vec<Diagnostic>,
) {
    if let Some(permit) = public_methods
        .iter()
        .find(|m| m.name.eq_ignore_ascii_case("permit"))
    {
        if permit.parameters.len() == 7 {
            diagnostics.push(
                Diagnostic::warning(
                    "ERC-2612 permit pattern detected (7-parameter permit function). \
                 Neo N3 uses Runtime.checkWitness() for authorization; off-chain \
                 signature permits are not needed.",
                )
                .with_code("W108")
                .with_suggestion("Use `Runtime.checkWitness()` instead of off-chain signatures"),
            );
        }
    }
}

/// Detect ERC-4626 tokenized vault pattern and suggest NEP-17 replacement.
fn check_erc4626_vault_pattern(
    public_methods: &[&FunctionMetadata],
    names: &std::collections::HashSet<String>,
    diagnostics: &mut Vec<Diagnostic>,
) {
    let has_deposit = public_methods
        .iter()
        .any(|m| m.name.eq_ignore_ascii_case("deposit") && m.parameters.len() == 2);
    let has_withdraw = public_methods
        .iter()
        .any(|m| m.name.eq_ignore_ascii_case("withdraw") && m.parameters.len() == 3);
    let has_convert_shares = names.contains("converttoshares");
    let has_convert_assets = names.contains("converttoassets");

    if has_deposit && has_withdraw && (has_convert_shares || has_convert_assets) {
        diagnostics.push(Diagnostic::warning(
            "ERC-4626 tokenized vault pattern detected. The vault logic compiles \
             correctly, but replace ERC-20 token interactions with NEP-17 equivalents."
        ).with_code("W109")
         .with_suggestion("Replace ERC-20 interactions with NEP-17 equivalents; use Runtime.checkWitness() for authorization"));
    }
}

/// Detect BN254 elliptic curve precompile usage and suggest Neo alternatives.
fn check_bn254_precompile_usage(
    public_methods: &[&FunctionMetadata],
    diagnostics: &mut Vec<Diagnostic>,
) {
    let bn254_indicators = [
        "ecadd",
        "ecmul",
        "ecpairing",
        "bn256add",
        "bn256scalarmul",
        "bn256pairing",
    ];
    for method in public_methods {
        let name_lower = method.name.to_ascii_lowercase();
        if bn254_indicators.iter().any(|ind| name_lower.contains(ind)) {
            diagnostics.push(Diagnostic::warning(format!(
                "function '{}' appears to use BN254 elliptic curve operations (ecAdd/ecMul/ecPairing). \
                 These precompiles (addresses 0x06, 0x07, 0x08) are not available on Neo N3.",
                method.name
            )).with_code("W110")
             .with_suggestion("Use CryptoLib BLS12-381 operations instead"));
        }
    }
}

/// Detect NEP-14 (Multi-Token) pattern and suggest splitting to NEP-17/NEP-11.
fn check_nep14_multitoken_pattern(
    public_methods: &[&FunctionMetadata],
    names: &std::collections::HashSet<String>,
    diagnostics: &mut Vec<Diagnostic>,
) {
    let has_balanceof_batch = public_methods
        .iter()
        .any(|m| m.name.eq_ignore_ascii_case("balanceOfBatch"));
    let has_safe_batch_transfer = public_methods
        .iter()
        .any(|m| m.name.eq_ignore_ascii_case("safeBatchTransferFrom"));
    let has_uri = names.contains("uri");

    if has_balanceof_batch || (has_safe_batch_transfer && has_uri) {
        diagnostics.push(
            Diagnostic::warning(
                "NEP-14 multi-token pattern detected. Neo N3 does not have a direct \
             NEP-14 equivalent. Consider splitting into separate NEP-17 (fungible) \
             and NEP-11 (non-fungible) contracts.",
            )
            .with_code("W111")
            .with_suggestion("Deploy separate NEP-17 and NEP-11 contracts"),
        );
    }
}

/// Detect large storage operations that could be optimized.
#[allow(dead_code)]
fn check_storage_efficiency(
    all_methods: &[FunctionMetadata],
    _state_vars: &[&StateVariableMetadata],
    diagnostics: &mut Vec<Diagnostic>,
) {
    let mut has_iteration = false;

    for method in all_methods {
        let name_lower = method.name.to_ascii_lowercase();
        if name_lower.contains("foreach") || name_lower.contains("iterate") {
            has_iteration = true;
        }
    }

    if has_iteration {
        diagnostics.push(
            Diagnostic::warning(
                "Contract has iteration operations. Be careful with large datasets - \
             consider using prefix-based iteration or indexes for better performance.",
            )
            .with_code("W112")
            .with_suggestion("Use Storage.find() with prefixes for efficient iteration"),
        );
    }
}

/// Detect potentially unsafe operations with block.gaslimit.
#[allow(dead_code)]
fn check_block_gaslimit_usage(
    _public_methods: &[&FunctionMetadata],
    diagnostics: &mut Vec<Diagnostic>,
) {
    // block.gaslimit is mapped to Policy.getExecFeeFactor() automatically
    diagnostics.push(
        Diagnostic::warning(
            "block.gaslimit is not directly available on Neo N3. \
         It is automatically mapped to Policy.getExecFeeFactor().",
        )
        .with_code("W115")
        .with_suggestion("Use Policy.getExecFeeFactor() for gas cost estimation"),
    );
}

/// Detect missing onNEP17Payment in contracts that handle payments.
fn check_payment_callback(
    _public_methods: &[&FunctionMetadata],
    names: &std::collections::HashSet<String>,
    all_methods: &[FunctionMetadata],
    diagnostics: &mut Vec<Diagnostic>,
) {
    let has_transfer = names.contains("transfer");
    let has_onnep17payment = all_methods
        .iter()
        .any(|m| m.name.eq_ignore_ascii_case("onnep17payment"));

    if has_transfer && !has_onnep17payment {
        diagnostics.push(
            Diagnostic::warning(
                "Contract has transfer function but no onNEP17Payment callback. \
             Other contracts cannot send tokens to this contract.",
            )
            .with_code("W113")
            .with_suggestion(
                "Add onNEP17Payment(address from, uint256 amount, bytes data) callback",
            ),
        );
    }
}

/// Detect missing onNEP11Payment in NFT contracts.
fn check_nft_payment_callback(
    _public_methods: &[&FunctionMetadata],
    names: &std::collections::HashSet<String>,
    all_methods: &[FunctionMetadata],
    diagnostics: &mut Vec<Diagnostic>,
) {
    let has_ownerof = names.contains("ownerof");
    let has_onnep11payment = all_methods
        .iter()
        .any(|m| m.name.eq_ignore_ascii_case("onnep11payment"));

    if has_ownerof && !has_onnep11payment {
        diagnostics.push(
            Diagnostic::warning(
                "NFT contract (has ownerOf) but missing onNEP11Payment callback. \
             Other contracts cannot send NFTs to this contract.",
            )
            .with_code("W114")
            .with_suggestion(
                "Add onNEP11Payment(address from, uint256 amount, bytes32 tokenId, bytes data)",
            ),
        );
    }
}

/// Detect payable modifier usage and warn about Neo N3 differences.
fn check_payable_modifier(public_methods: &[&FunctionMetadata], diagnostics: &mut Vec<Diagnostic>) {
    use crate::solidity::StateMutability;

    for method in public_methods {
        // Check if function is marked payable but is not onNEP17Payment
        if method.state_mutability == StateMutability::Payable
            && !method.name.eq_ignore_ascii_case("onnep17payment")
            && !method.name.eq_ignore_ascii_case("onnep11payment")
        {
            diagnostics.push(
                Diagnostic::warning(format!(
                    "function '{}' has payable modifier which has no effect on Neo N3. \
                 Use onNEP17Payment callback to receive token payments.",
                    method.name
                ))
                .with_code("W116")
                .with_suggestion(
                    "Remove payable or implement onNEP17Payment(address, uint256, bytes)",
                ),
            );
        }
    }
}

/// Detect heavy dependence on block.timestamp which can be manipulated in Neo N3.
fn check_block_timestamp_dependency(
    public_methods: &[&FunctionMetadata],
    diagnostics: &mut Vec<Diagnostic>,
) {
    for method in public_methods {
        let name_lower = method.name.to_ascii_lowercase();
        let time_sensitive = name_lower.contains("auition")
            || name_lower.contains("timelock")
            || name_lower.contains("deadline")
            || name_lower.contains("expire");

        if time_sensitive {
            diagnostics.push(Diagnostic::warning(format!(
                "function '{}' appears to be time-sensitive. block.timestamp on Neo N3 is \
                 deterministic but can be affected by block production timing.",
                method.name
            )).with_code("W117")
             .with_suggestion("Consider adding additional verification mechanisms for time-critical operations"));
        }
    }
}