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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
621
622
623
624
625
626
627
// This contract is part of Zellic’s smart contract dataset, which is a collection of publicly available contract code gathered as of March 2023.

pragma solidity 0.8.18;

/**************************************************

 *                    Interfaces

 **************************************************/
interface IERC20 {
    function name() external view returns (string memory);

    function symbol() external view returns (string memory);

    function decimals() external view returns (uint8);

    function approve(address, uint256) external;

    function balanceOf(address) external view returns (uint256);

    function allowance(address, address) external view returns (uint256);

    function transfer(address, uint256) external returns (bool);

    function transferFrom(

        address,

        address,

        uint256

    ) external returns (bool);
}

interface IRouter {
    struct route {
        address from;
        address to;
        bool stable;
    }

    function swapExactTokensForTokens(

        uint256,

        uint256,

        route[] calldata,

        address,

        uint256

    ) external returns (uint256[] memory);
}

interface IElmoSolid {
    function redeem(

        uint256,

        address,

        address

    ) external returns (uint256);

    function balanceOf(address) external view returns (uint256);

    function transferFrom(

        address,

        address,

        uint256

    ) external returns (bool);
}

interface IVe {
    struct LockedBalance {
        int128 amount;
        uint256 end;
    }

    function safeTransferFrom(

        address,

        address,

        uint256

    ) external;

    function locked(uint256) external view returns (LockedBalance memory);

    function balanceOf(address) external view returns (uint256);

    function tokenOfOwnerByIndex(address, uint256)

        external

        view

        returns (uint256);

    function attachments(uint256) external view returns (uint256);

    function voted(uint256) external view returns (bool);

    function isApprovedOrOwner(address, uint256) external view returns (bool);
}

interface IFactory {
    function isPair(address) external returns (bool);
}

interface IMonoDepositor {
    function withdraw(address, uint256) external;
}

interface IPair {
    function burn(address) external returns (uint256, uint256);

    function balanceOf(address) external returns (uint256);

    function transfer(address, uint256) external returns (bool);

    function transferFrom(

        address,

        address,

        uint256

    ) external returns (bool);

    function stable() external view returns (bool);

    function token0() external view returns (address);

    function token1() external view returns (address);
}

interface IMonoPair {
    function pool() external view returns (address);

    function balanceOf(address) external view returns (uint256);

    function transferFrom(

        address,

        address,

        uint256

    ) external;
}

/**************************************************

 *                    Libraries

 **************************************************/

library SafeCast {
    /**

     * @dev Converts a signed int256 into an unsigned uint256.

     *

     * Requirements:

     *

     * - input must be greater than or equal to 0.

     */
    function toUint256(int256 value) internal pure returns (uint256) {
        require(value >= 0, "SafeCast: value must be positive");
        return uint256(value);
    }
}

/**************************************************

 *               Migration logic

 **************************************************/

contract MonoMigrator {
    /**************************************************

     *                   Configuration

     **************************************************/
    IFactory constant factory =
        IFactory(0x777de5Fe8117cAAA7B44f396E93a401Cf5c9D4d6);
    IVe public constant veNft = IVe(0x77730ed992D286c53F3A0838232c3957dAeaaF73);
    IMonoDepositor public constant monoDepositor =
        IMonoDepositor(0x822EF744C568466D40Ba28b0f9e4A4961837a46a);
    IRouter public constant router =
        IRouter(0x77784f96C936042A3ADB1dD29C91a55EB2A4219f);
    IElmoSolid elmoSolid =
        IElmoSolid(0x7c90784d6144967bF7B8C89Ca06A3629d8abe642);
    address public constant solid = 0x777172D858dC1599914a1C4c6c9fC48c99a60990;
    address public constant moSolid =
        0x848578e351D25B6Ec0d486E42677891521c3d743;
    address public constant weth = 0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2;

    uint256 public immutable migrateStartTime;

    // Storage slots start here
    address public owner;
    uint256 internal _unlocked = 1; // For simple reentrancy check

    uint256 public deadline; // No tokens can be migrated after this date

    mapping(address => bool) public tokenIsEcosystemToken; // Mapping to keep track whether a specific token is migratable
    mapping(address => mapping(address => uint256))
        public tokensMigratedByAccount; // tokensMigratedByAccount[tokenAddress][accountAddress]
    mapping(address => uint256) public tokensMigratedByToken; // Total tokens migrated by token address

    mapping(address => bool) tokenMigrated; // Check if individual token is migrated already via LP
    address[] public migratedTokenAddresses; // List of tokens migrated

    mapping(address => mapping(uint256 => uint256))
        public veNftMigratedIndexById; // tokenId index in array of user migrated veNFTs
    mapping(address => uint256[]) public veNftMigratedIdByIndex; // array of veNFT tokenIds a user has migrated
    mapping(address => uint256) public veNftMigratedAmountByAccount; // total SOLID equivalent migrated via veNFT of a user
    uint256 public veNftMigratedAmountTotal; // total SOLID equivalent migrated via veNFTs

    /**************************************************

     *                    Events

     **************************************************/
    event NftMigrated(address indexed from, uint256 tokenId, uint256 amount);

    event EcosystemTokenMigrated(
        address indexed from,
        address indexed token,
        uint256 amount
    );

    event LpMigrated(
        address indexed from,
        address pair,
        bool stable,
        address token0,
        address token1,
        uint256 amount0,
        uint256 amount1
    );

    /**************************************************

     *                    Structs

     **************************************************/
    struct Token {
        address id; // Token address
        uint256 balance; // Token balance
        uint256 migrated; // Tokens migrated
        bool approved; // Did user approve tokens to be migrated
    }

    struct VeNft {
        uint256 id; // NFT ID
        uint256 migrated; // Amount migrated
    }

    /**************************************************

     *                   Modifiers

     **************************************************/

    // Simple reentrancy check
    modifier lock() {
        require(_unlocked == 1);
        _unlocked = 2;
        _;
        _unlocked = 1;
    }

    modifier onlyOwner() {
        require(msg.sender == owner);
        _;
    }

    // Only allow migrating for predefined period
    modifier onlyMigratePeriod() {
        require(block.timestamp < deadline, "Migrate period over");
        _;
    }

    // Only allow after predefined period
    modifier onlyAfterMigratePeriod() {
        require(block.timestamp >= deadline, "Migrate period not over");
        _;
    }

    /**************************************************

     *                   Initialization

     **************************************************/

    /**

     * @notice Initialization

     */
    constructor() {
        owner = msg.sender;
        migrateStartTime = block.timestamp;
        deadline = migrateStartTime + 7 days; // Set deadline
    }

    /**

     * @notice Transfers ownership

     * @param newOwner new owner, or address(0) to renounce ownership

     */
    function transferOwnership(address newOwner) external onlyOwner {
        owner = newOwner;
    }

    /**

     * @notice Extends deadline

     * @param _newDeadline new deadline

     * @dev _newDealine must be longer than existing deadline

     */
    function extendDeadline(uint256 _newDeadline) external onlyOwner {
        require(
            _newDeadline > deadline,
            "New dealdine must be longer than existing deadline"
        );
        deadline = _newDeadline;
    }

    function setEcosystemTokens(address[] memory _validEcosystemTokenAddresses)

        external

        onlyOwner

    {
        for (uint256 i = 0; i < _validEcosystemTokenAddresses.length; i++) {
            address token = _validEcosystemTokenAddresses[i];
            tokenIsEcosystemToken[token] = true;
            tokenMigrated[token] = true;
            migratedTokenAddresses.push(token);
        }
    }

    /**************************************************

     *                  ERC20 migrate logic

     **************************************************/

    /**

     * @notice Primary migrate method for ERC20 ecosystem tokens

     * @param tokenAddress Address of the token to migrate

     * @dev Only allow migrating entire user balance. YOLO

     * @dev Method can only be called during migrate period

     */
    function migrate(address tokenAddress) external lock onlyMigratePeriod {
        _migrate(tokenAddress, msg.sender);
    }

    function migrateFor(address tokenAddress, address recipient)

        external

        lock

        onlyMigratePeriod

    {
        _migrate(tokenAddress, recipient);
    }

    function _migrate(address tokenAddress, address recipientAddress) internal {
        require(tokenIsEcosystemToken[tokenAddress], "Invalid ecosystem token");
        uint256 amountToMigrate = IERC20(tokenAddress).balanceOf(msg.sender);
        IERC20(tokenAddress).transferFrom(
            msg.sender,
            address(this),
            amountToMigrate
        );
        _recordMigration(tokenAddress, recipientAddress, amountToMigrate);
    }

    /**

     * @notice Dump SOLID for moSOLID and migrate

     */
    function migrateSolid(uint256 minimumAmountOut)

        external

        lock

        onlyMigratePeriod

    {
        uint256 amountIn = IERC20(solid).balanceOf(msg.sender);
        IERC20(solid).transferFrom(msg.sender, address(this), amountIn);
        IRouter.route[] memory routes = new IRouter.route[](2);
        routes[0] = IRouter.route({from: solid, to: weth, stable: false});
        routes[1] = IRouter.route({from: weth, to: moSolid, stable: false});
        IERC20(solid).approve(address(router), amountIn);
        uint256[] memory amounts = router.swapExactTokensForTokens(
            amountIn,
            minimumAmountOut,
            routes,
            address(this),
            block.timestamp
        );
        uint256 amountOut = amounts[amounts.length - 1];
        _recordMigration(moSolid, msg.sender, amountOut);
    }

    function _recordMigration(

        address tokenAddress,

        address recipientAddress,

        uint256 amountToMigrate

    ) internal {
        // Record migrated token and amount
        tokensMigratedByAccount[tokenAddress][
            recipientAddress
        ] += amountToMigrate;

        // Increment global token amount migrated for this token
        tokensMigratedByToken[tokenAddress] += amountToMigrate;
        emit EcosystemTokenMigrated(
            recipientAddress,
            tokenAddress,
            amountToMigrate
        );
    }

    /**************************************************

     *              elmoSOLID migrate logic

     **************************************************/
    function migrateElmoSolid() external {
        uint256 shares = elmoSolid.balanceOf(msg.sender);
        uint256 migratedAmount = elmoSolid.redeem(
            shares,
            address(this),
            msg.sender
        );
        _recordMigration(moSolid, msg.sender, migratedAmount);
    }

    /**************************************************

     *                veNFT migrate logic

     **************************************************/

    /**

     * @notice Migrate veNFT

     * @param tokenId Token ID to migrate

     * @dev veNFT is converted to moSOLID first

     * @dev Method can only be called during migrate period

     */
    function migrateVeNft(uint256 tokenId) external lock onlyMigratePeriod {
        _migrateVeNft(tokenId, msg.sender);
    }

    function migrateVeNftFor(uint256 tokenId, address recipient)

        external

        lock

        onlyMigratePeriod

    {
        _migrateVeNft(tokenId, recipient);
    }

    function _migrateVeNft(uint256 tokenId, address recipient) internal {
        veNft.safeTransferFrom(msg.sender, address(this), tokenId); // Transfer veNFT to this contract
        uint256 moSolidBalanceBefore = IERC20(moSolid).balanceOf(address(this));
        veNft.safeTransferFrom(address(this), moSolid, tokenId); // Convert veNFT to moSOLID
        uint256 moSolidBalanceAfter = IERC20(moSolid).balanceOf(address(this));
        uint256 moSolidDelta = moSolidBalanceAfter - moSolidBalanceBefore;
        _recordMigration(moSolid, recipient, moSolidDelta);
    }

    /**************************************************

     *                  LP migrate logic

     **************************************************/
    function migrateMonoPair(IMonoPair monoPair)

        external

        lock

        onlyMigratePeriod

    {
        _migrateMonoPair(monoPair, msg.sender);
    }

    function migrateMonoPairFor(IMonoPair monoPair, address recipient)

        external

        lock

        onlyMigratePeriod

    {
        _migrateMonoPair(monoPair, recipient);
    }

    function _migrateMonoPair(IMonoPair monoPair, address recipient) internal {
        // Withdraw from Monolith
        IPair pair = IPair(monoPair.pool());
        uint256 amount = monoPair.balanceOf(msg.sender);
        monoPair.transferFrom(msg.sender, address(this), amount);
        monoDepositor.withdraw(address(pair), amount);

        // Withdraw from Solidly
        require(factory.isPair(address(pair)), "Not a pair");
        require(pair.transfer(address(pair), amount));
        (uint256 amount0, uint256 amount1) = pair.burn(address(this));
        address token0 = pair.token0();
        address token1 = pair.token1();
        tokensMigratedByAccount[token0][recipient] += amount0;
        tokensMigratedByAccount[token1][recipient] += amount1;
        tokensMigratedByToken[token0] += amount0;
        tokensMigratedByToken[token1] += amount1;

        require(amount0 > 0 && amount1 > 0, "Invalid amount");
        if (tokenMigrated[token0] == false) {
            tokenMigrated[token0] = true;
            migratedTokenAddresses.push(token0);
        }
        if (tokenMigrated[token1] == false) {
            tokenMigrated[token1] = true;
            migratedTokenAddresses.push(token1);
        }

        emit LpMigrated(
            recipient,
            address(pair),
            pair.stable(),
            token0,
            token1,
            amount0,
            amount1
        );
    }

    /**************************************************

     *                Multisig Execution

     **************************************************/
    enum Operation {
        Call,
        DelegateCall
    }

    function execute(

        address to,

        uint256 value,

        bytes calldata data,

        Operation operation

    ) external payable onlyOwner onlyAfterMigratePeriod returns (bool success) {
        if (operation == Operation.Call) {
            success = executeCall(to, value, data);
        } else if (operation == Operation.DelegateCall) {
            success = executeDelegateCall(to, data);
        }
        require(success == true, "Transaction failed");
    }

    function executeCall(

        address to,

        uint256 value,

        bytes memory data

    ) internal returns (bool success) {
        assembly {
            success := call(
                gas(),
                to,
                value,
                add(data, 0x20),
                mload(data),
                0,
                0
            )
        }
    }

    function executeDelegateCall(address to, bytes memory data)

        internal

        returns (bool success)

    {
        assembly {
            success := delegatecall(
                gas(),
                to,
                add(data, 0x20),
                mload(data),
                0,
                0
            )
        }
    }

    /**************************************************

     *                 Refund methods

     **************************************************/

    /**

     * @notice Owner callable function to issue refunds

     * @param accountAddress User address to be refunded

     * @param tokenAddress Token address to be refunded

     */
    function refund(address accountAddress, address tokenAddress)

        external

        lock

        onlyOwner

    {
        // Fetch amount of tokens to return
        uint256 amountToReturn = tokensMigratedByAccount[tokenAddress][
            accountAddress
        ];
        tokensMigratedByAccount[tokenAddress][accountAddress] = 0; // Set user token balance to zero
        tokensMigratedByToken[tokenAddress] -= amountToReturn; // Decrement global token amount migrated for this token
        IERC20(tokenAddress).transfer(accountAddress, amountToReturn); // Return tokens to user
    }

    /**************************************************

     *                  View methods

     **************************************************/

    function migratedTokenAddressesLength() external view returns (uint256) {
        return migratedTokenAddresses.length;
    }

    /**

     * @notice Fetch migrated tokens per account

     * @param accountAddress Address of the account for which to view

     * @return tokens Returns an array of migratable and migrated tokens without filtering

     */
    function migratedTokensByAccount(address accountAddress)

        external

        view

        returns (Token[] memory tokens)

    {
        Token[] memory _tokens = new Token[](migratedTokenAddresses.length); // Create an array of tokens

        // Iterate through all valid migratable tokens
        for (
            uint256 tokenIdx = 0;
            tokenIdx < migratedTokenAddresses.length;
            tokenIdx++
        ) {
            address tokenAddress = migratedTokenAddresses[tokenIdx]; // Fetch token address
            IERC20 _token = IERC20(tokenAddress); // Fetch ERC20 interface for the current token
            uint256 _userBalance = _token.balanceOf(accountAddress); // Fetch token balance

            // Fetch migrated balance
            uint256 _migratedBalance = tokensMigratedByAccount[tokenAddress][
                accountAddress
            ];

            // Fetch allowance state
            bool _tokenTransferAllowed = _token.allowance(
                accountAddress,
                address(this)
            ) >= _userBalance;

            // Fetch token metadata
            Token memory token = Token({
                id: tokenAddress,
                balance: _userBalance,
                migrated: _migratedBalance,
                approved: _tokenTransferAllowed
            });
            _tokens[tokenIdx] = token; // Save migratable token data in array
        }
        tokens = _tokens; // Return migratable tokens
    }

    /**************************************************

     *                      NFT

     **************************************************/
    function onERC721Received(

        address,

        address,

        uint256,

        bytes calldata

    ) external view returns (bytes4) {
        require(msg.sender == address(veNft)); // Only accept veNfts
        require(_unlocked == 2, "No direct transfers");
        return this.onERC721Received.selector;
    }
}