Skip to content

Commit

Permalink
code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Tonnanto committed Oct 2, 2022
1 parent 443757d commit c3995f4
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 81 deletions.
134 changes: 67 additions & 67 deletions flutter_bird_skins/contracts/FlutterBirdSkins.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
2 changes: 1 addition & 1 deletion flutter_bird_skins/migrations/1_initial_migration.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const FlutterBirdSkins = artifacts.require("FlutterBirdSkins");

module.exports = function (deployer) {
deployer.deploy(FlutterBirdSkins);
deployer.deploy(FlutterBirdSkins);
};
5 changes: 2 additions & 3 deletions flutter_bird_skins/scripts/create_data.js
Original file line number Diff line number Diff line change
@@ -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++) {
Expand Down
7 changes: 3 additions & 4 deletions flutter_bird_skins/scripts/generate_image.js
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -58,4 +57,4 @@ async function generateImage(tokenId, bird, head, eyes, mouth, neck) {
return filePath;
}

module.exports = { generateImage }
module.exports = {generateImage}
4 changes: 2 additions & 2 deletions flutter_bird_skins/scripts/generate_metadata.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -86,4 +86,4 @@ function generateMetadata(tokenId) {
return skinMetadata;
}

module.exports = { generateMetadata }
module.exports = {generateMetadata}
7 changes: 3 additions & 4 deletions flutter_bird_skins/scripts/update_image_url.js
Original file line number Diff line number Diff line change
@@ -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
Expand Down

0 comments on commit c3995f4

Please sign in to comment.