{
    "Function": "withdraw",
    "File": "contracts/MerkleResistor.sol",
    "Parent Contracts": [],
    "High-Level Calls": [
        "IERC20"
    ],
    "Internal Calls": [
        "require(bool,string)",
        "require(bool,string)",
        "require(bool,string)"
    ],
    "Library Calls": [],
    "Low-Level Calls": [],
    "Code": "function withdraw(uint treeIndex, address destination) public {\n        // initialize first, no operations on empty structs, I don't care if the values are \"probably zero\"\n        require(initialized[destination][treeIndex], \"You must initialize your account first.\");\n        // storage, since we are editing\n        Tranche storage tranche = tranches[destination][treeIndex];\n        // if it's empty, don't bother\n        require(tranche.currentCoins >  0, 'No coins left to withdraw');\n        uint currentWithdrawal = 0;\n\n        // if after vesting period ends, give them the remaining coins, also avoids dust from rounding errors\n        if (block.timestamp >= tranche.endTime) {\n            currentWithdrawal = tranche.currentCoins;\n        } else {\n            // compute allowed withdrawal\n            // secondsElapsedSinceLastWithdrawal * coinsPerSecond == coinsAccumulatedSinceLastWithdrawal\n            currentWithdrawal = (block.timestamp - tranche.lastWithdrawalTime) * tranche.coinsPerSecond;\n        }\n        // muto? servo\n        MerkleTree storage tree = merkleTrees[treeIndex];\n\n        // update struct, modern solidity will catch underflow and prevent currentWithdrawal from exceeding currentCoins\n        // but it's computed internally anyway, not user generated\n        tranche.currentCoins -= currentWithdrawal;\n        // move the time counter up so users can't double-withdraw allocated coins\n        // this also works as a re-entrance gate, so currentWithdrawal would be 0 upon re-entrance\n        tranche.lastWithdrawalTime = block.timestamp;\n        // handle the bookkeeping so trees don't share tokens, do it before transferring to create one more re-entrance gate\n        tree.tokenBalance -= currentWithdrawal;\n\n        // transfer the tokens, brah\n        // NOTE: if this is a malicious token, what could happen?\n        // 1/ token doesn't transfer given amount to recipient, this is bad for user, but does not effect other trees\n        // 2/ token fails for some reason, again bad for user, but this does not effect other trees\n        // 3/ token re-enters this function (or other, but this is the only one that transfers tokens out)\n        // in which case, lastWithdrawalTime == block.timestamp, so currentWithdrawal == 0\n        require(IERC20(tree.tokenAddress).transfer(destination, currentWithdrawal), 'Token transfer failed');\n        emit WithdrawalOccurred(treeIndex, destination, currentWithdrawal, tranche.currentCoins);\n    }"
}