Skip to content

Commit

Permalink
added some comments and overrides.
Browse files Browse the repository at this point in the history
  • Loading branch information
MrDeadCe11 committed Aug 3, 2023
1 parent c9be91c commit 8050c85
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 24 deletions.
6 changes: 3 additions & 3 deletions src/factories/CharacterSheetsFactory.sol
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ contract CharacterSheetsFactory is OwnableUpgradeable {
}

function create(
address dungeonMaster,
address[] calldata dungeonMasters,
address dao,
string calldata experienceBaseuri,
string calldata characterSheetsBaseUri
Expand All @@ -51,9 +51,9 @@ contract CharacterSheetsFactory is OwnableUpgradeable {
address experienceClone = ClonesUpgradeable.cloneDeterministic(experienceAndItemsImplementation, 0);

bytes memory encodedCharacterSheetParameters =
abi.encode(dao, dungeonMaster, experienceClone, characterSheetsBaseUri);
abi.encode(dao, dungeonMasters, experienceClone, characterSheetsBaseUri);
bytes memory encodedExperienceParameters =
abi.encode(dao, dungeonMaster, characterSheetsClone, hatsAddress, experienceBaseuri);
abi.encode(dao, dungeonMasters, characterSheetsClone, hatsAddress, experienceBaseuri);

CharacterSheetsImplementation(characterSheetsClone).initialize(encodedCharacterSheetParameters);
ExperienceAndItemsImplementation(experienceClone).initialize(encodedExperienceParameters);
Expand Down
77 changes: 71 additions & 6 deletions src/implementations/CharacterSheetsImplementation.sol
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import "forge-std/console2.sol";
string name;
address ERC6551TokenAddress;
address memberAddress;
uint256[] classes;
uint256[] items;
}

contract CharacterSheetsImplementation is
Expand Down Expand Up @@ -59,27 +61,45 @@ contract CharacterSheetsImplementation is
constructor() ERC721("CharacterSheet", "CHAS") {
_disableInitializers();
}

/**
*
* @param _encodedParameters encoded parameters must include:
* @dev address daoAddress the address of the dao who's member list will be allowed to become players and who will be able to interact with this contract
* @dev address[] dungeonMasters an array addresses of the person/persons who are authorized to issue player cards, classes, and items.
* @dev string baseURI the default uri of the player card images, arbitrary a different uri can be set when the character sheet is minted.
* @dev address experienceImplementation this is the address of the ERC1155 experience contract associated with this contract. this is assigned at contract creation.
*/
function initialize(bytes calldata _encodedParameters) public initializer {
_grantRole(DEFAULT_ADMIN_ROLE, msg.sender);
_grantRole(DUNGEON_MASTER, msg.sender);

address daoAddress;
address dungeonMaster;
address[] memory dungeonMasters;
string memory baseUri;
address experienceImplementation;

(daoAddress, dungeonMaster, experienceImplementation, baseUri) =
abi.decode(_encodedParameters, (address, address, address, string));
(daoAddress, dungeonMasters, experienceImplementation, baseUri) =
abi.decode(_encodedParameters, (address, address[], address, string));

_grantRole(DEFAULT_ADMIN_ROLE, dungeonMaster);
_grantRole(DUNGEON_MASTER, dungeonMaster);

for(uint256 i = 0; i<dungeonMasters.length; i++){
_grantRole(DUNGEON_MASTER, dungeonMasters[i]);
_grantRole(DEFAULT_ADMIN_ROLE, dungeonMasters[i]);
}

setBaseUri(baseUri);
_experience = ExperienceAndItemsImplementation(experienceImplementation);
_dao = IMolochDAO(daoAddress);
_tokenIdCounter.increment();
}

/**
*
* @param _to the address of the dao member wallet that will hold the character sheet nft
* @param _data encoded data that contains the name of the member and the uri of the base image for the nft.
* if no uri is stored then it will revert to the base uri of the contract
*/

function rollCharacterSheet(address _to, bytes calldata _data) public onlyRole(DUNGEON_MASTER) {
require(
erc6551AccountImplementation != address(0) && address(erc6551Registry) != address(0),
Expand All @@ -101,6 +121,7 @@ contract CharacterSheetsImplementation is

if (bytes(_tokenURI).length > 0) {
_setTokenURI(tokenId, _tokenURI);
} else { _setTokenURI(tokenId, _baseTokenURI);
}

//calculate ERC6551 account address
Expand Down Expand Up @@ -196,4 +217,48 @@ contract CharacterSheetsImplementation is
{
return super.supportsInterface(interfaceId);
}

/// transfer overrides since these tokens should be soulbound or only transferable by the dungeonMaster

/**
* @dev See {IERC721-approve}.
*/
function approve(address to, uint256 tokenId) public virtual override(ERC721, IERC721){
revert("This token can only be transfered by the dungeon master");
}

/**
* @dev See {IERC721-setApprovalForAll}.
*/
function setApprovalForAll(address operator, bool approved) public virtual override(ERC721, IERC721){
revert("This token can only be transfered by the dungeon master");
}

/**
* @dev See {IERC721-transferFrom}.
*/
function transferFrom(address from, address to, uint256 tokenId) public virtual override(ERC721, IERC721) onlyRole(DUNGEON_MASTER){
_transfer(from, to, tokenId);
}

/**
* @dev See {IERC721-safeTransferFrom}.
*/
function safeTransferFrom(address from, address to, uint256 tokenId) public virtual override(ERC721, IERC721) onlyRole(DUNGEON_MASTER) {
safeTransferFrom(from, to, tokenId, "");
}

/**
* @dev See {IERC721-safeTransferFrom}.
*/
function safeTransferFrom(address from, address to, uint256 tokenId, bytes memory data) public virtual override(ERC721, IERC721) onlyRole(DUNGEON_MASTER) {
_safeTransfer(from, to, tokenId, data);
}

function renounceSheet(uint256 _playerId)public returns(bool success){
address tokenOwner = ownerOf(_playerId);
require(msg.sender == tokenOwner, "You cannot renounce a token you don't own");
_burn(_playerId);
success = true;
}
}
34 changes: 23 additions & 11 deletions src/implementations/ExperienceAndItemsImplementation.sol
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,7 @@ struct Class {
contract ExperienceAndItemsImplementation is
ERC1155Holder,
Initializable,
ERC1155,
IMolochDAO
ERC1155
{
using Strings for uint256;
using Counters for Counters.Counter;
Expand Down Expand Up @@ -165,6 +164,13 @@ contract ExperienceAndItemsImplementation is
return (_tokenId, _itemId);
}

/**
*
* @param _newClass A class struct with all the class details filled out
* @return tokenId the ERC1155 token id
* @return classId the location of the class in the classes mapping
*/

function createClassType(
Class memory _newClass
) public onlyDungeonMaster returns (uint256 tokenId, uint256 classId) {
Expand All @@ -182,7 +188,12 @@ contract ExperienceAndItemsImplementation is
return (_tokenId, _classId);
}

//reverts if no item found
/**
*
* @param _name a string with the name of the item. is case sensetive so it is preffered that all names are lowercase alphanumeric names enforced in the frontend.
* @return tokenId the ERC1155 token id.
* @return itemId the location of the item in the items mapping;
*/
function findItemByName(
string memory _name
) public view returns (uint256 tokenId, uint256 itemId) {
Expand All @@ -200,7 +211,12 @@ contract ExperienceAndItemsImplementation is
revert("No item found.");
}

//reverts if no class found;
/**
*
* @param _name the name of the class. is case sensetive.
* @return tokenId the ERC1155 token id.
* @return classId storage location of the class in the classes mapping
*/
function findClassByName(
string calldata _name
) public view returns (uint256 tokenId, uint256 classId) {
Expand Down Expand Up @@ -365,7 +381,7 @@ contract ExperienceAndItemsImplementation is
}
}
/**
*
* this is to be claimed from the ERC6551 wallet of the player sheet.
* @param itemIds an array of item ids
* @param amounts an array of amounts to claim, must match the order of item ids
* @param proofs an array of proofs allowing this address to claim the item, must be in same order as item ids and amounts
Expand Down Expand Up @@ -411,12 +427,6 @@ contract ExperienceAndItemsImplementation is
super.setApprovalForAll(operator, approved);
}

function members(
address memberAddress
) external override returns (Member memory member) {
return molochDao.members(memberAddress);
}

function supportsInterface(
bytes4 interfaceId
) public view override(ERC1155Receiver, ERC1155) returns (bool) {
Expand Down Expand Up @@ -469,4 +479,6 @@ contract ExperienceAndItemsImplementation is
_baseURI = baseURI;
}

//#TODO OVER RIDE TRANSFER FUNCTIONS etc for correct soulbindind of tokens.

}
8 changes: 5 additions & 3 deletions test/ExperienceAndItems.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ contract ExperienceAndItemsTest is Test, SetUp {
}

function testCreateItemType() public {
Item memory newItem = createNewItem("Pirate", false, bytes32(0));
Item memory newItem = createNewItem("Pirate_Hat", false, bytes32(0));
vm.prank(admin);
(uint256 _tokenId, uint256 _itemId) = experience.createItemType(newItem);

Expand All @@ -52,15 +52,17 @@ contract ExperienceAndItemsTest is Test, SetUp {
) = experience.items(_itemId);

assert(experience.totalItemTypes() == 2);
assert(keccak256(abi.encode(name)) == keccak256(abi.encode("Pirate")));
assert(keccak256(abi.encode(name)) == keccak256(abi.encode("Pirate_Hat")));
assert(tokenId == 3);
assert(_tokenId == 3);
assert(supply == 10**18);
assert(supplied ==0 );
assert(experinceCost == 100);
assert(soulbound == false);
assert(claimable == bytes32(0));
assert(keccak256(abi.encode(cid)) == keccak256(abi.encode('test_item_cid/')));
assertEq(keccak256(abi.encode(cid)), keccak256(abi.encode('test_item_cid/')));
assertEq(keccak256(abi.encode(experience.uri(tokenId))), keccak256(abi.encode('test_base_uri_experience/test_item_cid/')), "uris not right");

}

function testDropLoot()public{
Expand Down
4 changes: 3 additions & 1 deletion test/helpers/SetUp.sol
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,9 @@ contract SetUp is Test {
characterSheetsFactory.updateCharacterSheetsImplementation(address(characterSheetsImplementation));
characterSheetsFactory.updateExperienceAndItemsImplementation(address(experienceAndItemsImplementation));
characterSheetsFactory.updateHats(address(hats));
(characterSheetsAddress, experienceAddress) = characterSheetsFactory.create(admin, address(dao), 'test_base_uri_experience/', 'test_base_uri_character_sheets/');
address[] memory dungeonMasters = new address[](1);
dungeonMasters[0] = admin;
(characterSheetsAddress, experienceAddress) = characterSheetsFactory.create(dungeonMasters, address(dao), 'test_base_uri_experience/', 'test_base_uri_character_sheets/');
characterSheets = CharacterSheetsImplementation(characterSheetsAddress);
characterSheets.setERC6551Registry(address(erc6551Registry));
characterSheets.setERC6551Implementation(address(erc6551Implementation));
Expand Down

0 comments on commit 8050c85

Please sign in to comment.