zellic-audit
Initial commit
f998fcd
raw
history blame
21.4 kB
{
"language": "Solidity",
"sources": {
"/contracts/libs/SlotsBitmapLibrary.sol": {
"content": "// SPDX-License-Identifier: AGPLv3\npragma solidity 0.8.16;\n\nimport {ISuperfluidToken} from \"../interfaces/superfluid/ISuperfluidToken.sol\";\n\n/**\n * @title Slots Bitmap library\n * @author Superfluid\n * @dev A library implements slots bitmap on Superfluid Token storage\n * NOTE:\n * - A slots bitmap allows you to iterate through a list of data efficiently.\n * - A data slot can be enabled or disabled with the help of bitmap.\n * - MAX_NUM_SLOTS is 256 in this implementation (using one uint256)\n * - Superfluid token storage usage:\n * - getAgreementStateSlot(bitmapStateSlotId) stores the bitmap of enabled data slots\n * - getAgreementStateSlot(dataStateSlotIDStart + stotId) stores the data of the slot\n */\nlibrary SlotsBitmapLibrary {\n\n uint32 internal constant _MAX_NUM_SLOTS = 256;\n\n function findEmptySlotAndFill(\n ISuperfluidToken token,\n address account,\n uint256 bitmapStateSlotId,\n uint256 dataStateSlotIDStart,\n bytes32 data\n )\n public\n returns (uint32 slotId)\n {\n uint256 subsBitmap = uint256(token.getAgreementStateSlot(\n address(this),\n account,\n bitmapStateSlotId, 1)[0]);\n for (slotId = 0; slotId < _MAX_NUM_SLOTS; ++slotId) {\n if ((uint256(subsBitmap >> slotId) & 1) == 0) {\n // update slot data\n bytes32[] memory slotData = new bytes32[](1);\n slotData[0] = data;\n token.updateAgreementStateSlot(\n account,\n dataStateSlotIDStart + slotId,\n slotData);\n // update slot map\n slotData[0] = bytes32(subsBitmap | (1 << uint256(slotId)));\n token.updateAgreementStateSlot(\n account,\n bitmapStateSlotId,\n slotData);\n // update the slots\n break;\n }\n }\n require(slotId < _MAX_NUM_SLOTS, \"SlotBitmap out of bound\");\n }\n\n function clearSlot(\n ISuperfluidToken token,\n address account,\n uint256 bitmapStateSlotId,\n uint32 slotId\n )\n public\n {\n uint256 subsBitmap = uint256(token.getAgreementStateSlot(\n address(this),\n account,\n bitmapStateSlotId, 1)[0]);\n bytes32[] memory slotData = new bytes32[](1);\n // [SECURITY] NOTE: We do not allow clearing of nonexistent slots\n assert(subsBitmap & (1 << uint256(slotId)) != 0);\n slotData[0] = bytes32(subsBitmap & ~(1 << uint256(slotId)));\n // zero the data\n token.updateAgreementStateSlot(\n account,\n bitmapStateSlotId,\n slotData);\n }\n\n function listData(\n ISuperfluidToken token,\n address account,\n uint256 bitmapStateSlotId,\n uint256 dataStateSlotIDStart\n )\n public view\n returns (\n uint32[] memory slotIds,\n bytes32[] memory dataList)\n {\n uint256 subsBitmap = uint256(token.getAgreementStateSlot(\n address(this),\n account,\n bitmapStateSlotId, 1)[0]);\n\n slotIds = new uint32[](_MAX_NUM_SLOTS);\n dataList = new bytes32[](_MAX_NUM_SLOTS);\n // read all slots\n uint nSlots;\n for (uint32 slotId = 0; slotId < _MAX_NUM_SLOTS; ++slotId) {\n if ((uint256(subsBitmap >> slotId) & 1) == 0) continue;\n slotIds[nSlots] = slotId;\n dataList[nSlots] = token.getAgreementStateSlot(\n address(this),\n account,\n dataStateSlotIDStart + slotId, 1)[0];\n ++nSlots;\n }\n // resize memory arrays\n assembly {\n mstore(slotIds, nSlots)\n mstore(dataList, nSlots)\n }\n }\n}\n"
},
"/contracts/interfaces/superfluid/ISuperfluidToken.sol": {
"content": "// SPDX-License-Identifier: AGPLv3\npragma solidity >= 0.8.4;\n\nimport { ISuperAgreement } from \"./ISuperAgreement.sol\";\n\n/**\n * @title Superfluid token interface\n * @author Superfluid\n */\ninterface ISuperfluidToken {\n\n /**************************************************************************\n * Errors\n *************************************************************************/\n error SF_TOKEN_AGREEMENT_ALREADY_EXISTS(); // 0xf05521f6\n error SF_TOKEN_AGREEMENT_DOES_NOT_EXIST(); // 0xdae18809\n error SF_TOKEN_BURN_INSUFFICIENT_BALANCE(); // 0x10ecdf44\n error SF_TOKEN_MOVE_INSUFFICIENT_BALANCE(); // 0x2f4cb941\n error SF_TOKEN_ONLY_LISTED_AGREEMENT(); // 0xc9ff6644\n error SF_TOKEN_ONLY_HOST(); // 0xc51efddd\n\n /**************************************************************************\n * Basic information\n *************************************************************************/\n\n /**\n * @dev Get superfluid host contract address\n */\n function getHost() external view returns(address host);\n\n /**\n * @dev Encoded liquidation type data mainly used for handling stack to deep errors\n *\n * @custom:note \n * - version: 1\n * - liquidationType key:\n * - 0 = reward account receives reward (PIC period)\n * - 1 = liquidator account receives reward (Pleb period)\n * - 2 = liquidator account receives reward (Pirate period/bailout)\n */\n struct LiquidationTypeData {\n uint256 version;\n uint8 liquidationType;\n }\n\n /**************************************************************************\n * Real-time balance functions\n *************************************************************************/\n\n /**\n * @dev Calculate the real balance of a user, taking in consideration all agreements of the account\n * @param account for the query\n * @param timestamp Time of balance\n * @return availableBalance Real-time balance\n * @return deposit Account deposit\n * @return owedDeposit Account owed Deposit\n */\n function realtimeBalanceOf(\n address account,\n uint256 timestamp\n )\n external view\n returns (\n int256 availableBalance,\n uint256 deposit,\n uint256 owedDeposit);\n\n /**\n * @notice Calculate the realtime balance given the current host.getNow() value\n * @dev realtimeBalanceOf with timestamp equals to block timestamp\n * @param account for the query\n * @return availableBalance Real-time balance\n * @return deposit Account deposit\n * @return owedDeposit Account owed Deposit\n */\n function realtimeBalanceOfNow(\n address account\n )\n external view\n returns (\n int256 availableBalance,\n uint256 deposit,\n uint256 owedDeposit,\n uint256 timestamp);\n\n /**\n * @notice Check if account is critical\n * @dev A critical account is when availableBalance < 0\n * @param account The account to check\n * @param timestamp The time we'd like to check if the account is critical (should use future)\n * @return isCritical Whether the account is critical\n */\n function isAccountCritical(\n address account,\n uint256 timestamp\n )\n external view\n returns(bool isCritical);\n\n /**\n * @notice Check if account is critical now (current host.getNow())\n * @dev A critical account is when availableBalance < 0\n * @param account The account to check\n * @return isCritical Whether the account is critical\n */\n function isAccountCriticalNow(\n address account\n )\n external view\n returns(bool isCritical);\n\n /**\n * @notice Check if account is solvent\n * @dev An account is insolvent when the sum of deposits for a token can't cover the negative availableBalance\n * @param account The account to check\n * @param timestamp The time we'd like to check if the account is solvent (should use future)\n * @return isSolvent True if the account is solvent, false otherwise\n */\n function isAccountSolvent(\n address account,\n uint256 timestamp\n )\n external view\n returns(bool isSolvent);\n\n /**\n * @notice Check if account is solvent now\n * @dev An account is insolvent when the sum of deposits for a token can't cover the negative availableBalance\n * @param account The account to check\n * @return isSolvent True if the account is solvent, false otherwise\n */\n function isAccountSolventNow(\n address account\n )\n external view\n returns(bool isSolvent);\n\n /**\n * @notice Get a list of agreements that is active for the account\n * @dev An active agreement is one that has state for the account\n * @param account Account to query\n * @return activeAgreements List of accounts that have non-zero states for the account\n */\n function getAccountActiveAgreements(address account)\n external view\n returns(ISuperAgreement[] memory activeAgreements);\n\n\n /**************************************************************************\n * Super Agreement hosting functions\n *************************************************************************/\n\n /**\n * @dev Create a new agreement\n * @param id Agreement ID\n * @param data Agreement data\n */\n function createAgreement(\n bytes32 id,\n bytes32[] calldata data\n )\n external;\n /**\n * @dev Agreement created event\n * @param agreementClass Contract address of the agreement\n * @param id Agreement ID\n * @param data Agreement data\n */\n event AgreementCreated(\n address indexed agreementClass,\n bytes32 id,\n bytes32[] data\n );\n\n /**\n * @dev Get data of the agreement\n * @param agreementClass Contract address of the agreement\n * @param id Agreement ID\n * @return data Data of the agreement\n */\n function getAgreementData(\n address agreementClass,\n bytes32 id,\n uint dataLength\n )\n external view\n returns(bytes32[] memory data);\n\n /**\n * @dev Create a new agreement\n * @param id Agreement ID\n * @param data Agreement data\n */\n function updateAgreementData(\n bytes32 id,\n bytes32[] calldata data\n )\n external;\n /**\n * @dev Agreement updated event\n * @param agreementClass Contract address of the agreement\n * @param id Agreement ID\n * @param data Agreement data\n */\n event AgreementUpdated(\n address indexed agreementClass,\n bytes32 id,\n bytes32[] data\n );\n\n /**\n * @dev Close the agreement\n * @param id Agreement ID\n */\n function terminateAgreement(\n bytes32 id,\n uint dataLength\n )\n external;\n /**\n * @dev Agreement terminated event\n * @param agreementClass Contract address of the agreement\n * @param id Agreement ID\n */\n event AgreementTerminated(\n address indexed agreementClass,\n bytes32 id\n );\n\n /**\n * @dev Update agreement state slot\n * @param account Account to be updated\n *\n * @custom:note \n * - To clear the storage out, provide zero-ed array of intended length\n */\n function updateAgreementStateSlot(\n address account,\n uint256 slotId,\n bytes32[] calldata slotData\n )\n external;\n /**\n * @dev Agreement account state updated event\n * @param agreementClass Contract address of the agreement\n * @param account Account updated\n * @param slotId slot id of the agreement state\n */\n event AgreementStateUpdated(\n address indexed agreementClass,\n address indexed account,\n uint256 slotId\n );\n\n /**\n * @dev Get data of the slot of the state of an agreement\n * @param agreementClass Contract address of the agreement\n * @param account Account to query\n * @param slotId slot id of the state\n * @param dataLength length of the state data\n */\n function getAgreementStateSlot(\n address agreementClass,\n address account,\n uint256 slotId,\n uint dataLength\n )\n external view\n returns (bytes32[] memory slotData);\n\n /**\n * @notice Settle balance from an account by the agreement\n * @dev The agreement needs to make sure that the balance delta is balanced afterwards\n * @param account Account to query.\n * @param delta Amount of balance delta to be settled\n *\n * @custom:modifiers \n * - onlyAgreement\n */\n function settleBalance(\n address account,\n int256 delta\n )\n external;\n\n /**\n * @dev Make liquidation payouts (v2)\n * @param id Agreement ID\n * @param liquidationTypeData Data regarding the version of the liquidation schema and the type\n * @param liquidatorAccount Address of the executor of the liquidation\n * @param useDefaultRewardAccount Whether or not the default reward account receives the rewardAmount\n * @param targetAccount Account to be liquidated\n * @param rewardAmount The amount the rewarded account will receive\n * @param targetAccountBalanceDelta The delta amount the target account balance should change by\n *\n * @custom:note \n * - If a bailout is required (bailoutAmount > 0)\n * - the actual reward (single deposit) goes to the executor,\n * - while the reward account becomes the bailout account\n * - total bailout include: bailout amount + reward amount\n * - the targetAccount will be bailed out\n * - If a bailout is not required\n * - the targetAccount will pay the rewardAmount\n * - the liquidator (reward account in PIC period) will receive the rewardAmount\n *\n * @custom:modifiers \n * - onlyAgreement\n */\n function makeLiquidationPayoutsV2\n (\n bytes32 id,\n bytes memory liquidationTypeData,\n address liquidatorAccount,\n bool useDefaultRewardAccount,\n address targetAccount,\n uint256 rewardAmount,\n int256 targetAccountBalanceDelta\n ) external;\n /**\n * @dev Agreement liquidation event v2 (including agent account)\n * @param agreementClass Contract address of the agreement\n * @param id Agreement ID\n * @param liquidatorAccount Address of the executor of the liquidation\n * @param targetAccount Account of the stream sender\n * @param rewardAmountReceiver Account that collects the reward or bails out insolvent accounts\n * @param rewardAmount The amount the reward recipient account balance should change by\n * @param targetAccountBalanceDelta The amount the sender account balance should change by\n * @param liquidationTypeData The encoded liquidation type data including the version (how to decode)\n *\n * @custom:note \n * Reward account rule:\n * - if the agreement is liquidated during the PIC period\n * - the rewardAmountReceiver will get the rewardAmount (remaining deposit), regardless of the liquidatorAccount\n * - the targetAccount will pay for the rewardAmount\n * - if the agreement is liquidated after the PIC period AND the targetAccount is solvent\n * - the rewardAmountReceiver will get the rewardAmount (remaining deposit)\n * - the targetAccount will pay for the rewardAmount\n * - if the targetAccount is insolvent\n * - the liquidatorAccount will get the rewardAmount (single deposit)\n * - the default reward account (governance) will pay for both the rewardAmount and bailoutAmount\n * - the targetAccount will receive the bailoutAmount\n */\n event AgreementLiquidatedV2(\n address indexed agreementClass,\n bytes32 id,\n address indexed liquidatorAccount,\n address indexed targetAccount,\n address rewardAmountReceiver,\n uint256 rewardAmount,\n int256 targetAccountBalanceDelta,\n bytes liquidationTypeData\n );\n\n /**************************************************************************\n * Function modifiers for access control and parameter validations\n *\n * While they cannot be explicitly stated in function definitions, they are\n * listed in function definition comments instead for clarity.\n *\n * NOTE: solidity-coverage not supporting it\n *************************************************************************/\n\n /// @dev The msg.sender must be host contract\n //modifier onlyHost() virtual;\n\n /// @dev The msg.sender must be a listed agreement.\n //modifier onlyAgreement() virtual;\n\n /**************************************************************************\n * DEPRECATED\n *************************************************************************/\n\n /**\n * @dev Agreement liquidation event (DEPRECATED BY AgreementLiquidatedBy)\n * @param agreementClass Contract address of the agreement\n * @param id Agreement ID\n * @param penaltyAccount Account of the agreement to be penalized\n * @param rewardAccount Account that collect the reward\n * @param rewardAmount Amount of liquidation reward\n *\n * @custom:deprecated Use AgreementLiquidatedV2 instead\n */\n event AgreementLiquidated(\n address indexed agreementClass,\n bytes32 id,\n address indexed penaltyAccount,\n address indexed rewardAccount,\n uint256 rewardAmount\n );\n\n /**\n * @dev System bailout occurred (DEPRECATED BY AgreementLiquidatedBy)\n * @param bailoutAccount Account that bailout the penalty account\n * @param bailoutAmount Amount of account bailout\n *\n * @custom:deprecated Use AgreementLiquidatedV2 instead\n */\n event Bailout(\n address indexed bailoutAccount,\n uint256 bailoutAmount\n );\n\n /**\n * @dev Agreement liquidation event (DEPRECATED BY AgreementLiquidatedV2)\n * @param liquidatorAccount Account of the agent that performed the liquidation.\n * @param agreementClass Contract address of the agreement\n * @param id Agreement ID\n * @param penaltyAccount Account of the agreement to be penalized\n * @param bondAccount Account that collect the reward or bailout accounts\n * @param rewardAmount Amount of liquidation reward\n * @param bailoutAmount Amount of liquidation bailouot\n *\n * @custom:deprecated Use AgreementLiquidatedV2 instead\n *\n * @custom:note \n * Reward account rule:\n * - if bailout is equal to 0, then\n * - the bondAccount will get the rewardAmount,\n * - the penaltyAccount will pay for the rewardAmount.\n * - if bailout is larger than 0, then\n * - the liquidatorAccount will get the rewardAmouont,\n * - the bondAccount will pay for both the rewardAmount and bailoutAmount,\n * - the penaltyAccount will pay for the rewardAmount while get the bailoutAmount.\n */\n event AgreementLiquidatedBy(\n address liquidatorAccount,\n address indexed agreementClass,\n bytes32 id,\n address indexed penaltyAccount,\n address indexed bondAccount,\n uint256 rewardAmount,\n uint256 bailoutAmount\n );\n}\n"
},
"/contracts/interfaces/superfluid/ISuperAgreement.sol": {
"content": "// SPDX-License-Identifier: AGPLv3\npragma solidity >= 0.8.4;\n\nimport { ISuperfluidToken } from \"./ISuperfluidToken.sol\";\n\n/**\n * @title Super agreement interface\n * @author Superfluid\n */\ninterface ISuperAgreement {\n\n /**\n * @dev Get the type of the agreement class\n */\n function agreementType() external view returns (bytes32);\n\n /**\n * @dev Calculate the real-time balance for the account of this agreement class\n * @param account Account the state belongs to\n * @param time Time used for the calculation\n * @return dynamicBalance Dynamic balance portion of real-time balance of this agreement\n * @return deposit Account deposit amount of this agreement\n * @return owedDeposit Account owed deposit amount of this agreement\n */\n function realtimeBalanceOf(\n ISuperfluidToken token,\n address account,\n uint256 time\n )\n external\n view\n returns (\n int256 dynamicBalance,\n uint256 deposit,\n uint256 owedDeposit\n );\n\n}\n"
}
},
"settings": {
"remappings": [],
"optimizer": {
"enabled": true,
"runs": 200
},
"evmVersion": "london",
"libraries": {},
"outputSelection": {
"*": {
"*": [
"evm.bytecode",
"evm.deployedBytecode",
"devdoc",
"userdoc",
"metadata",
"abi"
]
}
}
}
}