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_VAULT = 0x227c7DF69D3ed1ae7574A1a7685fDEd90292EB48;
    address constant MILADY_RESCUE = 0x000000000088Fa5EfA50Ff6d5CfAd19551cd3b36;

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

    function findMiladys() public view returns (MiladyRescue[] memory listings) {
        uint256[] memory miladys = INFTXV2Vault(MILADY_VAULT).allHoldings();

        uint256 milady;
        address pool;
        uint256 ethPrice;
        
        uint256 active;
        for (uint256 i; i != miladys.length; ++i) {
            pool = IMiladyRescue(MILADY_RESCUE).getPoolFromMilady(miladys[i]);
            if (pool.code.length != 0) {
                if (IMiladyPool(pool).redeemed()) {
                    if (!IMiladyPool(pool).listingSold()) {
                        ++active;
                    }
                }
            }
        }
        
        listings = new MiladyRescue[](active);
        
        uint256 index;
        for (uint256 i; i != miladys.length; ++i) {
            milady = miladys[i];
            pool = IMiladyRescue(MILADY_RESCUE).getPoolFromMilady(milady);
            if (pool.code.length != 0) {
                if (IMiladyPool(pool).redeemed()) {
                    if (!IMiladyPool(pool).listingSold()) {
                        ethPrice = IMiladyPool(pool).ethPrice();
                        listings[index] = MiladyRescue({
                            milady: milady,
                            pool: pool,
                            ethPrice: 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) {
        uint256[] memory miladys = INFTXV2Vault(MILADY_VAULT).allHoldings();
        
        uint256 milady;
        address pool;
        uint256 ethPrice;
        uint256 lowestPrice = type(uint256).max;
        bool found;
        
        for (uint256 i; i != miladys.length; ++i) {
            milady = miladys[i];
            pool = IMiladyRescue(MILADY_RESCUE).getPoolFromMilady(milady);
            
            if (pool.code.length != 0) {
                if (IMiladyPool(pool).redeemed()) {
                    if (!IMiladyPool(pool).listingSold()) {
                        ethPrice = IMiladyPool(pool).ethPrice();
                        
                        if (ethPrice < lowestPrice) {
                            lowestPrice = ethPrice;
                            cheapest = MiladyRescue({
                                milady: milady,
                                pool: pool,
                                ethPrice: ethPrice
                            });
                            found = true;
                        }
                    }
                }
            }
        }
        
        require(found, 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) {
        uint256[] memory miladys = INFTXV2Vault(MILADY_VAULT).allHoldings();
        
        uint256 milady;
        address pool;
        uint256 ethPrice;
        uint256 highestPrice;
        bool found;
        
        for (uint256 i; i != miladys.length; ++i) {
            milady = miladys[i];
            pool = IMiladyRescue(MILADY_RESCUE).getPoolFromMilady(milady);
            
            if (pool.code.length != 0) {
                if (IMiladyPool(pool).redeemed()) {
                    if (!IMiladyPool(pool).listingSold()) {
                        ethPrice = IMiladyPool(pool).ethPrice();
                        
                        if (ethPrice > highestPrice) {
                            highestPrice = ethPrice;
                            mostExpensive = MiladyRescue({
                                milady: milady,
                                pool: pool,
                                ethPrice: ethPrice
                            });
                            found = true;
                        }
                    }
                }
            }
        }
        
        require(found, 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]
            );
        }
    }
}

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 INFTXV2Vault {
    function allHoldings() external view returns (uint256[] memory);
}

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

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

Tags:
addr:0xdd566a1fc7c7c802afb942e759fc4dcbd68a59e2|verified:true|block:23497991|tx:0x4df0415fb52f000148017a2b6cc3099ce69a9ba46304d07aca4d89f514b4f3c5|first_check:1759507028

Submitted on: 2025-10-03 17:57:09

Comments

Log in to comment.

No comments yet.