From c3995f412d175dbd7c7e58a78ce6a74093509da7 Mon Sep 17 00:00:00 2001 From: Anton Stamme Date: Sun, 2 Oct 2022 21:23:07 +0200 Subject: [PATCH] code cleanup --- .../contracts/FlutterBirdSkins.sol | 134 +++++++++--------- .../migrations/1_initial_migration.js | 2 +- flutter_bird_skins/scripts/create_data.js | 5 +- flutter_bird_skins/scripts/generate_image.js | 7 +- .../scripts/generate_metadata.js | 4 +- .../scripts/update_image_url.js | 7 +- 6 files changed, 78 insertions(+), 81 deletions(-) diff --git a/flutter_bird_skins/contracts/FlutterBirdSkins.sol b/flutter_bird_skins/contracts/FlutterBirdSkins.sol index 65dd231..c406822 100644 --- a/flutter_bird_skins/contracts/FlutterBirdSkins.sol +++ b/flutter_bird_skins/contracts/FlutterBirdSkins.sol @@ -4,93 +4,93 @@ pragma solidity ^0.8.0; import "@openzeppelin/contracts/token/ERC721/extensions/ERC721Enumerable.sol"; contract FlutterBirdSkins is ERC721Enumerable { - using Strings for uint256; + using Strings for uint256; - uint256 private _maxSupply = 1000; - uint256 private _mintPrice = 0.01 ether; + uint256 private _maxSupply = 1000; + uint256 private _mintPrice = 0.01 ether; - // Optional mapping for token URIs - mapping(uint256 => string) private _tokenURIs; + // Optional mapping for token URIs + mapping(uint256 => string) private _tokenURIs; - // Assign owner on contract creation - address public owner = msg.sender; + // Assign owner on contract creation + address public owner = msg.sender; - constructor() ERC721("FlutterBirdSkins", "FBS") {} + constructor() ERC721("FlutterBirdSkins", "FBS") {} - event SkinMinted(uint256 indexed tokenId); + event SkinMinted(uint256 indexed tokenId); - function mintSkin(uint256 newTokenId) public payable { - require(newTokenId < _maxSupply, "invalid tokenId. must be #999"); - require(msg.value >= _mintPrice, "insufficient funds"); + function mintSkin(uint256 newTokenId) public payable { + require(newTokenId < _maxSupply, "invalid tokenId. must be #999"); + require(msg.value >= _mintPrice, "insufficient funds"); - _safeMint(msg.sender, newTokenId); + _safeMint(msg.sender, newTokenId); - string memory _tokenURI = string.concat(Strings.toString(newTokenId), ".json"); - _setTokenURI(newTokenId, _tokenURI); + string memory _tokenURI = string.concat(Strings.toString(newTokenId), ".json"); + _setTokenURI(newTokenId, _tokenURI); - emit SkinMinted(newTokenId); - } + emit SkinMinted(newTokenId); + } - /** - * @notice returns a list of tokenIds that are owned by the given address + /** + * @notice returns a list of tokenIds that are owned by the given address */ - function getTokensForOwner(address _owner) public view returns (uint[] memory) { - uint[] memory _tokensOfOwner = new uint[](ERC721.balanceOf(_owner)); - uint i; - - for (i=0; i < ERC721.balanceOf(_owner); i++) { - _tokensOfOwner[i] = ERC721Enumerable.tokenOfOwnerByIndex(_owner, i); + function getTokensForOwner(address _owner) public view returns (uint[] memory) { + uint[] memory _tokensOfOwner = new uint[](ERC721.balanceOf(_owner)); + uint i; + + for (i = 0; i < ERC721.balanceOf(_owner); i++) { + _tokensOfOwner[i] = ERC721Enumerable.tokenOfOwnerByIndex(_owner, i); + } + return (_tokensOfOwner); } - return (_tokensOfOwner); - } - /** - * @notice returns a list of boolean values indicating whether the skin with that index has been minted already. + /** + * @notice returns a list of boolean values indicating whether the skin with that index has been minted already. */ - function getMintedTokenList() public view returns (bool[] memory) { - bool[] memory _unmintedTokes = new bool[](_maxSupply); - uint i; - - for (i = 0; i < _maxSupply; i++) { - if (_exists(i)) { - _unmintedTokes[i] = true; - } + function getMintedTokenList() public view returns (bool[] memory) { + bool[] memory _unmintedTokes = new bool[](_maxSupply); + uint i; + + for (i = 0; i < _maxSupply; i++) { + if (_exists(i)) { + _unmintedTokes[i] = true; + } + } + return _unmintedTokes; + } + + function _baseURI() internal view virtual override returns (string memory) { + return "ipfs://bafybeiexvqt3uabqzmhquokzyl7gcdxyowz2hf2hdbbifkkyx3waghezqe/"; } - return _unmintedTokes; - } - function _baseURI() internal view virtual override returns (string memory) { - return "ipfs://bafybeiexvqt3uabqzmhquokzyl7gcdxyowz2hf2hdbbifkkyx3waghezqe/"; - } + function tokenURI(uint256 tokenId) public view virtual override returns (string memory) { + require(_exists(tokenId), "ERC721URIStorage: URI query for nonexistent token"); - function tokenURI(uint256 tokenId) public view virtual override returns (string memory) { - require(_exists(tokenId), "ERC721URIStorage: URI query for nonexistent token"); + string memory _tokenURI = _tokenURIs[tokenId]; + string memory base = _baseURI(); - string memory _tokenURI = _tokenURIs[tokenId]; - string memory base = _baseURI(); + // If there is no base URI, return the token URI. + if (bytes(base).length == 0) { + return _tokenURI; + } + // If both are set, concatenate the baseURI and tokenURI (via abi.encodePacked). + if (bytes(_tokenURI).length > 0) { + return string(abi.encodePacked(base, _tokenURI)); + } - // If there is no base URI, return the token URI. - if (bytes(base).length == 0) { - return _tokenURI; + return tokenURI(tokenId); } - // If both are set, concatenate the baseURI and tokenURI (via abi.encodePacked). - if (bytes(_tokenURI).length > 0) { - return string(abi.encodePacked(base, _tokenURI)); + + function setTokenUri(uint256 tokenId, string memory _tokenURI) public { + require( + _isApprovedOrOwner(_msgSender(), tokenId), + "ERC721: transfer caller is not owner nor approved" + ); + _setTokenURI(tokenId, _tokenURI); } - return tokenURI(tokenId); - } - - function setTokenUri(uint256 tokenId, string memory _tokenURI) public { - require( - _isApprovedOrOwner(_msgSender(), tokenId), - "ERC721: transfer caller is not owner nor approved" - ); - _setTokenURI(tokenId, _tokenURI); - } - - function _setTokenURI(uint256 tokenId, string memory _tokenURI) internal virtual { - require(_exists(tokenId), "ERC721URIStorage: URI set of nonexistent token"); - _tokenURIs[tokenId] = _tokenURI; - } + function _setTokenURI(uint256 tokenId, string memory _tokenURI) internal virtual { + require(_exists(tokenId), "ERC721URIStorage: URI set of nonexistent token"); + _tokenURIs[tokenId] = _tokenURI; + } } diff --git a/flutter_bird_skins/migrations/1_initial_migration.js b/flutter_bird_skins/migrations/1_initial_migration.js index b4cf4c5..ac4e78a 100644 --- a/flutter_bird_skins/migrations/1_initial_migration.js +++ b/flutter_bird_skins/migrations/1_initial_migration.js @@ -1,5 +1,5 @@ const FlutterBirdSkins = artifacts.require("FlutterBirdSkins"); module.exports = function (deployer) { - deployer.deploy(FlutterBirdSkins); + deployer.deploy(FlutterBirdSkins); }; diff --git a/flutter_bird_skins/scripts/create_data.js b/flutter_bird_skins/scripts/create_data.js index 3bf9288..c907a0a 100644 --- a/flutter_bird_skins/scripts/create_data.js +++ b/flutter_bird_skins/scripts/create_data.js @@ -1,6 +1,5 @@ - -const {generateImage} = require ("./generate_image") -const {generateMetadata} = require ("./generate_metadata") +const {generateImage} = require("./generate_image") +const {generateMetadata} = require("./generate_metadata") // Test data generation for (let tokenId = 0; tokenId < 1000; tokenId++) { diff --git a/flutter_bird_skins/scripts/generate_image.js b/flutter_bird_skins/scripts/generate_image.js index 240c332..e233df9 100644 --- a/flutter_bird_skins/scripts/generate_image.js +++ b/flutter_bird_skins/scripts/generate_image.js @@ -1,6 +1,5 @@ - -const fs = require("fs"); -const { createCanvas, loadImage } = require("canvas"); +const fs = require("fs"); +const {createCanvas, loadImage} = require("canvas"); const imageSize = { width: 750, @@ -58,4 +57,4 @@ async function generateImage(tokenId, bird, head, eyes, mouth, neck) { return filePath; } -module.exports = { generateImage } +module.exports = {generateImage} diff --git a/flutter_bird_skins/scripts/generate_metadata.js b/flutter_bird_skins/scripts/generate_metadata.js index 2d5f6ef..55d64c3 100644 --- a/flutter_bird_skins/scripts/generate_metadata.js +++ b/flutter_bird_skins/scripts/generate_metadata.js @@ -5,7 +5,7 @@ function getRandomWeightedTrait(traitList, guaranteed = false) { // Calculate total weights let totalWeight = 0; - traitList.forEach(function(trait) { + traitList.forEach(function (trait) { if (!guaranteed || (trait.name !== "none" && trait.name !== "default")) { totalWeight += trait.weight } @@ -86,4 +86,4 @@ function generateMetadata(tokenId) { return skinMetadata; } -module.exports = { generateMetadata } +module.exports = {generateMetadata} diff --git a/flutter_bird_skins/scripts/update_image_url.js b/flutter_bird_skins/scripts/update_image_url.js index de084b1..dc610a9 100644 --- a/flutter_bird_skins/scripts/update_image_url.js +++ b/flutter_bird_skins/scripts/update_image_url.js @@ -1,18 +1,17 @@ - const fs = require('fs'); const dirname = "../output/metadata" const cid = "bafybeib3ss7fb7wejue2b7nv3frucl3xvhd27yycyw37csodsytvv5ipou"; -fs.readdir(dirname, function(err, filenames) { +fs.readdir(dirname, function (err, filenames) { if (err) { console.error(err) return } - filenames.forEach(function(filename) { + filenames.forEach(function (filename) { const filePath = dirname + "/" + filename; - fs.readFile(filePath, 'utf-8', function(err, content) { + fs.readFile(filePath, 'utf-8', function (err, content) { if (err) { console.error(err) return