git.delta.rocks / unique-network / refs/commits / 8e1a9b3c3bc4

difftreelog

misk: add prettier configuration for solidity files

Trubnikov Sergey2022-09-13parent: #8c337cf.patch.diff
in: master

7 files changed

modified.maintain/scripts/generate_sol.shdiffbeforeafterboth
--- a/.maintain/scripts/generate_sol.sh
+++ b/.maintain/scripts/generate_sol.sh
@@ -1,11 +1,16 @@
 #!/bin/sh
 set -eu
 
+PRETTIER_CONFIG="$(pwd)""/.prettierrc"
+
 tmp=$(mktemp)
 cargo test --package $PACKAGE -- $NAME --exact --nocapture --ignored | tee $tmp
 raw=$(mktemp --suffix .sol)
 sed -n '/=== SNIP START ===/, /=== SNIP END ===/{ /=== SNIP START ===/! { /=== SNIP END ===/! p } }' $tmp > $raw
+
 formatted=$(mktemp)
-prettier --use-tabs $raw > $formatted
+echo $raw
+echo $formatted
+prettier --config $PRETTIER_CONFIG $raw > $formatted
 
 mv $formatted $OUTPUT
added.prettierignorediffbeforeafterboth
--- /dev/null
+++ b/.prettierignore
@@ -0,0 +1 @@
+!**/*.sol
added.prettierrcdiffbeforeafterboth
--- /dev/null
+++ b/.prettierrc
@@ -0,0 +1,16 @@
+{
+    "useTabs": true,
+    "tabWidth": 2,
+    "singleQuote": true,
+    "trailingComma": "all",
+    "overrides": [
+        {
+            "files": "*.sol",
+            "options": {
+                "singleQuote": false,
+                "printWidth": 120,
+                "explicitTypes": "always"
+            }
+        }
+    ]
+}
\ No newline at end of file
modifiedpallets/unique/src/eth/stubs/CollectionHelpers.rawdiffbeforeafterboth

binary blob — no preview

modifiedpallets/unique/src/eth/stubs/CollectionHelpers.soldiffbeforeafterboth
--- a/pallets/unique/src/eth/stubs/CollectionHelpers.sol
+++ b/pallets/unique/src/eth/stubs/CollectionHelpers.sol
@@ -10,11 +10,7 @@
 }
 
 contract ERC165 is Dummy {
-	function supportsInterface(bytes4 interfaceID)
-		external
-		view
-		returns (bool)
-	{
+	function supportsInterface(bytes4 interfaceID) external view returns (bool) {
 		require(false, stub_error);
 		interfaceID;
 		return true;
@@ -23,10 +19,7 @@
 
 /// @dev inlined interface
 contract CollectionHelpersEvents {
-	event CollectionCreated(
-		address indexed owner,
-		address indexed collectionId
-	);
+	event CollectionCreated(address indexed owner, address indexed collectionId);
 }
 
 /// @title Contract, which allows users to operate with collections
@@ -106,11 +99,7 @@
 	/// @return bool Does the collection exist?
 	/// @dev EVM selector for this function is: 0xc3de1494,
 	///  or in textual repr: isCollectionExist(address)
-	function isCollectionExist(address collectionAddress)
-		public
-		view
-		returns (bool)
-	{
+	function isCollectionExist(address collectionAddress) public view returns (bool) {
 		require(false, stub_error);
 		collectionAddress;
 		dummy;
modifiedtests/src/eth/api/CollectionHelpers.soldiffbeforeafterboth
--- a/tests/src/eth/api/CollectionHelpers.sol
+++ b/tests/src/eth/api/CollectionHelpers.sol
@@ -14,10 +14,7 @@
 
 /// @dev inlined interface
 interface CollectionHelpersEvents {
-	event CollectionCreated(
-		address indexed owner,
-		address indexed collectionId
-	);
+	event CollectionCreated(address indexed owner, address indexed collectionId);
 }
 
 /// @title Contract, which allows users to operate with collections
@@ -67,8 +64,5 @@
 	/// @return bool Does the collection exist?
 	/// @dev EVM selector for this function is: 0xc3de1494,
 	///  or in textual repr: isCollectionExist(address)
-	function isCollectionExist(address collectionAddress)
-		external
-		view
-		returns (bool);
+	function isCollectionExist(address collectionAddress) external view returns (bool);
 }
modifiedtests/src/eth/fractionalizer/Fractionalizer.soldiffbeforeafterboth
before · tests/src/eth/fractionalizer/Fractionalizer.sol
1// SPDX-License-Identifier:  Apache License2pragma solidity >=0.8.0;3import {CollectionHelpers} from "../api/CollectionHelpers.sol";4import {ContractHelpers} from "../api/ContractHelpers.sol";5import {UniqueRefungibleToken} from "../api/UniqueRefungibleToken.sol";6import {UniqueRefungible} from "../api/UniqueRefungible.sol";7import {UniqueNFT} from "../api/UniqueNFT.sol";89/// @dev Fractionalization contract. It stores mappings between NFT and RFT tokens,10///  stores allowlist of NFT tokens available for fractionalization, has methods11///  for fractionalization and defractionalization of NFT tokens.12contract Fractionalizer {13    struct Token {14        address _collection;15        uint256 _tokenId;16    }17    address rftCollection;18    mapping(address => bool) nftCollectionAllowList;19    mapping(address => mapping(uint256 => uint256)) public nft2rftMapping;20    mapping(address => Token) public rft2nftMapping;21    bytes32 refungibleCollectionType = keccak256(bytes("ReFungible"));2223    receive() external payable onlyOwner {}2425    /// @dev Method modifier to only allow contract owner to call it.26    modifier onlyOwner() {27        address contracthelpersAddress = 0x842899ECF380553E8a4de75bF534cdf6fBF64049;28        ContractHelpers contractHelpers = ContractHelpers(29            contracthelpersAddress30        );31        address contractOwner = contractHelpers.contractOwner(address(this));32        require(msg.sender == contractOwner, "Only owner can");33        _;34    }3536    /// @dev This emits when RFT collection setting is changed.37    event RFTCollectionSet(address _collection);3839    /// @dev This emits when NFT collection is allowed or disallowed.40    event AllowListSet(address _collection, bool _status);4142    /// @dev This emits when NFT token is fractionalized by contract.43    event Fractionalized(44        address _collection,45        uint256 _tokenId,46        address _rftToken,47        uint128 _amount48    );4950    /// @dev This emits when NFT token is defractionalized by contract.51    event Defractionalized(52        address _rftToken,53        address _nftCollection,54        uint256 _nftTokenId55    );5657    /// Set RFT collection that contract will work with. RFT tokens for fractionalized NFT tokens58    /// would be created in this collection.59    /// @dev Throws if RFT collection is already configured for this contract.60    ///  Throws if collection of wrong type (NFT, Fungible) is provided instead61    ///  of RFT collection.62    ///  Throws if `msg.sender` is not owner or admin of provided RFT collection.63    ///  Can only be called by contract owner.64    /// @param _collection address of RFT collection.65    function setRFTCollection(address _collection) public onlyOwner {66        require(rftCollection == address(0), "RFT collection is already set");67        UniqueRefungible refungibleContract = UniqueRefungible(_collection);68        string memory collectionType = refungibleContract69            .uniqueCollectionType();7071        require(72            keccak256(bytes(collectionType)) == refungibleCollectionType,73            "Wrong collection type. Collection is not refungible."74        );75        require(76            refungibleContract.isOwnerOrAdmin(address(this)),77            "Fractionalizer contract should be an admin of the collection"78        );79        rftCollection = _collection;80        emit RFTCollectionSet(rftCollection);81    }8283    /// Creates and sets RFT collection that contract will work with. RFT tokens for fractionalized NFT tokens84    /// would be created in this collection.85    /// @dev Throws if RFT collection is already configured for this contract.86    ///  Can only be called by contract owner.87    /// @param _name name for created RFT collection.88    /// @param _description description for created RFT collection.89    /// @param _tokenPrefix token prefix for created RFT collection.90    function createAndSetRFTCollection(91        string calldata _name,92        string calldata _description,93        string calldata _tokenPrefix94    ) public onlyOwner {95        require(rftCollection == address(0), "RFT collection is already set");96        address collectionHelpers = 0x6C4E9fE1AE37a41E93CEE429e8E1881aBdcbb54F;97        rftCollection = CollectionHelpers(collectionHelpers)98            .createRFTCollection(_name, _description, _tokenPrefix);99        emit RFTCollectionSet(rftCollection);100    }101102    /// Allow or disallow NFT collection tokens from being fractionalized by this contract.103    /// @dev Can only be called by contract owner.104    /// @param collection NFT token address.105    /// @param status `true` to allow and `false` to disallow NFT token.106    function setNftCollectionIsAllowed(address collection, bool status)107        public108        onlyOwner109    {110        nftCollectionAllowList[collection] = status;111        emit AllowListSet(collection, status);112    }113114    /// Fractionilize NFT token.115    /// @dev Takes NFT token from `msg.sender` and transfers RFT token to `msg.sender`116    ///  instead. Creates new RFT token if provided NFT token never was fractionalized117    ///  by this contract or existing RFT token if it was.118    ///  Throws if RFT collection isn't configured for this contract.119    ///  Throws if fractionalization of provided NFT token is not allowed120    ///  Throws if `msg.sender` is not owner of provided NFT token121    /// @param  _collection NFT collection address122    /// @param  _token id of NFT token to be fractionalized123    /// @param  _pieces number of pieces new RFT token would have124    function nft2rft(125        address _collection,126        uint256 _token,127        uint128 _pieces128    ) public {129        require(rftCollection != address(0), "RFT collection is not set");130        UniqueRefungible rftCollectionContract = UniqueRefungible(131            rftCollection132        );133        require(134            nftCollectionAllowList[_collection] == true,135            "Fractionalization of this collection is not allowed by admin"136        );137        require(138            UniqueNFT(_collection).ownerOf(_token) == msg.sender,139            "Only token owner could fractionalize it"140        );141        UniqueNFT(_collection).transferFrom(msg.sender, address(this), _token);142        uint256 rftTokenId;143        address rftTokenAddress;144        UniqueRefungibleToken rftTokenContract;145        if (nft2rftMapping[_collection][_token] == 0) {146            rftTokenId = rftCollectionContract.nextTokenId();147            rftCollectionContract.mint(address(this), rftTokenId);148            rftTokenAddress = rftCollectionContract.tokenContractAddress(149                rftTokenId150            );151            nft2rftMapping[_collection][_token] = rftTokenId;152            rft2nftMapping[rftTokenAddress] = Token(_collection, _token);153154            rftTokenContract = UniqueRefungibleToken(rftTokenAddress);155        } else {156            rftTokenId = nft2rftMapping[_collection][_token];157            rftTokenAddress = rftCollectionContract.tokenContractAddress(158                rftTokenId159            );160            rftTokenContract = UniqueRefungibleToken(rftTokenAddress);161        }162        rftTokenContract.repartition(_pieces);163        rftTokenContract.transfer(msg.sender, _pieces);164        emit Fractionalized(_collection, _token, rftTokenAddress, _pieces);165    }166167    /// Defrationalize NFT token.168    /// @dev Takes RFT token from `msg.sender` and transfers corresponding NFT token169    ///  to `msg.sender` instead.170    ///  Throws if RFT collection isn't configured for this contract.171    ///  Throws if provided RFT token is no from configured RFT collection.172    ///  Throws if RFT token was not created by this contract.173    ///  Throws if `msg.sender` isn't owner of all RFT token pieces.174    /// @param _collection RFT collection address175    /// @param _token id of RFT token176    function rft2nft(address _collection, uint256 _token) public {177        require(rftCollection != address(0), "RFT collection is not set");178        require(rftCollection == _collection, "Wrong RFT collection");179        UniqueRefungible rftCollectionContract = UniqueRefungible(180            rftCollection181        );182        address rftTokenAddress = rftCollectionContract.tokenContractAddress(183            _token184        );185        Token memory nftToken = rft2nftMapping[rftTokenAddress];186        require(187            nftToken._collection != address(0),188            "No corresponding NFT token found"189        );190        UniqueRefungibleToken rftTokenContract = UniqueRefungibleToken(191            rftTokenAddress192        );193        require(194            rftTokenContract.balanceOf(msg.sender) ==195                rftTokenContract.totalSupply(),196            "Not all pieces are owned by the caller"197        );198        rftCollectionContract.transferFrom(msg.sender, address(this), _token);199        UniqueNFT(nftToken._collection).transferFrom(200            address(this),201            msg.sender,202            nftToken._tokenId203        );204        emit Defractionalized(205            rftTokenAddress,206            nftToken._collection,207            nftToken._tokenId208        );209    }210}
after · tests/src/eth/fractionalizer/Fractionalizer.sol
1// SPDX-License-Identifier:  Apache License2pragma solidity >=0.8.0;3import {CollectionHelpers} from "../api/CollectionHelpers.sol";4import {ContractHelpers} from "../api/ContractHelpers.sol";5import {UniqueRefungibleToken} from "../api/UniqueRefungibleToken.sol";6import {UniqueRefungible} from "../api/UniqueRefungible.sol";7import {UniqueNFT} from "../api/UniqueNFT.sol";89/// @dev Fractionalization contract. It stores mappings between NFT and RFT tokens,10///  stores allowlist of NFT tokens available for fractionalization, has methods11///  for fractionalization and defractionalization of NFT tokens.12contract Fractionalizer {13	struct Token {14		address _collection;15		uint256 _tokenId;16	}17	address rftCollection;18	mapping(address => bool) nftCollectionAllowList;19	mapping(address => mapping(uint256 => uint256)) public nft2rftMapping;20	mapping(address => Token) public rft2nftMapping;21	bytes32 refungibleCollectionType = keccak256(bytes("ReFungible"));2223	receive() external payable onlyOwner {}2425	/// @dev Method modifier to only allow contract owner to call it.26	modifier onlyOwner() {27		address contracthelpersAddress = 0x842899ECF380553E8a4de75bF534cdf6fBF64049;28		ContractHelpers contractHelpers = ContractHelpers(contracthelpersAddress);29		address contractOwner = contractHelpers.contractOwner(address(this));30		require(msg.sender == contractOwner, "Only owner can");31		_;32	}3334	/// @dev This emits when RFT collection setting is changed.35	event RFTCollectionSet(address _collection);3637	/// @dev This emits when NFT collection is allowed or disallowed.38	event AllowListSet(address _collection, bool _status);3940	/// @dev This emits when NFT token is fractionalized by contract.41	event Fractionalized(address _collection, uint256 _tokenId, address _rftToken, uint128 _amount);4243	/// @dev This emits when NFT token is defractionalized by contract.44	event Defractionalized(address _rftToken, address _nftCollection, uint256 _nftTokenId);4546	/// Set RFT collection that contract will work with. RFT tokens for fractionalized NFT tokens47	/// would be created in this collection.48	/// @dev Throws if RFT collection is already configured for this contract.49	///  Throws if collection of wrong type (NFT, Fungible) is provided instead50	///  of RFT collection.51	///  Throws if `msg.sender` is not owner or admin of provided RFT collection.52	///  Can only be called by contract owner.53	/// @param _collection address of RFT collection.54	function setRFTCollection(address _collection) public onlyOwner {55		require(rftCollection == address(0), "RFT collection is already set");56		UniqueRefungible refungibleContract = UniqueRefungible(_collection);57		string memory collectionType = refungibleContract.uniqueCollectionType();5859		require(60			keccak256(bytes(collectionType)) == refungibleCollectionType,61			"Wrong collection type. Collection is not refungible."62		);63		require(64			refungibleContract.isOwnerOrAdmin(address(this)),65			"Fractionalizer contract should be an admin of the collection"66		);67		rftCollection = _collection;68		emit RFTCollectionSet(rftCollection);69	}7071	/// Creates and sets RFT collection that contract will work with. RFT tokens for fractionalized NFT tokens72	/// would be created in this collection.73	/// @dev Throws if RFT collection is already configured for this contract.74	///  Can only be called by contract owner.75	/// @param _name name for created RFT collection.76	/// @param _description description for created RFT collection.77	/// @param _tokenPrefix token prefix for created RFT collection.78	function createAndSetRFTCollection(79		string calldata _name,80		string calldata _description,81		string calldata _tokenPrefix82	) public onlyOwner {83		require(rftCollection == address(0), "RFT collection is already set");84		address collectionHelpers = 0x6C4E9fE1AE37a41E93CEE429e8E1881aBdcbb54F;85		rftCollection = CollectionHelpers(collectionHelpers).createRFTCollection(_name, _description, _tokenPrefix);86		emit RFTCollectionSet(rftCollection);87	}8889	/// Allow or disallow NFT collection tokens from being fractionalized by this contract.90	/// @dev Can only be called by contract owner.91	/// @param collection NFT token address.92	/// @param status `true` to allow and `false` to disallow NFT token.93	function setNftCollectionIsAllowed(address collection, bool status) public onlyOwner {94		nftCollectionAllowList[collection] = status;95		emit AllowListSet(collection, status);96	}9798	/// Fractionilize NFT token.99	/// @dev Takes NFT token from `msg.sender` and transfers RFT token to `msg.sender`100	///  instead. Creates new RFT token if provided NFT token never was fractionalized101	///  by this contract or existing RFT token if it was.102	///  Throws if RFT collection isn't configured for this contract.103	///  Throws if fractionalization of provided NFT token is not allowed104	///  Throws if `msg.sender` is not owner of provided NFT token105	/// @param  _collection NFT collection address106	/// @param  _token id of NFT token to be fractionalized107	/// @param  _pieces number of pieces new RFT token would have108	function nft2rft(109		address _collection,110		uint256 _token,111		uint128 _pieces112	) public {113		require(rftCollection != address(0), "RFT collection is not set");114		UniqueRefungible rftCollectionContract = UniqueRefungible(rftCollection);115		require(116			nftCollectionAllowList[_collection] == true,117			"Fractionalization of this collection is not allowed by admin"118		);119		require(UniqueNFT(_collection).ownerOf(_token) == msg.sender, "Only token owner could fractionalize it");120		UniqueNFT(_collection).transferFrom(msg.sender, address(this), _token);121		uint256 rftTokenId;122		address rftTokenAddress;123		UniqueRefungibleToken rftTokenContract;124		if (nft2rftMapping[_collection][_token] == 0) {125			rftTokenId = rftCollectionContract.nextTokenId();126			rftCollectionContract.mint(address(this), rftTokenId);127			rftTokenAddress = rftCollectionContract.tokenContractAddress(rftTokenId);128			nft2rftMapping[_collection][_token] = rftTokenId;129			rft2nftMapping[rftTokenAddress] = Token(_collection, _token);130131			rftTokenContract = UniqueRefungibleToken(rftTokenAddress);132		} else {133			rftTokenId = nft2rftMapping[_collection][_token];134			rftTokenAddress = rftCollectionContract.tokenContractAddress(rftTokenId);135			rftTokenContract = UniqueRefungibleToken(rftTokenAddress);136		}137		rftTokenContract.repartition(_pieces);138		rftTokenContract.transfer(msg.sender, _pieces);139		emit Fractionalized(_collection, _token, rftTokenAddress, _pieces);140	}141142	/// Defrationalize NFT token.143	/// @dev Takes RFT token from `msg.sender` and transfers corresponding NFT token144	///  to `msg.sender` instead.145	///  Throws if RFT collection isn't configured for this contract.146	///  Throws if provided RFT token is no from configured RFT collection.147	///  Throws if RFT token was not created by this contract.148	///  Throws if `msg.sender` isn't owner of all RFT token pieces.149	/// @param _collection RFT collection address150	/// @param _token id of RFT token151	function rft2nft(address _collection, uint256 _token) public {152		require(rftCollection != address(0), "RFT collection is not set");153		require(rftCollection == _collection, "Wrong RFT collection");154		UniqueRefungible rftCollectionContract = UniqueRefungible(rftCollection);155		address rftTokenAddress = rftCollectionContract.tokenContractAddress(_token);156		Token memory nftToken = rft2nftMapping[rftTokenAddress];157		require(nftToken._collection != address(0), "No corresponding NFT token found");158		UniqueRefungibleToken rftTokenContract = UniqueRefungibleToken(rftTokenAddress);159		require(160			rftTokenContract.balanceOf(msg.sender) == rftTokenContract.totalSupply(),161			"Not all pieces are owned by the caller"162		);163		rftCollectionContract.transferFrom(msg.sender, address(this), _token);164		UniqueNFT(nftToken._collection).transferFrom(address(this), msg.sender, nftToken._tokenId);165		emit Defractionalized(rftTokenAddress, nftToken._collection, nftToken._tokenId);166	}167}