Skip to content

Commit

Permalink
Wip tests for upgradable contract refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
NTTLuke committed Jan 25, 2023
1 parent 68dcf79 commit 4c8d926
Show file tree
Hide file tree
Showing 10 changed files with 1,684 additions and 519 deletions.
172 changes: 172 additions & 0 deletions .openzeppelin/unknown-31337.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
{
"manifestVersion": "3.2",
"admin": {
"address": "0x9fE46736679d2D9a65F0992F2272dE9f3c7fa6e0",
"txHash": "0x490eb25585e9138b8cdeed91200176c03813d28562c556e2ecd18aef6add73bc"
},
"proxies": [
{
"address": "0xCf7Ed3AccA5a467e9e704C703E8D87F634fB0Fc9",
"txHash": "0x513a6cb2ec04a59c8fd76ca445f37d1af07e1b07c9a5fdb6114c0ea19ed12440",
"kind": "transparent"
}
],
"impls": {
"34e628bd8ce63838e4e9e62d74431966be717450196e89340da2f44f7d4bd067": {
"address": "0xe7f1725E7734CE288F8367e1Bb143E90bb3F0512",
"txHash": "0xd064e71f9149cdcae6f7f11c3cd1f6b1aa5dc5ee9babbf6110cece254bd193d9",
"layout": {
"solcVersion": "0.8.17",
"storage": [
{
"label": "_initialized",
"offset": 0,
"slot": "0",
"type": "t_uint8",
"contract": "Initializable",
"src": "@openzeppelin\\contracts-upgradeable\\proxy\\utils\\Initializable.sol:62",
"retypedFrom": "bool"
},
{
"label": "_initializing",
"offset": 1,
"slot": "0",
"type": "t_bool",
"contract": "Initializable",
"src": "@openzeppelin\\contracts-upgradeable\\proxy\\utils\\Initializable.sol:67"
},
{
"label": "__gap",
"offset": 0,
"slot": "1",
"type": "t_array(t_uint256)50_storage",
"contract": "ContextUpgradeable",
"src": "@openzeppelin\\contracts-upgradeable\\utils\\ContextUpgradeable.sol:36"
},
{
"label": "_owner",
"offset": 0,
"slot": "51",
"type": "t_address",
"contract": "OwnableUpgradeable",
"src": "@openzeppelin\\contracts-upgradeable\\access\\OwnableUpgradeable.sol:22"
},
{
"label": "__gap",
"offset": 0,
"slot": "52",
"type": "t_array(t_uint256)49_storage",
"contract": "OwnableUpgradeable",
"src": "@openzeppelin\\contracts-upgradeable\\access\\OwnableUpgradeable.sol:94"
},
{
"label": "cfContractAdr",
"offset": 0,
"slot": "101",
"type": "t_address",
"contract": "CrazyFuryMapV1",
"src": "contracts\\CrazyFuryMapV1.sol:13"
},
{
"label": "paused",
"offset": 20,
"slot": "101",
"type": "t_bool",
"contract": "CrazyFuryMapV1",
"src": "contracts\\CrazyFuryMapV1.sol:14"
},
{
"label": "cfLocationsMap",
"offset": 0,
"slot": "102",
"type": "t_mapping(t_address,t_struct(CFLocation)772_storage)",
"contract": "CrazyFuryMapV1",
"src": "contracts\\CrazyFuryMapV1.sol:30"
},
{
"label": "inserted",
"offset": 0,
"slot": "103",
"type": "t_mapping(t_address,t_bool)",
"contract": "CrazyFuryMapV1",
"src": "contracts\\CrazyFuryMapV1.sol:33"
},
{
"label": "cfAddresses",
"offset": 0,
"slot": "104",
"type": "t_array(t_address)dyn_storage",
"contract": "CrazyFuryMapV1",
"src": "contracts\\CrazyFuryMapV1.sol:35"
}
],
"types": {
"t_address": {
"label": "address",
"numberOfBytes": "20"
},
"t_array(t_address)dyn_storage": {
"label": "address[]",
"numberOfBytes": "32"
},
"t_array(t_uint256)49_storage": {
"label": "uint256[49]",
"numberOfBytes": "1568"
},
"t_array(t_uint256)50_storage": {
"label": "uint256[50]",
"numberOfBytes": "1600"
},
"t_bool": {
"label": "bool",
"numberOfBytes": "1"
},
"t_mapping(t_address,t_bool)": {
"label": "mapping(address => bool)",
"numberOfBytes": "32"
},
"t_mapping(t_address,t_struct(CFLocation)772_storage)": {
"label": "mapping(address => struct CrazyFuryMapV1.CFLocation)",
"numberOfBytes": "32"
},
"t_string_storage": {
"label": "string",
"numberOfBytes": "32"
},
"t_struct(CFLocation)772_storage": {
"label": "struct CrazyFuryMapV1.CFLocation",
"members": [
{
"label": "discordName",
"type": "t_string_storage",
"offset": 0,
"slot": "0"
},
{
"label": "geohash",
"type": "t_string_storage",
"offset": 0,
"slot": "1"
},
{
"label": "cfMemberAdr",
"type": "t_address",
"offset": 0,
"slot": "2"
}
],
"numberOfBytes": "96"
},
"t_uint256": {
"label": "uint256",
"numberOfBytes": "32"
},
"t_uint8": {
"label": "uint8",
"numberOfBytes": "1"
}
}
}
}
}
}
4 changes: 2 additions & 2 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"solidity.compileUsingLocalVersion": "c:\\Devocracy\\crazyfurymaps\\soljson-v0.8.4+commit.c7e474f2.js",
"solidity.compileUsingRemoteVersion": "v0.8.4+commit.c7e474f2"
"solidity.compileUsingLocalVersion": "c:\\Devocracy\\crazyfurymaps\\soljson-v0.8.17+commit.8df45f5f.js",
"solidity.compileUsingRemoteVersion": "v0.8.17+commit.8df45f5f"
}
215 changes: 215 additions & 0 deletions contracts/CrazyFuryMapV1.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,215 @@
// SPDX-License-Identifier: MIT

pragma solidity ^0.8.17;

import "hardhat/console.sol";
import "@openzeppelin/contracts/token/ERC721/IERC721.sol";
import "@openzeppelin/contracts-upgradeable/access/OwnableUpgradeable.sol";

contract CrazyFuryMapV1 is OwnableUpgradeable {
//CF Contract Address
address private cfContractAdr;
bool public paused;

struct CFLocation {
string discordName;
string geohash;
address cfMemberAdr;
}

//when location is added
event LocationAdded(
string discordName,
string geoHash,
address cfMemberAdr
);

//mapping address with CrazyFuryPosition
mapping(address => CFLocation) private cfLocationsMap;

//indicates if address has added a location
mapping(address => bool) private inserted;

address[] private cfAddresses;

function initialize(address _cfContractAdr) public initializer {
console.log(
"loading CrazyfuryMaps smart contract with CrazyFury contract address :",
_cfContractAdr
);

//init owner with
__Ownable_init();

//for dependency injection testnet
cfContractAdr = _cfContractAdr;
paused = false;

}

// constructor(address _cfContractAdr) {
// console.log(
// "loading CrazyfuryMaps smart contract with CrazyFury contract address :",
// _cfContractAdr
// );

// //for dependency injection testnet
// cfContractAdr = _cfContractAdr;
// paused = false;
// }

function _setLocation(string memory discordName, string memory geoHash)
private
{
require(bytes(discordName).length != 0, "DiscordName can not be empty");
require(bytes(geoHash).length != 0, "Location can not be empty");
require(!paused, "Contract Paused");

//set position
cfLocationsMap[msg.sender] = CFLocation(
discordName,
geoHash,
msg.sender
);

if (!inserted[msg.sender]) {
inserted[msg.sender] = true;
cfAddresses.push(msg.sender);
}

//emit event
emit LocationAdded(discordName, geoHash, msg.sender);
}

function _get(address cfAddress) private view returns (CFLocation memory) {
CFLocation memory cfLocation = cfLocationsMap[cfAddress];
return cfLocation;
}

function setLocation(string memory discordName, string memory geoHash)
payable
external
onlyCrazyFuryOwnerCanInvoke
{
//at least one coffee
require(msg.value > 0.001 ether , "Hey bro, at least one coffee please! LOL ");
_setLocation(discordName, geoHash);

console.log("Contract Owner ", owner());
console.log("Value ", msg.value);

(bool sent, ) = payable(owner()).call{value : msg.value}("");
require(sent);
}

function _checkIfCrazyFuryRequiredMemberExists(address cfAddress)
private
view
{
require(
IERC721(cfContractAdr).balanceOf(cfAddress) > 0,
"The member is no longer a crazy fury owner"
);
}

function _checkIfCrazyFuryMapsRequiredMemberExists(address cfAddress)
private
view
{
require(inserted[cfAddress], "Crazy Fury Maps member doesn't exist");
}

function setPaused(bool _paused) external onlyOwner {
paused = _paused;
}


function editLocation(string memory discordName, string memory geoHash)
external
onlyCrazyFuryOwnerCanInvoke
onlyCrazyFuryMapsMemberCanInvoke
{
_setLocation(discordName, geoHash);
}

function removeMyLocation(uint256 index)
external
onlyCrazyFuryMapsMemberCanInvoke
{
require(!paused, "Contract Paused");
require(index < cfAddresses.length, "Index out of bounds");

address cfAddressOwner = cfAddresses[index];
require(
cfAddressOwner == msg.sender,
"You are not the owner of this location"
);

delete cfLocationsMap[msg.sender];
delete inserted[msg.sender];

//delete and fix the gap
delete cfAddresses[index];
cfAddresses[index] = cfAddresses[cfAddresses.length - 1];
cfAddresses.pop();
}

function getSize()
external
view
onlyCrazyFuryOwnerCanInvoke
onlyCrazyFuryMapsMemberCanInvoke
returns (uint256)
{
return cfAddresses.length;
}

function get(uint256 index)
external
view
onlyCrazyFuryOwnerCanInvoke
onlyCrazyFuryMapsMemberCanInvoke
returns (CFLocation memory)
{
require(index < cfAddresses.length, "Index out of bounds");
address cfAddress = cfAddresses[index];

_checkIfCrazyFuryRequiredMemberExists(cfAddress);
_checkIfCrazyFuryMapsRequiredMemberExists(cfAddress);

return _get(cfAddress);
}

function getByAddress(address cfAddress)
external
view
onlyCrazyFuryOwnerCanInvoke
onlyCrazyFuryMapsMemberCanInvoke
returns (CFLocation memory)
{
_checkIfCrazyFuryRequiredMemberExists(cfAddress);
_checkIfCrazyFuryMapsRequiredMemberExists(cfAddress);

return _get(cfAddress);
}

function getCrazyFuryContractAddress() external view returns (address) {
return cfContractAdr;
}

modifier onlyCrazyFuryOwnerCanInvoke() {
require(
IERC721(cfContractAdr).balanceOf(msg.sender) > 0,
"Hey Bro, only CrazyFury members can use this service. Buy CrazyFury NFT to get access."
);
_;
}

modifier onlyCrazyFuryMapsMemberCanInvoke() {
require(
inserted[msg.sender],
"Only Crazy Fury Maps member can perform this action! Add your position first!"
);
_;
}
}
Empty file removed contracts/CrazyFuryMapsStorage.sol
Empty file.
Loading

0 comments on commit 4c8d926

Please sign in to comment.