{
    "Function": "buy",
    "File": "contracts/Witch.sol",
    "Parent Contracts": [
        "contracts/utils/access/AccessControl.sol"
    ],
    "High-Level Calls": [
        "WDiv",
        "WMul",
        "ILadle",
        "WMul",
        "CastU256U128",
        "WDivUp",
        "WDiv",
        "ICauldron",
        "WDiv",
        "ICauldron",
        "ICauldron"
    ],
    "Internal Calls": [
        "require(bool,string)",
        "require(bool,string)"
    ],
    "Library Calls": [
        "wdivup",
        "u128",
        "wmul",
        "wdiv",
        "wmul",
        "wdiv",
        "wdiv"
    ],
    "Low-Level Calls": [],
    "Code": "function buy(bytes12 vaultId, uint128 art, uint128 min) public {\n        DataTypes.Balances memory balances_ = cauldron.balances(vaultId);\n\n        require (balances_.art > 0, \"Nothing to buy\");                                      // Cheapest way of failing gracefully if given a non existing vault\n        uint256 elapsed = uint32(block.timestamp) - cauldron.auctions(vaultId);           // Auctions will malfunction on the 7th of February 2106, at 06:28:16 GMT, we should replace this contract before then.\n        uint256 price;\n        {\n            // Price of a collateral unit, in underlying, at the present moment, for a given vault\n            //\n            //                ink                     min(auction, elapsed)\n            // price = 1 / (------- * (p + (1 - p) * -----------------------))\n            //                art                          auction\n            (uint256 auctionTime_, uint256 initialProportion_) = (auctionTime, initialProportion);\n            uint256 term1 = uint256(balances_.ink).wdiv(balances_.art);\n            uint256 dividend2 = auctionTime_ < elapsed ? auctionTime_ : elapsed;\n            uint256 divisor2 = auctionTime_;\n            uint256 term2 = initialProportion_ + (1e18 - initialProportion_).wmul(dividend2.wdiv(divisor2));\n            price = uint256(1e18).wdiv(term1.wmul(term2));\n        }\n        uint256 ink = uint256(art).wdivup(price);                                                    // Calculate collateral to sell. Using divdrup stops rounding from leaving 1 stray wei in vaults.\n        require (ink >= min, \"Not enough bought\");\n\n        ladle.settle(vaultId, msg.sender, ink.u128(), art);                                        // Move the assets\n        if (balances_.art - art == 0) {                                                             // If there is no debt left, return the vault with the collateral to the owner\n            cauldron.give(vaultId, vaultOwners[vaultId]);\n            delete vaultOwners[vaultId];\n        }\n\n        emit Bought(vaultId, msg.sender, ink, art);\n    }"
}