@venusprotocol/protocol-reserve 1.3.0 → 1.4.0-dev.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (311) hide show
  1. package/README.md +6 -0
  2. package/artifacts/@openzeppelin/contracts/access/AccessControl.sol/AccessControl.dbg.json +4 -0
  3. package/artifacts/@openzeppelin/contracts/access/AccessControl.sol/AccessControl.json +215 -0
  4. package/artifacts/@openzeppelin/contracts/access/IAccessControl.sol/IAccessControl.dbg.json +1 -1
  5. package/artifacts/@openzeppelin/contracts/access/Ownable.sol/Ownable.dbg.json +4 -0
  6. package/artifacts/@openzeppelin/contracts/access/Ownable.sol/Ownable.json +63 -0
  7. package/artifacts/@openzeppelin/contracts/interfaces/IERC1967.sol/IERC1967.dbg.json +4 -0
  8. package/artifacts/@openzeppelin/contracts/interfaces/IERC1967.sol/IERC1967.json +56 -0
  9. package/artifacts/@openzeppelin/contracts/interfaces/draft-IERC1822.sol/IERC1822Proxiable.dbg.json +4 -0
  10. package/artifacts/@openzeppelin/contracts/interfaces/draft-IERC1822.sol/IERC1822Proxiable.json +24 -0
  11. package/artifacts/@openzeppelin/contracts/proxy/ERC1967/ERC1967Upgrade.sol/ERC1967Upgrade.dbg.json +4 -0
  12. package/artifacts/@openzeppelin/contracts/proxy/ERC1967/ERC1967Upgrade.sol/ERC1967Upgrade.json +56 -0
  13. package/artifacts/@openzeppelin/contracts/proxy/Proxy.sol/Proxy.dbg.json +4 -0
  14. package/artifacts/@openzeppelin/contracts/proxy/Proxy.sol/Proxy.json +19 -0
  15. package/artifacts/@openzeppelin/contracts/proxy/beacon/BeaconProxy.sol/BeaconProxy.dbg.json +4 -0
  16. package/artifacts/@openzeppelin/contracts/proxy/beacon/BeaconProxy.sol/BeaconProxy.json +80 -0
  17. package/artifacts/@openzeppelin/contracts/proxy/beacon/IBeacon.sol/IBeacon.dbg.json +4 -0
  18. package/artifacts/{contracts/Interfaces/ComptrollerInterface.sol/ComptrollerInterface.json → @openzeppelin/contracts/proxy/beacon/IBeacon.sol/IBeacon.json} +5 -5
  19. package/artifacts/@openzeppelin/contracts/proxy/beacon/UpgradeableBeacon.sol/UpgradeableBeacon.dbg.json +4 -0
  20. package/artifacts/@openzeppelin/contracts/proxy/beacon/UpgradeableBeacon.sol/UpgradeableBeacon.json +113 -0
  21. package/artifacts/@openzeppelin/contracts/token/ERC20/ERC20.sol/ERC20.dbg.json +1 -1
  22. package/artifacts/@openzeppelin/contracts/token/ERC20/IERC20.sol/IERC20.dbg.json +1 -1
  23. package/artifacts/@openzeppelin/contracts/token/ERC20/extensions/IERC20Metadata.sol/IERC20Metadata.dbg.json +1 -1
  24. package/artifacts/@openzeppelin/contracts/utils/Address.sol/Address.dbg.json +4 -0
  25. package/artifacts/@openzeppelin/contracts/utils/Address.sol/Address.json +10 -0
  26. package/artifacts/@openzeppelin/contracts/utils/Context.sol/Context.dbg.json +1 -1
  27. package/artifacts/@openzeppelin/contracts/utils/StorageSlot.sol/StorageSlot.dbg.json +4 -0
  28. package/artifacts/@openzeppelin/contracts/utils/StorageSlot.sol/StorageSlot.json +10 -0
  29. package/artifacts/@openzeppelin/contracts/utils/Strings.sol/Strings.dbg.json +4 -0
  30. package/artifacts/@openzeppelin/contracts/utils/Strings.sol/Strings.json +10 -0
  31. package/artifacts/@openzeppelin/contracts/utils/introspection/ERC165.sol/ERC165.dbg.json +4 -0
  32. package/artifacts/@openzeppelin/contracts/utils/introspection/ERC165.sol/ERC165.json +30 -0
  33. package/artifacts/@openzeppelin/contracts/utils/introspection/IERC165.sol/IERC165.dbg.json +4 -0
  34. package/artifacts/@openzeppelin/contracts/utils/introspection/IERC165.sol/IERC165.json +30 -0
  35. package/artifacts/@openzeppelin/contracts/utils/math/Math.sol/Math.dbg.json +4 -0
  36. package/artifacts/@openzeppelin/contracts/utils/math/Math.sol/Math.json +10 -0
  37. package/artifacts/@openzeppelin/contracts/utils/math/SignedMath.sol/SignedMath.dbg.json +4 -0
  38. package/artifacts/@openzeppelin/contracts/utils/math/SignedMath.sol/SignedMath.json +10 -0
  39. package/artifacts/@openzeppelin/contracts-upgradeable/access/Ownable2StepUpgradeable.sol/Ownable2StepUpgradeable.dbg.json +1 -1
  40. package/artifacts/@openzeppelin/contracts-upgradeable/access/OwnableUpgradeable.sol/OwnableUpgradeable.dbg.json +1 -1
  41. package/artifacts/@openzeppelin/contracts-upgradeable/proxy/utils/Initializable.sol/Initializable.dbg.json +1 -1
  42. package/artifacts/@openzeppelin/contracts-upgradeable/security/PausableUpgradeable.sol/PausableUpgradeable.dbg.json +4 -0
  43. package/artifacts/@openzeppelin/contracts-upgradeable/security/PausableUpgradeable.sol/PausableUpgradeable.json +63 -0
  44. package/artifacts/@openzeppelin/contracts-upgradeable/security/ReentrancyGuardUpgradeable.sol/ReentrancyGuardUpgradeable.dbg.json +1 -1
  45. package/artifacts/@openzeppelin/contracts-upgradeable/token/ERC20/IERC20Upgradeable.sol/IERC20Upgradeable.dbg.json +1 -1
  46. package/artifacts/@openzeppelin/contracts-upgradeable/token/ERC20/extensions/IERC20PermitUpgradeable.sol/IERC20PermitUpgradeable.dbg.json +1 -1
  47. package/artifacts/@openzeppelin/contracts-upgradeable/token/ERC20/utils/SafeERC20Upgradeable.sol/SafeERC20Upgradeable.dbg.json +1 -1
  48. package/artifacts/@openzeppelin/contracts-upgradeable/utils/AddressUpgradeable.sol/AddressUpgradeable.dbg.json +1 -1
  49. package/artifacts/@openzeppelin/contracts-upgradeable/utils/ContextUpgradeable.sol/ContextUpgradeable.dbg.json +1 -1
  50. package/artifacts/@venusprotocol/governance-contracts/contracts/Governance/AccessControlledV8.sol/AccessControlledV8.dbg.json +1 -1
  51. package/artifacts/@venusprotocol/governance-contracts/contracts/Governance/IAccessControlManagerV8.sol/IAccessControlManagerV8.dbg.json +1 -1
  52. package/artifacts/@venusprotocol/oracle/contracts/ResilientOracle.sol/ResilientOracle.dbg.json +4 -0
  53. package/artifacts/@venusprotocol/oracle/contracts/ResilientOracle.sol/ResilientOracle.json +649 -0
  54. package/artifacts/@venusprotocol/oracle/contracts/interfaces/OracleInterface.sol/BoundValidatorInterface.dbg.json +4 -0
  55. package/artifacts/@venusprotocol/oracle/contracts/interfaces/OracleInterface.sol/BoundValidatorInterface.json +40 -0
  56. package/artifacts/@venusprotocol/oracle/contracts/interfaces/OracleInterface.sol/OracleInterface.dbg.json +4 -0
  57. package/artifacts/@venusprotocol/oracle/contracts/interfaces/OracleInterface.sol/OracleInterface.json +30 -0
  58. package/artifacts/@venusprotocol/oracle/contracts/interfaces/OracleInterface.sol/ResilientOracleInterface.dbg.json +4 -0
  59. package/artifacts/@venusprotocol/oracle/contracts/interfaces/OracleInterface.sol/ResilientOracleInterface.json +75 -0
  60. package/artifacts/@venusprotocol/oracle/contracts/interfaces/OracleInterface.sol/TwapInterface.dbg.json +4 -0
  61. package/artifacts/@venusprotocol/oracle/contracts/interfaces/OracleInterface.sol/TwapInterface.json +49 -0
  62. package/artifacts/@venusprotocol/oracle/contracts/interfaces/VBep20Interface.sol/VBep20Interface.dbg.json +4 -0
  63. package/artifacts/@venusprotocol/oracle/contracts/interfaces/VBep20Interface.sol/VBep20Interface.json +246 -0
  64. package/artifacts/@venusprotocol/solidity-utilities/contracts/MaxLoopsLimitHelper.sol/MaxLoopsLimitHelper.dbg.json +1 -1
  65. package/artifacts/build-info/e6962244d9988e3e4bb55e591cf3b5d8.json +1 -0
  66. package/artifacts/contracts/Interfaces/IComptroller.sol/IComptroller.dbg.json +4 -0
  67. package/artifacts/contracts/Interfaces/IComptroller.sol/IComptroller.json +56 -0
  68. package/artifacts/contracts/Interfaces/IConverterNetwork.sol/IConverterNetwork.dbg.json +4 -0
  69. package/artifacts/contracts/Interfaces/IConverterNetwork.sol/IConverterNetwork.json +127 -0
  70. package/artifacts/contracts/Interfaces/IIncomeDestination.sol/IIncomeDestination.dbg.json +1 -1
  71. package/artifacts/contracts/Interfaces/IPoolRegistry.sol/IPoolRegistry.dbg.json +4 -0
  72. package/artifacts/contracts/Interfaces/IPoolRegistry.sol/IPoolRegistry.json +54 -0
  73. package/artifacts/contracts/Interfaces/IProtocolShareReserve.sol/IProtocolShareReserve.dbg.json +1 -1
  74. package/artifacts/contracts/Interfaces/IRiskFund.sol/IRiskFund.dbg.json +4 -0
  75. package/artifacts/contracts/Interfaces/IRiskFund.sol/IRiskFund.json +77 -0
  76. package/artifacts/contracts/Interfaces/IRiskFund.sol/IRiskFundGetters.dbg.json +4 -0
  77. package/artifacts/contracts/Interfaces/IRiskFund.sol/IRiskFundGetters.json +24 -0
  78. package/artifacts/contracts/Interfaces/IRiskFundConverter.sol/IRiskFundConverter.dbg.json +4 -0
  79. package/artifacts/contracts/Interfaces/{PoolRegistryInterface.sol/PoolRegistryInterface.json → IRiskFundConverter.sol/IRiskFundConverter.json} +23 -10
  80. package/artifacts/contracts/Interfaces/IShortfall.sol/IShortfall.dbg.json +4 -0
  81. package/artifacts/contracts/Interfaces/IShortfall.sol/IShortfall.json +24 -0
  82. package/artifacts/contracts/Interfaces/IVToken.sol/IVToken.dbg.json +1 -1
  83. package/artifacts/contracts/Interfaces/IXVSVault.sol/IXVSVault.dbg.json +4 -0
  84. package/artifacts/contracts/Interfaces/IXVSVault.sol/IXVSVault.json +24 -0
  85. package/artifacts/contracts/ProtocolReserve/ProtocolShareReserve.sol/ProtocolShareReserve.dbg.json +1 -1
  86. package/artifacts/contracts/ProtocolReserve/ProtocolShareReserve.sol/ProtocolShareReserve.json +7 -2
  87. package/artifacts/contracts/ProtocolReserve/RiskFundStorage.sol/MaxLoopsLimitHelpersStorage.dbg.json +4 -0
  88. package/artifacts/contracts/ProtocolReserve/RiskFundStorage.sol/MaxLoopsLimitHelpersStorage.json +24 -0
  89. package/artifacts/contracts/ProtocolReserve/RiskFundStorage.sol/ReserveHelpersStorage.dbg.json +4 -0
  90. package/artifacts/contracts/ProtocolReserve/RiskFundStorage.sol/ReserveHelpersStorage.json +139 -0
  91. package/artifacts/contracts/ProtocolReserve/RiskFundStorage.sol/RiskFundV1Storage.dbg.json +4 -0
  92. package/artifacts/contracts/ProtocolReserve/RiskFundStorage.sol/RiskFundV1Storage.json +178 -0
  93. package/artifacts/contracts/ProtocolReserve/RiskFundStorage.sol/RiskFundV2Storage.dbg.json +4 -0
  94. package/artifacts/contracts/ProtocolReserve/RiskFundStorage.sol/RiskFundV2Storage.json +191 -0
  95. package/artifacts/contracts/ProtocolReserve/RiskFundV2.sol/RiskFundV2.dbg.json +4 -0
  96. package/artifacts/contracts/ProtocolReserve/RiskFundV2.sol/RiskFundV2.json +630 -0
  97. package/artifacts/contracts/ProtocolReserve/XVSVaultTreasury.sol/XVSVaultTreasury.dbg.json +4 -0
  98. package/artifacts/contracts/ProtocolReserve/XVSVaultTreasury.sol/XVSVaultTreasury.json +315 -0
  99. package/artifacts/contracts/Test/Mocks/MockACM.sol/MockACM.dbg.json +4 -0
  100. package/artifacts/contracts/Test/Mocks/MockACM.sol/MockACM.json +369 -0
  101. package/artifacts/contracts/Test/Mocks/MockArraySorter.sol/MockArraySorter.dbg.json +4 -0
  102. package/artifacts/contracts/Test/Mocks/MockArraySorter.sol/MockArraySorter.json +40 -0
  103. package/artifacts/contracts/Test/Mocks/MockConverter.sol/MockConverter.dbg.json +4 -0
  104. package/artifacts/contracts/Test/Mocks/MockConverter.sol/MockConverter.json +1395 -0
  105. package/artifacts/contracts/Test/Mocks/MockDeflationaryToken.sol/MockDeflatingToken.dbg.json +4 -0
  106. package/artifacts/contracts/Test/Mocks/MockDeflationaryToken.sol/MockDeflatingToken.json +332 -0
  107. package/artifacts/contracts/Test/Mocks/MockRiskFundConverter.sol/MockRiskFundConverter.dbg.json +4 -0
  108. package/artifacts/contracts/Test/Mocks/MockRiskFundConverter.sol/MockRiskFundConverter.json +1761 -0
  109. package/artifacts/contracts/Test/Mocks/MockToken.sol/MockToken.dbg.json +1 -1
  110. package/artifacts/contracts/Test/Mocks/MockToken.sol/MockToken.json +20 -2
  111. package/artifacts/contracts/TokenConverter/AbstractTokenConverter.sol/AbstractTokenConverter.dbg.json +4 -0
  112. package/artifacts/contracts/TokenConverter/AbstractTokenConverter.sol/AbstractTokenConverter.json +1276 -0
  113. package/artifacts/contracts/TokenConverter/ConverterNetwork.sol/ConverterNetwork.dbg.json +4 -0
  114. package/artifacts/contracts/TokenConverter/ConverterNetwork.sol/ConverterNetwork.json +457 -0
  115. package/artifacts/contracts/TokenConverter/IAbstractTokenConverter.sol/IAbstractTokenConverter.dbg.json +4 -0
  116. package/artifacts/contracts/TokenConverter/IAbstractTokenConverter.sol/IAbstractTokenConverter.json +446 -0
  117. package/artifacts/contracts/TokenConverter/RiskFundConverter.sol/RiskFundConverter.dbg.json +4 -0
  118. package/artifacts/contracts/TokenConverter/RiskFundConverter.sol/RiskFundConverter.json +1631 -0
  119. package/artifacts/contracts/TokenConverter/SingleTokenConverter.sol/SingleTokenConverter.dbg.json +4 -0
  120. package/artifacts/contracts/TokenConverter/SingleTokenConverter.sol/SingleTokenConverter.json +1390 -0
  121. package/contracts/Interfaces/IComptroller.sol +10 -0
  122. package/contracts/Interfaces/IConverterNetwork.sol +46 -0
  123. package/contracts/Interfaces/IPoolRegistry.sol +10 -0
  124. package/contracts/Interfaces/IRiskFund.sol +28 -0
  125. package/contracts/Interfaces/IRiskFundConverter.sol +8 -0
  126. package/contracts/Interfaces/IShortfall.sol +11 -0
  127. package/contracts/Interfaces/IXVSVault.sol +9 -0
  128. package/contracts/ProtocolReserve/ProtocolShareReserve.sol +15 -12
  129. package/contracts/ProtocolReserve/RiskFundStorage.sol +67 -0
  130. package/contracts/ProtocolReserve/RiskFundV2.sol +253 -0
  131. package/contracts/ProtocolReserve/XVSVaultTreasury.sol +95 -0
  132. package/contracts/Test/Mocks/MockACM.sol +94 -0
  133. package/contracts/Test/Mocks/MockArraySorter.sol +14 -0
  134. package/contracts/Test/Mocks/MockConverter.sol +70 -0
  135. package/contracts/Test/Mocks/MockDeflationaryToken.sol +113 -0
  136. package/contracts/Test/Mocks/MockRiskFundConverter.sol +45 -0
  137. package/contracts/Test/Mocks/MockToken.sol +4 -0
  138. package/contracts/Test/imports.sol +11 -0
  139. package/contracts/TokenConverter/AbstractTokenConverter.sol +1167 -0
  140. package/contracts/TokenConverter/ConverterNetwork.sol +243 -0
  141. package/contracts/TokenConverter/IAbstractTokenConverter.sol +178 -0
  142. package/contracts/TokenConverter/RiskFundConverter.sol +449 -0
  143. package/contracts/TokenConverter/SingleTokenConverter.sol +103 -0
  144. package/contracts/Utils/ArrayHelpers.sol +41 -0
  145. package/deployments/bscmainnet/BTCBPrimeConverter.json +218 -0
  146. package/deployments/bscmainnet/ConverterNetwork.json +723 -0
  147. package/deployments/bscmainnet/ConverterNetwork_Implementation.json +823 -0
  148. package/deployments/bscmainnet/ConverterNetwork_Proxy.json +277 -0
  149. package/deployments/bscmainnet/ETHPrimeConverter.json +218 -0
  150. package/deployments/bscmainnet/ProtocolShareReserve_Implementation.json +89 -73
  151. package/deployments/bscmainnet/RiskFundConverter.json +2035 -0
  152. package/deployments/bscmainnet/RiskFundConverter_Implementation.json +2563 -0
  153. package/deployments/bscmainnet/RiskFundConverter_Proxy.json +403 -0
  154. package/deployments/bscmainnet/RiskFundV2.json +1049 -0
  155. package/deployments/bscmainnet/SingleTokenConverterBeacon.json +206 -0
  156. package/deployments/bscmainnet/SingleTokenConverterImp.json +2164 -0
  157. package/deployments/bscmainnet/USDCPrimeConverter.json +218 -0
  158. package/deployments/bscmainnet/USDTPrimeConverter.json +218 -0
  159. package/deployments/bscmainnet/XVSVaultConverter.json +218 -0
  160. package/deployments/bscmainnet/XVSVaultTreasury.json +579 -0
  161. package/deployments/bscmainnet/XVSVaultTreasury_Implementation.json +603 -0
  162. package/deployments/bscmainnet/XVSVaultTreasury_Proxy.json +281 -0
  163. package/deployments/bscmainnet/solcInputs/d02e743d886d63634298edb2c3d7a019.json +123 -0
  164. package/deployments/bscmainnet/solcInputs/ee8827a99e03cdf6f6a94fb8782ddf44.json +228 -0
  165. package/deployments/bscmainnet.json +8927 -873
  166. package/deployments/bscmainnet_addresses.json +19 -2
  167. package/deployments/bsctestnet/BTCBPrimeConverter.json +218 -0
  168. package/deployments/bsctestnet/ConverterNetwork.json +723 -0
  169. package/deployments/bsctestnet/ConverterNetwork_Implementation.json +823 -0
  170. package/deployments/bsctestnet/ConverterNetwork_Proxy.json +277 -0
  171. package/deployments/bsctestnet/ETHPrimeConverter.json +218 -0
  172. package/deployments/bsctestnet/ProtocolShareReserve_Implementation.json +89 -73
  173. package/deployments/bsctestnet/RiskFundConverter.json +2035 -0
  174. package/deployments/bsctestnet/RiskFundConverter_Implementation.json +2563 -0
  175. package/deployments/bsctestnet/RiskFundConverter_Proxy.json +403 -0
  176. package/deployments/bsctestnet/RiskFundV2.json +1049 -0
  177. package/deployments/bsctestnet/SingleTokenConverterBeacon.json +206 -0
  178. package/deployments/bsctestnet/SingleTokenConverterImp.json +2164 -0
  179. package/deployments/bsctestnet/USDCPrimeConverter.json +218 -0
  180. package/deployments/bsctestnet/USDTPrimeConverter.json +218 -0
  181. package/deployments/bsctestnet/XVSVaultConverter.json +218 -0
  182. package/deployments/bsctestnet/XVSVaultTreasury.json +579 -0
  183. package/deployments/bsctestnet/XVSVaultTreasury_Implementation.json +603 -0
  184. package/deployments/bsctestnet/XVSVaultTreasury_Proxy.json +281 -0
  185. package/deployments/bsctestnet/solcInputs/d02e743d886d63634298edb2c3d7a019.json +123 -0
  186. package/deployments/bsctestnet/solcInputs/ee8827a99e03cdf6f6a94fb8782ddf44.json +228 -0
  187. package/deployments/bsctestnet.json +8927 -873
  188. package/deployments/bsctestnet_addresses.json +19 -2
  189. package/dist/deploy/{1-deploy.d.ts → 001-psr.d.ts} +1 -1
  190. package/dist/deploy/001-psr.d.ts.map +1 -0
  191. package/dist/deploy/{1-deploy.js → 001-psr.js} +2 -2
  192. package/dist/deploy/001-psr.js.map +1 -0
  193. package/dist/deploy/002-risk-fund-converter.d.ts +4 -0
  194. package/dist/deploy/002-risk-fund-converter.d.ts.map +1 -0
  195. package/dist/deploy/002-risk-fund-converter.js +78 -0
  196. package/dist/deploy/002-risk-fund-converter.js.map +1 -0
  197. package/dist/deploy/003-xvs-vault-teasury.d.ts +2 -0
  198. package/dist/deploy/003-xvs-vault-teasury.d.ts.map +1 -0
  199. package/dist/deploy/003-xvs-vault-teasury.js +44 -0
  200. package/dist/deploy/003-xvs-vault-teasury.js.map +1 -0
  201. package/dist/deploy/004-single-token-converter.d.ts +4 -0
  202. package/dist/deploy/004-single-token-converter.d.ts.map +1 -0
  203. package/dist/deploy/004-single-token-converter.js +65 -0
  204. package/dist/deploy/004-single-token-converter.js.map +1 -0
  205. package/dist/deploy/005-converter-network.d.ts +2 -0
  206. package/dist/deploy/005-converter-network.d.ts.map +1 -0
  207. package/dist/deploy/005-converter-network.js +42 -0
  208. package/dist/deploy/005-converter-network.js.map +1 -0
  209. package/dist/hardhat.config.d.ts.map +1 -1
  210. package/dist/hardhat.config.js +6 -0
  211. package/dist/hardhat.config.js.map +1 -1
  212. package/dist/helpers/utils.d.ts +1 -0
  213. package/dist/helpers/utils.d.ts.map +1 -1
  214. package/dist/helpers/utils.js +2 -1
  215. package/dist/helpers/utils.js.map +1 -1
  216. package/package.json +19 -16
  217. package/typechain/AbstractTokenConverter.d.ts +1775 -0
  218. package/typechain/AccessControl.d.ts +388 -0
  219. package/typechain/BeaconProxy.d.ts +126 -0
  220. package/typechain/BoundValidatorInterface.d.ts +126 -0
  221. package/typechain/ConverterNetwork.d.ts +723 -0
  222. package/typechain/{PoolRegistryInterface.d.ts → ERC165.d.ts} +20 -25
  223. package/typechain/ERC1967Upgrade.d.ts +126 -0
  224. package/typechain/IAbstractTokenConverter.d.ts +773 -0
  225. package/typechain/IBeacon.d.ts +101 -0
  226. package/typechain/IComptroller.d.ts +136 -0
  227. package/typechain/IConverterNetwork.d.ts +292 -0
  228. package/typechain/IERC165.d.ts +116 -0
  229. package/typechain/{ComptrollerInterface.d.ts → IERC1822Proxiable.d.ts} +11 -11
  230. package/typechain/IERC1967.d.ts +126 -0
  231. package/typechain/IPoolRegistry.d.ts +155 -0
  232. package/typechain/IRiskFund.d.ts +200 -0
  233. package/typechain/IRiskFundConverter.d.ts +138 -0
  234. package/typechain/IRiskFundGetters.d.ts +103 -0
  235. package/typechain/IShortfall.d.ts +110 -0
  236. package/typechain/IXVSVault.d.ts +95 -0
  237. package/typechain/MaxLoopsLimitHelpersStorage.d.ts +101 -0
  238. package/typechain/MockACM.d.ts +615 -0
  239. package/typechain/MockArraySorter.d.ts +118 -0
  240. package/typechain/MockConverter.d.ts +1969 -0
  241. package/typechain/MockDeflatingToken.d.ts +486 -0
  242. package/typechain/MockRiskFundConverter.d.ts +2479 -0
  243. package/typechain/MockToken.d.ts +36 -0
  244. package/typechain/OracleInterface.d.ts +98 -0
  245. package/typechain/Ownable.d.ts +181 -0
  246. package/typechain/PausableUpgradeable.d.ts +129 -0
  247. package/typechain/Proxy.d.ts +78 -0
  248. package/typechain/ReserveHelpersStorage.d.ts +300 -0
  249. package/typechain/ResilientOracle.d.ts +1034 -0
  250. package/typechain/ResilientOracleInterface.d.ts +192 -0
  251. package/typechain/RiskFundConverter.d.ts +2235 -0
  252. package/typechain/RiskFundV1Storage.d.ts +353 -0
  253. package/typechain/RiskFundV2.d.ts +965 -0
  254. package/typechain/RiskFundV2Storage.d.ts +372 -0
  255. package/typechain/SingleTokenConverter.d.ts +1944 -0
  256. package/typechain/TwapInterface.d.ts +124 -0
  257. package/typechain/UpgradeableBeacon.d.ts +240 -0
  258. package/typechain/VBep20Interface.d.ts +379 -0
  259. package/typechain/XVSVaultTreasury.d.ts +515 -0
  260. package/typechain/factories/AbstractTokenConverter__factory.ts +1297 -0
  261. package/typechain/factories/AccessControl__factory.ts +227 -0
  262. package/typechain/factories/BeaconProxy__factory.ts +133 -0
  263. package/typechain/factories/BoundValidatorInterface__factory.ts +59 -0
  264. package/typechain/factories/ConverterNetwork__factory.ts +502 -0
  265. package/typechain/factories/ERC165__factory.ts +39 -0
  266. package/typechain/factories/ERC1967Upgrade__factory.ts +71 -0
  267. package/typechain/factories/IAbstractTokenConverter__factory.ts +466 -0
  268. package/typechain/factories/IBeacon__factory.ts +36 -0
  269. package/typechain/factories/IComptroller__factory.ts +68 -0
  270. package/typechain/factories/IConverterNetwork__factory.ts +142 -0
  271. package/typechain/factories/{ComptrollerInterface__factory.ts → IERC165__factory.ts} +14 -15
  272. package/typechain/factories/IERC1822Proxiable__factory.ts +39 -0
  273. package/typechain/factories/IERC1967__factory.ts +68 -0
  274. package/typechain/factories/IPoolRegistry__factory.ts +66 -0
  275. package/typechain/factories/{PoolRegistryInterface__factory.ts → IRiskFundConverter__factory.ts} +28 -19
  276. package/typechain/factories/IRiskFundGetters__factory.ts +39 -0
  277. package/typechain/factories/IRiskFund__factory.ts +89 -0
  278. package/typechain/factories/IShortfall__factory.ts +36 -0
  279. package/typechain/factories/IXVSVault__factory.ts +36 -0
  280. package/typechain/factories/MaxLoopsLimitHelpersStorage__factory.ts +75 -0
  281. package/typechain/factories/MockACM__factory.ts +411 -0
  282. package/typechain/factories/MockArraySorter__factory.ts +85 -0
  283. package/typechain/factories/MockConverter__factory.ts +1439 -0
  284. package/typechain/factories/MockDeflatingToken__factory.ts +389 -0
  285. package/typechain/factories/MockRiskFundConverter__factory.ts +1828 -0
  286. package/typechain/factories/MockToken__factory.ts +19 -1
  287. package/typechain/factories/OracleInterface__factory.ts +45 -0
  288. package/typechain/factories/Ownable__factory.ts +75 -0
  289. package/typechain/factories/PausableUpgradeable__factory.ts +78 -0
  290. package/typechain/factories/ProtocolShareReserve__factory.ts +6 -1
  291. package/typechain/factories/Proxy__factory.ts +28 -0
  292. package/typechain/factories/ReserveHelpersStorage__factory.ts +188 -0
  293. package/typechain/factories/ResilientOracleInterface__factory.ts +94 -0
  294. package/typechain/factories/ResilientOracle__factory.ts +710 -0
  295. package/typechain/factories/RiskFundConverter__factory.ts +1694 -0
  296. package/typechain/factories/RiskFundV1Storage__factory.ts +223 -0
  297. package/typechain/factories/RiskFundV2Storage__factory.ts +236 -0
  298. package/typechain/factories/RiskFundV2__factory.ts +672 -0
  299. package/typechain/factories/SingleTokenConverter__factory.ts +1441 -0
  300. package/typechain/factories/TwapInterface__factory.ts +61 -0
  301. package/typechain/factories/UpgradeableBeacon__factory.ts +163 -0
  302. package/typechain/factories/VBep20Interface__factory.ts +261 -0
  303. package/typechain/factories/XVSVaultTreasury__factory.ts +365 -0
  304. package/typechain/index.ts +84 -4
  305. package/artifacts/build-info/ac790619cb42a0f08af77d9ea733874e.json +0 -1
  306. package/artifacts/contracts/Interfaces/ComptrollerInterface.sol/ComptrollerInterface.dbg.json +0 -4
  307. package/artifacts/contracts/Interfaces/PoolRegistryInterface.sol/PoolRegistryInterface.dbg.json +0 -4
  308. package/contracts/Interfaces/ComptrollerInterface.sol +0 -6
  309. package/contracts/Interfaces/PoolRegistryInterface.sol +0 -7
  310. package/dist/deploy/1-deploy.d.ts.map +0 -1
  311. package/dist/deploy/1-deploy.js.map +0 -1
@@ -0,0 +1,449 @@
1
+ // SPDX-License-Identifier: BSD-3-Clause
2
+ pragma solidity 0.8.13;
3
+
4
+ import { SafeERC20Upgradeable } from "@openzeppelin/contracts-upgradeable/token/ERC20/utils/SafeERC20Upgradeable.sol";
5
+ import { IERC20Upgradeable } from "@openzeppelin/contracts-upgradeable/token/ERC20/IERC20Upgradeable.sol";
6
+ import { ResilientOracle } from "@venusprotocol/oracle/contracts/ResilientOracle.sol";
7
+ import { ensureNonzeroAddress, ensureNonzeroValue } from "@venusprotocol/solidity-utilities/contracts/validators.sol";
8
+
9
+ import { AbstractTokenConverter } from "./AbstractTokenConverter.sol";
10
+ import { IPoolRegistry } from "../Interfaces/IPoolRegistry.sol";
11
+ import { IComptroller } from "../Interfaces/IComptroller.sol";
12
+ import { IRiskFund, IRiskFundGetters } from "../Interfaces/IRiskFund.sol";
13
+ import { IVToken } from "../Interfaces/IVToken.sol";
14
+
15
+ /// @title RiskFundConverter
16
+ /// @author Venus
17
+ /// @notice RiskFundConverter used for token conversions and sends received token to RiskFund
18
+ /// @custom:security-contact https://github.com/VenusProtocol/protocol-reserve#discussion
19
+ contract RiskFundConverter is AbstractTokenConverter {
20
+ using SafeERC20Upgradeable for IERC20Upgradeable;
21
+
22
+ /// @notice Address of the core pool comptroller
23
+ /// @custom:oz-upgrades-unsafe-allow state-variable-immutable
24
+ address public immutable CORE_POOL_COMPTROLLER;
25
+
26
+ ///@notice Address of the vBNB
27
+ ///@dev This address is used to include the BNB market while in getPools method
28
+ /// @custom:oz-upgrades-unsafe-allow state-variable-immutable
29
+ address public immutable VBNB;
30
+
31
+ ///@notice Address of the native wrapped currency
32
+ /// @custom:oz-upgrades-unsafe-allow state-variable-immutable
33
+ address public immutable NATIVE_WRAPPED;
34
+
35
+ /// @notice Store the previous state for the asset transferred to ProtocolShareReserve combined(for all pools)
36
+ mapping(address => uint256) internal assetsReserves;
37
+
38
+ /// @notice Store the asset's reserve per pool in the ProtocolShareReserve
39
+ /// @dev Comptroller(pool) -> Asset -> amount
40
+ mapping(address => mapping(address => uint256)) internal poolsAssetsReserves;
41
+
42
+ /// @notice Address of pool registry contract
43
+ address public poolRegistry;
44
+
45
+ /// @notice The mapping contains the assets for each pool which are sent to RiskFund directly
46
+ /// @dev Comptroller(pool) -> Asset -> bool(should transfer directly on true)
47
+ mapping(address => mapping(address => bool)) public poolsAssetsDirectTransfer;
48
+
49
+ /// @notice Emitted when pool registry address is updated
50
+ event PoolRegistryUpdated(address indexed oldPoolRegistry, address indexed newPoolRegistry);
51
+
52
+ /// @notice Emitted after updating of the assets reserves
53
+ /// amount -> reserve increased by amount
54
+ event AssetsReservesUpdated(address indexed comptroller, address indexed asset, uint256 amount);
55
+
56
+ /// @notice Emmitted after the funds transferred to the destination address
57
+ event AssetTransferredToDestination(
58
+ address indexed receiver,
59
+ address indexed comptroller,
60
+ address indexed asset,
61
+ uint256 amount
62
+ );
63
+
64
+ /// @notice Emitted after the poolsAssetsDirectTransfer mapping is updated
65
+ event PoolAssetsDirectTransferUpdated(address indexed comptroller, address indexed asset, bool value);
66
+
67
+ // Error thrown when comptrollers array length is not equal to assets array length
68
+ error InvalidArguments();
69
+
70
+ /// @notice thrown when amount entered is greater than balance
71
+ error InsufficientBalance();
72
+
73
+ /// @notice thrown when asset does not exist in the pool
74
+ error MarketNotExistInPool(address comptroller, address asset);
75
+
76
+ /// @notice thrown to prevent reentrancy
77
+ /// @dev This error is used to safeguard against reentrancy attacks, ensuring that a certain operation
78
+ /// cannot be called recursively within the same transaction.
79
+ error ReentrancyGuardError();
80
+
81
+ /// @param corePoolComptroller_ Address of the Comptroller pool
82
+ /// @param vBNB_ Address of the vBNB
83
+ /// @param nativeWrapped_ Address of the wrapped native currency
84
+ /// @custom:oz-upgrades-unsafe-allow constructor
85
+ constructor(
86
+ address corePoolComptroller_,
87
+ address vBNB_,
88
+ address nativeWrapped_
89
+ ) {
90
+ ensureNonzeroAddress(corePoolComptroller_);
91
+ ensureNonzeroAddress(vBNB_);
92
+ ensureNonzeroAddress(nativeWrapped_);
93
+
94
+ CORE_POOL_COMPTROLLER = corePoolComptroller_;
95
+ VBNB = vBNB_;
96
+ NATIVE_WRAPPED = nativeWrapped_;
97
+
98
+ // Note that the contract is upgradeable. Use initialize() or reinitializers
99
+ // to set the state variables.
100
+ _disableInitializers();
101
+ }
102
+
103
+ /// @param accessControlManager_ Access control manager contract address
104
+ /// @param priceOracle_ Resilient oracle address
105
+ /// @param destinationAddress_ Address at all incoming tokens will transferred to
106
+ /// @param poolRegistry_ Address of the pool registry
107
+ /// @param minAmountToConvert_ minimum amount to convert
108
+ /// @param comptrollers Addresses of the pools
109
+ /// @param assets Addresses of the assets need to be added for direct transfer
110
+ /// @param values Boolean value to indicate whether direct transfer is allowed for each asset.
111
+ /// @custom:event PoolAssetsDirectTransferUpdated emits on success
112
+ function initialize(
113
+ address accessControlManager_,
114
+ ResilientOracle priceOracle_,
115
+ address destinationAddress_,
116
+ address poolRegistry_,
117
+ uint256 minAmountToConvert_,
118
+ address[] calldata comptrollers,
119
+ address[][] calldata assets,
120
+ bool[][] calldata values
121
+ ) public initializer {
122
+ // Initialize AbstractTokenConverter
123
+ __AbstractTokenConverter_init(accessControlManager_, priceOracle_, destinationAddress_, minAmountToConvert_);
124
+ ensureNonzeroAddress(poolRegistry_);
125
+ poolRegistry = poolRegistry_;
126
+ _setPoolsAssetsDirectTransfer(comptrollers, assets, values);
127
+ }
128
+
129
+ /// @dev Pool registry setter
130
+ /// @param poolRegistry_ Address of the pool registry
131
+ /// @custom:event PoolRegistryUpdated emits on success
132
+ /// @custom:error ZeroAddressNotAllowed is thrown when pool registry address is zero
133
+ /// @custom:access Only Governance
134
+ function setPoolRegistry(address poolRegistry_) external onlyOwner {
135
+ ensureNonzeroAddress(poolRegistry_);
136
+ emit PoolRegistryUpdated(poolRegistry, poolRegistry_);
137
+ poolRegistry = poolRegistry_;
138
+ }
139
+
140
+ /// @notice Update the poolsAssetsDirectTransfer mapping
141
+ /// @param comptrollers Addresses of the pools
142
+ /// @param assets Addresses of the assets need to be added for direct transfer
143
+ /// @param values Boolean value to indicate whether direct transfer is allowed for each asset.
144
+ /// @custom:event PoolAssetsDirectTransferUpdated emits on success
145
+ /// @custom:access Restricted by ACM
146
+ function setPoolsAssetsDirectTransfer(
147
+ address[] calldata comptrollers,
148
+ address[][] calldata assets,
149
+ bool[][] calldata values
150
+ ) external {
151
+ _checkAccessAllowed("setPoolsAssetsDirectTransfer(address[],address[][],bool[][])");
152
+ _setPoolsAssetsDirectTransfer(comptrollers, assets, values);
153
+ }
154
+
155
+ /// @dev Get the Amount of the asset in the risk fund for the specific pool
156
+ /// @param comptroller Comptroller address (pool)
157
+ /// @param asset Asset address
158
+ /// @return reserves Asset's reserve in risk fund
159
+ /// @custom:error MarketNotExistInPool When asset does not exist in the pool(comptroller)
160
+ /// @custom:error ReentrancyGuardError thrown to prevent reentrancy during the function execution
161
+ function getPoolAssetReserve(address comptroller, address asset) external view returns (uint256 reserves) {
162
+ if (_reentrancyGuardEntered()) revert ReentrancyGuardError();
163
+ if (!ensureAssetListed(comptroller, asset)) revert MarketNotExistInPool(comptroller, asset);
164
+
165
+ reserves = poolsAssetsReserves[comptroller][asset];
166
+ }
167
+
168
+ /// @notice Get the balance for specific token
169
+ /// @param tokenAddress Address of the token
170
+ /// @return tokenBalance Reserves of the token the contract has
171
+ function balanceOf(address tokenAddress) public view override returns (uint256 tokenBalance) {
172
+ tokenBalance = assetsReserves[tokenAddress];
173
+ }
174
+
175
+ /// @notice Get the array of all pools addresses
176
+ /// @param tokenAddress Address of the token
177
+ /// @return poolsWithCore Array of the pools addresses in which token is available
178
+ function getPools(address tokenAddress) public view returns (address[] memory poolsWithCore) {
179
+ poolsWithCore = IPoolRegistry(poolRegistry).getPoolsSupportedByAsset(tokenAddress);
180
+
181
+ if (isAssetListedInCore(tokenAddress)) {
182
+ uint256 poolsLength = poolsWithCore.length;
183
+ address[] memory extendedPools = new address[](poolsLength + 1);
184
+
185
+ for (uint256 i; i < poolsLength; ) {
186
+ extendedPools[i] = poolsWithCore[i];
187
+ unchecked {
188
+ ++i;
189
+ }
190
+ }
191
+
192
+ extendedPools[poolsLength] = CORE_POOL_COMPTROLLER;
193
+ poolsWithCore = extendedPools;
194
+ }
195
+ }
196
+
197
+ /// @dev This hook is used to update the state for asset reserves before transferring tokenOut to user
198
+ /// @param tokenOutAddress Address of the asset to be transferred to the user
199
+ /// @param amountOut Amount of tokenAddressOut transferred from this converter
200
+ function _preTransferHook(address tokenOutAddress, uint256 amountOut) internal override {
201
+ assetsReserves[tokenOutAddress] -= amountOut;
202
+ }
203
+
204
+ /// @notice Hook to perform after converting tokens
205
+ /// @dev After transformation poolsAssetsReserves are settled by pool's reserves fraction
206
+ /// @param tokenInAddress Address of the tokenIn
207
+ /// @param tokenOutAddress Address of the tokenOut
208
+ /// @param amountIn Amount of tokenIn transferred
209
+ /// @param amountOut Amount of tokenOut transferred
210
+ /// @custom:event AssetTransferredToDestination emits on success for each pool which has share
211
+ function _postConversionHook(
212
+ address tokenInAddress,
213
+ address tokenOutAddress,
214
+ uint256 amountIn,
215
+ uint256 amountOut
216
+ ) internal override {
217
+ address[] memory pools = getPools(tokenOutAddress);
218
+ uint256 assetReserve = assetsReserves[tokenOutAddress] + amountOut;
219
+ ensureNonzeroValue(assetReserve);
220
+
221
+ uint256 poolsLength = pools.length;
222
+ uint256 distributedOutShare;
223
+ uint256 poolAmountInShare;
224
+ uint256 distributedInShare;
225
+
226
+ for (uint256 i; i < poolsLength; ) {
227
+ uint256 currentPoolsAssetsReserves = poolsAssetsReserves[pools[i]][tokenOutAddress];
228
+ if (currentPoolsAssetsReserves != 0) {
229
+ if (i < (poolsLength - 1)) {
230
+ distributedOutShare += updatePoolAssetsReserve(pools[i], tokenOutAddress, amountOut, assetReserve);
231
+ poolAmountInShare = (amountIn * currentPoolsAssetsReserves) / assetReserve;
232
+ distributedInShare += poolAmountInShare;
233
+ } else {
234
+ uint256 distributedDiff = amountOut - distributedOutShare;
235
+ poolsAssetsReserves[pools[i]][tokenOutAddress] -= distributedDiff;
236
+ emit AssetsReservesUpdated(pools[i], tokenOutAddress, distributedDiff);
237
+ poolAmountInShare = amountIn - distributedInShare;
238
+ }
239
+ emit AssetTransferredToDestination(destinationAddress, pools[i], tokenInAddress, poolAmountInShare);
240
+ IRiskFund(destinationAddress).updatePoolState(pools[i], tokenInAddress, poolAmountInShare);
241
+ }
242
+ unchecked {
243
+ ++i;
244
+ }
245
+ }
246
+ }
247
+
248
+ /// @dev Operations to perform before sweeping tokens
249
+ /// @param tokenAddress Address of the token
250
+ /// @param amount Amount transferred to address(to)
251
+ /// @custom:error InsufficientBalance is thrown when amount entered is greater than balance of token
252
+ function preSweepToken(address tokenAddress, uint256 amount) internal override {
253
+ uint256 balance = IERC20Upgradeable(tokenAddress).balanceOf(address(this));
254
+ if (amount > balance) revert InsufficientBalance();
255
+ uint256 balanceDiff = balance - assetsReserves[tokenAddress];
256
+
257
+ if (balanceDiff < amount) {
258
+ uint256 amountDiff;
259
+ unchecked {
260
+ amountDiff = amount - balanceDiff;
261
+ }
262
+
263
+ address[] memory pools = getPools(tokenAddress);
264
+ uint256 assetReserve = assetsReserves[tokenAddress];
265
+ uint256 poolsLength = pools.length;
266
+ uint256 distributedShare;
267
+
268
+ for (uint256 i; i < poolsLength; ) {
269
+ if (poolsAssetsReserves[pools[i]][tokenAddress] != 0) {
270
+ if (i < (poolsLength - 1)) {
271
+ distributedShare += updatePoolAssetsReserve(pools[i], tokenAddress, amountDiff, assetReserve);
272
+ } else {
273
+ uint256 distributedDiff = amountDiff - distributedShare;
274
+ poolsAssetsReserves[pools[i]][tokenAddress] -= distributedDiff;
275
+ emit AssetsReservesUpdated(pools[i], tokenAddress, distributedDiff);
276
+ }
277
+ }
278
+ unchecked {
279
+ ++i;
280
+ }
281
+ }
282
+ assetsReserves[tokenAddress] -= amountDiff;
283
+ }
284
+ }
285
+
286
+ /// @dev Update the poolAssetsReserves upon transferring the tokens
287
+ /// @param pool Address of the pool
288
+ /// @param tokenAddress Address of the token
289
+ /// @param amount Amount transferred to address(to)
290
+ /// @param assetReserve Asset's reserve for the pool
291
+ /// @return poolAmountShare Share of the pool as per it's reserve in compare to total reserves for the asset
292
+ /// @custom:event AssetsReservesUpdated emits on success
293
+ function updatePoolAssetsReserve(
294
+ address pool,
295
+ address tokenAddress,
296
+ uint256 amount,
297
+ uint256 assetReserve
298
+ ) internal returns (uint256 poolAmountShare) {
299
+ poolAmountShare = (poolsAssetsReserves[pool][tokenAddress] * amount) / assetReserve;
300
+ poolsAssetsReserves[pool][tokenAddress] -= poolAmountShare;
301
+ emit AssetsReservesUpdated(pool, tokenAddress, poolAmountShare);
302
+ }
303
+
304
+ /// @dev Update the poolsAssetsDirectTransfer mapping
305
+ /// @param comptrollers Addresses of the pools
306
+ /// @param assets Addresses of the assets need to be added for direct transfer
307
+ /// @param values Boolean value to indicate whether direct transfer is allowed for each asset.
308
+ /// @custom:event PoolAssetsDirectTransferUpdated emits on success
309
+ /// @custom:error InvalidArguments thrown when comptrollers array length is not equal to assets array length
310
+ function _setPoolsAssetsDirectTransfer(
311
+ address[] calldata comptrollers,
312
+ address[][] calldata assets,
313
+ bool[][] calldata values
314
+ ) internal {
315
+ uint256 comptrollersLength = comptrollers.length;
316
+
317
+ if ((comptrollersLength != assets.length) || (comptrollersLength != values.length)) {
318
+ revert InvalidArguments();
319
+ }
320
+
321
+ for (uint256 i; i < comptrollersLength; ) {
322
+ address[] memory poolAssets = assets[i];
323
+ bool[] memory assetsValues = values[i];
324
+ uint256 poolAssetsLength = poolAssets.length;
325
+
326
+ if (poolAssetsLength != assetsValues.length) {
327
+ revert InvalidArguments();
328
+ }
329
+
330
+ for (uint256 j; j < poolAssetsLength; ) {
331
+ poolsAssetsDirectTransfer[comptrollers[i]][poolAssets[j]] = assetsValues[j];
332
+ emit PoolAssetsDirectTransferUpdated(comptrollers[i], poolAssets[j], assetsValues[j]);
333
+ unchecked {
334
+ ++j;
335
+ }
336
+ }
337
+
338
+ unchecked {
339
+ ++i;
340
+ }
341
+ }
342
+ }
343
+
344
+ /// @dev Update the reserve of the asset for the specific pool after transferring to risk fund
345
+ /// and transferring funds to the protocol share reserve
346
+ /// @param comptroller Comptroller address (pool)
347
+ /// @param asset Asset address
348
+ /// @return balanceDifference Amount of asset, for _privateConversion
349
+ /// @custom:event AssetTransferredToDestination emits when poolsAssetsDirectTransfer is enabled for entered comptroller and asset
350
+ /// @custom:error MarketNotExistInPool When asset does not exist in the pool(comptroller)
351
+ function _updateAssetsState(address comptroller, address asset)
352
+ internal
353
+ override
354
+ returns (uint256 balanceDifference)
355
+ {
356
+ if (!ensureAssetListed(comptroller, asset)) revert MarketNotExistInPool(comptroller, asset);
357
+
358
+ IERC20Upgradeable token = IERC20Upgradeable(asset);
359
+ uint256 currentBalance = token.balanceOf(address(this));
360
+ uint256 assetReserve = assetsReserves[asset];
361
+ if (currentBalance > assetReserve) {
362
+ unchecked {
363
+ balanceDifference = currentBalance - assetReserve;
364
+ }
365
+ if (poolsAssetsDirectTransfer[comptroller][asset]) {
366
+ uint256 previousDestinationBalance = token.balanceOf(destinationAddress);
367
+ token.safeTransfer(destinationAddress, balanceDifference);
368
+ uint256 newDestinationBalance = token.balanceOf(destinationAddress);
369
+
370
+ emit AssetTransferredToDestination(destinationAddress, comptroller, asset, balanceDifference);
371
+ IRiskFund(destinationAddress).updatePoolState(
372
+ comptroller,
373
+ asset,
374
+ newDestinationBalance - previousDestinationBalance
375
+ );
376
+ balanceDifference = 0;
377
+ }
378
+ }
379
+ }
380
+
381
+ /// @dev This hook is used to update states for the converter after the privateConversion
382
+ /// @param comptroller Comptroller address (pool)
383
+ /// @param tokenAddressIn Address of the destination's base asset
384
+ /// @param convertedTokenInBalance Amount of the base asset received after the conversion
385
+ /// @param tokenAddressOut Address of the asset transferred to other converter in exchange of base asset
386
+ /// @param convertedTokenOutBalance Amount of tokenAddressOut transferred from this converter
387
+ function _postPrivateConversionHook(
388
+ address comptroller,
389
+ address tokenAddressIn,
390
+ uint256 convertedTokenInBalance,
391
+ address tokenAddressOut,
392
+ uint256 convertedTokenOutBalance
393
+ ) internal override {
394
+ if (convertedTokenInBalance > 0) {
395
+ emit AssetTransferredToDestination(
396
+ destinationAddress,
397
+ comptroller,
398
+ tokenAddressIn,
399
+ convertedTokenInBalance
400
+ );
401
+ IRiskFund(destinationAddress).updatePoolState(comptroller, tokenAddressIn, convertedTokenInBalance);
402
+ }
403
+ if (convertedTokenOutBalance > 0) {
404
+ assetsReserves[tokenAddressOut] += convertedTokenOutBalance;
405
+ poolsAssetsReserves[comptroller][tokenAddressOut] += convertedTokenOutBalance;
406
+ emit AssetsReservesUpdated(comptroller, tokenAddressOut, convertedTokenOutBalance);
407
+ }
408
+ }
409
+
410
+ /// @dev This function checks for the given asset is listed in core pool or not
411
+ /// @param tokenAddress Address of the asset
412
+ /// @return isAssetListed true if the asset is listed
413
+ function isAssetListedInCore(address tokenAddress) internal view returns (bool isAssetListed) {
414
+ address[] memory coreMarkets = IComptroller(CORE_POOL_COMPTROLLER).getAllMarkets();
415
+
416
+ uint256 coreMarketsLength = coreMarkets.length;
417
+ for (uint256 i; i < coreMarketsLength; ) {
418
+ isAssetListed = (VBNB == coreMarkets[i])
419
+ ? (tokenAddress == NATIVE_WRAPPED)
420
+ : (IVToken(coreMarkets[i]).underlying() == tokenAddress);
421
+
422
+ if (isAssetListed) {
423
+ break;
424
+ }
425
+
426
+ unchecked {
427
+ ++i;
428
+ }
429
+ }
430
+ }
431
+
432
+ /// @dev This function checks for the given asset is listed or not
433
+ /// @param comptroller Address of the comptroller
434
+ /// @param asset Address of the asset
435
+ /// @return isListed true if the asset is listed
436
+ function ensureAssetListed(address comptroller, address asset) internal view returns (bool isListed) {
437
+ if (comptroller == CORE_POOL_COMPTROLLER) {
438
+ isListed = isAssetListedInCore(asset);
439
+ } else {
440
+ isListed = IPoolRegistry(poolRegistry).getVTokenForAsset(comptroller, asset) != address(0);
441
+ }
442
+ }
443
+
444
+ /// @dev Get base asset address of the RiskFund
445
+ /// @return destinationBaseAsset Address of the base asset(RiskFund)
446
+ function _getDestinationBaseAsset() internal view override returns (address destinationBaseAsset) {
447
+ destinationBaseAsset = IRiskFundGetters(destinationAddress).convertibleBaseAsset();
448
+ }
449
+ }
@@ -0,0 +1,103 @@
1
+ // SPDX-License-Identifier: BSD-3-Clause
2
+ pragma solidity 0.8.13;
3
+
4
+ import { SafeERC20Upgradeable } from "@openzeppelin/contracts-upgradeable/token/ERC20/utils/SafeERC20Upgradeable.sol";
5
+ import { IERC20Upgradeable } from "@openzeppelin/contracts-upgradeable/token/ERC20/IERC20Upgradeable.sol";
6
+ import { ResilientOracle } from "@venusprotocol/oracle/contracts/ResilientOracle.sol";
7
+ import { ensureNonzeroAddress } from "@venusprotocol/solidity-utilities/contracts/validators.sol";
8
+
9
+ import { AbstractTokenConverter } from "./AbstractTokenConverter.sol";
10
+
11
+ /// @title SingleTokenConverter
12
+ /// @author Venus
13
+ /// @notice SingleTokenConverter used for token conversions and sends received tokens
14
+ /// @custom:security-contact https://github.com/VenusProtocol/protocol-reserve#discussion
15
+ contract SingleTokenConverter is AbstractTokenConverter {
16
+ using SafeERC20Upgradeable for IERC20Upgradeable;
17
+
18
+ /// @notice Address of the base asset token
19
+ address public baseAsset;
20
+
21
+ /// @notice Emitted when base asset is updated
22
+ event BaseAssetUpdated(address indexed oldBaseAsset, address indexed newBaseAsset);
23
+
24
+ /// @notice Emmitted after the funds transferred to the destination address
25
+ event AssetTransferredToDestination(
26
+ address indexed receiver,
27
+ address indexed comptroller,
28
+ address indexed asset,
29
+ uint256 amount
30
+ );
31
+
32
+ /// @custom:oz-upgrades-unsafe-allow constructor
33
+ constructor() {
34
+ // Note that the contract is upgradeable. Use initialize() or reinitializers
35
+ // to set the state variables.
36
+ _disableInitializers();
37
+ }
38
+
39
+ /// @param accessControlManager_ Access control manager contract address
40
+ /// @param priceOracle_ Resilient oracle address
41
+ /// @param destinationAddress_ Address at all incoming tokens will transferred to
42
+ /// @param baseAsset_ Address of the base asset
43
+ /// @param minAmountToConvert_ Minimum amount to convert
44
+ function initialize(
45
+ address accessControlManager_,
46
+ ResilientOracle priceOracle_,
47
+ address destinationAddress_,
48
+ address baseAsset_,
49
+ uint256 minAmountToConvert_
50
+ ) public initializer {
51
+ _setBaseAsset(baseAsset_);
52
+
53
+ // Initialize AbstractTokenConverter
54
+ __AbstractTokenConverter_init(accessControlManager_, priceOracle_, destinationAddress_, minAmountToConvert_);
55
+ }
56
+
57
+ /// @notice Sets the base asset for the contract
58
+ /// @param baseAsset_ The new address of the base asset
59
+ /// @custom:access Only Governance
60
+ function setBaseAsset(address baseAsset_) external onlyOwner {
61
+ _setBaseAsset(baseAsset_);
62
+ }
63
+
64
+ /// @notice Get the balance for specific token
65
+ /// @param tokenAddress Address of the token
66
+ /// @return tokenBalance Balance of the token the contract has
67
+ function balanceOf(address tokenAddress) public view override returns (uint256 tokenBalance) {
68
+ IERC20Upgradeable token = IERC20Upgradeable(tokenAddress);
69
+ tokenBalance = token.balanceOf(address(this));
70
+ }
71
+
72
+ /// @param comptroller Comptroller address (pool)
73
+ /// @param asset Asset address.
74
+ /// @return balanceLeft Amount of asset, for _privateConversion
75
+ // solhint-disable-next-line
76
+ function _updateAssetsState(address comptroller, address asset) internal override returns (uint256 balanceLeft) {
77
+ IERC20Upgradeable token = IERC20Upgradeable(asset);
78
+ uint256 balance = token.balanceOf(address(this));
79
+ balanceLeft = balance;
80
+
81
+ if (asset == baseAsset) {
82
+ balanceLeft = 0;
83
+ token.safeTransfer(destinationAddress, balance);
84
+ emit AssetTransferredToDestination(destinationAddress, comptroller, asset, balance);
85
+ }
86
+ }
87
+
88
+ /// @dev Sets the base asset for the contract
89
+ /// @param baseAsset_ The new address of the base asset
90
+ /// @custom:error ZeroAddressNotAllowed is thrown when address is zero
91
+ /// @custom:event BaseAssetUpdated is emitted on success
92
+ function _setBaseAsset(address baseAsset_) internal {
93
+ ensureNonzeroAddress(baseAsset_);
94
+ emit BaseAssetUpdated(baseAsset, baseAsset_);
95
+ baseAsset = baseAsset_;
96
+ }
97
+
98
+ /// @dev Get base asset address
99
+ /// @return destinationBaseAsset Address of the base asset(baseAsset)
100
+ function _getDestinationBaseAsset() internal view override returns (address destinationBaseAsset) {
101
+ destinationBaseAsset = baseAsset;
102
+ }
103
+ }
@@ -0,0 +1,41 @@
1
+ // SPDX-License-Identifier: BSD-3-Clause
2
+ pragma solidity 0.8.13;
3
+
4
+ /// @notice Used to sort addresses array based on their token balances
5
+ /// @param arr Array of token balances of different addresses
6
+ /// @param addrs Array containing these addresses
7
+ function sort(uint256[] memory arr, address[] memory addrs) pure {
8
+ if (arr.length > 1) {
9
+ return quickSortDescending(arr, addrs, 0, arr.length - 1);
10
+ }
11
+ }
12
+
13
+ /// @notice Used to sort addresses array based on their token balances
14
+ /// @param arr Array of token balances of different addresses
15
+ /// @param addrs Array containing these addresses
16
+ /// @param left index of first value
17
+ /// @param right index of last value
18
+ function quickSortDescending(
19
+ uint256[] memory arr,
20
+ address[] memory addrs,
21
+ uint256 left,
22
+ uint256 right
23
+ ) pure {
24
+ if (left >= right) return;
25
+ uint256 p = arr[(left + right) / 2]; // p = the pivot element
26
+ uint256 i = left;
27
+ uint256 j = right;
28
+ while (i < j) {
29
+ while (arr[i] > p) ++i;
30
+ while (arr[j] < p) --j; // arr[j] < p means p still to the right, so j > 0
31
+ if (arr[i] < arr[j]) {
32
+ (arr[i], arr[j]) = (arr[j], arr[i]);
33
+ (addrs[i], addrs[j]) = (addrs[j], addrs[i]);
34
+ } else {
35
+ ++i;
36
+ }
37
+ }
38
+
39
+ if (j > left) quickSortDescending(arr, addrs, left, j - 1); // j > left, so j > 0
40
+ quickSortDescending(arr, addrs, j + 1, right);
41
+ }