MiladyRescueFinder

Description:

Smart contract deployed on Ethereum.

Blockchain: Ethereum

Source Code: View Code On The Blockchain

Solidity Source Code:

// SPDX-License-Identifier: MIT
pragma solidity 0.8.30;

contract MiladyRescueFinder {
    address constant MILADY = 0x5Af0D9827E0c53E4799BB226655A1de152A425a5;
    address constant MILADY_RESCUE = 0x000000000088Fa5EfA50Ff6d5CfAd19551cd3b36;

    struct MiladyRescue {
        uint256 milady;
        address pool;
        uint256 ethPrice;
    }

    function findMiladys() public view returns (MiladyRescue[] memory listings) {
        address[] memory pools = IMiladyRescue(MILADY_RESCUE).getPools();
        
        uint256 active;
        for (uint256 i; i != pools.length; ++i) {
            if (IMiladyPool(pools[i]).redeemed()) {
                if (!IMiladyPool(pools[i]).listingSold()) {
                    ++active;
                }
            }
        }
        
        listings = new MiladyRescue[](active);
        
        uint256 index;
        for (uint256 i; i != pools.length; ++i) {
            address pool = pools[i];
            if (IMiladyPool(pool).redeemed()) {
                if (!IMiladyPool(pool).listingSold()) {
                    listings[index] = MiladyRescue({
                        milady: IMiladyPool(pool).milady(),
                        pool: pool,
                        ethPrice: IMiladyPool(pool).ethPrice()
                    });
                    ++index;
                }
            }
        }
    }

    function sortByCheapest(MiladyRescue[] memory listings) 
        public 
        pure 
        returns (MiladyRescue[] memory) 
    {
        uint256 length = listings.length;
        
        for (uint256 i = 1; i < length; ++i) {
            MiladyRescue memory key = listings[i];
            uint256 j = i;
            
            while (j > 0 && listings[j - 1].ethPrice > key.ethPrice) {
                listings[j] = listings[j - 1];
                --j;
            }
            
            listings[j] = key;
        }
        
        return listings;
    }

    function findMiladysSortedByCheapest() public view returns (MiladyRescue[] memory listings) {
        listings = findMiladys();
        return sortByCheapest(listings);
    }

    error NoActiveListings();

    function findCheapestMilady() public view returns (MiladyRescue memory cheapest) {
        address[] memory pools = IMiladyRescue(MILADY_RESCUE).getPools();
        
        uint256 lowestPrice = type(uint256).max;
        bool found;
        
        for (uint256 i; i != pools.length; ++i) {
            address pool = pools[i];
            if (IMiladyPool(pool).redeemed()) {
                if (!IMiladyPool(pool).listingSold()) {
                    uint256 ethPrice = IMiladyPool(pool).ethPrice();
                    
                    if (ethPrice < lowestPrice) {
                        lowestPrice = ethPrice;
                        cheapest = MiladyRescue({
                            milady: IMiladyPool(pool).milady(),
                            pool: pool,
                            ethPrice: ethPrice
                        });
                        found = true;
                    }
                }
            }
        }
        
        if (!found) revert NoActiveListings();
    }

    function sortByMostExpensive(MiladyRescue[] memory listings) 
        public 
        pure 
        returns (MiladyRescue[] memory) 
    {
        uint256 length = listings.length;
        
        for (uint256 i = 1; i < length; ++i) {
            MiladyRescue memory key = listings[i];
            uint256 j = i;
            
            while (j > 0 && listings[j - 1].ethPrice < key.ethPrice) {
                listings[j] = listings[j - 1];
                --j;
            }
            
            listings[j] = key;
        }
        
        return listings;
    }

    function findMiladysByMostExpensive() public view returns (MiladyRescue[] memory listings) {
        listings = findMiladys();
        return sortByMostExpensive(listings);
    }

    function findMostExpensiveMilady() public view returns (MiladyRescue memory mostExpensive) {
        address[] memory pools = IMiladyRescue(MILADY_RESCUE).getPools();
        
        uint256 highestPrice;
        bool found;
        
        for (uint256 i; i != pools.length; ++i) {
            address pool = pools[i];
            if (IMiladyPool(pool).redeemed()) {
                if (!IMiladyPool(pool).listingSold()) {
                    uint256 ethPrice = IMiladyPool(pool).ethPrice();
                    
                    if (ethPrice > highestPrice) {
                        highestPrice = ethPrice;
                        mostExpensive = MiladyRescue({
                            milady: IMiladyPool(pool).milady(),
                            pool: pool,
                            ethPrice: ethPrice
                        });
                        found = true;
                    }
                }
            }
        }
        
        if (!found) revert NoActiveListings();
    }

    function batchBuyListings(address[] calldata pools, uint256[] calldata prices, uint256[] calldata miladys) public payable {
        for (uint256 i; i != pools.length; ++i) {
            IMiladyPool(pools[i]).buyListing{value: prices[i]}();
            IERC721(MILADY).transferFrom(address(this), msg.sender, miladys[i]);
        }
    }

    function triggerStrategyBuy(
        address strategy,
        address pool,
        uint256 milady,
        uint256 ethValue
    ) public {
        bytes memory data = abi.encodeWithSelector(
            IMiladyPool.buyListing.selector
        );
        
        IStrategy(strategy).buyTargetNFT(
            ethValue,    // value to send
            data,        // encoded buyListing() call
            milady,      // expectedId
            pool         // target (the pool address)
        );
    }

    function batchTriggerStrategyBuy(
        address strategy,
        address[] calldata pools,
        uint256[] calldata miladys,
        uint256[] calldata ethValues
    ) public {
        for (uint256 i; i != pools.length; ++i) {
            bytes memory data = abi.encodeWithSelector(
                IMiladyPool.buyListing.selector
            );
            
            IStrategy(strategy).buyTargetNFT(
                ethValues[i],
                data,
                miladys[i],
                pools[i]
            );
        }
    }

    function sweepAll() public payable {
        MiladyRescue[] memory listings = findMiladys();
        
        if (listings.length == 0) revert NoActiveListings();
        
        for (uint256 i; i != listings.length; ++i) {
            IMiladyPool(listings[i].pool).buyListing{value: listings[i].ethPrice}();
            IERC721(MILADY).transferFrom(address(this), msg.sender, listings[i].milady);
        }
    }

    function sweepAllWithStrategy(address strategy) public {
        MiladyRescue[] memory listings = findMiladys();
        
        if (listings.length == 0) revert NoActiveListings();
        
        bytes memory data = abi.encodeWithSelector(IMiladyPool.buyListing.selector);
        
        for (uint256 i; i != listings.length; ++i) {
            IStrategy(strategy).buyTargetNFT(
                listings[i].ethPrice,
                data,
                listings[i].milady,
                listings[i].pool
            );
        }
    }

    function buyCheapest() public payable {
        MiladyRescue memory cheapest = findCheapestMilady();
        
        IMiladyPool(cheapest.pool).buyListing{value: cheapest.ethPrice}();
        IERC721(MILADY).transferFrom(address(this), msg.sender, cheapest.milady);
    }

    function buyCheapestWithStrategy(address strategy) public {
        MiladyRescue memory cheapest = findCheapestMilady();
        
        bytes memory data = abi.encodeWithSelector(IMiladyPool.buyListing.selector);
        
        IStrategy(strategy).buyTargetNFT(
            cheapest.ethPrice,
            data,
            cheapest.milady,
            cheapest.pool
        );
    }

    function buyMostExpensive() public payable {
        MiladyRescue memory mostExpensive = findMostExpensiveMilady();
        
        IMiladyPool(mostExpensive.pool).buyListing{value: mostExpensive.ethPrice}();
        IERC721(MILADY).transferFrom(address(this), msg.sender, mostExpensive.milady);
    }

    function buyMostExpensiveWithStrategy(address strategy) public {
        MiladyRescue memory mostExpensive = findMostExpensiveMilady();
        
        bytes memory data = abi.encodeWithSelector(IMiladyPool.buyListing.selector);
        
        IStrategy(strategy).buyTargetNFT(
            mostExpensive.ethPrice,
            data,
            mostExpensive.milady,
            mostExpensive.pool
        );
    }
}

interface IStrategy {
    function buyTargetNFT(uint256 value, bytes calldata data, uint256 expectedId, address target) external;
}

interface IERC721 {
    function transferFrom(address from, address to, uint256 tokenId) external;
}

interface IMiladyRescue {
    function getPools() external view returns (address[] memory);
    function getPoolFromMilady(uint256 milady) external view returns (address pool);
}

interface IMiladyPool {
    function buyListing() external payable;
    function milady() external view returns (uint256);
    function redeemed() external view returns (bool);
    function listingSold() external view returns (bool);
    function ethPrice() external view returns (uint256);
}

Tags:
addr:0x6d8b3f51ddfddca55fed8a36293e8ae45dd37a57|verified:true|block:23498155|tx:0x823b32e021962d0394ec7812bb89d05cb1640810173d8fb584d3969e0c651c07|first_check:1759509028

Submitted on: 2025-10-03 18:30:29

Comments

Log in to comment.

No comments yet.