@towns-protocol/contracts 0.0.302

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 (396) hide show
  1. package/LICENSE.txt +21 -0
  2. package/README.md +206 -0
  3. package/docs/permitAndStake_integration_guide.md +266 -0
  4. package/docs/swap_integration_guide.md +591 -0
  5. package/package.json +57 -0
  6. package/scripts/common/DeployBase.s.sol +26 -0
  7. package/scripts/common/DeployFacet.s.sol +7 -0
  8. package/scripts/common/Deployer.s.sol +33 -0
  9. package/scripts/common/Interaction.s.sol +51 -0
  10. package/scripts/deployments/diamonds/DeployAppRegistry.s.sol +168 -0
  11. package/scripts/deployments/diamonds/DeployBaseRegistry.s.sol +263 -0
  12. package/scripts/deployments/diamonds/DeployRiverAirdrop.s.sol +203 -0
  13. package/scripts/deployments/diamonds/DeployRiverMigration.s.sol +129 -0
  14. package/scripts/deployments/diamonds/DeployRiverRegistry.s.sol +175 -0
  15. package/scripts/deployments/diamonds/DeploySpace.s.sol +285 -0
  16. package/scripts/deployments/diamonds/DeploySpaceFactory.s.sol +416 -0
  17. package/scripts/deployments/diamonds/DeploySpaceOwner.s.sol +183 -0
  18. package/scripts/deployments/diamonds/DeploySwapRouter.s.sol +176 -0
  19. package/scripts/deployments/diamonds/IDiamondInitHelper.sol +11 -0
  20. package/scripts/deployments/facets/DeployAppAccount.s.sol +51 -0
  21. package/scripts/deployments/facets/DeployAppRegistryFacet.s.sol +69 -0
  22. package/scripts/deployments/facets/DeployArchitect.s.sol +51 -0
  23. package/scripts/deployments/facets/DeployAttestationRegistry.s.sol +36 -0
  24. package/scripts/deployments/facets/DeployBanning.s.sol +30 -0
  25. package/scripts/deployments/facets/DeployChannels.s.sol +35 -0
  26. package/scripts/deployments/facets/DeployCreateSpace.s.sol +42 -0
  27. package/scripts/deployments/facets/DeployDropFacet.s.sol +49 -0
  28. package/scripts/deployments/facets/DeployERC721A.s.sol +48 -0
  29. package/scripts/deployments/facets/DeployERC721ANonTransferable.s.sol +49 -0
  30. package/scripts/deployments/facets/DeployERC721AQueryable.s.sol +30 -0
  31. package/scripts/deployments/facets/DeployEntitlementChecker.s.sol +49 -0
  32. package/scripts/deployments/facets/DeployEntitlementDataQueryable.s.sol +29 -0
  33. package/scripts/deployments/facets/DeployEntitlementsManager.s.sol +33 -0
  34. package/scripts/deployments/facets/DeployExecutorFacet.s.sol +60 -0
  35. package/scripts/deployments/facets/DeployFeatureManager.s.sol +38 -0
  36. package/scripts/deployments/facets/DeployGuardianFacet.s.sol +39 -0
  37. package/scripts/deployments/facets/DeployImplementationRegistry.s.sol +36 -0
  38. package/scripts/deployments/facets/DeployMainnetDelegation.s.sol +43 -0
  39. package/scripts/deployments/facets/DeployMembership.s.sol +77 -0
  40. package/scripts/deployments/facets/DeployMembershipMetadata.s.sol +30 -0
  41. package/scripts/deployments/facets/DeployMembershipToken.s.sol +47 -0
  42. package/scripts/deployments/facets/DeployMerkleAirdrop.s.sol +37 -0
  43. package/scripts/deployments/facets/DeployMetadata.s.sol +39 -0
  44. package/scripts/deployments/facets/DeployMockLegacyArchitect.s.sol +33 -0
  45. package/scripts/deployments/facets/DeployNodeOperator.s.sol +49 -0
  46. package/scripts/deployments/facets/DeployNodeRegistry.s.sol +37 -0
  47. package/scripts/deployments/facets/DeployOperatorRegistry.s.sol +36 -0
  48. package/scripts/deployments/facets/DeployPartnerRegistry.s.sol +41 -0
  49. package/scripts/deployments/facets/DeployPlatformRequirements.s.sol +75 -0
  50. package/scripts/deployments/facets/DeployPrepayFacet.s.sol +31 -0
  51. package/scripts/deployments/facets/DeployPricingModules.s.sol +36 -0
  52. package/scripts/deployments/facets/DeployReferrals.s.sol +34 -0
  53. package/scripts/deployments/facets/DeployReviewFacet.s.sol +31 -0
  54. package/scripts/deployments/facets/DeployRewardsDistributionV2.s.sol +76 -0
  55. package/scripts/deployments/facets/DeployRiverConfig.s.sol +41 -0
  56. package/scripts/deployments/facets/DeployRoles.s.sol +48 -0
  57. package/scripts/deployments/facets/DeploySchemaRegistry.s.sol +35 -0
  58. package/scripts/deployments/facets/DeploySignerFacet.s.sol +47 -0
  59. package/scripts/deployments/facets/DeploySimpleApp.s.sol +10 -0
  60. package/scripts/deployments/facets/DeploySpaceDelegation.s.sol +43 -0
  61. package/scripts/deployments/facets/DeploySpaceEntitlementGated.s.sol +31 -0
  62. package/scripts/deployments/facets/DeploySpaceFactoryInit.s.sol +33 -0
  63. package/scripts/deployments/facets/DeploySpaceOwnerFacet.s.sol +71 -0
  64. package/scripts/deployments/facets/DeployStreamRegistry.s.sol +50 -0
  65. package/scripts/deployments/facets/DeploySwapFacet.s.sol +33 -0
  66. package/scripts/deployments/facets/DeploySwapRouterFacet.s.sol +38 -0
  67. package/scripts/deployments/facets/DeployTipping.s.sol +31 -0
  68. package/scripts/deployments/facets/DeployTokenMigration.s.sol +40 -0
  69. package/scripts/deployments/facets/DeployTownsPoints.s.sol +53 -0
  70. package/scripts/deployments/facets/DeployTreasury.s.sol +30 -0
  71. package/scripts/deployments/facets/DeployUpgradeableBeacon.s.sol +34 -0
  72. package/scripts/deployments/facets/DeployWalletLink.s.sol +56 -0
  73. package/scripts/deployments/facets/DeployXChain.s.sol +35 -0
  74. package/scripts/deployments/utils/DeployAccountFactory.s.sol +40 -0
  75. package/scripts/deployments/utils/DeployEntitlementGatedExample.s.sol +23 -0
  76. package/scripts/deployments/utils/DeployEntrypoint.s.sol +28 -0
  77. package/scripts/deployments/utils/DeployMember.s.sol +95 -0
  78. package/scripts/deployments/utils/DeployMockDiamond.s.sol +7 -0
  79. package/scripts/deployments/utils/DeployMockERC20.s.sol +24 -0
  80. package/scripts/deployments/utils/DeployMockERC721A.s.sol +45 -0
  81. package/scripts/deployments/utils/DeployMockLegacyMembership.s.sol +29 -0
  82. package/scripts/deployments/utils/DeployMockMessenger.s.sol +42 -0
  83. package/scripts/deployments/utils/DeployMockNFT.s.sol +86 -0
  84. package/scripts/deployments/utils/DeployPoapEntitlement.s.sol +16 -0
  85. package/scripts/deployments/utils/DeployProxyBatchDelegation.s.sol +112 -0
  86. package/scripts/deployments/utils/DeploySpaceProxyInitializer.s.sol +28 -0
  87. package/scripts/deployments/utils/DeployTieredLogPricingV2.s.sol +23 -0
  88. package/scripts/deployments/utils/DeployTieredLogPricingV3.s.sol +23 -0
  89. package/scripts/deployments/utils/DeployTownsBase.s.sol +95 -0
  90. package/scripts/deployments/utils/DeployTownsMainnet.s.sol +66 -0
  91. package/scripts/deployments/utils/DeployTownsMulti.s.sol +53 -0
  92. package/scripts/deployments/utils/DeployWrappedTowns.s.sol +52 -0
  93. package/scripts/deployments/utils/LibLayerZeroValues.sol +34 -0
  94. package/scripts/deployments/utils/pricing/TieredLogPricing.s.sol +45 -0
  95. package/scripts/interactions/InteractAirdrop.s.sol +57 -0
  96. package/scripts/interactions/InteractAlphaPost.s.sol +32 -0
  97. package/scripts/interactions/InteractAlphaSparse.s.sol +173 -0
  98. package/scripts/interactions/InteractBaseAlpha.s.sol +84 -0
  99. package/scripts/interactions/InteractBaseBridge.s.sol +48 -0
  100. package/scripts/interactions/InteractBridgeLayerZero.s.sol +102 -0
  101. package/scripts/interactions/InteractClaimCondition.s.sol +56 -0
  102. package/scripts/interactions/InteractCreateSpace.s.sol +50 -0
  103. package/scripts/interactions/InteractDiamondCut.s.sol +47 -0
  104. package/scripts/interactions/InteractDropFacet.s.sol +32 -0
  105. package/scripts/interactions/InteractEnableNewSnapshotFormat.s.sol +27 -0
  106. package/scripts/interactions/InteractEnableNode2NodeAuth.s.sol +27 -0
  107. package/scripts/interactions/InteractMembership.s.sol +42 -0
  108. package/scripts/interactions/InteractMockERC721A.s.sol +20 -0
  109. package/scripts/interactions/InteractNodeOperators.s.sol +41 -0
  110. package/scripts/interactions/InteractPostDeploy.s.sol +59 -0
  111. package/scripts/interactions/InteractPrepay.s.sol +30 -0
  112. package/scripts/interactions/InteractRegisterApp.s.sol +61 -0
  113. package/scripts/interactions/InteractRiverAlpha.s.sol +30 -0
  114. package/scripts/interactions/InteractRiverAlphaSparse.s.sol +117 -0
  115. package/scripts/interactions/InteractRiverMainnet.s.sol +27 -0
  116. package/scripts/interactions/InteractRiverRegistry.s.sol +36 -0
  117. package/scripts/interactions/InteractRiverRegistrySetBlocklist.s.sol +30 -0
  118. package/scripts/interactions/InteractRiverRegistrySetFreq.s.sol +27 -0
  119. package/scripts/interactions/InteractRiverRegistrySetRepl.s.sol +30 -0
  120. package/scripts/interactions/InteractSetDefaultUri.s.sol +20 -0
  121. package/scripts/interactions/InteractSetDefaultUriLocalhost.s.sol +19 -0
  122. package/scripts/interactions/InteractTransferOwnership.s.sol +21 -0
  123. package/scripts/interactions/InteractUpdateMbRecencyCheck.s.sol +27 -0
  124. package/scripts/interactions/InteractUpdateMediaChunkCount.s.sol +27 -0
  125. package/scripts/interactions/InteractUpdateMediaChunkSize.s.sol +27 -0
  126. package/scripts/interactions/helpers/AlphaHelper.sol +149 -0
  127. package/scripts/interactions/helpers/RiverConfigValues.sol +22 -0
  128. package/scripts/interactions/interfaces/IL1StandardBridge.sol +35 -0
  129. package/scripts/interactions/interfaces/IL2StandardBridge.sol +69 -0
  130. package/src/airdrop/drop/DropBase.sol +210 -0
  131. package/src/airdrop/drop/DropClaim.sol +55 -0
  132. package/src/airdrop/drop/DropFacet.sol +176 -0
  133. package/src/airdrop/drop/DropGroup.sol +111 -0
  134. package/src/airdrop/drop/DropStorage.sol +23 -0
  135. package/src/airdrop/drop/IDropFacet.sol +136 -0
  136. package/src/airdrop/points/CheckIn.sol +71 -0
  137. package/src/airdrop/points/ITownsPoints.sol +64 -0
  138. package/src/airdrop/points/TownsPoints.sol +196 -0
  139. package/src/airdrop/points/TownsPointsStorage.sol +22 -0
  140. package/src/apps/BaseApp.sol +62 -0
  141. package/src/apps/ITownsApp.sol +28 -0
  142. package/src/apps/SchemaResolver.sol +170 -0
  143. package/src/apps/facets/attest/AttestationBase.sol +335 -0
  144. package/src/apps/facets/attest/AttestationLib.sol +64 -0
  145. package/src/apps/facets/attest/AttestationRegistry.sol +39 -0
  146. package/src/apps/facets/attest/AttestationStorage.sol +35 -0
  147. package/src/apps/facets/attest/IAttestationRegistry.sol +43 -0
  148. package/src/apps/facets/registry/AppRegistryBase.sol +403 -0
  149. package/src/apps/facets/registry/AppRegistryFacet.sol +173 -0
  150. package/src/apps/facets/registry/AppRegistryStorage.sol +53 -0
  151. package/src/apps/facets/registry/IAppRegistry.sol +159 -0
  152. package/src/apps/facets/schema/ISchema.sol +14 -0
  153. package/src/apps/facets/schema/SchemaBase.sol +88 -0
  154. package/src/apps/facets/schema/SchemaLib.sol +14 -0
  155. package/src/apps/facets/schema/SchemaRegistry.sol +51 -0
  156. package/src/apps/facets/schema/SchemaStorage.sol +34 -0
  157. package/src/apps/helpers/ISimpleApp.sol +51 -0
  158. package/src/apps/helpers/SimpleApp.sol +97 -0
  159. package/src/apps/helpers/SimpleAppStorage.sol +27 -0
  160. package/src/base/registry/facets/checker/EntitlementChecker.sol +237 -0
  161. package/src/base/registry/facets/checker/EntitlementCheckerStorage.sol +28 -0
  162. package/src/base/registry/facets/checker/IEntitlementChecker.sol +95 -0
  163. package/src/base/registry/facets/delegation/ISpaceDelegation.sol +69 -0
  164. package/src/base/registry/facets/delegation/SpaceDelegationFacet.sol +250 -0
  165. package/src/base/registry/facets/delegation/SpaceDelegationStorage.sol +35 -0
  166. package/src/base/registry/facets/distribution/v1/IRewardsDistribution.sol +51 -0
  167. package/src/base/registry/facets/distribution/v1/RewardsDistribution.sol +439 -0
  168. package/src/base/registry/facets/distribution/v1/RewardsDistributionStorage.sol +32 -0
  169. package/src/base/registry/facets/distribution/v2/DelegationProxy.sol +53 -0
  170. package/src/base/registry/facets/distribution/v2/IRewardsDistribution.sol +372 -0
  171. package/src/base/registry/facets/distribution/v2/RewardsDistributionBase.sol +299 -0
  172. package/src/base/registry/facets/distribution/v2/RewardsDistributionStorage.sol +37 -0
  173. package/src/base/registry/facets/distribution/v2/RewardsDistributionV2.sol +392 -0
  174. package/src/base/registry/facets/distribution/v2/StakingRewards.sol +466 -0
  175. package/src/base/registry/facets/mainnet/ICrossDomainMessenger.sol +58 -0
  176. package/src/base/registry/facets/mainnet/IMainnetDelegation.sol +127 -0
  177. package/src/base/registry/facets/mainnet/MainnetDelegation.sol +119 -0
  178. package/src/base/registry/facets/mainnet/MainnetDelegationBase.sol +274 -0
  179. package/src/base/registry/facets/mainnet/MainnetDelegationStorage.sol +36 -0
  180. package/src/base/registry/facets/operator/INodeOperator.sol +91 -0
  181. package/src/base/registry/facets/operator/NodeOperatorFacet.sol +189 -0
  182. package/src/base/registry/facets/operator/NodeOperatorStorage.sol +39 -0
  183. package/src/base/registry/facets/xchain/IXChain.sol +54 -0
  184. package/src/base/registry/facets/xchain/XChain.sol +158 -0
  185. package/src/base/registry/facets/xchain/XChainCheckLib.sol +105 -0
  186. package/src/base/registry/facets/xchain/XChainLib.sol +46 -0
  187. package/src/diamond/facets/beacon/UpgradeableBeacon.sol +38 -0
  188. package/src/diamond/facets/beacon/UpgradeableBeaconFacet.sol +34 -0
  189. package/src/diamond/facets/governance/votes/Checkpoints.sol +642 -0
  190. package/src/diamond/facets/governance/votes/Votes.sol +63 -0
  191. package/src/diamond/facets/governance/votes/VotesBase.sol +274 -0
  192. package/src/diamond/facets/governance/votes/VotesStorage.sol +21 -0
  193. package/src/diamond/facets/governance/votes/enumerable/IVotesEnumerable.sol +38 -0
  194. package/src/diamond/facets/governance/votes/enumerable/VotesEnumerable.sol +39 -0
  195. package/src/diamond/facets/governance/votes/enumerable/VotesEnumerableLib.sol +102 -0
  196. package/src/diamond/facets/metadata/IMetadata.sol +27 -0
  197. package/src/diamond/facets/metadata/MetadataFacet.sol +71 -0
  198. package/src/diamond/facets/token/ERC5643/ERC5643.sol +51 -0
  199. package/src/diamond/facets/token/ERC5643/ERC5643Base.sol +48 -0
  200. package/src/diamond/facets/token/ERC5643/ERC5643Storage.sol +26 -0
  201. package/src/diamond/facets/token/ERC5643/IERC5643.sol +44 -0
  202. package/src/diamond/facets/token/ERC721A/ERC721A.sol +270 -0
  203. package/src/diamond/facets/token/ERC721A/ERC721ABase.sol +829 -0
  204. package/src/diamond/facets/token/ERC721A/ERC721ANonTransferable.sol +21 -0
  205. package/src/diamond/facets/token/ERC721A/ERC721AStorage.sol +115 -0
  206. package/src/diamond/facets/token/ERC721A/IERC721A.sol +283 -0
  207. package/src/diamond/facets/token/ERC721A/extensions/ERC721AQueryable.sol +134 -0
  208. package/src/diamond/facets/token/ERC721A/extensions/IERC721AQueryable.sol +83 -0
  209. package/src/diamond/utils/Context.sol +19 -0
  210. package/src/factory/SpaceFactoryInit.sol +17 -0
  211. package/src/factory/facets/architect/Architect.sol +98 -0
  212. package/src/factory/facets/architect/ArchitectBase.sol +95 -0
  213. package/src/factory/facets/architect/ArchitectStorage.sol +28 -0
  214. package/src/factory/facets/architect/IArchitect.sol +155 -0
  215. package/src/factory/facets/architect/ImplementationStorage.sol +42 -0
  216. package/src/factory/facets/architect/pricing/IPricingModules.sol +41 -0
  217. package/src/factory/facets/architect/pricing/PricingModulesBase.sol +89 -0
  218. package/src/factory/facets/architect/pricing/PricingModulesFacet.sol +40 -0
  219. package/src/factory/facets/architect/pricing/PricingModulesStorage.sol +30 -0
  220. package/src/factory/facets/create/CreateSpace.sol +107 -0
  221. package/src/factory/facets/create/CreateSpaceLib.sol +335 -0
  222. package/src/factory/facets/create/ICreateSpace.sol +70 -0
  223. package/src/factory/facets/feature/FeatureConditionLib.sol +53 -0
  224. package/src/factory/facets/feature/FeatureManagerFacet.sol +66 -0
  225. package/src/factory/facets/feature/FeatureManagerLib.sol +168 -0
  226. package/src/factory/facets/feature/IFeatureManagerFacet.sol +73 -0
  227. package/src/factory/facets/partner/IPartnerRegistry.sol +56 -0
  228. package/src/factory/facets/partner/PartnerRegistry.sol +57 -0
  229. package/src/factory/facets/partner/PartnerRegistryBase.sol +132 -0
  230. package/src/factory/facets/partner/PartnerRegistryStorage.sol +40 -0
  231. package/src/factory/facets/platform/requirements/IPlatformRequirements.sol +143 -0
  232. package/src/factory/facets/platform/requirements/PlatformRequirementsBase.sol +124 -0
  233. package/src/factory/facets/platform/requirements/PlatformRequirementsFacet.sol +122 -0
  234. package/src/factory/facets/platform/requirements/PlatformRequirementsStorage.sol +41 -0
  235. package/src/factory/facets/registry/IImplementationRegistry.sol +46 -0
  236. package/src/factory/facets/registry/ImplementationRegistry.sol +64 -0
  237. package/src/factory/facets/registry/ImplementationRegistryStorage.sol +28 -0
  238. package/src/factory/facets/wallet-link/IWalletLink.sol +218 -0
  239. package/src/factory/facets/wallet-link/WalletLink.sol +108 -0
  240. package/src/factory/facets/wallet-link/WalletLinkBase.sol +492 -0
  241. package/src/factory/facets/wallet-link/interfaces/IDelegateRegistry.sol +63 -0
  242. package/src/factory/facets/wallet-link/interfaces/IDelegateRegistryV1.sol +35 -0
  243. package/src/factory/facets/wallet-link/interfaces/ISCL_EIP6565.sol +24 -0
  244. package/src/factory/facets/wallet-link/libraries/SolanaUtils.sol +161 -0
  245. package/src/factory/facets/wallet-link/libraries/WalletLib.sol +62 -0
  246. package/src/river/registry/facets/config/IRiverConfig.sol +117 -0
  247. package/src/river/registry/facets/config/RiverConfig.sol +174 -0
  248. package/src/river/registry/facets/node/INodeRegistry.sol +69 -0
  249. package/src/river/registry/facets/node/NodeRegistry.sol +143 -0
  250. package/src/river/registry/facets/operator/IOperatorRegistry.sol +28 -0
  251. package/src/river/registry/facets/operator/OperatorRegistry.sol +86 -0
  252. package/src/river/registry/facets/stream/IStreamRegistry.sol +184 -0
  253. package/src/river/registry/facets/stream/StreamRegistry.sol +396 -0
  254. package/src/river/registry/libraries/RegistryErrors.sol +24 -0
  255. package/src/river/registry/libraries/RegistryStorage.sol +181 -0
  256. package/src/router/ISwapRouter.sol +226 -0
  257. package/src/router/Permit2Hash.sol +92 -0
  258. package/src/router/SwapRouter.sol +463 -0
  259. package/src/router/SwapRouterStorage.sol +21 -0
  260. package/src/spaces/entitlements/ICrossChainEntitlement.sol +22 -0
  261. package/src/spaces/entitlements/IEntitlement.sol +61 -0
  262. package/src/spaces/entitlements/PolymarketEntitlement.sol +79 -0
  263. package/src/spaces/entitlements/poap/IPOAP.sol +26 -0
  264. package/src/spaces/entitlements/poap/PoapEntitlement.sol +56 -0
  265. package/src/spaces/entitlements/rule/IRuleEntitlement.sol +174 -0
  266. package/src/spaces/entitlements/rule/RuleEntitlement.sol +183 -0
  267. package/src/spaces/entitlements/rule/RuleEntitlementV2.sol +219 -0
  268. package/src/spaces/entitlements/user/IUserEntitlement.sol +24 -0
  269. package/src/spaces/entitlements/user/UserEntitlement.sol +273 -0
  270. package/src/spaces/facets/DependencyLib.sol +60 -0
  271. package/src/spaces/facets/Entitled.sol +172 -0
  272. package/src/spaces/facets/Permissions.sol +21 -0
  273. package/src/spaces/facets/account/AppAccount.sol +93 -0
  274. package/src/spaces/facets/account/AppAccountBase.sol +275 -0
  275. package/src/spaces/facets/account/AppAccountStorage.sol +63 -0
  276. package/src/spaces/facets/account/IAppAccount.sol +71 -0
  277. package/src/spaces/facets/account/SignerFacet.sol +26 -0
  278. package/src/spaces/facets/banning/Banning.sol +41 -0
  279. package/src/spaces/facets/banning/BanningBase.sol +33 -0
  280. package/src/spaces/facets/banning/BanningStorage.sol +23 -0
  281. package/src/spaces/facets/banning/IBanning.sol +35 -0
  282. package/src/spaces/facets/channels/ChannelBase.sol +87 -0
  283. package/src/spaces/facets/channels/ChannelService.sol +177 -0
  284. package/src/spaces/facets/channels/ChannelStorage.sol +34 -0
  285. package/src/spaces/facets/channels/Channels.sol +80 -0
  286. package/src/spaces/facets/channels/IChannel.sol +98 -0
  287. package/src/spaces/facets/dispatcher/DispatcherBase.sol +86 -0
  288. package/src/spaces/facets/dispatcher/DispatcherStorage.sol +21 -0
  289. package/src/spaces/facets/dispatcher/IDispatcher.sol +6 -0
  290. package/src/spaces/facets/entitlements/EntitlementsManager.sol +49 -0
  291. package/src/spaces/facets/entitlements/EntitlementsManagerBase.sol +87 -0
  292. package/src/spaces/facets/entitlements/EntitlementsManagerService.sol +142 -0
  293. package/src/spaces/facets/entitlements/EntitlementsManagerStorage.sol +34 -0
  294. package/src/spaces/facets/entitlements/IEntitlementsManager.sol +67 -0
  295. package/src/spaces/facets/entitlements/extensions/EntitlementDataQueryable.sol +153 -0
  296. package/src/spaces/facets/entitlements/extensions/IEntitlementDataQueryable.sol +32 -0
  297. package/src/spaces/facets/executor/ExecutorBase.sol +564 -0
  298. package/src/spaces/facets/executor/ExecutorFacet.sol +178 -0
  299. package/src/spaces/facets/executor/ExecutorStorage.sol +99 -0
  300. package/src/spaces/facets/executor/GroupLib.sol +128 -0
  301. package/src/spaces/facets/executor/IExecutor.sol +287 -0
  302. package/src/spaces/facets/executor/hooks/HookBase.sol +172 -0
  303. package/src/spaces/facets/executor/hooks/HookLib.sol +38 -0
  304. package/src/spaces/facets/executor/hooks/IHookBase.sol +48 -0
  305. package/src/spaces/facets/gated/EntitlementGated.sol +59 -0
  306. package/src/spaces/facets/gated/EntitlementGatedBase.sol +324 -0
  307. package/src/spaces/facets/gated/EntitlementGatedStorage.sol +29 -0
  308. package/src/spaces/facets/gated/IEntitlementGated.sol +55 -0
  309. package/src/spaces/facets/guardian/GuardianBase.sol +80 -0
  310. package/src/spaces/facets/guardian/GuardianFacet.sol +43 -0
  311. package/src/spaces/facets/guardian/GuardianStorage.sol +27 -0
  312. package/src/spaces/facets/guardian/IGuardian.sol +54 -0
  313. package/src/spaces/facets/membership/IMembership.sol +216 -0
  314. package/src/spaces/facets/membership/MembershipBase.sol +272 -0
  315. package/src/spaces/facets/membership/MembershipFacet.sol +191 -0
  316. package/src/spaces/facets/membership/MembershipStorage.sol +40 -0
  317. package/src/spaces/facets/membership/join/MembershipJoin.sol +547 -0
  318. package/src/spaces/facets/membership/metadata/IMembershipMetadata.sol +9 -0
  319. package/src/spaces/facets/membership/metadata/MembershipMetadata.sol +32 -0
  320. package/src/spaces/facets/membership/pricing/IMembershipPricing.sol +18 -0
  321. package/src/spaces/facets/membership/pricing/fixed/FixedPricing.sol +29 -0
  322. package/src/spaces/facets/membership/pricing/fixed/FixedPricingStorage.sol +27 -0
  323. package/src/spaces/facets/membership/pricing/tiered/TieredLogPricingOracleV2.sol +148 -0
  324. package/src/spaces/facets/membership/pricing/tiered/TieredLogPricingOracleV3.sol +137 -0
  325. package/src/spaces/facets/membership/token/MembershipToken.sol +25 -0
  326. package/src/spaces/facets/owner/ISpaceOwner.sol +85 -0
  327. package/src/spaces/facets/owner/SpaceOwner.sol +174 -0
  328. package/src/spaces/facets/owner/SpaceOwnerBase.sol +121 -0
  329. package/src/spaces/facets/owner/SpaceOwnerStorage.sol +41 -0
  330. package/src/spaces/facets/owner/SpaceOwnerUriBase.sol +54 -0
  331. package/src/spaces/facets/points/PointsBase.sol +35 -0
  332. package/src/spaces/facets/prepay/IPrepay.sol +43 -0
  333. package/src/spaces/facets/prepay/PrepayBase.sol +27 -0
  334. package/src/spaces/facets/prepay/PrepayFacet.sol +59 -0
  335. package/src/spaces/facets/prepay/PrepayStorage.sol +26 -0
  336. package/src/spaces/facets/proxy/ISpaceProxyInitializer.sol +21 -0
  337. package/src/spaces/facets/proxy/SpaceProxy.sol +15 -0
  338. package/src/spaces/facets/proxy/SpaceProxyInitializer.sol +55 -0
  339. package/src/spaces/facets/referrals/IReferrals.sol +98 -0
  340. package/src/spaces/facets/referrals/ReferralsBase.sol +81 -0
  341. package/src/spaces/facets/referrals/ReferralsFacet.sol +65 -0
  342. package/src/spaces/facets/referrals/ReferralsStorage.sol +36 -0
  343. package/src/spaces/facets/review/IReview.sol +50 -0
  344. package/src/spaces/facets/review/ReviewFacet.sol +105 -0
  345. package/src/spaces/facets/review/ReviewStorage.sol +29 -0
  346. package/src/spaces/facets/roles/IRoles.sol +197 -0
  347. package/src/spaces/facets/roles/Roles.sol +123 -0
  348. package/src/spaces/facets/roles/RolesBase.sol +420 -0
  349. package/src/spaces/facets/roles/RolesStorage.sol +132 -0
  350. package/src/spaces/facets/swap/ISwapFacet.sol +91 -0
  351. package/src/spaces/facets/swap/SwapFacet.sol +290 -0
  352. package/src/spaces/facets/swap/SwapFacetStorage.sol +24 -0
  353. package/src/spaces/facets/tipping/ITipping.sol +80 -0
  354. package/src/spaces/facets/tipping/TippingBase.sol +73 -0
  355. package/src/spaces/facets/tipping/TippingFacet.sol +123 -0
  356. package/src/spaces/facets/treasury/ITreasury.sol +64 -0
  357. package/src/spaces/facets/treasury/Treasury.sol +82 -0
  358. package/src/spaces/facets/xchain/SpaceEntitlementGated.sol +62 -0
  359. package/src/tokens/Member.sol +246 -0
  360. package/src/tokens/lock/ILock.sol +42 -0
  361. package/src/tokens/lock/LockBase.sol +64 -0
  362. package/src/tokens/lock/LockFacet.sol +44 -0
  363. package/src/tokens/lock/LockStorage.sol +26 -0
  364. package/src/tokens/mainnet/claimer/AuthorizedClaimerStorage.sol +26 -0
  365. package/src/tokens/mainnet/claimer/AuthorizedClaimers.sol +84 -0
  366. package/src/tokens/mainnet/claimer/IAuthorizedClaimers.sol +36 -0
  367. package/src/tokens/mainnet/delegation/ProxyBatchDelegation.sol +86 -0
  368. package/src/tokens/migration/ITokenMigration.sol +35 -0
  369. package/src/tokens/migration/TokenMigrationFacet.sol +86 -0
  370. package/src/tokens/migration/TokenMigrationStorage.sol +27 -0
  371. package/src/tokens/towns/base/IERC7802.sol +30 -0
  372. package/src/tokens/towns/base/IOptimismMintableERC20.sol +31 -0
  373. package/src/tokens/towns/base/ISemver.sol +13 -0
  374. package/src/tokens/towns/base/Towns.sol +283 -0
  375. package/src/tokens/towns/base/TownsDeployer.sol +32 -0
  376. package/src/tokens/towns/base/TownsLib.sol +31 -0
  377. package/src/tokens/towns/base/versions/TownsV2.sol +15 -0
  378. package/src/tokens/towns/mainnet/ITowns.sol +56 -0
  379. package/src/tokens/towns/mainnet/Towns.sol +220 -0
  380. package/src/tokens/towns/mainnet/libs/TokenInflationLib.sol +89 -0
  381. package/src/tokens/towns/multichain/Towns.sol +19 -0
  382. package/src/tokens/towns/multichain/wTowns.sol +18 -0
  383. package/src/utils/Airdrop.sol +156 -0
  384. package/src/utils/airdrop/merkle/IMerkleAirdrop.sol +55 -0
  385. package/src/utils/airdrop/merkle/MerkleAirdrop.sol +118 -0
  386. package/src/utils/airdrop/merkle/MerkleAirdropStorage.sol +29 -0
  387. package/src/utils/interfaces/AggregatorV3Interface.sol +37 -0
  388. package/src/utils/interfaces/IMulticall.sol +10 -0
  389. package/src/utils/interfaces/IWETH.sol +10 -0
  390. package/src/utils/libraries/BasisPoints.sol +24 -0
  391. package/src/utils/libraries/Create2Utils.sol +74 -0
  392. package/src/utils/libraries/CurrencyTransfer.sol +99 -0
  393. package/src/utils/libraries/CustomRevert.sol +49 -0
  394. package/src/utils/libraries/Factory.sol +66 -0
  395. package/src/utils/libraries/StringSet.sol +190 -0
  396. package/src/utils/libraries/Validator.sol +31 -0
@@ -0,0 +1,829 @@
1
+ // SPDX-License-Identifier: MIT
2
+ pragma solidity ^0.8.23;
3
+
4
+ // interfaces
5
+ import {ERC721A__IERC721ReceiverUpgradeable, IERC721ABase} from "./IERC721A.sol";
6
+
7
+ // libraries
8
+ import {ERC721AStorage} from "./ERC721AStorage.sol";
9
+
10
+ // contracts
11
+
12
+ abstract contract ERC721ABase is IERC721ABase {
13
+ // =============================================================
14
+ // CONSTANTS
15
+ // =============================================================
16
+
17
+ // The bit position of `numberMinted` in packed address data.
18
+ uint256 internal constant _BITPOS_NUMBER_MINTED = 64;
19
+
20
+ // The bit position of `numberBurned` in packed address data.
21
+ uint256 internal constant _BITPOS_NUMBER_BURNED = 128;
22
+
23
+ // The bit position of `aux` in packed address data.
24
+ uint256 internal constant _BITPOS_AUX = 192;
25
+
26
+ // Mask of all 256 bits in packed address data except the 64 bits for `aux`.
27
+ uint256 internal constant _BITMASK_AUX_COMPLEMENT = (1 << 192) - 1;
28
+
29
+ // The bit position of `startTimestamp` in packed ownership.
30
+ uint256 internal constant _BITPOS_START_TIMESTAMP = 160;
31
+
32
+ // The bit position of the `nextInitialized` bit in packed ownership.
33
+ uint256 internal constant _BITPOS_NEXT_INITIALIZED = 225;
34
+
35
+ // The bit mask of the `nextInitialized` bit in packed ownership.
36
+ uint256 internal constant _BITMASK_NEXT_INITIALIZED = 1 << 225;
37
+
38
+ // The bit position of `extraData` in packed ownership.
39
+ uint256 internal constant _BITPOS_EXTRA_DATA = 232;
40
+
41
+ // Mask of all 256 bits in a packed ownership except the 24 bits for `extraData`.
42
+ uint256 internal constant _BITMASK_EXTRA_DATA_COMPLEMENT = (1 << 232) - 1;
43
+
44
+ // The mask of the lower 160 bits for addresses.
45
+ uint256 internal constant _BITMASK_ADDRESS = (1 << 160) - 1;
46
+
47
+ // The maximum `quantity` that can be minted with {_mintERC2309}.
48
+ // This limit is to prevent overflows on the address data entries.
49
+ // For a limit of 5000, a total of 3.689e15 calls to {_mintERC2309}
50
+ // is required to cause an overflow, which is unrealistic.
51
+ uint256 internal constant _MAX_MINT_ERC2309_QUANTITY_LIMIT = 5000;
52
+
53
+ // The `Transfer` event signature is given by:
54
+ // `keccak256(bytes("Transfer(address,address,uint256)"))`.
55
+ bytes32 internal constant _TRANSFER_EVENT_SIGNATURE =
56
+ 0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef;
57
+
58
+ function __ERC721ABase_init(string memory name, string memory symbol) internal {
59
+ ERC721AStorage.Layout storage ds = ERC721AStorage.layout();
60
+
61
+ ds._name = name;
62
+ ds._symbol = symbol;
63
+ ds._currentIndex = _startTokenId();
64
+ }
65
+
66
+ // =============================================================
67
+ // EXTERNAL
68
+ // =============================================================
69
+ function _totalSupply() internal view returns (uint256) {
70
+ // Counter underflow is impossible as _burnCounter cannot be incremented
71
+ // more than `_currentIndex - _startTokenId()` times.
72
+ unchecked {
73
+ return
74
+ ERC721AStorage.layout()._currentIndex -
75
+ ERC721AStorage.layout()._burnCounter -
76
+ _startTokenId();
77
+ }
78
+ }
79
+
80
+ function _balanceOf(address owner) internal view returns (uint256) {
81
+ return ERC721AStorage.balanceOf(owner);
82
+ }
83
+
84
+ // =============================================================
85
+ // TOKEN COUNTING OPERATIONS
86
+ // =============================================================
87
+
88
+ /**
89
+ * @dev Returns the starting token ID.
90
+ * To change the starting token ID, please override this function.
91
+ */
92
+ function _startTokenId() internal view virtual returns (uint256) {
93
+ return 0;
94
+ }
95
+
96
+ /**
97
+ * @dev Returns the next token ID to be minted.
98
+ */
99
+ function _nextTokenId() internal view virtual returns (uint256) {
100
+ return ERC721AStorage.layout()._currentIndex;
101
+ }
102
+
103
+ /**
104
+ * @dev Returns the total amount of tokens minted in the contract.
105
+ */
106
+ function _totalMinted() internal view virtual returns (uint256) {
107
+ // Counter underflow is impossible as `_currentIndex` does not decrement,
108
+ // and it is initialized to `_startTokenId()`.
109
+ unchecked {
110
+ return ERC721AStorage.layout()._currentIndex - _startTokenId();
111
+ }
112
+ }
113
+
114
+ /**
115
+ * @dev Returns the total number of tokens burned.
116
+ */
117
+ function _totalBurned() internal view virtual returns (uint256) {
118
+ return ERC721AStorage.layout()._burnCounter;
119
+ }
120
+
121
+ // =============================================================
122
+ // ADDRESS DATA OPERATIONS
123
+ // =============================================================
124
+
125
+ /**
126
+ * Returns the number of tokens minted by `owner`.
127
+ */
128
+ function _numberMinted(address owner) internal view returns (uint256) {
129
+ return
130
+ (ERC721AStorage.layout()._packedAddressData[owner] >> _BITPOS_NUMBER_MINTED) &
131
+ ERC721AStorage._BITMASK_ADDRESS_DATA_ENTRY;
132
+ }
133
+
134
+ /**
135
+ * Returns the number of tokens burned by or on behalf of `owner`.
136
+ */
137
+ function _numberBurned(address owner) internal view returns (uint256) {
138
+ return
139
+ (ERC721AStorage.layout()._packedAddressData[owner] >> _BITPOS_NUMBER_BURNED) &
140
+ ERC721AStorage._BITMASK_ADDRESS_DATA_ENTRY;
141
+ }
142
+
143
+ /**
144
+ * Returns the auxiliary data for `owner`. (e.g. number of whitelist mint slots used).
145
+ */
146
+ function _getAux(address owner) internal view returns (uint64) {
147
+ return uint64(ERC721AStorage.layout()._packedAddressData[owner] >> _BITPOS_AUX);
148
+ }
149
+
150
+ /**
151
+ * Sets the auxiliary data for `owner`. (e.g. number of whitelist mint slots used).
152
+ * If there are multiple variables, please pack them into a uint64.
153
+ */
154
+ function _setAux(address owner, uint64 aux) internal virtual {
155
+ uint256 packed = ERC721AStorage.layout()._packedAddressData[owner];
156
+ uint256 auxCasted;
157
+ // Cast `aux` with assembly to avoid redundant masking.
158
+ assembly {
159
+ auxCasted := aux
160
+ }
161
+ packed = (packed & _BITMASK_AUX_COMPLEMENT) | (auxCasted << _BITPOS_AUX);
162
+ ERC721AStorage.layout()._packedAddressData[owner] = packed;
163
+ }
164
+
165
+ /**
166
+ * @dev Base URI for computing {tokenURI}. If set, the resulting URI for each
167
+ * token will be the concatenation of the `baseURI` and the `tokenId`. Empty
168
+ * by default, it can be overridden in child contracts.
169
+ */
170
+ function _baseURI() internal view virtual returns (string memory) {
171
+ return "";
172
+ }
173
+
174
+ // =============================================================
175
+ // OWNERSHIPS OPERATIONS
176
+ // =============================================================
177
+
178
+ /**
179
+ * @dev Gas spent here starts off proportional to the maximum mint batch size.
180
+ * It gradually moves to O(1) as tokens get transferred around over time.
181
+ */
182
+ function _ownershipOf(uint256 tokenId) internal view virtual returns (TokenOwnership memory) {
183
+ return _unpackedOwnership(ERC721AStorage.packedOwnershipOf(_startTokenId(), tokenId));
184
+ }
185
+
186
+ /**
187
+ * @dev Returns the unpacked `TokenOwnership` struct at `index`.
188
+ */
189
+ function _ownershipAt(uint256 index) internal view virtual returns (TokenOwnership memory) {
190
+ return _unpackedOwnership(ERC721AStorage.layout()._packedOwnerships[index]);
191
+ }
192
+
193
+ /**
194
+ * @dev Initializes the ownership slot minted at `index` for efficiency purposes.
195
+ */
196
+ function _initializeOwnershipAt(uint256 index) internal virtual {
197
+ if (ERC721AStorage.layout()._packedOwnerships[index] == 0) {
198
+ ERC721AStorage.layout()._packedOwnerships[index] = ERC721AStorage.packedOwnershipOf(
199
+ _startTokenId(),
200
+ index
201
+ );
202
+ }
203
+ }
204
+
205
+ /**
206
+ * @dev Returns the unpacked `TokenOwnership` struct from `packed`.
207
+ */
208
+ function _unpackedOwnership(
209
+ uint256 packed
210
+ ) internal pure returns (TokenOwnership memory ownership) {
211
+ ownership.addr = address(uint160(packed));
212
+ ownership.startTimestamp = uint64(packed >> _BITPOS_START_TIMESTAMP);
213
+ ownership.burned = packed & ERC721AStorage._BITMASK_BURNED != 0;
214
+ ownership.extraData = uint24(packed >> _BITPOS_EXTRA_DATA);
215
+ }
216
+
217
+ /**
218
+ * @dev Packs ownership data into a single uint256.
219
+ */
220
+ function _packOwnershipData(
221
+ address owner,
222
+ uint256 flags
223
+ ) internal view returns (uint256 result) {
224
+ assembly {
225
+ // Mask `owner` to the lower 160 bits, in case the upper bits somehow aren't clean.
226
+ owner := and(owner, _BITMASK_ADDRESS)
227
+ // `owner | (block.timestamp << _BITPOS_START_TIMESTAMP) | flags`.
228
+ result := or(owner, or(shl(_BITPOS_START_TIMESTAMP, timestamp()), flags))
229
+ }
230
+ }
231
+
232
+ /**
233
+ * @dev Returns the `nextInitialized` flag set if `quantity` equals 1.
234
+ */
235
+ function _nextInitializedFlag(uint256 quantity) internal pure returns (uint256 result) {
236
+ // For branchless setting of the `nextInitialized` flag.
237
+ assembly {
238
+ // `(quantity == 1) << _BITPOS_NEXT_INITIALIZED`.
239
+ result := shl(_BITPOS_NEXT_INITIALIZED, eq(quantity, 1))
240
+ }
241
+ }
242
+
243
+ // =============================================================
244
+ // APPROVAL OPERATIONS
245
+ // =============================================================
246
+
247
+ /**
248
+ * @dev Returns whether `tokenId` exists.
249
+ *
250
+ * Tokens can be managed by their owner or approved accounts via {approve} or
251
+ * {setApprovalForAll}.
252
+ *
253
+ * Tokens start existing when they are minted. See {_mint}.
254
+ */
255
+ function _exists(uint256 tokenId) internal view virtual returns (bool) {
256
+ return
257
+ _startTokenId() <= tokenId &&
258
+ tokenId < ERC721AStorage.layout()._currentIndex && // If
259
+ // within bounds,
260
+ ERC721AStorage.layout()._packedOwnerships[tokenId] & ERC721AStorage._BITMASK_BURNED ==
261
+ 0; // and not
262
+ // burned.
263
+ }
264
+
265
+ /**
266
+ * @dev Returns whether `msgSender` is equal to `approvedAddress` or `owner`.
267
+ */
268
+ function _isSenderApprovedOrOwner(
269
+ address approvedAddress,
270
+ address owner,
271
+ address msgSender
272
+ ) internal pure returns (bool result) {
273
+ assembly {
274
+ // Mask `owner` to the lower 160 bits, in case the upper bits somehow aren't clean.
275
+ owner := and(owner, _BITMASK_ADDRESS)
276
+ // Mask `msgSender` to the lower 160 bits, in case the upper bits somehow aren't clean.
277
+ msgSender := and(msgSender, _BITMASK_ADDRESS)
278
+ // `msgSender == owner || msgSender == approvedAddress`.
279
+ result := or(eq(msgSender, owner), eq(msgSender, approvedAddress))
280
+ }
281
+ }
282
+
283
+ /**
284
+ * @dev Returns the storage slot and value for the approved address of `tokenId`.
285
+ */
286
+ function _getApprovedSlotAndAddress(
287
+ uint256 tokenId
288
+ ) internal view returns (uint256 approvedAddressSlot, address approvedAddress) {
289
+ TokenApprovalRef storage tokenApproval = ERC721AStorage.layout()._tokenApprovals[tokenId];
290
+
291
+ // The following is equivalent to `approvedAddress = _tokenApprovals[tokenId].value`.
292
+ assembly {
293
+ approvedAddressSlot := tokenApproval.slot
294
+ approvedAddress := sload(approvedAddressSlot)
295
+ }
296
+ }
297
+
298
+ // =============================================================
299
+ // TRANSFER OPERATIONS
300
+ // =============================================================
301
+
302
+ /**
303
+ * @dev Hook that is called before a set of serially-ordered token IDs
304
+ * are about to be transferred. This includes minting.
305
+ * And also called before burning one token.
306
+ *
307
+ * `startTokenId` - the first token ID to be transferred.
308
+ * `quantity` - the amount to be transferred.
309
+ *
310
+ * Calling conditions:
311
+ *
312
+ * - When `from` and `to` are both non-zero, `from`'s `tokenId` will be
313
+ * transferred to `to`.
314
+ * - When `from` is zero, `tokenId` will be minted for `to`.
315
+ * - When `to` is zero, `tokenId` will be burned by `from`.
316
+ * - `from` and `to` are never both zero.
317
+ */
318
+ function _beforeTokenTransfers(
319
+ address from,
320
+ address to,
321
+ uint256 startTokenId,
322
+ uint256 quantity
323
+ ) internal virtual {}
324
+
325
+ /**
326
+ * @dev Hook that is called after a set of serially-ordered token IDs
327
+ * have been transferred. This includes minting.
328
+ * And also called after one token has been burned.
329
+ *
330
+ * `startTokenId` - the first token ID to be transferred.
331
+ * `quantity` - the amount to be transferred.
332
+ *
333
+ * Calling conditions:
334
+ *
335
+ * - When `from` and `to` are both non-zero, `from`'s `tokenId` has been
336
+ * transferred to `to`.
337
+ * - When `from` is zero, `tokenId` has been minted for `to`.
338
+ * - When `to` is zero, `tokenId` has been burned by `from`.
339
+ * - `from` and `to` are never both zero.
340
+ */
341
+ function _afterTokenTransfers(
342
+ address from,
343
+ address to,
344
+ uint256 startTokenId,
345
+ uint256 quantity
346
+ ) internal virtual {}
347
+
348
+ /**
349
+ * @dev Private function to invoke {IERC721Receiver-onERC721Received} on a target contract.
350
+ *
351
+ * `from` - Previous owner of the given token ID.
352
+ * `to` - Target address that will receive the token.
353
+ * `tokenId` - Token ID to be transferred.
354
+ * `_data` - Optional data to send along with the call.
355
+ *
356
+ * Returns whether the call correctly returned the expected magic value.
357
+ */
358
+ function _checkContractOnERC721Received(
359
+ address from,
360
+ address to,
361
+ uint256 tokenId,
362
+ bytes memory _data
363
+ ) internal returns (bool) {
364
+ try
365
+ ERC721A__IERC721ReceiverUpgradeable(to).onERC721Received(
366
+ _msgSenderERC721A(),
367
+ from,
368
+ tokenId,
369
+ _data
370
+ )
371
+ returns (bytes4 retval) {
372
+ return retval == ERC721A__IERC721ReceiverUpgradeable(to).onERC721Received.selector;
373
+ } catch (bytes memory reason) {
374
+ if (reason.length == 0) {
375
+ revert TransferToNonERC721ReceiverImplementer();
376
+ } else {
377
+ assembly {
378
+ revert(add(32, reason), mload(reason))
379
+ }
380
+ }
381
+ }
382
+ }
383
+
384
+ // =============================================================
385
+ // MINT OPERATIONS
386
+ // =============================================================
387
+
388
+ /**
389
+ * @dev Mints `quantity` tokens and transfers them to `to`.
390
+ *
391
+ * Requirements:
392
+ *
393
+ * - `to` cannot be the zero address.
394
+ * - `quantity` must be greater than 0.
395
+ *
396
+ * Emits a {Transfer} event for each mint.
397
+ */
398
+ function _mint(address to, uint256 quantity) internal virtual {
399
+ uint256 startTokenId = ERC721AStorage.layout()._currentIndex;
400
+ if (quantity == 0) revert MintZeroQuantity();
401
+
402
+ _beforeTokenTransfers(address(0), to, startTokenId, quantity);
403
+
404
+ // Overflows are incredibly unrealistic.
405
+ // `balance` and `numberMinted` have a maximum limit of 2**64.
406
+ // `tokenId` has a maximum limit of 2**256.
407
+ unchecked {
408
+ // Updates:
409
+ // - `balance += quantity`.
410
+ // - `numberMinted += quantity`.
411
+ //
412
+ // We can directly add to the `balance` and `numberMinted`.
413
+ ERC721AStorage.layout()._packedAddressData[to] +=
414
+ quantity *
415
+ ((1 << _BITPOS_NUMBER_MINTED) | 1);
416
+
417
+ // Updates:
418
+ // - `address` to the owner.
419
+ // - `startTimestamp` to the timestamp of minting.
420
+ // - `burned` to `false`.
421
+ // - `nextInitialized` to `quantity == 1`.
422
+ ERC721AStorage.layout()._packedOwnerships[startTokenId] = _packOwnershipData(
423
+ to,
424
+ _nextInitializedFlag(quantity) | _nextExtraData(address(0), to, 0)
425
+ );
426
+
427
+ uint256 toMasked;
428
+ uint256 end = startTokenId + quantity;
429
+
430
+ // Use assembly to loop and emit the `Transfer` event for gas savings.
431
+ // The duplicated `log4` removes an extra check and reduces stack juggling.
432
+ // The assembly, together with the surrounding Solidity code, have been
433
+ // delicately arranged to nudge the compiler into producing optimized opcodes.
434
+ assembly {
435
+ // Mask `to` to the lower 160 bits, in case the upper bits somehow aren't clean.
436
+ toMasked := and(to, _BITMASK_ADDRESS)
437
+ // Emit the `Transfer` event.
438
+ log4(
439
+ 0, // Start of data (0, since no data).
440
+ 0, // End of data (0, since no data).
441
+ _TRANSFER_EVENT_SIGNATURE, // Signature.
442
+ 0, // `address(0)`.
443
+ toMasked, // `to`.
444
+ startTokenId // `tokenId`.
445
+ )
446
+
447
+ // The `iszero(eq(,))` check ensures that large values of `quantity`
448
+ // that overflows uint256 will make the loop run out of gas.
449
+ // The compiler will optimize the `iszero` away for performance.
450
+ for {
451
+ let tokenId := add(startTokenId, 1)
452
+ } iszero(eq(tokenId, end)) {
453
+ tokenId := add(tokenId, 1)
454
+ } {
455
+ // Emit the `Transfer` event. Similar to above.
456
+ log4(0, 0, _TRANSFER_EVENT_SIGNATURE, 0, toMasked, tokenId)
457
+ }
458
+ }
459
+ if (toMasked == 0) revert MintToZeroAddress();
460
+
461
+ ERC721AStorage.layout()._currentIndex = end;
462
+ }
463
+ _afterTokenTransfers(address(0), to, startTokenId, quantity);
464
+ }
465
+
466
+ /**
467
+ * @dev Mints `quantity` tokens and transfers them to `to`.
468
+ *
469
+ * This function is intended for efficient minting only during contract creation.
470
+ *
471
+ * It emits only one {ConsecutiveTransfer} as defined in
472
+ * [ERC2309](https://eips.ethereum.org/EIPS/eip-2309),
473
+ * instead of a sequence of {Transfer} event(s).
474
+ *
475
+ * Calling this function outside of contract creation WILL make your contract
476
+ * non-compliant with the ERC721 standard.
477
+ * For full ERC721 compliance, substituting ERC721 {Transfer} event(s) with the ERC2309
478
+ * {ConsecutiveTransfer} event is only permissible during contract creation.
479
+ *
480
+ * Requirements:
481
+ *
482
+ * - `to` cannot be the zero address.
483
+ * - `quantity` must be greater than 0.
484
+ *
485
+ * Emits a {ConsecutiveTransfer} event.
486
+ */
487
+ function _mintERC2309(address to, uint256 quantity) internal virtual {
488
+ uint256 startTokenId = ERC721AStorage.layout()._currentIndex;
489
+ if (to == address(0)) revert MintToZeroAddress();
490
+ if (quantity == 0) revert MintZeroQuantity();
491
+ if (quantity > _MAX_MINT_ERC2309_QUANTITY_LIMIT) {
492
+ revert MintERC2309QuantityExceedsLimit();
493
+ }
494
+
495
+ _beforeTokenTransfers(address(0), to, startTokenId, quantity);
496
+
497
+ // Overflows are unrealistic due to the above check for `quantity` to be below the limit.
498
+ unchecked {
499
+ // Updates:
500
+ // - `balance += quantity`.
501
+ // - `numberMinted += quantity`.
502
+ //
503
+ // We can directly add to the `balance` and `numberMinted`.
504
+ ERC721AStorage.layout()._packedAddressData[to] +=
505
+ quantity *
506
+ ((1 << _BITPOS_NUMBER_MINTED) | 1);
507
+
508
+ // Updates:
509
+ // - `address` to the owner.
510
+ // - `startTimestamp` to the timestamp of minting.
511
+ // - `burned` to `false`.
512
+ // - `nextInitialized` to `quantity == 1`.
513
+ ERC721AStorage.layout()._packedOwnerships[startTokenId] = _packOwnershipData(
514
+ to,
515
+ _nextInitializedFlag(quantity) | _nextExtraData(address(0), to, 0)
516
+ );
517
+
518
+ emit ConsecutiveTransfer(startTokenId, startTokenId + quantity - 1, address(0), to);
519
+
520
+ ERC721AStorage.layout()._currentIndex = startTokenId + quantity;
521
+ }
522
+ _afterTokenTransfers(address(0), to, startTokenId, quantity);
523
+ }
524
+
525
+ /**
526
+ * @dev Safely mints `quantity` tokens and transfers them to `to`.
527
+ *
528
+ * Requirements:
529
+ *
530
+ * - If `to` refers to a smart contract, it must implement
531
+ * {IERC721Receiver-onERC721Received}, which is called for each safe transfer.
532
+ * - `quantity` must be greater than 0.
533
+ *
534
+ * See {_mint}.
535
+ *
536
+ * Emits a {Transfer} event for each mint.
537
+ */
538
+ function _safeMint(address to, uint256 quantity, bytes memory _data) internal virtual {
539
+ _mint(to, quantity);
540
+
541
+ unchecked {
542
+ if (to.code.length != 0) {
543
+ uint256 end = ERC721AStorage.layout()._currentIndex;
544
+ uint256 index = end - quantity;
545
+ do {
546
+ if (!_checkContractOnERC721Received(address(0), to, index++, _data)) {
547
+ revert TransferToNonERC721ReceiverImplementer();
548
+ }
549
+ } while (index < end);
550
+ // Reentrancy protection.
551
+ if (ERC721AStorage.layout()._currentIndex != end) revert();
552
+ }
553
+ }
554
+ }
555
+
556
+ /**
557
+ * @dev Equivalent to `_safeMint(to, quantity, '')`.
558
+ */
559
+ function _safeMint(address to, uint256 quantity) internal virtual {
560
+ _safeMint(to, quantity, "");
561
+ }
562
+
563
+ // =============================================================
564
+ // APPROVAL OPERATIONS
565
+ // =============================================================
566
+ function _ownerOf(uint256 tokenId) internal view virtual returns (address) {
567
+ return ERC721AStorage.ownerAt(_startTokenId(), tokenId);
568
+ }
569
+
570
+ /**
571
+ * @dev Returns if the `operator` is allowed to manage all of the assets of `owner`.
572
+ *
573
+ * See {setApprovalForAll}.
574
+ */
575
+ function _isApprovedForAll(
576
+ address owner,
577
+ address operator
578
+ ) internal view virtual returns (bool) {
579
+ return ERC721AStorage.layout()._operatorApprovals[owner][operator];
580
+ }
581
+
582
+ function _getApproved(uint256 tokenId) internal view virtual returns (address) {
583
+ if (!_exists(tokenId)) revert ApprovalQueryForNonexistentToken();
584
+
585
+ return ERC721AStorage.layout()._tokenApprovals[tokenId].value;
586
+ }
587
+
588
+ /**
589
+ * @dev Equivalent to `_approve(to, tokenId, false)`.
590
+ */
591
+ function _approve(address to, uint256 tokenId) internal virtual {
592
+ _approve(to, tokenId, false);
593
+ }
594
+
595
+ /**
596
+ * @dev Gives permission to `to` to transfer `tokenId` token to another account.
597
+ * The approval is cleared when the token is transferred.
598
+ *
599
+ * Only a single account can be approved at a time, so approving the
600
+ * zero address clears previous approvals.
601
+ *
602
+ * Requirements:
603
+ *
604
+ * - `tokenId` must exist.
605
+ *
606
+ * Emits an {Approval} event.
607
+ */
608
+ function _approve(address to, uint256 tokenId, bool approvalCheck) internal virtual {
609
+ address owner = _ownerOf(tokenId);
610
+
611
+ if (approvalCheck) {
612
+ if (_msgSenderERC721A() != owner) {
613
+ if (!_isApprovedForAll(owner, _msgSenderERC721A())) {
614
+ revert ApprovalCallerNotOwnerNorApproved();
615
+ }
616
+ }
617
+ }
618
+
619
+ ERC721AStorage.layout()._tokenApprovals[tokenId].value = to;
620
+ emit Approval(owner, to, tokenId);
621
+ }
622
+
623
+ // =============================================================
624
+ // BURN OPERATIONS
625
+ // =============================================================
626
+
627
+ /**
628
+ * @dev Equivalent to `_burn(tokenId, false)`.
629
+ */
630
+ function _burn(uint256 tokenId) internal virtual {
631
+ _burn(tokenId, false);
632
+ }
633
+
634
+ /**
635
+ * @dev Destroys `tokenId`.
636
+ * The approval is cleared when the token is burned.
637
+ *
638
+ * Requirements:
639
+ *
640
+ * - `tokenId` must exist.
641
+ *
642
+ * Emits a {Transfer} event.
643
+ */
644
+ function _burn(uint256 tokenId, bool approvalCheck) internal virtual {
645
+ uint256 prevOwnershipPacked = ERC721AStorage.packedOwnershipOf(_startTokenId(), tokenId);
646
+
647
+ address from = address(uint160(prevOwnershipPacked));
648
+
649
+ (uint256 approvedAddressSlot, address approvedAddress) = _getApprovedSlotAndAddress(
650
+ tokenId
651
+ );
652
+
653
+ if (approvalCheck) {
654
+ // The nested ifs save around 20+ gas over a compound boolean condition.
655
+ if (!_isSenderApprovedOrOwner(approvedAddress, from, _msgSenderERC721A())) {
656
+ if (!_isApprovedForAll(from, _msgSenderERC721A())) {
657
+ revert TransferCallerNotOwnerNorApproved();
658
+ }
659
+ }
660
+ }
661
+
662
+ _beforeTokenTransfers(from, address(0), tokenId, 1);
663
+
664
+ // Clear approvals from the previous owner.
665
+ assembly {
666
+ if approvedAddress {
667
+ // This is equivalent to `delete _tokenApprovals[tokenId]`.
668
+ sstore(approvedAddressSlot, 0)
669
+ }
670
+ }
671
+
672
+ ERC721AStorage.Layout storage ds = ERC721AStorage.layout();
673
+
674
+ // Underflow of the sender's balance is impossible because we check for
675
+ // ownership above and the recipient's balance can't realistically overflow.
676
+ // Counter overflow is incredibly unrealistic as `tokenId` would have to be 2**256.
677
+ unchecked {
678
+ // Updates:
679
+ // - `balance -= 1`.
680
+ // - `numberBurned += 1`.
681
+ //
682
+ // We can directly decrement the balance, and increment the number burned.
683
+ // This is equivalent to `packed -= 1; packed += 1 << _BITPOS_NUMBER_BURNED;`.
684
+ ds._packedAddressData[from] += (1 << _BITPOS_NUMBER_BURNED) - 1;
685
+
686
+ // Updates:
687
+ // - `address` to the last owner.
688
+ // - `startTimestamp` to the timestamp of burning.
689
+ // - `burned` to `true`.
690
+ // - `nextInitialized` to `true`.
691
+ ds._packedOwnerships[tokenId] = _packOwnershipData(
692
+ from,
693
+ (ERC721AStorage._BITMASK_BURNED | _BITMASK_NEXT_INITIALIZED) |
694
+ _nextExtraData(from, address(0), prevOwnershipPacked)
695
+ );
696
+
697
+ // If the next slot may not have been initialized (i.e. `nextInitialized == false`) .
698
+ if (prevOwnershipPacked & _BITMASK_NEXT_INITIALIZED == 0) {
699
+ uint256 nextTokenId = tokenId + 1;
700
+ // If the next slot's address is zero and not burned (i.e. packed value is zero).
701
+ if (ds._packedOwnerships[nextTokenId] == 0) {
702
+ // If the next slot is within bounds.
703
+ if (nextTokenId != ds._currentIndex) {
704
+ // Initialize the next slot to maintain correctness for `ownerOf(tokenId +
705
+ // 1)`.
706
+ ds._packedOwnerships[nextTokenId] = prevOwnershipPacked;
707
+ }
708
+ }
709
+ }
710
+ }
711
+
712
+ emit Transfer(from, address(0), tokenId);
713
+ _afterTokenTransfers(from, address(0), tokenId, 1);
714
+
715
+ // Overflow not possible, as _burnCounter cannot be exceed _currentIndex times.
716
+ unchecked {
717
+ ds._burnCounter++;
718
+ }
719
+ }
720
+
721
+ // =============================================================
722
+ // EXTRA DATA OPERATIONS
723
+ // =============================================================
724
+
725
+ /**
726
+ * @dev Directly sets the extra data for the ownership data `index`.
727
+ */
728
+ function _setExtraDataAt(uint256 index, uint24 extraData) internal virtual {
729
+ uint256 packed = ERC721AStorage.layout()._packedOwnerships[index];
730
+ if (packed == 0) revert OwnershipNotInitializedForExtraData();
731
+ uint256 extraDataCasted;
732
+ // Cast `extraData` with assembly to avoid redundant masking.
733
+ assembly {
734
+ extraDataCasted := extraData
735
+ }
736
+ packed =
737
+ (packed & _BITMASK_EXTRA_DATA_COMPLEMENT) |
738
+ (extraDataCasted << _BITPOS_EXTRA_DATA);
739
+ ERC721AStorage.layout()._packedOwnerships[index] = packed;
740
+ }
741
+
742
+ /**
743
+ * @dev Called during each token transfer to set the 24bit `extraData` field.
744
+ * Intended to be overridden by the cosumer contract.
745
+ *
746
+ * `previousExtraData` - the value of `extraData` before transfer.
747
+ *
748
+ * Calling conditions:
749
+ *
750
+ * - When `from` and `to` are both non-zero, `from`'s `tokenId` will be
751
+ * transferred to `to`.
752
+ * - When `from` is zero, `tokenId` will be minted for `to`.
753
+ * - When `to` is zero, `tokenId` will be burned by `from`.
754
+ * - `from` and `to` are never both zero.
755
+ */
756
+ function _extraData(
757
+ address from,
758
+ address to,
759
+ uint24 previousExtraData
760
+ ) internal view virtual returns (uint24) {}
761
+
762
+ /**
763
+ * @dev Returns the next extra data for the packed ownership data.
764
+ * The returned result is shifted into position.
765
+ */
766
+ function _nextExtraData(
767
+ address from,
768
+ address to,
769
+ uint256 prevOwnershipPacked
770
+ ) internal view returns (uint256) {
771
+ uint24 extraData = uint24(prevOwnershipPacked >> _BITPOS_EXTRA_DATA);
772
+ return uint256(_extraData(from, to, extraData)) << _BITPOS_EXTRA_DATA;
773
+ }
774
+
775
+ // =============================================================
776
+ // OTHER OPERATIONS
777
+ // =============================================================
778
+
779
+ /**
780
+ * @dev Returns the message sender (defaults to `msg.sender`).
781
+ *
782
+ * If you are writing GSN compatible contracts, you need to override this function.
783
+ */
784
+ function _msgSenderERC721A() internal view virtual returns (address) {
785
+ return msg.sender;
786
+ }
787
+
788
+ /**
789
+ * @dev Converts a uint256 to its ASCII string decimal representation.
790
+ */
791
+ function _toString(uint256 value) internal pure virtual returns (string memory str) {
792
+ assembly {
793
+ // The maximum value of a uint256 contains 78 digits (1 byte per digit), but
794
+ // we allocate 0xa0 bytes to keep the free memory pointer 32-byte word aligned.
795
+ // We will need 1 word for the trailing zeros padding, 1 word for the length,
796
+ // and 3 words for a maximum of 78 digits. Total: 5 * 0x20 = 0xa0.
797
+ let m := add(mload(0x40), 0xa0)
798
+ // Update the free memory pointer to allocate.
799
+ mstore(0x40, m)
800
+ // Assign the `str` to the end.
801
+ str := sub(m, 0x20)
802
+ // Zeroize the slot after the string.
803
+ mstore(str, 0)
804
+
805
+ // Cache the end of the memory to calculate the length later.
806
+ let end := str
807
+
808
+ // We write the string from rightmost digit to leftmost digit.
809
+ // The following is essentially a do-while loop that also handles the zero case.
810
+ // prettier-ignore
811
+ for { let temp := value } 1 {} {
812
+ str := sub(str, 1)
813
+ // Write the character to the pointer.
814
+ // The ASCII index of the '0' character is 48.
815
+ mstore8(str, add(48, mod(temp, 10)))
816
+ // Keep dividing `temp` until zero.
817
+ temp := div(temp, 10)
818
+ // prettier-ignore
819
+ if iszero(temp) { break }
820
+ }
821
+
822
+ let length := sub(end, str)
823
+ // Move the pointer 32 bytes leftwards to make room for the length.
824
+ str := sub(str, 0x20)
825
+ // Store the length.
826
+ mstore(str, length)
827
+ }
828
+ }
829
+ }