编译器
0.8.17+commit.8df45f5f
文件 1 的 36:AppStorage.sol
pragma solidity 0.8.17;
import "./interfaces/FreeStructs.sol";
struct AppStorage {
bool diamondInitialized;
uint256 initialChainId;
bytes32 initialDomainSeparator;
uint256 reentrancyStatus;
string name;
mapping(address => mapping(address => uint256)) allowance;
uint256 totalSupply;
mapping(bytes32 => bool) internalToken;
mapping(address => uint256) balances;
mapping(bytes32 => bool) existingObjects;
mapping(bytes32 => bytes32) objectParent;
mapping(bytes32 => bytes32) objectDataHashes;
mapping(bytes32 => string) objectTokenSymbol;
mapping(bytes32 => string) objectTokenName;
mapping(bytes32 => address) objectTokenWrapper;
mapping(bytes32 => bool) existingEntities;
mapping(bytes32 => bool) existingSimplePolicies;
mapping(bytes32 => Entity) entities;
mapping(bytes32 => SimplePolicy) simplePolicies;
mapping(address => bool) externalTokenSupported;
address[] supportedExternalTokens;
mapping(bytes32 => mapping(bytes32 => uint256)) tokenBalances;
mapping(bytes32 => uint256) tokenSupply;
uint8 maxDividendDenominations;
mapping(bytes32 => bytes32[]) dividendDenominations;
mapping(bytes32 => mapping(bytes32 => uint8)) dividendDenominationIndex;
mapping(bytes32 => mapping(uint8 => bytes32)) dividendDenominationAtIndex;
mapping(bytes32 => mapping(bytes32 => uint256)) totalDividends;
mapping(bytes32 => mapping(bytes32 => mapping(bytes32 => uint256))) withdrawnDividendPerOwner;
mapping(bytes32 => mapping(bytes32 => bool)) groups;
mapping(bytes32 => bytes32) canAssign;
mapping(bytes32 => mapping(bytes32 => bytes32)) roles;
uint256 lastOfferId;
mapping(uint256 => MarketInfo) offers;
mapping(bytes32 => mapping(bytes32 => uint256)) bestOfferId;
mapping(bytes32 => mapping(bytes32 => uint256)) span;
address naymsToken;
bytes32 naymsTokenId;
uint16 tradingCommissionTotalBP;
uint16 tradingCommissionNaymsLtdBP;
uint16 tradingCommissionNDFBP;
uint16 tradingCommissionSTMBP;
uint16 tradingCommissionMakerBP;
uint16 premiumCommissionNaymsLtdBP;
uint16 premiumCommissionNDFBP;
uint16 premiumCommissionSTMBP;
mapping(bytes32 => mapping(bytes32 => uint256)) lockedBalances;
mapping(bytes32 => uint256) upgradeScheduled;
uint256 upgradeExpiration;
uint256 sysAdmins;
}
struct FunctionLockedStorage {
mapping(bytes4 => bool) locked;
}
library LibAppStorage {
bytes32 internal constant NAYMS_DIAMOND_STORAGE_POSITION = keccak256("diamond.standard.nayms.storage");
bytes32 internal constant FUNCTION_LOCK_STORAGE_POSITION = keccak256("diamond.function.lock.storage");
function diamondStorage() internal pure returns (AppStorage storage ds) {
bytes32 position = NAYMS_DIAMOND_STORAGE_POSITION;
assembly {
ds.slot := position
}
}
function functionLockStorage() internal pure returns (FunctionLockedStorage storage ds) {
bytes32 position = FUNCTION_LOCK_STORAGE_POSITION;
assembly {
ds.slot := position
}
}
}
文件 2 的 36:CustomErrors.sol
pragma solidity 0.8.17;
error RoleIsMissing();
error AssignerGroupIsMissing();
error CannotAddNullSupportedExternalToken();
error CannotSupportExternalTokenWithMoreThan18Decimals();
error CannotAddNullDiscountToken();
error EntityDoesNotExist(bytes32 objectId);
error CreatingEntityThatAlreadyExists(bytes32 entityId);
error ObjectCannotBeTokenized(bytes32 objectId);
error MissingSymbolWhenEnablingTokenization(bytes32 objectId);
error ExternalDepositAmountCannotBeZero();
error ExternalWithdrawAmountCannotBeZero();
error PolicyIdCannotBeZero();
error PolicyCommissionsBasisPointsCannotBeGreaterThan10000(uint256 calculatedTotalBp);
error UtilizedCapacityGreaterThanMaxCapacity(uint256 utilizedCapacity, uint256 maxCapacity);
error SimplePolicyStakeholderSignatureInvalid(bytes32 signingHash, bytes signature, bytes32 signerId, bytes32 signersParent, bytes32 entityId);
error SimplePolicyClaimsPaidShouldStartAtZero();
error SimplePolicyPremiumsPaidShouldStartAtZero();
error CancelCannotBeTrueWhenCreatingSimplePolicy();
error PolicyDoesNotExist(bytes32 policyId);
error DuplicateSignerCreatingSimplePolicy(address previousSigner, address nextSigner);
文件 3 的 36:DiamondCutFacet.sol
pragma solidity 0.8.17;
import { IDiamondCut } from "../interfaces/IDiamondCut.sol";
import { LibDiamond } from "../libs/LibDiamond.sol";
contract DiamondCutFacet is IDiamondCut {
function diamondCut(
FacetCut[] calldata _diamondCut,
address _init,
bytes calldata _calldata
) external override {
LibDiamond.enforceIsContractOwner();
LibDiamond.DiamondStorage storage ds = LibDiamond.diamondStorage();
uint256 originalSelectorCount = ds.selectorCount;
uint256 selectorCount = originalSelectorCount;
bytes32 selectorSlot;
if (selectorCount & 7 > 0) {
selectorSlot = ds.selectorSlots[selectorCount >> 3];
}
for (uint256 facetIndex = 0; facetIndex < _diamondCut.length; facetIndex++) {
(selectorCount, selectorSlot) = LibDiamond.addReplaceRemoveFacetSelectors(
selectorCount,
selectorSlot,
_diamondCut[facetIndex].facetAddress,
_diamondCut[facetIndex].action,
_diamondCut[facetIndex].functionSelectors
);
}
if (selectorCount != originalSelectorCount) {
ds.selectorCount = uint16(selectorCount);
}
if (selectorCount & 7 > 0) {
ds.selectorSlots[selectorCount >> 3] = selectorSlot;
}
emit DiamondCut(_diamondCut, _init, _calldata);
LibDiamond.initializeDiamondCut(_init, _calldata);
}
}
文件 4 的 36:DiamondLoupeFacet.sol
pragma solidity 0.8.17;
import { LibDiamond } from "../libs/LibDiamond.sol";
import { IDiamondLoupe } from "../interfaces/IDiamondLoupe.sol";
import { IERC165 } from "../interfaces/IERC165.sol";
contract DiamondLoupeFacet is IDiamondLoupe, IERC165 {
function facets() external view override returns (Facet[] memory facets_) {
LibDiamond.DiamondStorage storage ds = LibDiamond.diamondStorage();
facets_ = new Facet[](ds.selectorCount);
uint8[] memory numFacetSelectors = new uint8[](ds.selectorCount);
uint256 numFacets;
uint256 selectorIndex;
for (uint256 slotIndex; selectorIndex < ds.selectorCount; slotIndex++) {
bytes32 slot = ds.selectorSlots[slotIndex];
for (uint256 selectorSlotIndex; selectorSlotIndex < 8; selectorSlotIndex++) {
selectorIndex++;
if (selectorIndex > ds.selectorCount) {
break;
}
bytes4 selector = bytes4(slot << (selectorSlotIndex << 5));
address facetAddress_ = address(bytes20(ds.facets[selector]));
bool continueLoop;
for (uint256 facetIndex; facetIndex < numFacets; facetIndex++) {
if (facets_[facetIndex].facetAddress == facetAddress_) {
facets_[facetIndex].functionSelectors[numFacetSelectors[facetIndex]] = selector;
require(numFacetSelectors[facetIndex] < 255);
numFacetSelectors[facetIndex]++;
continueLoop = true;
break;
}
}
if (continueLoop) {
continue;
}
facets_[numFacets].facetAddress = facetAddress_;
facets_[numFacets].functionSelectors = new bytes4[](ds.selectorCount);
facets_[numFacets].functionSelectors[0] = selector;
numFacetSelectors[numFacets] = 1;
numFacets++;
}
}
for (uint256 facetIndex; facetIndex < numFacets; facetIndex++) {
uint256 numSelectors = numFacetSelectors[facetIndex];
bytes4[] memory selectors = facets_[facetIndex].functionSelectors;
assembly {
mstore(selectors, numSelectors)
}
}
assembly {
mstore(facets_, numFacets)
}
}
function facetFunctionSelectors(address _facet) external view override returns (bytes4[] memory facetFunctionSelectors_) {
LibDiamond.DiamondStorage storage ds = LibDiamond.diamondStorage();
uint256 numSelectors;
facetFunctionSelectors_ = new bytes4[](ds.selectorCount);
uint256 selectorIndex;
for (uint256 slotIndex; selectorIndex < ds.selectorCount; slotIndex++) {
bytes32 slot = ds.selectorSlots[slotIndex];
for (uint256 selectorSlotIndex; selectorSlotIndex < 8; selectorSlotIndex++) {
selectorIndex++;
if (selectorIndex > ds.selectorCount) {
break;
}
bytes4 selector = bytes4(slot << (selectorSlotIndex << 5));
address facet = address(bytes20(ds.facets[selector]));
if (_facet == facet) {
facetFunctionSelectors_[numSelectors] = selector;
numSelectors++;
}
}
}
assembly {
mstore(facetFunctionSelectors_, numSelectors)
}
}
function facetAddresses() external view override returns (address[] memory facetAddresses_) {
LibDiamond.DiamondStorage storage ds = LibDiamond.diamondStorage();
facetAddresses_ = new address[](ds.selectorCount);
uint256 numFacets;
uint256 selectorIndex;
for (uint256 slotIndex; selectorIndex < ds.selectorCount; slotIndex++) {
bytes32 slot = ds.selectorSlots[slotIndex];
for (uint256 selectorSlotIndex; selectorSlotIndex < 8; selectorSlotIndex++) {
selectorIndex++;
if (selectorIndex > ds.selectorCount) {
break;
}
bytes4 selector = bytes4(slot << (selectorSlotIndex << 5));
address facetAddress_ = address(bytes20(ds.facets[selector]));
bool continueLoop;
for (uint256 facetIndex; facetIndex < numFacets; facetIndex++) {
if (facetAddress_ == facetAddresses_[facetIndex]) {
continueLoop = true;
break;
}
}
if (continueLoop) {
continue;
}
facetAddresses_[numFacets] = facetAddress_;
numFacets++;
}
}
assembly {
mstore(facetAddresses_, numFacets)
}
}
function facetAddress(bytes4 _functionSelector) external view override returns (address facetAddress_) {
LibDiamond.DiamondStorage storage ds = LibDiamond.diamondStorage();
facetAddress_ = address(bytes20(ds.facets[_functionSelector]));
}
function supportsInterface(bytes4 _interfaceId) external view override returns (bool) {
LibDiamond.DiamondStorage storage ds = LibDiamond.diamondStorage();
return ds.supportedInterfaces[_interfaceId];
}
}
文件 5 的 36:ERC20Wrapper.sol
pragma solidity 0.8.17;
import { IERC20 } from "./IERC20.sol";
import { INayms } from "../diamonds/nayms/INayms.sol";
import { LibHelpers } from "../diamonds/nayms/libs/LibHelpers.sol";
import { LibConstants } from "../diamonds/nayms/libs/LibConstants.sol";
import { ReentrancyGuard } from "../utils/ReentrancyGuard.sol";
contract ERC20Wrapper is IERC20, ReentrancyGuard {
bytes32 internal immutable tokenId;
INayms internal immutable nayms;
mapping(address => mapping(address => uint256)) public allowances;
uint256 internal immutable INITIAL_CHAIN_ID;
bytes32 internal immutable INITIAL_DOMAIN_SEPARATOR;
mapping(address => uint256) public nonces;
constructor(bytes32 _tokenId) {
nayms = INayms(msg.sender);
require(nayms.isObjectTokenizable(_tokenId), "must be tokenizable");
require(!nayms.isTokenWrapped(_tokenId), "must not be wrapped already");
tokenId = _tokenId;
INITIAL_CHAIN_ID = block.chainid;
INITIAL_DOMAIN_SEPARATOR = computeDomainSeparator();
}
function name() external view returns (string memory) {
(, , , string memory tokenName, ) = nayms.getObjectMeta(tokenId);
return tokenName;
}
function symbol() external view returns (string memory) {
(, , string memory tokenSymbol, , ) = nayms.getObjectMeta(tokenId);
return tokenSymbol;
}
function decimals() external pure returns (uint8) {
return 18;
}
function totalSupply() external view returns (uint256) {
return nayms.internalTokenSupply(tokenId);
}
function balanceOf(address who) external view returns (uint256) {
return nayms.internalBalanceOf(LibHelpers._getIdForAddress(who), tokenId);
}
function allowance(address owner, address spender) external view returns (uint256) {
return allowances[owner][spender];
}
function transfer(address to, uint256 value) external nonReentrant returns (bool) {
bytes32 fromId = LibHelpers._getIdForAddress(msg.sender);
bytes32 toId = LibHelpers._getIdForAddress(to);
emit Transfer(msg.sender, to, value);
nayms.wrapperInternalTransferFrom(fromId, toId, tokenId, value);
return true;
}
function approve(address spender, uint256 value) external returns (bool) {
allowances[msg.sender][spender] = value;
return true;
}
function transferFrom(
address from,
address to,
uint256 value
) external nonReentrant returns (bool) {
if (value == 0) {
revert();
}
uint256 allowed = allowances[from][msg.sender];
require(allowed >= value, "not enough allowance");
if (allowed != type(uint256).max) allowances[from][msg.sender] = allowed - value;
bytes32 fromId = LibHelpers._getIdForAddress(from);
bytes32 toId = LibHelpers._getIdForAddress(to);
emit Transfer(from, to, value);
nayms.wrapperInternalTransferFrom(fromId, toId, tokenId, value);
return true;
}
function permit(
address owner,
address spender,
uint256 value,
uint256 deadline,
uint8 v,
bytes32 r,
bytes32 s
) external {
require(deadline >= block.timestamp, "PERMIT_DEADLINE_EXPIRED");
unchecked {
address recoveredAddress = ecrecover(
keccak256(
abi.encodePacked(
"\x19\x01",
DOMAIN_SEPARATOR(),
keccak256(
abi.encode(
keccak256("Permit(address owner,address spender,uint256 value,uint256 nonce,uint256 deadline)"),
owner,
spender,
value,
nonces[owner]++,
deadline
)
)
)
),
v,
r,
s
);
require(recoveredAddress != address(0) && recoveredAddress == owner, "INVALID_SIGNER");
allowances[recoveredAddress][spender] = value;
}
emit Approval(owner, spender, value);
}
function DOMAIN_SEPARATOR() public view virtual returns (bytes32) {
return block.chainid == INITIAL_CHAIN_ID ? INITIAL_DOMAIN_SEPARATOR : computeDomainSeparator();
}
function computeDomainSeparator() internal view virtual returns (bytes32) {
return
keccak256(
abi.encode(
keccak256("EIP712Domain(string name,string version,uint256 chainId,address verifyingContract)"),
keccak256(LibHelpers._bytes32ToBytes(tokenId)),
keccak256("1"),
block.chainid,
address(this)
)
);
}
}
文件 6 的 36:FreeStructs.sol
pragma solidity 0.8.17;
struct MarketInfo {
bytes32 creator;
bytes32 sellToken;
uint256 sellAmount;
uint256 sellAmountInitial;
bytes32 buyToken;
uint256 buyAmount;
uint256 buyAmountInitial;
uint256 feeSchedule;
uint256 state;
uint256 rankNext;
uint256 rankPrev;
}
struct TokenAmount {
bytes32 token;
uint256 amount;
}
struct Entity {
bytes32 assetId;
uint256 collateralRatio;
uint256 maxCapacity;
uint256 utilizedCapacity;
bool simplePolicyEnabled;
}
struct SimplePolicy {
uint256 startDate;
uint256 maturationDate;
bytes32 asset;
uint256 limit;
bool fundsLocked;
bool cancelled;
uint256 claimsPaid;
uint256 premiumsPaid;
bytes32[] commissionReceivers;
uint256[] commissionBasisPoints;
}
struct SimplePolicyInfo {
uint256 startDate;
uint256 maturationDate;
bytes32 asset;
uint256 limit;
bool fundsLocked;
bool cancelled;
uint256 claimsPaid;
uint256 premiumsPaid;
}
struct PolicyCommissionsBasisPoints {
uint16 premiumCommissionNaymsLtdBP;
uint16 premiumCommissionNDFBP;
uint16 premiumCommissionSTMBP;
}
struct Stakeholders {
bytes32[] roles;
bytes32[] entityIds;
bytes[] signatures;
}
struct LockedBalance {
uint256 amount;
uint256 endTime;
}
struct StakingCheckpoint {
int128 bias;
int128 slope;
uint256 ts;
uint256 blk;
}
struct FeeRatio {
uint256 brokerShareRatio;
uint256 naymsLtdShareRatio;
uint256 ndfShareRatio;
}
struct TradingCommissions {
uint256 roughCommissionPaid;
uint256 commissionNaymsLtd;
uint256 commissionNDF;
uint256 commissionSTM;
uint256 commissionMaker;
uint256 totalCommissions;
}
struct TradingCommissionsBasisPoints {
uint16 tradingCommissionTotalBP;
uint16 tradingCommissionNaymsLtdBP;
uint16 tradingCommissionNDFBP;
uint16 tradingCommissionSTMBP;
uint16 tradingCommissionMakerBP;
}
文件 7 的 36:IACLFacet.sol
pragma solidity 0.8.17;
interface IACLFacet {
function assignRole(
bytes32 _objectId,
bytes32 _contextId,
string memory _role
) external;
function unassignRole(bytes32 _objectId, bytes32 _contextId) external;
function isInGroup(
bytes32 _objectId,
bytes32 _contextId,
string memory _group
) external view returns (bool);
function isParentInGroup(
bytes32 _objectId,
bytes32 _contextId,
string memory _group
) external view returns (bool);
function canAssign(
bytes32 _assignerId,
bytes32 _objectId,
bytes32 _contextId,
string memory _role
) external view returns (bool);
function getRoleInContext(bytes32 objectId, bytes32 contextId) external view returns (bytes32);
function isRoleInGroup(string memory role, string memory group) external view returns (bool);
function canGroupAssignRole(string memory role, string memory group) external view returns (bool);
function updateRoleAssigner(string memory _role, string memory _assignerGroup) external;
function updateRoleGroup(
string memory _role,
string memory _group,
bool _roleInGroup
) external;
}
文件 8 的 36:IAdminFacet.sol
pragma solidity 0.8.17;
import { PolicyCommissionsBasisPoints, TradingCommissionsBasisPoints } from "./FreeStructs.sol";
interface IAdminFacet {
function setMaxDividendDenominations(uint8 _newMax) external;
function setPolicyCommissionsBasisPoints(PolicyCommissionsBasisPoints calldata _policyCommissions) external;
function setTradingCommissionsBasisPoints(TradingCommissionsBasisPoints calldata _tradingCommissions) external;
function getMaxDividendDenominations() external view returns (uint8);
function isSupportedExternalToken(bytes32 _tokenId) external view returns (bool);
function addSupportedExternalToken(address _tokenAddress) external;
function getSupportedExternalTokens() external view returns (address[] memory);
function getSystemId() external pure returns (bytes32);
function isObjectTokenizable(bytes32 _objectId) external returns (bool);
function lockFunction(bytes4 functionSelector) external;
function unlockFunction(bytes4 functionSelector) external;
function isFunctionLocked(bytes4 functionSelector) external view returns (bool);
}
文件 9 的 36:IDiamondCut.sol
pragma solidity 0.8.17;
interface IDiamondCut {
enum FacetCutAction {
Add,
Replace,
Remove
}
struct FacetCut {
address facetAddress;
FacetCutAction action;
bytes4[] functionSelectors;
}
function diamondCut(
FacetCut[] calldata _diamondCut,
address _init,
bytes calldata _calldata
) external;
event DiamondCut(FacetCut[] _diamondCut, address _init, bytes _calldata);
}
文件 10 的 36:IDiamondLoupe.sol
pragma solidity 0.8.17;
interface IDiamondLoupe {
struct Facet {
address facetAddress;
bytes4[] functionSelectors;
}
function facets() external view returns (Facet[] memory facets_);
function facetFunctionSelectors(address _facet) external view returns (bytes4[] memory facetFunctionSelectors_);
function facetAddresses() external view returns (address[] memory facetAddresses_);
function facetAddress(bytes4 _functionSelector) external view returns (address facetAddress_);
}
文件 11 的 36:IERC165.sol
pragma solidity 0.8.17;
interface IERC165 {
function supportsInterface(bytes4 interfaceId) external view returns (bool);
}
文件 12 的 36:IERC173.sol
pragma solidity 0.8.17;
interface IERC173 {
event OwnershipTransferred(address indexed previousOwner, address indexed newOwner);
function owner() external view returns (address owner_);
function transferOwnership(address _newOwner) external;
}
文件 13 的 36:IERC20.sol
pragma solidity 0.8.17;
interface IERC20 {
function name() external view returns (string memory);
function symbol() external view returns (string memory);
function decimals() external view returns (uint8);
function totalSupply() external view returns (uint256);
function balanceOf(address who) external view returns (uint256);
function allowance(address owner, address spender) external view returns (uint256);
function transfer(address to, uint256 value) external returns (bool);
function approve(address spender, uint256 value) external returns (bool);
function transferFrom(
address from,
address to,
uint256 value
) external returns (bool);
function permit(
address owner,
address spender,
uint256 value,
uint256 deadline,
uint8 v,
bytes32 r,
bytes32 s
) external;
event Transfer(address indexed from, address indexed to, uint256 value);
event Approval(address indexed owner, address indexed spender, uint256 value);
}
文件 14 的 36:IEntityFacet.sol
pragma solidity 0.8.17;
import { SimplePolicy, Entity, Stakeholders } from "./FreeStructs.sol";
interface IEntityFacet {
function domainSeparatorV4() external view returns (bytes32);
function hashTypedDataV4(bytes32 structHash) external view returns (bytes32);
function createSimplePolicy(
bytes32 _policyId,
bytes32 _entityId,
Stakeholders calldata _stakeholders,
SimplePolicy calldata _simplePolicy,
bytes32 _dataHash
) external;
function enableEntityTokenization(
bytes32 _entityId,
string memory _symbol,
string memory _name
) external;
function startTokenSale(
bytes32 _entityId,
uint256 _amount,
uint256 _totalPrice
) external;
function isTokenWrapped(bytes32 _entityId) external view returns (bool);
function updateEntity(bytes32 _entityId, Entity calldata _entity) external;
function getEntityInfo(bytes32 _entityId) external view returns (Entity memory);
}
文件 15 的 36:IGovernanceFacet.sol
pragma solidity 0.8.17;
interface IGovernanceFacet {
function createUpgrade(bytes32 id) external;
function updateUpgradeExpiration(uint256 duration) external;
function cancelUpgrade(bytes32 id) external;
function getUpgrade(bytes32 id) external view returns (uint256 expiry);
}
文件 16 的 36:IMarketFacet.sol
pragma solidity 0.8.17;
import { MarketInfo, TradingCommissions, TradingCommissionsBasisPoints } from "./FreeStructs.sol";
interface IMarketFacet {
function executeLimitOffer(
bytes32 _sellToken,
uint256 _sellAmount,
bytes32 _buyToken,
uint256 _buyAmount
)
external
returns (
uint256 offerId_,
uint256 buyTokenCommissionsPaid_,
uint256 sellTokenCommissionsPaid_
);
function cancelOffer(uint256 _offerId) external;
function getBestOfferId(bytes32 _sellToken, bytes32 _buyToken) external view returns (uint256);
function getLastOfferId() external view returns (uint256);
function getOffer(uint256 _offerId) external view returns (MarketInfo memory _offerState);
function isActiveOffer(uint256 _offerId) external view returns (bool);
function calculateTradingCommissions(uint256 buyAmount) external view returns (TradingCommissions memory tc);
function getTradingCommissionsBasisPoints() external view returns (TradingCommissionsBasisPoints memory bp);
}
文件 17 的 36:INayms.sol
pragma solidity 0.8.17;
import { IDiamondCut } from "../shared/interfaces/IDiamondCut.sol";
import { IDiamondLoupe } from "../shared/interfaces/IDiamondLoupe.sol";
import { IERC165 } from "../shared/interfaces/IERC165.sol";
import { IERC173 } from "../shared/interfaces/IERC173.sol";
import { IACLFacet } from "./interfaces/IACLFacet.sol";
import { IUserFacet } from "./interfaces/IUserFacet.sol";
import { IAdminFacet } from "./interfaces/IAdminFacet.sol";
import { ISystemFacet } from "./interfaces/ISystemFacet.sol";
import { INaymsTokenFacet } from "./interfaces/INaymsTokenFacet.sol";
import { ITokenizedVaultFacet } from "./interfaces/ITokenizedVaultFacet.sol";
import { ITokenizedVaultIOFacet } from "./interfaces/ITokenizedVaultIOFacet.sol";
import { IMarketFacet } from "./interfaces/IMarketFacet.sol";
import { IEntityFacet } from "./interfaces/IEntityFacet.sol";
import { ISimplePolicyFacet } from "./interfaces/ISimplePolicyFacet.sol";
import { IGovernanceFacet } from "./interfaces/IGovernanceFacet.sol";
interface INayms is
IDiamondCut,
IDiamondLoupe,
IERC165,
IERC173,
IACLFacet,
IAdminFacet,
IUserFacet,
ISystemFacet,
INaymsTokenFacet,
ITokenizedVaultFacet,
ITokenizedVaultIOFacet,
IMarketFacet,
IEntityFacet,
ISimplePolicyFacet,
IGovernanceFacet
{
}
文件 18 的 36:INaymsTokenFacet.sol
pragma solidity 0.8.17;
interface INaymsTokenFacet {
function totalSupply() external view returns (uint256);
function balanceOf(address addr) external view returns (uint256);
}
文件 19 的 36:ISimplePolicyFacet.sol
pragma solidity 0.8.17;
import { SimplePolicy, SimplePolicyInfo, PolicyCommissionsBasisPoints } from "./FreeStructs.sol";
interface ISimplePolicyFacet {
function getSigningHash(
uint256 _startDate,
uint256 _maturationDate,
bytes32 _asset,
uint256 _limit,
bytes32 _offchainDataHash
) external view returns (bytes32 signingHash_);
function paySimplePremium(bytes32 _policyId, uint256 _amount) external;
function paySimpleClaim(
bytes32 _claimId,
bytes32 _policyId,
bytes32 _insuredId,
uint256 _amount
) external;
function getSimplePolicyInfo(bytes32 _id) external view returns (SimplePolicyInfo memory);
function getPremiumCommissionBasisPoints() external view returns (PolicyCommissionsBasisPoints memory);
function checkAndUpdateSimplePolicyState(bytes32 _id) external;
function cancelSimplePolicy(bytes32 _policyId) external;
}
文件 20 的 36:ISystemFacet.sol
pragma solidity 0.8.17;
import { Entity } from "./FreeStructs.sol";
interface ISystemFacet {
function createEntity(
bytes32 _entityId,
bytes32 _entityAdmin,
Entity memory _entityData,
bytes32 _dataHash
) external;
function stringToBytes32(string memory _strIn) external pure returns (bytes32 result);
function isObject(bytes32 _id) external view returns (bool);
function getObjectMeta(bytes32 _id)
external
view
returns (
bytes32 parent,
bytes32 dataHash,
string memory tokenSymbol,
string memory tokenName,
address tokenWrapper
);
function wrapToken(bytes32 _objectId) external;
}
文件 21 的 36:ITokenizedVaultFacet.sol
pragma solidity 0.8.17;
interface ITokenizedVaultFacet {
function internalBalanceOf(bytes32 accountId, bytes32 tokenId) external view returns (uint256);
function internalTokenSupply(bytes32 tokenId) external view returns (uint256);
function internalTransferFromEntity(
bytes32 to,
bytes32 tokenId,
uint256 amount
) external;
function wrapperInternalTransferFrom(
bytes32 from,
bytes32 to,
bytes32 tokenId,
uint256 amount
) external;
function internalBurn(
bytes32 from,
bytes32 tokenId,
uint256 amount
) external;
function getWithdrawableDividend(
bytes32 _entityId,
bytes32 _tokenId,
bytes32 _dividendTokenId
) external view returns (uint256 _entityPayout);
function withdrawDividend(
bytes32 ownerId,
bytes32 tokenId,
bytes32 dividendTokenId
) external;
function withdrawAllDividends(bytes32 ownerId, bytes32 tokenId) external;
function payDividendFromEntity(bytes32 guid, uint256 amount) external;
function getLockedBalance(bytes32 _entityId, bytes32 _tokenId) external view returns (uint256 amount);
}
文件 22 的 36:ITokenizedVaultIOFacet.sol
pragma solidity 0.8.17;
interface ITokenizedVaultIOFacet {
function externalDeposit(address _externalTokenAddress, uint256 _amount) external;
function externalWithdrawFromEntity(
bytes32 _entityId,
address _receiverId,
address _externalTokenAddress,
uint256 _amount
) external;
}
文件 23 的 36:IUserFacet.sol
pragma solidity 0.8.17;
interface IUserFacet {
function getUserIdFromAddress(address addr) external pure returns (bytes32 userId);
function getAddressFromExternalTokenId(bytes32 _externalTokenId) external pure returns (address tokenAddress);
function setEntity(bytes32 _userId, bytes32 _entityId) external;
function getEntity(bytes32 _userId) external view returns (bytes32 entityId);
}
文件 24 的 36:LibACL.sol
pragma solidity 0.8.17;
import { AppStorage, LibAppStorage } from "../AppStorage.sol";
import { LibHelpers } from "./LibHelpers.sol";
import { LibAdmin } from "./LibAdmin.sol";
import { LibObject } from "./LibObject.sol";
import { LibConstants } from "./LibConstants.sol";
import { RoleIsMissing, AssignerGroupIsMissing } from "src/diamonds/nayms/interfaces/CustomErrors.sol";
library LibACL {
event RoleUpdate(bytes32 indexed objectId, bytes32 contextId, bytes32 assignedRoleId, string functionName);
event RoleGroupUpdated(string role, string group, bool roleInGroup);
event RoleCanAssignUpdated(string role, string group);
function _assignRole(
bytes32 _objectId,
bytes32 _contextId,
bytes32 _roleId
) internal {
AppStorage storage s = LibAppStorage.diamondStorage();
require(_objectId != "", "invalid object ID");
require(_contextId != "", "invalid context ID");
require(_roleId != "", "invalid role ID");
s.roles[_objectId][_contextId] = _roleId;
if (_contextId == LibAdmin._getSystemId() && _roleId == LibHelpers._stringToBytes32(LibConstants.ROLE_SYSTEM_ADMIN)) {
unchecked {
s.sysAdmins += 1;
}
}
emit RoleUpdate(_objectId, _contextId, _roleId, "_assignRole");
}
function _unassignRole(bytes32 _objectId, bytes32 _contextId) internal {
AppStorage storage s = LibAppStorage.diamondStorage();
bytes32 roleId = s.roles[_objectId][_contextId];
if (_contextId == LibAdmin._getSystemId() && roleId == LibHelpers._stringToBytes32(LibConstants.ROLE_SYSTEM_ADMIN)) {
require(s.sysAdmins > 1, "must have at least one system admin");
unchecked {
s.sysAdmins -= 1;
}
}
emit RoleUpdate(_objectId, _contextId, s.roles[_objectId][_contextId], "_unassignRole");
delete s.roles[_objectId][_contextId];
}
function _isInGroup(
bytes32 _objectId,
bytes32 _contextId,
bytes32 _groupId
) internal view returns (bool ret) {
AppStorage storage s = LibAppStorage.diamondStorage();
bytes32 objectRoleInContext = s.roles[_objectId][_contextId];
if (objectRoleInContext != 0 && s.groups[objectRoleInContext][_groupId]) {
ret = true;
} else {
bytes32 objectRoleInSystem = s.roles[_objectId][LibAdmin._getSystemId()];
if (objectRoleInSystem != 0 && s.groups[objectRoleInSystem][_groupId]) {
ret = true;
}
}
}
function _isParentInGroup(
bytes32 _objectId,
bytes32 _contextId,
bytes32 _groupId
) internal view returns (bool) {
bytes32 parentId = LibObject._getParent(_objectId);
return _isInGroup(parentId, _contextId, _groupId);
}
function _canAssign(
bytes32 _assignerId,
bytes32 _objectId,
bytes32 _contextId,
bytes32 _roleId
) internal view returns (bool) {
require(_objectId != "", "invalid object ID");
bool ret = false;
AppStorage storage s = LibAppStorage.diamondStorage();
bytes32 assignerGroup = s.canAssign[_roleId];
if (assignerGroup == 0) {
ret = false;
}
else if (_isInGroup(_assignerId, _contextId, assignerGroup)) {
ret = true;
}
else if (_isParentInGroup(_assignerId, LibAdmin._getSystemId(), assignerGroup)) {
ret = true;
}
return ret;
}
function _getRoleInContext(bytes32 _objectId, bytes32 _contextId) internal view returns (bytes32) {
AppStorage storage s = LibAppStorage.diamondStorage();
return s.roles[_objectId][_contextId];
}
function _isRoleInGroup(string memory role, string memory group) internal view returns (bool) {
AppStorage storage s = LibAppStorage.diamondStorage();
return s.groups[LibHelpers._stringToBytes32(role)][LibHelpers._stringToBytes32(group)];
}
function _canGroupAssignRole(string memory role, string memory group) internal view returns (bool) {
AppStorage storage s = LibAppStorage.diamondStorage();
return s.canAssign[LibHelpers._stringToBytes32(role)] == LibHelpers._stringToBytes32(group);
}
function _updateRoleAssigner(string memory _role, string memory _assignerGroup) internal {
if (bytes32(bytes(_role)) == "") {
revert RoleIsMissing();
}
if (bytes32(bytes(_assignerGroup)) == "") {
revert AssignerGroupIsMissing();
}
AppStorage storage s = LibAppStorage.diamondStorage();
s.canAssign[LibHelpers._stringToBytes32(_role)] = LibHelpers._stringToBytes32(_assignerGroup);
emit RoleCanAssignUpdated(_role, _assignerGroup);
}
function _updateRoleGroup(
string memory _role,
string memory _group,
bool _roleInGroup
) internal {
AppStorage storage s = LibAppStorage.diamondStorage();
if (bytes32(bytes(_role)) == "") {
revert RoleIsMissing();
}
if (bytes32(bytes(_group)) == "") {
revert AssignerGroupIsMissing();
}
s.groups[LibHelpers._stringToBytes32(_role)][LibHelpers._stringToBytes32(_group)] = _roleInGroup;
emit RoleGroupUpdated(_role, _group, _roleInGroup);
}
}
文件 25 的 36:LibAdmin.sol
pragma solidity 0.8.17;
import { AppStorage, FunctionLockedStorage, LibAppStorage } from "../AppStorage.sol";
import { LibConstants } from "./LibConstants.sol";
import { LibHelpers } from "./LibHelpers.sol";
import { LibObject } from "./LibObject.sol";
import { LibERC20 } from "src/erc20/LibERC20.sol";
import { CannotAddNullDiscountToken, CannotAddNullSupportedExternalToken, CannotSupportExternalTokenWithMoreThan18Decimals } from "src/diamonds/nayms/interfaces/CustomErrors.sol";
library LibAdmin {
event MaxDividendDenominationsUpdated(uint8 oldMax, uint8 newMax);
event SupportedTokenAdded(address tokenAddress);
function _getSystemId() internal pure returns (bytes32) {
return LibHelpers._stringToBytes32(LibConstants.SYSTEM_IDENTIFIER);
}
function _getEmptyId() internal pure returns (bytes32) {
return LibHelpers._stringToBytes32(LibConstants.EMPTY_IDENTIFIER);
}
function _updateMaxDividendDenominations(uint8 _newMaxDividendDenominations) internal {
AppStorage storage s = LibAppStorage.diamondStorage();
require(_newMaxDividendDenominations > s.maxDividendDenominations, "_updateMaxDividendDenominations: cannot reduce");
uint8 old = s.maxDividendDenominations;
s.maxDividendDenominations = _newMaxDividendDenominations;
emit MaxDividendDenominationsUpdated(old, _newMaxDividendDenominations);
}
function _getMaxDividendDenominations() internal view returns (uint8) {
AppStorage storage s = LibAppStorage.diamondStorage();
return s.maxDividendDenominations;
}
function _isSupportedExternalTokenAddress(address _tokenId) internal view returns (bool) {
AppStorage storage s = LibAppStorage.diamondStorage();
return s.externalTokenSupported[_tokenId];
}
function _isSupportedExternalToken(bytes32 _tokenId) internal view returns (bool) {
AppStorage storage s = LibAppStorage.diamondStorage();
return s.externalTokenSupported[LibHelpers._getAddressFromId(_tokenId)];
}
function _addSupportedExternalToken(address _tokenAddress) internal {
if (LibERC20.decimals(_tokenAddress) > 18) {
revert CannotSupportExternalTokenWithMoreThan18Decimals();
}
AppStorage storage s = LibAppStorage.diamondStorage();
bool alreadyAdded = s.externalTokenSupported[_tokenAddress];
if (!alreadyAdded) {
s.externalTokenSupported[_tokenAddress] = true;
LibObject._createObject(LibHelpers._getIdForAddress(_tokenAddress));
s.supportedExternalTokens.push(_tokenAddress);
emit SupportedTokenAdded(_tokenAddress);
}
}
function _getSupportedExternalTokens() internal view returns (address[] memory) {
AppStorage storage s = LibAppStorage.diamondStorage();
return s.supportedExternalTokens;
}
function _lockFunction(bytes4 functionSelector) internal {
FunctionLockedStorage storage s = LibAppStorage.functionLockStorage();
s.locked[functionSelector] = true;
}
function _unlockFunction(bytes4 functionSelector) internal {
FunctionLockedStorage storage s = LibAppStorage.functionLockStorage();
s.locked[functionSelector] = false;
}
function _isFunctionLocked(bytes4 functionSelector) internal view returns (bool) {
FunctionLockedStorage storage s = LibAppStorage.functionLockStorage();
return s.locked[functionSelector];
}
}
文件 26 的 36:LibConstants.sol
pragma solidity 0.8.17;
library LibConstants {
string internal constant EMPTY_IDENTIFIER = "";
string internal constant SYSTEM_IDENTIFIER = "System";
string internal constant NDF_IDENTIFIER = "NDF";
string internal constant STM_IDENTIFIER = "Staking Mechanism";
string internal constant SSF_IDENTIFIER = "SSF";
string internal constant NAYM_TOKEN_IDENTIFIER = "NAYM";
string internal constant DIVIDEND_BANK_IDENTIFIER = "Dividend Bank";
string internal constant NAYMS_LTD_IDENTIFIER = "Nayms Ltd";
string internal constant ROLE_SYSTEM_ADMIN = "System Admin";
string internal constant ROLE_SYSTEM_MANAGER = "System Manager";
string internal constant ROLE_ENTITY_ADMIN = "Entity Admin";
string internal constant ROLE_ENTITY_MANAGER = "Entity Manager";
string internal constant ROLE_BROKER = "Broker";
string internal constant ROLE_INSURED_PARTY = "Insured";
string internal constant ROLE_UNDERWRITER = "Underwriter";
string internal constant ROLE_CAPITAL_PROVIDER = "Capital Provider";
string internal constant ROLE_CLAIMS_ADMIN = "Claims Admin";
string internal constant ROLE_TRADER = "Trader";
string internal constant ROLE_SEGREGATED_ACCOUNT = "Segregated Account";
string internal constant ROLE_SERVICE_PROVIDER = "Service Provider";
string internal constant GROUP_SYSTEM_ADMINS = "System Admins";
string internal constant GROUP_SYSTEM_MANAGERS = "System Managers";
string internal constant GROUP_ENTITY_ADMINS = "Entity Admins";
string internal constant GROUP_ENTITY_MANAGERS = "Entity Managers";
string internal constant GROUP_APPROVED_USERS = "Approved Users";
string internal constant GROUP_BROKERS = "Brokers";
string internal constant GROUP_INSURED_PARTIES = "Insured Parties";
string internal constant GROUP_UNDERWRITERS = "Underwriters";
string internal constant GROUP_CAPITAL_PROVIDERS = "Capital Providers";
string internal constant GROUP_CLAIMS_ADMINS = "Claims Admins";
string internal constant GROUP_TRADERS = "Traders";
string internal constant GROUP_SEGREGATED_ACCOUNTS = "Segregated Accounts";
string internal constant GROUP_SERVICE_PROVIDERS = "Service Providers";
string internal constant GROUP_POLICY_HANDLERS = "Policy Handlers";
uint256 internal constant FEE_SCHEDULE_STANDARD = 1;
uint256 internal constant FEE_SCHEDULE_PLATFORM_ACTION = 2;
uint256 internal constant OFFER_STATE_ACTIVE = 1;
uint256 internal constant OFFER_STATE_CANCELLED = 2;
uint256 internal constant OFFER_STATE_FULFILLED = 3;
uint256 internal constant DUST = 1;
uint256 internal constant BP_FACTOR = 10000;
uint256 internal constant SIMPLE_POLICY_STATE_CREATED = 0;
uint256 internal constant SIMPLE_POLICY_STATE_APPROVED = 1;
uint256 internal constant SIMPLE_POLICY_STATE_ACTIVE = 2;
uint256 internal constant SIMPLE_POLICY_STATE_MATURED = 3;
uint256 internal constant SIMPLE_POLICY_STATE_CANCELLED = 4;
uint256 internal constant STAKING_WEEK = 7 days;
uint256 internal constant STAKING_MINTIME = 60 days;
uint256 internal constant STAKING_MAXTIME = 4 * 365 days;
uint256 internal constant SCALE = 1e18;
int128 internal constant STAKING_DEPOSIT_FOR_TYPE = 0;
int128 internal constant STAKING_CREATE_LOCK_TYPE = 1;
int128 internal constant STAKING_INCREASE_LOCK_AMOUNT = 2;
int128 internal constant STAKING_INCREASE_UNLOCK_TIME = 3;
string internal constant VE_NAYM_NAME = "veNAYM";
string internal constant VE_NAYM_SYMBOL = "veNAYM";
uint8 internal constant VE_NAYM_DECIMALS = 18;
uint8 internal constant INTERNAL_TOKEN_DECIMALS = 18;
address internal constant DAI_CONSTANT = 0x6B175474E89094C44Da98b954EedeAC495271d0F;
}
文件 27 的 36:LibDiamond.sol
pragma solidity 0.8.17;
import { IDiamondCut } from "../interfaces/IDiamondCut.sol";
import { IDiamondLoupe } from "../interfaces/IDiamondLoupe.sol";
import { IERC165 } from "../interfaces/IERC165.sol";
import { IERC173 } from "../interfaces/IERC173.sol";
library LibDiamond {
bytes32 internal constant DIAMOND_STORAGE_POSITION = keccak256("diamond.standard.diamond.storage");
struct DiamondStorage {
mapping(bytes4 => bytes32) facets;
mapping(uint256 => bytes32) selectorSlots;
uint16 selectorCount;
mapping(bytes4 => bool) supportedInterfaces;
address contractOwner;
}
function diamondStorage() internal pure returns (DiamondStorage storage ds) {
bytes32 position = DIAMOND_STORAGE_POSITION;
assembly {
ds.slot := position
}
}
event OwnershipTransferred(address indexed previousOwner, address indexed newOwner);
function setContractOwner(address _newOwner) internal {
DiamondStorage storage ds = diamondStorage();
address previousOwner = ds.contractOwner;
ds.contractOwner = _newOwner;
emit OwnershipTransferred(previousOwner, _newOwner);
}
function contractOwner() internal view returns (address contractOwner_) {
contractOwner_ = diamondStorage().contractOwner;
}
function enforceIsContractOwner() internal view {
require(msg.sender == diamondStorage().contractOwner, "LibDiamond: Must be contract owner");
}
function addDiamondFunctions(
address _diamondCutFacet,
address _diamondLoupeFacet,
address _ownershipFacet
) internal {
IDiamondCut.FacetCut[] memory cut = new IDiamondCut.FacetCut[](3);
bytes4[] memory functionSelectors = new bytes4[](1);
functionSelectors[0] = IDiamondCut.diamondCut.selector;
cut[0] = IDiamondCut.FacetCut({ facetAddress: _diamondCutFacet, action: IDiamondCut.FacetCutAction.Add, functionSelectors: functionSelectors });
functionSelectors = new bytes4[](5);
functionSelectors[0] = IDiamondLoupe.facets.selector;
functionSelectors[1] = IDiamondLoupe.facetFunctionSelectors.selector;
functionSelectors[2] = IDiamondLoupe.facetAddresses.selector;
functionSelectors[3] = IDiamondLoupe.facetAddress.selector;
functionSelectors[4] = IERC165.supportsInterface.selector;
cut[1] = IDiamondCut.FacetCut({ facetAddress: _diamondLoupeFacet, action: IDiamondCut.FacetCutAction.Add, functionSelectors: functionSelectors });
functionSelectors = new bytes4[](2);
functionSelectors[0] = IERC173.transferOwnership.selector;
functionSelectors[1] = IERC173.owner.selector;
cut[2] = IDiamondCut.FacetCut({ facetAddress: _ownershipFacet, action: IDiamondCut.FacetCutAction.Add, functionSelectors: functionSelectors });
diamondCut(cut, address(0), "");
}
event DiamondCut(IDiamondCut.FacetCut[] diamondCut, address init, bytes _calldata);
bytes32 internal constant CLEAR_ADDRESS_MASK = bytes32(uint256(0xffffffffffffffffffffffff));
bytes32 internal constant CLEAR_SELECTOR_MASK = bytes32(uint256(0xffffffff << 224));
function diamondCut(
IDiamondCut.FacetCut[] memory _diamondCut,
address _init,
bytes memory _calldata
) internal {
DiamondStorage storage ds = diamondStorage();
uint256 originalSelectorCount = ds.selectorCount;
uint256 selectorCount = originalSelectorCount;
bytes32 selectorSlot;
if (selectorCount & 7 > 0) {
selectorSlot = ds.selectorSlots[selectorCount >> 3];
}
for (uint256 facetIndex; facetIndex < _diamondCut.length; facetIndex++) {
(selectorCount, selectorSlot) = addReplaceRemoveFacetSelectors(
selectorCount,
selectorSlot,
_diamondCut[facetIndex].facetAddress,
_diamondCut[facetIndex].action,
_diamondCut[facetIndex].functionSelectors
);
}
if (selectorCount != originalSelectorCount) {
ds.selectorCount = uint16(selectorCount);
}
if (selectorCount & 7 > 0) {
ds.selectorSlots[selectorCount >> 3] = selectorSlot;
}
emit DiamondCut(_diamondCut, _init, _calldata);
initializeDiamondCut(_init, _calldata);
}
function addReplaceRemoveFacetSelectors(
uint256 _selectorCount,
bytes32 _selectorSlot,
address _newFacetAddress,
IDiamondCut.FacetCutAction _action,
bytes4[] memory _selectors
) internal returns (uint256, bytes32) {
DiamondStorage storage ds = diamondStorage();
require(_selectors.length > 0, "LibDiamondCut: No selectors in facet to cut");
if (_action == IDiamondCut.FacetCutAction.Add) {
enforceHasContractCode(_newFacetAddress, "LibDiamondCut: Add facet has no code");
for (uint256 selectorIndex; selectorIndex < _selectors.length; selectorIndex++) {
bytes4 selector = _selectors[selectorIndex];
bytes32 oldFacet = ds.facets[selector];
require(address(bytes20(oldFacet)) == address(0), "LibDiamondCut: Can't add function that already exists");
ds.facets[selector] = bytes20(_newFacetAddress) | bytes32(_selectorCount);
uint256 selectorInSlotPosition = (_selectorCount & 7) << 5;
_selectorSlot = (_selectorSlot & ~(CLEAR_SELECTOR_MASK >> selectorInSlotPosition)) | (bytes32(selector) >> selectorInSlotPosition);
if (selectorInSlotPosition == 224) {
ds.selectorSlots[_selectorCount >> 3] = _selectorSlot;
_selectorSlot = 0;
}
_selectorCount++;
}
} else if (_action == IDiamondCut.FacetCutAction.Replace) {
enforceHasContractCode(_newFacetAddress, "LibDiamondCut: Replace facet has no code");
for (uint256 selectorIndex; selectorIndex < _selectors.length; selectorIndex++) {
bytes4 selector = _selectors[selectorIndex];
bytes32 oldFacet = ds.facets[selector];
address oldFacetAddress = address(bytes20(oldFacet));
require(oldFacetAddress != address(this), "LibDiamondCut: Can't replace immutable function");
require(oldFacetAddress != _newFacetAddress, "LibDiamondCut: Can't replace function with same function");
require(oldFacetAddress != address(0), "LibDiamondCut: Can't replace function that doesn't exist");
ds.facets[selector] = (oldFacet & CLEAR_ADDRESS_MASK) | bytes20(_newFacetAddress);
}
} else if (_action == IDiamondCut.FacetCutAction.Remove) {
require(_newFacetAddress == address(0), "LibDiamondCut: Remove facet address must be address(0)");
uint256 selectorSlotCount = _selectorCount >> 3;
uint256 selectorInSlotIndex = _selectorCount & 7;
for (uint256 selectorIndex; selectorIndex < _selectors.length; selectorIndex++) {
if (_selectorSlot == 0) {
selectorSlotCount--;
_selectorSlot = ds.selectorSlots[selectorSlotCount];
selectorInSlotIndex = 7;
} else {
selectorInSlotIndex--;
}
bytes4 lastSelector;
uint256 oldSelectorsSlotCount;
uint256 oldSelectorInSlotPosition;
{
bytes4 selector = _selectors[selectorIndex];
bytes32 oldFacet = ds.facets[selector];
require(address(bytes20(oldFacet)) != address(0), "LibDiamondCut: Can't remove function that doesn't exist");
require(address(bytes20(oldFacet)) != address(this), "LibDiamondCut: Can't remove immutable function");
lastSelector = bytes4(_selectorSlot << (selectorInSlotIndex << 5));
if (lastSelector != selector) {
ds.facets[lastSelector] = (oldFacet & CLEAR_ADDRESS_MASK) | bytes20(ds.facets[lastSelector]);
}
delete ds.facets[selector];
uint256 oldSelectorCount = uint16(uint256(oldFacet));
oldSelectorsSlotCount = oldSelectorCount >> 3;
oldSelectorInSlotPosition = (oldSelectorCount & 7) << 5;
}
if (oldSelectorsSlotCount != selectorSlotCount) {
bytes32 oldSelectorSlot = ds.selectorSlots[oldSelectorsSlotCount];
oldSelectorSlot = (oldSelectorSlot & ~(CLEAR_SELECTOR_MASK >> oldSelectorInSlotPosition)) | (bytes32(lastSelector) >> oldSelectorInSlotPosition);
ds.selectorSlots[oldSelectorsSlotCount] = oldSelectorSlot;
} else {
_selectorSlot = (_selectorSlot & ~(CLEAR_SELECTOR_MASK >> oldSelectorInSlotPosition)) | (bytes32(lastSelector) >> oldSelectorInSlotPosition);
}
if (selectorInSlotIndex == 0) {
delete ds.selectorSlots[selectorSlotCount];
_selectorSlot = 0;
}
}
_selectorCount = selectorSlotCount * 8 + selectorInSlotIndex;
} else {
revert("LibDiamondCut: Incorrect FacetCutAction");
}
return (_selectorCount, _selectorSlot);
}
function initializeDiamondCut(address _init, bytes memory _calldata) internal {
if (_init == address(0)) {
require(_calldata.length == 0, "LibDiamondCut: _init is address(0) but_calldata is not empty");
} else {
require(_calldata.length > 0, "LibDiamondCut: _calldata is empty but _init is not address(0)");
if (_init != address(this)) {
enforceHasContractCode(_init, "LibDiamondCut: _init address has no code");
}
(bool success, bytes memory error) = _init.delegatecall(_calldata);
if (!success) {
if (error.length > 0) {
revert(string(error));
} else {
revert("LibDiamondCut: _init function reverted");
}
}
}
}
function enforceHasContractCode(address _contract, string memory _errorMessage) internal view {
uint256 contractSize;
assembly {
contractSize := extcodesize(_contract)
}
require(contractSize > 0, _errorMessage);
}
}
文件 28 的 36:LibERC20.sol
pragma solidity 0.8.17;
import { IERC20 } from "./IERC20.sol";
library LibERC20 {
function decimals(address _token) internal returns (uint8) {
uint256 size;
assembly {
size := extcodesize(_token)
}
require(size > 0, "LibERC20: ERC20 token address has no code");
(bool success, bytes memory result) = _token.call(abi.encodeWithSelector(IERC20.decimals.selector));
if (success) {
return abi.decode(result, (uint8));
} else {
revert("LibERC20: call to decimals() failed");
}
}
function balanceOf(address _token, address _who) internal returns (uint256) {
uint256 size;
assembly {
size := extcodesize(_token)
}
require(size > 0, "LibERC20: ERC20 token address has no code");
(bool success, bytes memory result) = _token.call(abi.encodeWithSelector(IERC20.balanceOf.selector, _who));
if (success) {
return abi.decode(result, (uint256));
} else {
revert("LibERC20: call to balanceOf() failed");
}
}
function transferFrom(
address _token,
address _from,
address _to,
uint256 _value
) internal {
uint256 size;
assembly {
size := extcodesize(_token)
}
require(size > 0, "LibERC20: ERC20 token address has no code");
(bool success, bytes memory result) = _token.call(abi.encodeWithSelector(IERC20.transferFrom.selector, _from, _to, _value));
handleReturn(success, result);
}
function transfer(
address _token,
address _to,
uint256 _value
) internal {
uint256 size;
assembly {
size := extcodesize(_token)
}
require(size > 0, "LibERC20: ERC20 token address has no code");
(bool success, bytes memory result) = _token.call(abi.encodeWithSelector(IERC20.transfer.selector, _to, _value));
handleReturn(success, result);
}
function handleReturn(bool _success, bytes memory _result) internal pure {
if (_success) {
if (_result.length > 0) {
require(abi.decode(_result, (bool)), "LibERC20: transfer or transferFrom returned false");
}
} else {
if (_result.length > 0) {
assembly {
revert(add(32, _result), mload(_result))
}
} else {
revert("LibERC20: transfer or transferFrom reverted");
}
}
}
}
文件 29 的 36:LibHelpers.sol
pragma solidity 0.8.17;
library LibHelpers {
function _getIdForObjectAtIndex(uint256 _index) internal pure returns (bytes32) {
return keccak256(abi.encodePacked(_index));
}
function _getIdForAddress(address _addr) internal pure returns (bytes32) {
return bytes32(bytes20(_addr));
}
function _getSenderId() internal view returns (bytes32) {
return _getIdForAddress(msg.sender);
}
function _getAddressFromId(bytes32 _id) internal pure returns (address) {
return address(bytes20(_id));
}
function _addressToBytes32(address addr) internal pure returns (bytes32) {
return _bytesToBytes32(abi.encode(addr));
}
function _stringToBytes32(string memory strIn) internal pure returns (bytes32) {
return _bytesToBytes32(bytes(strIn));
}
function _bytes32ToString(bytes32 bytesIn) internal pure returns (string memory) {
return string(_bytes32ToBytes(bytesIn));
}
function _bytesToBytes32(bytes memory source) internal pure returns (bytes32 result) {
if (source.length == 0) {
return 0x0;
}
assembly {
result := mload(add(source, 32))
}
}
function _bytes32ToBytes(bytes32 input) internal pure returns (bytes memory) {
bytes memory b = new bytes(32);
assembly {
mstore(add(b, 32), input)
}
return b;
}
}
文件 30 的 36:LibMeta.sol
pragma solidity 0.8.17;
library LibMeta {
function msgSender() internal view returns (address sender_) {
if (msg.sender == address(this)) {
bytes memory array = msg.data;
uint256 index = msg.data.length;
assembly {
sender_ := and(mload(add(array, index)), 0xffffffffffffffffffffffffffffffffffffffff)
}
} else {
sender_ = msg.sender;
}
}
}
文件 31 的 36:LibObject.sol
pragma solidity 0.8.17;
import { AppStorage, LibAppStorage } from "../AppStorage.sol";
import { LibHelpers } from "./LibHelpers.sol";
import { LibAdmin } from "./LibAdmin.sol";
import { EntityDoesNotExist, MissingSymbolWhenEnablingTokenization } from "src/diamonds/nayms/interfaces/CustomErrors.sol";
import { ERC20Wrapper } from "../../../erc20/ERC20Wrapper.sol";
library LibObject {
event TokenWrapped(bytes32 indexed entityId, address tokenWrapper);
function _createObject(
bytes32 _objectId,
bytes32 _parentId,
bytes32 _dataHash
) internal {
AppStorage storage s = LibAppStorage.diamondStorage();
require(!s.existingObjects[_objectId], "objectId is already being used by another object");
s.existingObjects[_objectId] = true;
s.objectParent[_objectId] = _parentId;
s.objectDataHashes[_objectId] = _dataHash;
}
function _createObject(bytes32 _objectId, bytes32 _dataHash) internal {
AppStorage storage s = LibAppStorage.diamondStorage();
require(!s.existingObjects[_objectId], "objectId is already being used by another object");
s.existingObjects[_objectId] = true;
s.objectDataHashes[_objectId] = _dataHash;
}
function _createObject(bytes32 _objectId) internal {
AppStorage storage s = LibAppStorage.diamondStorage();
require(!s.existingObjects[_objectId], "objectId is already being used by another object");
s.existingObjects[_objectId] = true;
}
function _setDataHash(bytes32 _objectId, bytes32 _dataHash) internal {
AppStorage storage s = LibAppStorage.diamondStorage();
require(s.existingObjects[_objectId], "setDataHash: object doesn't exist");
s.objectDataHashes[_objectId] = _dataHash;
}
function _getDataHash(bytes32 _objectId) internal view returns (bytes32 objectDataHash) {
AppStorage storage s = LibAppStorage.diamondStorage();
return s.objectDataHashes[_objectId];
}
function _getParent(bytes32 _objectId) internal view returns (bytes32) {
AppStorage storage s = LibAppStorage.diamondStorage();
return s.objectParent[_objectId];
}
function _getParentFromAddress(address addr) internal view returns (bytes32) {
AppStorage storage s = LibAppStorage.diamondStorage();
bytes32 objectId = LibHelpers._getIdForAddress(addr);
return s.objectParent[objectId];
}
function _setParent(bytes32 _objectId, bytes32 _parentId) internal {
AppStorage storage s = LibAppStorage.diamondStorage();
s.objectParent[_objectId] = _parentId;
}
function _isObjectTokenizable(bytes32 _objectId) internal view returns (bool) {
AppStorage storage s = LibAppStorage.diamondStorage();
return (bytes(s.objectTokenSymbol[_objectId]).length != 0);
}
function _enableObjectTokenization(
bytes32 _objectId,
string memory _symbol,
string memory _name
) internal {
AppStorage storage s = LibAppStorage.diamondStorage();
if (bytes(_symbol).length == 0) {
revert MissingSymbolWhenEnablingTokenization(_objectId);
}
if (!s.existingEntities[_objectId]) {
revert EntityDoesNotExist(_objectId);
}
require(!_isObjectTokenizable(_objectId), "object already tokenized");
require(bytes(_symbol).length < 16, "symbol must be less than 16 characters");
s.objectTokenSymbol[_objectId] = _symbol;
s.objectTokenName[_objectId] = _name;
}
function _isObjectTokenWrapped(bytes32 _objectId) internal view returns (bool) {
AppStorage storage s = LibAppStorage.diamondStorage();
return (s.objectTokenWrapper[_objectId] != address(0));
}
function _wrapToken(bytes32 _entityId) internal {
AppStorage storage s = LibAppStorage.diamondStorage();
require(_isObjectTokenizable(_entityId), "must be tokenizable");
require(!_isObjectTokenWrapped(_entityId), "must not be wrapped already");
ERC20Wrapper tokenWrapper = new ERC20Wrapper(_entityId);
address wrapperAddress = address(tokenWrapper);
s.objectTokenWrapper[_entityId] = wrapperAddress;
emit TokenWrapped(_entityId, wrapperAddress);
}
function _isObject(bytes32 _id) internal view returns (bool) {
AppStorage storage s = LibAppStorage.diamondStorage();
return s.existingObjects[_id];
}
function _getObjectMeta(bytes32 _id)
internal
view
returns (
bytes32 parent,
bytes32 dataHash,
string memory tokenSymbol,
string memory tokenName,
address tokenWrapper
)
{
AppStorage storage s = LibAppStorage.diamondStorage();
parent = s.objectParent[_id];
dataHash = s.objectDataHashes[_id];
tokenSymbol = s.objectTokenSymbol[_id];
tokenName = s.objectTokenName[_id];
tokenWrapper = s.objectTokenWrapper[_id];
}
}
文件 32 的 36:Modifiers.sol
pragma solidity 0.8.17;
import { LibMeta } from "../shared/libs/LibMeta.sol";
import { LibAdmin } from "./libs/LibAdmin.sol";
import { LibConstants } from "./libs/LibConstants.sol";
import { LibHelpers } from "./libs/LibHelpers.sol";
import { LibObject } from "./libs/LibObject.sol";
import { LibACL } from "./libs/LibACL.sol";
contract Modifiers {
modifier assertIsFunctionLocked(bytes4 functionSelector) {
require(!LibAdmin._isFunctionLocked(functionSelector), "function is locked");
_;
}
modifier assertSysAdmin() {
require(
LibACL._isInGroup(LibHelpers._getIdForAddress(LibMeta.msgSender()), LibAdmin._getSystemId(), LibHelpers._stringToBytes32(LibConstants.GROUP_SYSTEM_ADMINS)),
"not a system admin"
);
_;
}
modifier assertSysMgr() {
require(
LibACL._isInGroup(LibHelpers._getIdForAddress(LibMeta.msgSender()), LibAdmin._getSystemId(), LibHelpers._stringToBytes32(LibConstants.GROUP_SYSTEM_MANAGERS)),
"not a system manager"
);
_;
}
modifier assertEntityAdmin(bytes32 _context) {
require(
LibACL._isInGroup(LibHelpers._getIdForAddress(LibMeta.msgSender()), _context, LibHelpers._stringToBytes32(LibConstants.GROUP_ENTITY_ADMINS)),
"not the entity's admin"
);
_;
}
modifier assertPolicyHandler(bytes32 _context) {
require(
LibACL._isInGroup(LibObject._getParentFromAddress(LibMeta.msgSender()), _context, LibHelpers._stringToBytes32(LibConstants.GROUP_POLICY_HANDLERS)),
"not a policy handler"
);
_;
}
modifier assertIsInGroup(
bytes32 _objectId,
bytes32 _contextId,
bytes32 _group
) {
require(LibACL._isInGroup(_objectId, _contextId, _group), "not in group");
_;
}
modifier assertERC20Wrapper(bytes32 _tokenId) {
(, , , , address erc20Wrapper) = LibObject._getObjectMeta(_tokenId);
require(msg.sender == erc20Wrapper, "only wrapper calls allowed");
_;
}
}
文件 33 的 36:Nayms.sol
pragma solidity 0.8.17;
import { LibDiamond } from "../shared/libs/LibDiamond.sol";
import { DiamondCutFacet } from "../shared/facets/DiamondCutFacet.sol";
import { DiamondLoupeFacet } from "../shared/facets/DiamondLoupeFacet.sol";
import { NaymsOwnershipFacet } from "src/diamonds/shared/facets/NaymsOwnershipFacet.sol";
contract Nayms {
constructor(address _contractOwner) payable {
LibDiamond.setContractOwner(_contractOwner);
LibDiamond.addDiamondFunctions(address(new DiamondCutFacet()), address(new DiamondLoupeFacet()), address(new NaymsOwnershipFacet()));
}
fallback() external payable {
LibDiamond.DiamondStorage storage ds = LibDiamond.diamondStorage();
address facet = address(bytes20(ds.facets[msg.sig]));
LibDiamond.enforceHasContractCode(facet, "Diamond: Facet has no code");
assembly {
calldatacopy(0, 0, calldatasize())
let result := delegatecall(gas(), facet, 0, calldatasize(), 0, 0)
returndatacopy(0, 0, returndatasize())
switch result
case 0 {
revert(0, returndatasize())
}
default {
return(0, returndatasize())
}
}
}
receive() external payable {}
}
文件 34 的 36:NaymsOwnershipFacet.sol
pragma solidity 0.8.17;
import { LibACL } from "src/diamonds/nayms/libs/LibACL.sol";
import { LibHelpers } from "src/diamonds/nayms/libs/LibHelpers.sol";
import { LibConstants } from "src/diamonds/nayms/libs/LibConstants.sol";
import { LibDiamond } from "src/diamonds/shared/libs/LibDiamond.sol";
import { OwnershipFacet } from "src/diamonds/shared/facets/OwnershipFacet.sol";
import { Modifiers } from "src/diamonds/nayms/Modifiers.sol";
contract NaymsOwnershipFacet is OwnershipFacet, Modifiers {
function transferOwnership(address _newOwner) public override assertSysAdmin {
bytes32 systemID = LibHelpers._stringToBytes32(LibConstants.SYSTEM_IDENTIFIER);
bytes32 newAcc1Id = LibHelpers._getIdForAddress(_newOwner);
require(!LibACL._isInGroup(newAcc1Id, systemID, LibHelpers._stringToBytes32(LibConstants.GROUP_SYSTEM_ADMINS)), "NEW owner MUST NOT be sys admin");
require(!LibACL._isInGroup(newAcc1Id, systemID, LibHelpers._stringToBytes32(LibConstants.GROUP_SYSTEM_MANAGERS)), "NEW owner MUST NOT be sys manager");
super.transferOwnership(_newOwner);
}
}
文件 35 的 36:OwnershipFacet.sol
pragma solidity 0.8.17;
import { LibDiamond } from "../libs/LibDiamond.sol";
import { IERC173 } from "../interfaces/IERC173.sol";
contract OwnershipFacet is IERC173 {
function transferOwnership(address _newOwner) public virtual override {
LibDiamond.setContractOwner(_newOwner);
}
function owner() external view override returns (address owner_) {
owner_ = LibDiamond.contractOwner();
}
}
文件 36 的 36:ReentrancyGuard.sol
pragma solidity 0.8.17;
import { LibAppStorage } from "src/diamonds/nayms/AppStorage.sol";
abstract contract ReentrancyGuard {
uint256 private constant _NOT_ENTERED = 1;
uint256 private constant _ENTERED = 2;
modifier nonReentrant() {
require(LibAppStorage.diamondStorage().reentrancyStatus != _ENTERED, "ReentrancyGuard: reentrant call");
LibAppStorage.diamondStorage().reentrancyStatus = _ENTERED;
_;
LibAppStorage.diamondStorage().reentrancyStatus = _NOT_ENTERED;
}
}
{
"compilationTarget": {
"src/diamonds/nayms/Nayms.sol": "Nayms"
},
"evmVersion": "london",
"libraries": {},
"metadata": {
"bytecodeHash": "ipfs"
},
"optimizer": {
"enabled": true,
"runs": 200
},
"remappings": [
":@openzeppelin/=lib/ozv4/",
":@uniswap/lib/=lib/solidity-lib/",
":@uniswap/v2-core/=lib/v2-core/",
":@uniswap/v3-core/=lib/v3-core/",
":@uniswap/v3-periphery/=lib/v3-periphery/",
":base64-sol/=lib/base64/",
":ds-test/=lib/ds-test/src/",
":erc4626-tests/=lib/ozv4/lib/erc4626-tests/",
":forge-std/=lib/forge-std/src/",
":ozv4/=lib/ozv4/",
":script/=script/",
":solidity-lib/=lib/solidity-lib/contracts/",
":solidity-stringutils/=lib/solidity-stringutils/",
":solmate/=lib/solmate/src/",
":src/=src/",
":test/=test/"
]
}
[{"inputs":[{"internalType":"address","name":"_contractOwner","type":"address"}],"stateMutability":"payable","type":"constructor"},{"stateMutability":"payable","type":"fallback"},{"stateMutability":"payable","type":"receive"}]