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
/// Severity level for standards-detection diagnostics.
#[derive(Debug, Clone, PartialEq)]
enum StandardsDiagnosticLevel {
    /// Contract almost matches a standard, or a detected standard has issues.
    Warning,
    /// Informational hint about parameter signatures or events.
    Info,
}

/// A diagnostic emitted during standards detection.
#[derive(Debug, Clone)]
struct StandardsDiagnostic {
    level: StandardsDiagnosticLevel,
    standard: &'static str,
    message: String,
}

/// Result of standards detection: detected standards + any diagnostics.
struct StandardsDetectionResult {
    standards: Vec<String>,
    diagnostics: Vec<StandardsDiagnostic>,
}

fn detect_supported_standards(
    methods: &[FunctionMetadata],
    events: &[EventMetadata],
) -> StandardsDetectionResult {
    let public_methods: Vec<&FunctionMetadata> = methods
        .iter()
        .filter(|m| {
            !matches!(m.kind, FunctionKind::Constructor)
                && matches!(m.visibility, VisibilityKind::Public | VisibilityKind::External)
        })
        .collect();
    let names: HashSet<String> = public_methods
        .iter()
        .map(|m| m.name.to_ascii_lowercase())
        .collect();
    let mut standards = Vec::new();
    let mut diagnostics: Vec<StandardsDiagnostic> = Vec::new();

    let has_ownerof = names.contains("ownerof");

    // ── NEP-17: Fungible Token Standard (ERC-20 equivalent) ──────────
    //
    // Claiming a standard the contract cannot fulfil ships a manifest that
    // wallets/exchanges act on (`transfer(from, to, amount, data)` invokes),
    // so detection requires actual signature conformance — not just the five
    // method names: a 4-parameter `transfer` AND a 3-parameter `Transfer`
    // event. A vanilla ERC-20 port (`transfer(to, amount)`) gets a warning
    // explaining how to conform instead of a false NEP-17 claim.
    let nep17_required = ["symbol", "decimals", "totalsupply", "balanceof", "transfer"];
    let nep17_present: Vec<&&str> = nep17_required.iter().filter(|m| names.contains(**m)).collect();
    let nep17_names_match = nep17_present.len() == nep17_required.len() && !has_ownerof;
    let nep17_transfer_ok = methods_named(&public_methods, "transfer")
        .iter()
        .any(|method| method.parameters.len() == 4);
    let nep17_event_ok = events
        .iter()
        .any(|event| event.name == "Transfer" && event.parameters.len() == 3);

    if nep17_names_match && nep17_transfer_ok && nep17_event_ok {
        standards.push("NEP-17".to_string());
        // Count-conformant, but warn when the parameter TYPES keep the
        // compiler from emitting the native NEP-17 Transfer notification.
        validate_transfer_event(events, "NEP-17", 3, &mut diagnostics);
    } else if nep17_names_match {
        let mut problems: Vec<&str> = Vec::new();
        if !nep17_transfer_ok {
            problems.push("`transfer` must take 4 parameters (from, to, amount, data)");
        }
        if !nep17_event_ok {
            problems.push("a 3-parameter `Transfer(from, to, amount)` event must be declared");
        }
        diagnostics.push(StandardsDiagnostic {
            level: StandardsDiagnosticLevel::Warning,
            standard: "NEP-17",
            message: format!(
                "contract has all NEP-17 method names but does not conform to the standard: {}. \
                 NEP-17 was not added to supportedstandards.",
                problems.join("; "),
            ),
        });
    } else if nep17_present.len() >= 3 && !has_ownerof {
        // Near-miss: contract has most NEP-17 methods but not all
        let missing: Vec<&str> = nep17_required
            .iter()
            .filter(|m| !names.contains(**m))
            .copied()
            .collect();
        diagnostics.push(StandardsDiagnostic {
            level: StandardsDiagnosticLevel::Warning,
            standard: "NEP-17",
            message: format!(
                "contract has {} of {} required NEP-17 methods (missing: {}). \
                 Add the missing method(s) to enable NEP-17 standard detection.",
                nep17_present.len(),
                nep17_required.len(),
                missing.join(", "),
            ),
        });
    }

    // ── NEP-11: Non-Fungible Token Standard (ERC-721 equivalent) ─────
    //
    // NEP-11 mandates the full method set below — `transferFrom` is not even
    // a NEP-11 method, so an ERC-721-shaped contract (balanceOf + ownerOf +
    // transferFrom) must NOT be advertised as NEP-11: wallets would call
    // symbol()/decimals()/totalSupply()/tokensOf()/transfer() and fail. As
    // with NEP-17 above, detection also requires signature conformance: a
    // 3-parameter `transfer(to, tokenId, data)` and a 4-parameter `Transfer`
    // event.
    let nep11_required = [
        "symbol",
        "decimals",
        "totalsupply",
        "balanceof",
        "tokensof",
        "ownerof",
        "transfer",
    ];
    let nep11_present: Vec<&&str> = nep11_required.iter().filter(|m| names.contains(**m)).collect();
    let nep11_names_match = nep11_present.len() == nep11_required.len();
    let nep11_transfer_ok = methods_named(&public_methods, "transfer")
        .iter()
        .any(|method| method.parameters.len() == 3);
    let nep11_event_ok = events
        .iter()
        .any(|event| event.name == "Transfer" && event.parameters.len() == 4);

    if nep11_names_match && nep11_transfer_ok && nep11_event_ok {
        standards.push("NEP-11".to_string());
        // Count-conformant, but warn when the parameter TYPES keep the
        // compiler from emitting the native NEP-11 Transfer notification.
        validate_transfer_event(events, "NEP-11", 4, &mut diagnostics);
    } else if nep11_names_match {
        let mut problems: Vec<&str> = Vec::new();
        if !nep11_transfer_ok {
            problems.push("`transfer` must take 3 parameters (to, tokenId, data)");
        }
        if !nep11_event_ok {
            problems.push(
                "a 4-parameter `Transfer(from, to, amount, tokenId)` event must be declared",
            );
        }
        diagnostics.push(StandardsDiagnostic {
            level: StandardsDiagnosticLevel::Warning,
            standard: "NEP-11",
            message: format!(
                "contract has all NEP-11 method names but does not conform to the standard: {}. \
                 NEP-11 was not added to supportedstandards.",
                problems.join("; "),
            ),
        });
    } else if has_ownerof {
        // Near-miss: ownerOf is a strong NFT signal — list what is missing
        // for genuine NEP-11 support instead of advertising a standard the
        // contract cannot fulfil.
        let missing: Vec<&str> = nep11_required
            .iter()
            .filter(|m| !names.contains(**m))
            .copied()
            .collect();
        diagnostics.push(StandardsDiagnostic {
            level: StandardsDiagnosticLevel::Warning,
            standard: "NEP-11",
            message: format!(
                "contract has `ownerOf` (NFT signal) but is missing required NEP-11 method(s): {}. \
                 Add the missing method(s) to enable NEP-11 standard detection.",
                missing.join(", "),
            ),
        });
    }

    // ── NEP-24: Royalty Standard ─────────────────────────────────────
    //
    // NEP-24's single mandatory method is
    // `royaltyInfo(tokenId, royaltyToken, salePrice)`. `tokenURI` is an
    // ERC-721 metadata method with no NEP-24 significance and must not
    // trigger the royalty standard — marketplaces honouring the manifest
    // would call a nonexistent `royaltyInfo` and fault.
    let royalty_methods = methods_named(&public_methods, "royaltyInfo");
    if royalty_methods
        .iter()
        .any(|method| method.parameters.len() == 3)
    {
        standards.push("NEP-24".to_string());
    } else if !royalty_methods.is_empty() {
        diagnostics.push(StandardsDiagnostic {
            level: StandardsDiagnosticLevel::Info,
            standard: "NEP-24",
            message: "contract has `royaltyInfo` but signature does not match \
                      NEP-24 (`royaltyInfo(tokenId, royaltyToken, salePrice)`)."
                .to_string(),
        });
    }

    // ── NEP-22: Contract Update Function ─────────────────────────────
    let update_methods = methods_named(&public_methods, "update");
    if update_methods
        .iter()
        .any(|method| method.parameters.len() == 3)
    {
        standards.push("NEP-22".to_string());
    } else if !update_methods.is_empty() {
        diagnostics.push(StandardsDiagnostic {
            level: StandardsDiagnosticLevel::Info,
            standard: "NEP-22",
            message: "contract has `update` but no 3-parameter NEP-22 signature \
                      `update(nefFile, manifest, data)`."
                .to_string(),
        });
    }

    // ── NEP-26: NEP-11 Receiver Callback ─────────────────────────────
    let nep11_payment_methods = methods_named(&public_methods, "onNEP11Payment");
    if nep11_payment_methods
        .iter()
        .any(|method| method.parameters.len() == 4)
    {
        standards.push("NEP-26".to_string());
    } else if !nep11_payment_methods.is_empty() {
        diagnostics.push(StandardsDiagnostic {
            level: StandardsDiagnosticLevel::Info,
            standard: "NEP-26",
            message: "contract has `onNEP11Payment` but signature does not match \
                      NEP-26 (`onNEP11Payment(from, amount, tokenId, data)`)."
                .to_string(),
        });
    }

    // ── NEP-27: NEP-17 Receiver Callback ─────────────────────────────
    let nep17_payment_methods = methods_named(&public_methods, "onNEP17Payment");
    if nep17_payment_methods
        .iter()
        .any(|method| method.parameters.len() == 3)
    {
        standards.push("NEP-27".to_string());
    } else if !nep17_payment_methods.is_empty() {
        diagnostics.push(StandardsDiagnostic {
            level: StandardsDiagnosticLevel::Info,
            standard: "NEP-27",
            message: "contract has `onNEP17Payment` but signature does not match \
                      NEP-27 (`onNEP17Payment(from, amount, data)`)."
                .to_string(),
        });
    }

    // ── NEP-29: Deployment/Update Callback ───────────────────────────
    let deploy_methods = methods_named(&public_methods, "_deploy");
    if deploy_methods
        .iter()
        .any(|method| method.parameters.len() == 2)
    {
        standards.push("NEP-29".to_string());
    } else if !deploy_methods.is_empty() {
        diagnostics.push(StandardsDiagnostic {
            level: StandardsDiagnosticLevel::Info,
            standard: "NEP-29",
            message: "contract has `_deploy` but signature does not match \
                      NEP-29 (`_deploy(data, update)`)."
                .to_string(),
        });
    }

    // ── NEP-30: Contract Witness Verification Callback ───────────────
    let verify_methods = methods_named(&public_methods, "verify");
    if verify_methods.len() == 1 && method_returns_bool(verify_methods[0]) {
        standards.push("NEP-30".to_string());
    } else if verify_methods.len() > 1 {
        diagnostics.push(StandardsDiagnostic {
            level: StandardsDiagnosticLevel::Warning,
            standard: "NEP-30",
            message: "contract has multiple `verify` methods. NEP-30 requires a \
                      single `verify` entrypoint."
                .to_string(),
        });
    } else if verify_methods.len() == 1 {
        diagnostics.push(StandardsDiagnostic {
            level: StandardsDiagnosticLevel::Info,
            standard: "NEP-30",
            message: "contract has `verify` but it does not return `bool` as \
                      required by NEP-30."
                .to_string(),
        });
    }

    // ── NEP-31: Contract Destroy Guideline ───────────────────────────
    let destroy_methods = methods_named(&public_methods, "destroy");
    if destroy_methods
        .iter()
        .any(|method| method.parameters.is_empty() && method.return_parameters.is_empty())
    {
        standards.push("NEP-31".to_string());
    } else if !destroy_methods.is_empty() {
        diagnostics.push(StandardsDiagnostic {
            level: StandardsDiagnosticLevel::Info,
            standard: "NEP-31",
            message: "contract has `destroy` but no parameterless void signature \
                      required by NEP-31."
                .to_string(),
        });
    }

    StandardsDetectionResult {
        standards,
        diagnostics,
    }
}

/// Determine whether an event declaration will be emitted (and declared in
/// the manifest) in NATIVE NEP `Transfer` shape, and for which standard.
///
/// Mirrors the emit lowering / manifest builder by delegating to the shared
/// `ir::native_transfer_standard` predicate. The enum set passed to the
/// canonicalizer is empty: enum names can never spell `address` / `uint256`
/// / `bytes32` / `bytes` (reserved words), so positive matches are identical
/// with or without enum knowledge.
fn event_native_transfer_standard(event: &EventMetadata) -> Option<ir::NativeTransferStandard> {
    let enum_names: HashSet<String> = HashSet::new();
    let canonical_types: Vec<String> = event
        .parameters
        .iter()
        .map(|param| ir::event_canonical_param_type(&param.ty, &enum_names))
        .collect();
    ir::native_transfer_standard(&event.name, event.anonymous, &canonical_types)
}

/// Check that a `Transfer` event exists with the expected parameter count
/// AND the parameter types that make the compiler emit it in native NEP
/// shape (`[from, to, amount(, tokenId)]`, readable by NEP trackers).
fn validate_transfer_event(
    events: &[EventMetadata],
    standard: &'static str,
    expected_params: usize,
    diagnostics: &mut Vec<StandardsDiagnostic>,
) {
    let expected_kind = if expected_params == 4 {
        ir::NativeTransferStandard::Nep11
    } else {
        ir::NativeTransferStandard::Nep17
    };
    let transfer_event = events.iter().find(|e| e.name == "Transfer");
    match transfer_event {
        None => {
            diagnostics.push(StandardsDiagnostic {
                level: StandardsDiagnosticLevel::Warning,
                standard,
                message: format!(
                    "{standard} detected but contract is missing the required `Transfer` event \
                     ({expected_params} parameters expected).",
                ),
            });
        }
        Some(evt) if evt.parameters.len() != expected_params => {
            diagnostics.push(StandardsDiagnostic {
                level: StandardsDiagnosticLevel::Info,
                standard,
                message: format!(
                    "{standard} `Transfer` event has {} parameter(s), expected {expected_params}.",
                    evt.parameters.len(),
                ),
            });
        }
        Some(evt) if event_native_transfer_standard(evt) != Some(expected_kind) => {
            diagnostics.push(StandardsDiagnostic {
                level: StandardsDiagnosticLevel::Warning,
                standard,
                message: format!(
                    "{standard} `Transfer` event parameter types do not match the standard \
                     signature, so it will be emitted in EVM log shape (unreadable by NEP \
                     trackers). Declare it as `Transfer(address from, address to, uint256 \
                     amount{})` for native emission.",
                    if expected_params == 4 { ", bytes tokenId" } else { "" },
                ),
            });
        }
        _ => {} // Event present in native NEP shape — all good.
    }
}

/// Returns all public/external methods with a given name (case-insensitive).
fn methods_named<'a>(
    public_methods: &[&'a FunctionMetadata],
    name: &str,
) -> Vec<&'a FunctionMetadata> {
    public_methods
        .iter()
        .copied()
        .filter(|method| method.name.eq_ignore_ascii_case(name))
        .collect()
}

/// Best-effort bool return detection for standards validation.
fn method_returns_bool(method: &FunctionMetadata) -> bool {
    if method.return_parameters.len() != 1 {
        return false;
    }
    let ret = &method.return_parameters[0];
    if let Some(neo_type) = &ret.neo_type {
        if matches!(neo_type, NeoType::Boolean) {
            return true;
        }
    }
    ret.ty.eq_ignore_ascii_case("bool")
}

/// Task #28: hard-validate standards the user EXPLICITLY asserted via
/// `@custom:neo.manifest.supportedstandards`. For NEP-17 / NEP-11 the
/// required methods and `Transfer` event are mandatory — shipping a
/// contract that claims the standard without them is a manifest error,
/// not a warning. Implicit (auto-detected) standards are gated on full
/// conformance in `detect_supported_standards` (method names plus the
/// `transfer` signature and `Transfer` event shape), so near-misses
/// surface as warnings instead of false manifest claims.
fn validate_declared_standards(
    declared: &[String],
    methods: &[FunctionMetadata],
    events: &[EventMetadata],
) -> Result<(), String> {
    let public_methods: Vec<&FunctionMetadata> = methods
        .iter()
        .filter(|m| {
            !matches!(m.kind, FunctionKind::Constructor)
                && matches!(m.visibility, VisibilityKind::Public | VisibilityKind::External)
        })
        .collect();
    let names: HashSet<String> = public_methods
        .iter()
        .map(|m| m.name.to_ascii_lowercase())
        .collect();
    let transfer_event = events.iter().find(|e| e.name == "Transfer");

    for std in declared {
        // `(required method names, Transfer-event param count,
        // native Transfer-event kind, transfer-method param count)`
        let (required, expected_transfer_params, expected_kind, expected_transfer_method_params): (
            &[&str],
            usize,
            ir::NativeTransferStandard,
            usize,
        ) = match std.as_str() {
            "NEP-17" => (
                &["symbol", "decimals", "totalsupply", "balanceof", "transfer"],
                3,
                ir::NativeTransferStandard::Nep17,
                4,
            ),
            "NEP-11" => (
                // Mandatory NEP-11 method set — must match the auto-detection
                // list (`nep11_required`) so the explicit-declaration gate is no
                // weaker than auto-detection. `totalSupply` and `tokensOf` are
                // mandatory per the NEP-11 spec; omitting them previously let a
                // contract ship a manifest falsely advertising `supportedstandards:
                // ["NEP-11"]` while lacking methods wallets/indexers rely on.
                &["symbol", "decimals", "totalsupply", "balanceof", "tokensof", "ownerof", "transfer"],
                4,
                ir::NativeTransferStandard::Nep11,
                3,
            ),
            _ => continue,
        };
        let missing: Vec<&str> = required
            .iter()
            .copied()
            .filter(|m| !names.contains(*m))
            .collect();
        if !missing.is_empty() {
            return Err(format!(
                "contract declares `{std}` in supportedstandards but is missing required method(s): {}. \
                 Either implement the missing method(s) or remove `{std}` from @custom:neo.manifest.supportedstandards.",
                missing.join(", "),
            ));
        }
        let transfer_method_ok = public_methods.iter().any(|m| {
            m.name.eq_ignore_ascii_case("transfer")
                && m.parameters.len() == expected_transfer_method_params
        });
        if !transfer_method_ok {
            return Err(format!(
                "contract declares `{std}` in supportedstandards but no `transfer` overload takes \
                 {expected_transfer_method_params} parameter(s) as the {std} spec requires. \
                 Fix the `transfer` signature or remove `{std}` from @custom:neo.manifest.supportedstandards."
            ));
        }
        match transfer_event {
            None => {
                return Err(format!(
                    "contract declares `{std}` in supportedstandards but is missing the required `Transfer` event \
                     ({expected_transfer_params} parameters expected). Either emit a `Transfer` event or remove `{std}` \
                     from @custom:neo.manifest.supportedstandards."
                ));
            }
            Some(evt) if evt.parameters.len() != expected_transfer_params => {
                return Err(format!(
                    "contract declares `{std}` in supportedstandards but its `Transfer` event has {} parameter(s); \
                     {std} requires {expected_transfer_params}. Fix the event signature or remove `{std}` \
                     from @custom:neo.manifest.supportedstandards.",
                    evt.parameters.len(),
                ));
            }
            Some(evt) if event_native_transfer_standard(evt) != Some(expected_kind) => {
                // Right arity but wrong parameter types (or `anonymous`):
                // the compiler will emit this event in EVM log shape, which
                // NEP trackers cannot read — the declared standard would be
                // a lie. Hard error, same policy as a missing event.
                return Err(format!(
                    "contract declares `{std}` in supportedstandards but its `Transfer` event does not match the \
                     {std} signature `Transfer(address from, address to, uint256 amount{})`, so it will not be \
                     emitted as a native NEP transfer notification. Fix the event parameter types or remove `{std}` \
                     from @custom:neo.manifest.supportedstandards.",
                    if expected_transfer_params == 4 { ", bytes-or-bytes32 tokenId" } else { "" },
                ));
            }
            _ => {}
        }
    }
    Ok(())
}