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
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.19;

/**
 * @title ERC20Token
 * @dev NEP-17 compliant fungible token for Neo N3 blockchain.
 *
 * Compiler constraints respected:
 *   - NEP-17 transfer(from, to, amount, data) with Runtime.checkWitness(from)
 *   - No {value: ...} — uses NativeCalls where needed
 *   - No receive()/fallback() — uses onNEP17Payment() callback
 *   - Parameterless constructor (Neo deploy constraint)
 *   - Import devpack via -I devpack
 *
 * Features: transfers, allowances, minting, burning, pausable, ownership
 */
contract ERC20Token {
    // State variables
    mapping(address => uint256) private _balances;
    mapping(address => mapping(address => uint256)) private _allowances;
    
    uint256 private _totalSupply;
    uint8 private _decimals;
    string private _name;
    string private _symbol;
    address private _owner;
    bool private _paused;
    
    // Events
    event Transfer(address indexed from, address indexed to, uint256 value);
    event Approval(address indexed owner, address indexed spender, uint256 value);
    event Mint(address indexed to, uint256 value);
    event Burn(address indexed from, uint256 value);
    event OwnershipTransferred(address indexed previousOwner, address indexed newOwner);
    event Pause();
    event Unpause();
    
    // Modifiers
    modifier onlyOwner() {
        require(msg.sender == _owner, "ERC20: caller is not the owner");
        _;
    }
    
    modifier whenNotPaused() {
        require(!_paused, "ERC20: token transfer while paused");
        _;
    }
    
    modifier validAddress(address account) {
        require(account != address(0), "ERC20: invalid address");
        _;
    }
    
    bool private _initialized;

    /// @dev Parameterless constructor — Neo N3 deploy constraint.
    /// Sets sensible defaults; call initialize() to customize.
    constructor() {
        _name = "MyToken";
        _symbol = "MTK";
        _decimals = 8;
        _owner = msg.sender;
        _paused = false;
        emit OwnershipTransferred(address(0), msg.sender);
    }

    /// @notice Initialize token metadata. Only owner, only once.
    function initialize(
        string memory name_,
        string memory symbol_,
        uint8 decimals_,
        uint256 initialSupply
    ) external onlyOwner {
        require(!_initialized, "ERC20: already initialized");
        require(bytes(name_).length > 0, "ERC20: name cannot be empty");
        require(bytes(symbol_).length > 0, "ERC20: symbol cannot be empty");
        require(decimals_ <= 18, "ERC20: decimals cannot exceed 18");

        _name = name_;
        _symbol = symbol_;
        _decimals = decimals_;
        _initialized = true;

        if (initialSupply > 0) {
            _mint(msg.sender, initialSupply);
        }
    }
    
    /**
     * @dev Returns the name of the token
     */
    function name() public view returns (string memory) {
        return _name;
    }
    
    /**
     * @dev Returns the symbol of the token
     */
    function symbol() public view returns (string memory) {
        return _symbol;
    }
    
    /**
     * @dev Returns the number of decimals used for token amounts
     */
    function decimals() public view returns (uint8) {
        return _decimals;
    }
    
    /**
     * @dev Returns the total supply of tokens
     */
    function totalSupply() public view returns (uint256) {
        return _totalSupply;
    }
    
    /**
     * @dev Returns the balance of the specified account
     * @param account The address to query the balance of
     */
    function balanceOf(address account) public view returns (uint256) {
        return _balances[account];
    }
    
    /**
     * @dev Returns the remaining number of tokens that spender can spend on behalf of owner
     * @param owner The address that owns the funds
     * @param spender The address that will spend the funds
     */
    function allowance(address owner, address spender) public view returns (uint256) {
        return _allowances[owner][spender];
    }
    
    /**
     * @dev Returns the address of the current owner
     */
    function owner() public view returns (address) {
        return _owner;
    }
    
    /**
     * @dev Returns true if the contract is paused
     */
    function paused() public view returns (bool) {
        return _paused;
    }
    
    /**
     * @dev NEP-17 standard transfer (4-parameter signature).
     * Authorization via Runtime.checkWitness(from) instead of msg.sender.
     * @param from The address to transfer from (must pass witness check)
     * @param to The address to transfer to
     * @param amount The amount to transfer
     * @param data Arbitrary data passed to onNEP17Payment callback
     */
    function transfer(address from, address to, uint256 amount, Any calldata data)
        public
        whenNotPaused
        validAddress(to)
        returns (bool)
    {
        require(Runtime.checkWitness(from), "ERC20: unauthorized");
        _transfer(from, to, amount);
        return true;
    }
    
    /**
     * @dev Approves spender to spend tokens on behalf of caller
     * @param spender The address which will spend the funds
     * @param amount The amount of tokens to approve
     */
    function approve(address spender, uint256 amount) 
        public 
        whenNotPaused 
        validAddress(spender) 
        returns (bool) 
    {
        _approve(msg.sender, spender, amount);
        return true;
    }
    
    /**
     * @dev Transfers tokens from sender to recipient using allowance mechanism
     * @param from The address to transfer from
     * @param to The address to transfer to
     * @param amount The amount to transfer
     */
    function transferFrom(address from, address to, uint256 amount) 
        public 
        whenNotPaused 
        validAddress(from) 
        validAddress(to) 
        returns (bool) 
    {
        uint256 currentAllowance = _allowances[from][msg.sender];
        require(currentAllowance >= amount, "ERC20: transfer amount exceeds allowance");
        
        _transfer(from, to, amount);
        _approve(from, msg.sender, currentAllowance - amount);
        
        return true;
    }
    
    /**
     * @dev Atomically increases the allowance granted to spender
     * @param spender The address which will spend the funds
     * @param addedValue The amount to increase the allowance by
     */
    function increaseAllowance(address spender, uint256 addedValue) 
        public 
        whenNotPaused 
        validAddress(spender) 
        returns (bool) 
    {
        uint256 currentAllowance = _allowances[msg.sender][spender];
        _approve(msg.sender, spender, currentAllowance + addedValue);
        return true;
    }
    
    /**
     * @dev Atomically decreases the allowance granted to spender
     * @param spender The address which will spend the funds
     * @param subtractedValue The amount to decrease the allowance by
     */
    function decreaseAllowance(address spender, uint256 subtractedValue) 
        public 
        whenNotPaused 
        validAddress(spender) 
        returns (bool) 
    {
        uint256 currentAllowance = _allowances[msg.sender][spender];
        require(currentAllowance >= subtractedValue, "ERC20: decreased allowance below zero");
        _approve(msg.sender, spender, currentAllowance - subtractedValue);
        return true;
    }
    
    /**
     * @dev Creates tokens and assigns them to account
     * @param to The address to mint tokens to
     * @param amount The amount of tokens to mint
     */
    function mint(address to, uint256 amount) 
        public 
        onlyOwner 
        validAddress(to) 
    {
        require(amount > 0, "ERC20: mint amount must be greater than 0");
        _mint(to, amount);
    }
    
    /**
     * @dev Destroys tokens from caller's account
     * @param amount The amount of tokens to burn
     */
    function burn(uint256 amount) public whenNotPaused {
        require(amount > 0, "ERC20: burn amount must be greater than 0");
        _burn(msg.sender, amount);
    }
    
    /**
     * @dev Destroys tokens from account using allowance mechanism
     * @param from The address to burn tokens from
     * @param amount The amount of tokens to burn
     */
    function burnFrom(address from, uint256 amount) 
        public 
        whenNotPaused 
        validAddress(from) 
    {
        require(amount > 0, "ERC20: burn amount must be greater than 0");
        
        uint256 currentAllowance = _allowances[from][msg.sender];
        require(currentAllowance >= amount, "ERC20: burn amount exceeds allowance");
        
        _burn(from, amount);
        _approve(from, msg.sender, currentAllowance - amount);
    }
    
    /**
     * @dev Transfers ownership of the contract to a new account
     * @param newOwner The address of the new owner
     */
    function transferOwnership(address newOwner) 
        public 
        onlyOwner 
        validAddress(newOwner) 
    {
        emit OwnershipTransferred(_owner, newOwner);
        _owner = newOwner;
    }
    
    /**
     * @dev Renounces ownership of the contract
     */
    function renounceOwnership() public onlyOwner {
        emit OwnershipTransferred(_owner, address(0));
        _owner = address(0);
    }
    
    /**
     * @dev Pauses all token transfers
     */
    function pause() public onlyOwner {
        require(!_paused, "ERC20: already paused");
        _paused = true;
        emit Pause();
    }
    
    /**
     * @dev Unpauses all token transfers
     */
    function unpause() public onlyOwner {
        require(_paused, "ERC20: not paused");
        _paused = false;
        emit Unpause();
    }
    
    /**
     * @dev Internal transfer function
     * @param from The address to transfer from
     * @param to The address to transfer to  
     * @param amount The amount to transfer
     */
    function _transfer(address from, address to, uint256 amount) internal {
        require(from != address(0), "ERC20: transfer from the zero address");
        require(to != address(0), "ERC20: transfer to the zero address");
        require(amount > 0, "ERC20: transfer amount must be greater than 0");
        
        uint256 fromBalance = _balances[from];
        require(fromBalance >= amount, "ERC20: transfer amount exceeds balance");
        
        unchecked {
            _balances[from] = fromBalance - amount;
        }
        _balances[to] += amount;
        
        emit Transfer(from, to, amount);
    }
    
    /**
     * @dev Internal mint function
     * @param to The address to mint tokens to
     * @param amount The amount to mint
     */
    function _mint(address to, uint256 amount) internal {
        require(to != address(0), "ERC20: mint to the zero address");
        require(amount > 0, "ERC20: mint amount must be greater than 0");
        
        // Check for overflow
        require(_totalSupply + amount >= _totalSupply, "ERC20: total supply overflow");
        
        _totalSupply += amount;
        _balances[to] += amount;
        
        emit Transfer(address(0), to, amount);
        emit Mint(to, amount);
    }
    
    /**
     * @dev Internal burn function
     * @param from The address to burn tokens from
     * @param amount The amount to burn
     */
    function _burn(address from, uint256 amount) internal {
        require(from != address(0), "ERC20: burn from the zero address");
        require(amount > 0, "ERC20: burn amount must be greater than 0");
        
        uint256 accountBalance = _balances[from];
        require(accountBalance >= amount, "ERC20: burn amount exceeds balance");
        
        unchecked {
            _balances[from] = accountBalance - amount;
            _totalSupply -= amount;
        }
        
        emit Transfer(from, address(0), amount);
        emit Burn(from, amount);
    }
    
    /**
     * @dev Internal approve function
     * @param owner The address that owns the funds
     * @param spender The address that will spend the funds
     * @param amount The amount to approve
     */
    function _approve(address owner, address spender, uint256 amount) internal {
        require(owner != address(0), "ERC20: approve from the zero address");
        require(spender != address(0), "ERC20: approve to the zero address");
        
        _allowances[owner][spender] = amount;
        emit Approval(owner, spender, amount);
    }
    
    /**
     * @dev Batch transfer to multiple recipients
     * @param recipients Array of recipient addresses
     * @param amounts Array of amounts to transfer
     */
    function batchTransfer(address[] memory recipients, uint256[] memory amounts) 
        public 
        whenNotPaused 
        returns (bool) 
    {
        require(recipients.length == amounts.length, "ERC20: arrays length mismatch");
        require(recipients.length > 0, "ERC20: empty arrays");
        require(recipients.length <= 100, "ERC20: too many recipients");
        
        for (uint256 i = 0; i < recipients.length; i++) {
            require(recipients[i] != address(0), "ERC20: transfer to zero address");
            require(amounts[i] > 0, "ERC20: transfer amount must be greater than 0");
            _transfer(msg.sender, recipients[i], amounts[i]);
        }
        
        return true;
    }
    
    /// @notice NEP-17 callback — receives token payments.
    function onNEP17Payment(address from, uint256 amount, bytes memory /*data*/) external {
        // Accept any NEP-17 token payment; override for custom logic.
    }

    /**
     * @dev Emergency function to recover accidentally sent tokens.
     * Uses abi.encodeWithSignature for Neo-compatible cross-contract call.
     * @param token The address of the token contract to recover from
     * @param amount The amount to recover
     */
    function emergencyTokenRecovery(address token, uint256 amount)
        public
        onlyOwner
        validAddress(token)
    {
        require(token != address(this), "ERC20: cannot recover own tokens");
        require(amount > 0, "ERC20: recovery amount must be greater than 0");

        (bool success, bytes memory data) = token.call(
            abi.encodeWithSignature("transfer(address,address,uint256,bytes)", address(this), _owner, amount, "")
        );

        require(success && (data.length == 0 || abi.decode(data, (bool))),
                "ERC20: token recovery failed");
    }
}