Last active
February 7, 2025 02:30
-
-
Save valterlobo/af901245e56f2de6eb01d606d7aaf82f to your computer and use it in GitHub Desktop.
PhygitalAssets -
This file contains hidden or 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.20; | |
import "@openzeppelin/contracts/token/ERC1155/ERC1155.sol"; | |
import "@openzeppelin/contracts/access/Ownable.sol"; | |
import "@openzeppelin/contracts/utils/Strings.sol"; | |
import "./PhygitalAssetsStruct.sol"; | |
contract PhygitalAssets is ERC1155, Ownable { | |
using Strings for uint256; | |
mapping(uint256 => Asset) public assets; | |
uint256 private nextTokenId; | |
event AssetCreated(uint256 indexed tokenId, string name, uint256 maxSupply, bool supplyCapped); | |
event AssetMinted(uint256 indexed tokenId, address indexed to, uint256 amount); | |
constructor(address initialOwner) ERC1155("") Ownable(initialOwner) {} | |
function createAsset(string memory _name, string calldata _uri, uint256 _maxSupply, bool _supplyCapped) | |
external | |
onlyOwner | |
{ | |
uint256 tokenId = nextTokenId++; | |
assets[tokenId] = Asset({ | |
id: tokenId, | |
name: _name, | |
totalSupply: 0, | |
maxSupply: _maxSupply, | |
supplyCapped: _supplyCapped, | |
uri: _uri | |
}); | |
emit AssetCreated(tokenId, _name, _maxSupply, _supplyCapped); | |
} | |
function mintAsset(uint256 _tokenId, address _to, uint256 _amount) external onlyOwner { | |
require(bytes(assets[_tokenId].name).length > 0, "Asset does not exist"); | |
if (assets[_tokenId].supplyCapped) { | |
require(assets[_tokenId].totalSupply + _amount <= assets[_tokenId].maxSupply, "Max supply reached"); | |
} | |
assets[_tokenId].totalSupply += _amount; | |
if (!assets[_tokenId].supplyCapped) { | |
assets[_tokenId].maxSupply = assets[_tokenId].totalSupply; | |
} | |
_mint(_to, _tokenId, _amount, ""); | |
emit AssetMinted(_tokenId, _to, _amount); | |
} | |
function uri(uint256 _tokenId) public view override(ERC1155) returns (string memory) { | |
require(bytes(assets[_tokenId].name).length > 0, "Asset does not exist"); | |
return assets[_tokenId].uri; | |
} | |
function setUri(uint256 _tokenId, string memory _newUri) external onlyOwner returns (string memory) { | |
require(bytes(assets[_tokenId].name).length > 0, "Asset does not exist"); | |
assets[_tokenId].uri = _newUri; | |
return assets[_tokenId].uri; | |
} | |
} | |
//-------------- | |
// SPDX-License-Identifier: MIT | |
pragma solidity ^0.8.20; | |
struct Asset { | |
uint256 id; | |
string name; | |
uint256 totalSupply; | |
uint256 maxSupply; | |
bool supplyCapped; | |
string uri; | |
} | |
/// -TEST - PhygitalAssets.t.sol | |
// SPDX-License-Identifier: MIT | |
pragma solidity ^0.8.20; | |
import "forge-std/Test.sol"; | |
import "../src/PhygitalAssets.sol"; | |
contract PhygitalAssetsTest is Test { | |
PhygitalAssets phygitalAssets; | |
address owner = address(this); | |
address user1 = address(0x123); | |
function setUp() public { | |
phygitalAssets = new PhygitalAssets(owner); | |
} | |
function testCreateAsset() public { | |
//createAsset(string memory _name, string calldata _uri, uint256 _maxSupply, bool _supplyCapped) | |
phygitalAssets.createAsset("Arte Exclusiva", "ipfs://metadata1", 100, true); | |
(uint256 id, string memory name, , uint256 maxSupply, bool supplyCapped, string memory uri) = | |
phygitalAssets.assets(0); | |
assertEq(id, 0); | |
assertEq(name, "Arte Exclusiva"); | |
assertEq(maxSupply, 100); | |
assertEq(supplyCapped, true); | |
assertEq(uri, "ipfs://metadata1"); | |
} | |
function testMintAssetWithinLimit() public { | |
phygitalAssets.createAsset("Colecionavel", "ipfs://metadata2", 50, true); | |
phygitalAssets.mintAsset(0, user1, 10); | |
(, , uint256 totalSupply, uint256 maxSupply, , ) = phygitalAssets.assets(0); | |
assertEq(totalSupply, 10); | |
assertEq(maxSupply, 50); | |
} | |
function testFailMintExceedingSupply() public { | |
phygitalAssets.createAsset("Escultura", "ipfs://metadata3", 5, true); | |
phygitalAssets.mintAsset(0, user1, 6); // Deve falhar pois excede maxSupply | |
} | |
function testMintUnlimitedSupply() public { | |
phygitalAssets.createAsset("Obra Sem Limite", "ipfs://metadata4", 0, false); | |
// mintAsset(uint256 _tokenId, address _to, uint256 _amount) | |
phygitalAssets.mintAsset( 0, user1, 30); | |
(, , uint256 totalSupply, uint256 maxSupply, , ) = phygitalAssets.assets(0); | |
assertEq(totalSupply, 30); | |
assertEq(maxSupply, 30); | |
} | |
function testUriRetrieval() public { | |
phygitalAssets.createAsset("Quadro", "ipfs://metadata5", 20, true); | |
string memory retrievedUri = phygitalAssets.uri(0); | |
assertEq(retrievedUri, "ipfs://metadata5"); | |
} | |
function testFailMintNonExistentAsset() public { | |
phygitalAssets.mintAsset(99, user1,1); // Deve falhar pois o ID 99 não existe | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment