Created
October 14, 2021 23:42
-
-
Save farzaa/91a0c0cf48e71d0391069964a65271b4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const main = async () => { | |
const gameContractFactory = await hre.ethers.getContractFactory('MyEpicGame'); | |
const gameContract = await gameContractFactory.deploy( | |
[0, 1, 2], | |
["Leo", "Aang", "Tom Brady"], | |
["https://i.imgur.com/pKd5Sdk.png", | |
"https://i.imgur.com/xVu4vFL.png", | |
"https://i.imgur.com/FcXCH9W.jpg"], | |
[100, 200, 300], | |
[100, 50, 25] | |
); | |
await gameContract.deployed(); | |
console.log("Contract deployed to:", gameContract.address); | |
let txn; | |
txn = await gameContract.mintCharacterNFT(0); | |
txn.wait(); | |
txn = await gameContract.mintCharacterNFT(1); | |
txn.wait(); | |
txn = await gameContract.mintCharacterNFT(1); | |
txn.wait(); | |
txn = await gameContract.mintCharacterNFT(2); | |
txn.wait(); | |
console.log("Done deploying and minting!"); | |
}; | |
const runMain = async () => { | |
try { | |
await main(); | |
process.exit(0); | |
} catch (error) { | |
console.log(error); | |
process.exit(1); | |
} | |
}; | |
runMain(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// SPDX-License-Identifier: MIT | |
pragma solidity ^0.8.0; | |
import "@openzeppelin/contracts/token/ERC721/ERC721.sol"; | |
import "@openzeppelin/contracts/utils/Counters.sol"; | |
import "@openzeppelin/contracts/utils/Strings.sol"; | |
// String encoding libraries | |
import { Base64 } from "./libraries/Base64.sol"; | |
import "hardhat/console.sol"; | |
contract MyEpicGame is ERC721 { | |
struct CharacterAttributes { | |
string name; | |
string imageURI; | |
uint hp; | |
uint attackDamage; | |
} | |
using Counters for Counters.Counter; | |
Counters.Counter private _tokenIds; | |
mapping(uint => CharacterAttributes) public defaultAttributes; | |
mapping(uint256 => CharacterAttributes) public nftHolderAttributes; | |
constructor( | |
uint[] memory characterIds, | |
string[] memory characterNames, | |
string[] memory characterImageURIs, | |
uint[] memory characterHp, | |
uint[] memory characterAttackDmg | |
) | |
ERC721("Heroes", "HERO") | |
{ | |
for(uint i = 0; i < characterIds.length; i += 1) { | |
defaultAttributes[characterIds[i]] = CharacterAttributes({ | |
name: characterNames[i], | |
imageURI: characterImageURIs[i], | |
hp: characterHp[i], | |
attackDamage: characterAttackDmg[i] | |
}); | |
CharacterAttributes memory c = defaultAttributes[characterIds[i]]; | |
console.log("Done initializing %s w/ HP %s, img %s", c.name, c.hp, c.imageURI); | |
} | |
} | |
function tokenURI(uint256 _tokenId) public view override returns (string memory) { | |
CharacterAttributes memory charAttributes = nftHolderAttributes[_tokenId]; | |
string memory strHp = Strings.toString(charAttributes.hp); | |
string memory strAttackDamage = Strings.toString(charAttributes.attackDamage); | |
string memory json = Base64.encode( | |
bytes( | |
string( | |
abi.encodePacked( | |
'{"name": "', | |
charAttributes.name, | |
' -- NFT #: ', | |
Strings.toString(_tokenId), | |
'", "description": "CriticalHit is a turn-based NFT game where you take turns to attack the boos.", "image": "', | |
charAttributes.imageURI, | |
'", "attributes": [ { "trait_type": "Health Points", "value": ',strHp,', "max_value": 300 }, { "trait_type": "Attack Damage", "value": ', | |
strAttackDamage,', "max_value": 400} ]}' | |
) | |
) | |
) | |
); | |
string memory output = string( | |
abi.encodePacked("data:application/json;base64,", json) | |
); | |
return output; | |
} | |
function mintCharacterNFT(uint _characterId) external { | |
uint256 newItemId = _tokenIds.current(); | |
_safeMint(msg.sender, newItemId); | |
nftHolderAttributes[newItemId] = CharacterAttributes({ | |
name: defaultAttributes[_characterId].name, | |
imageURI: defaultAttributes[_characterId].imageURI, | |
hp: defaultAttributes[_characterId].hp, | |
attackDamage: defaultAttributes[_characterId].attackDamage | |
}); | |
console.log("Minted NFT w/ tokenId %s and characterId %s", newItemId, _characterId); | |
_tokenIds.increment(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const main = async () => { | |
const gameContractFactory = await hre.ethers.getContractFactory('MyEpicGame'); | |
const gameContract = await gameContractFactory.deploy( | |
[0, 1, 2], | |
["Leo", "Aang", "Tom Brady"], | |
["https://i.imgur.com/pKd5Sdk.png", | |
"https://i.imgur.com/xVu4vFL.png", | |
"https://i.imgur.com/FcXCH9W.jpg"], | |
[100, 200, 300], | |
[100, 50, 25] | |
); | |
await gameContract.deployed(); | |
console.log("Contract deployed to:", gameContract.address); | |
let txn; | |
txn = await gameContract.mintCharacterNFT(0); | |
txn.wait(); | |
txn = await gameContract.mintCharacterNFT(1); | |
txn.wait(); | |
txn = await gameContract.mintCharacterNFT(1); | |
txn.wait(); | |
txn = await gameContract.mintCharacterNFT(2); | |
txn.wait(); | |
console.log("Done deploying and minting!"); | |
}; | |
const runMain = async () => { | |
try { | |
await main(); | |
process.exit(0); | |
} catch (error) { | |
console.log(error); | |
process.exit(1); | |
} | |
}; | |
runMain(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment