Skip to content

Commit

Permalink
✏️ init: tested out on remix
Browse files Browse the repository at this point in the history
  • Loading branch information
sanjay-sol committed Sep 15, 2024
1 parent 4e45b11 commit 9fc3df2
Show file tree
Hide file tree
Showing 13 changed files with 8,046 additions and 0 deletions.
17 changes: 17 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
node_modules
.env

# Hardhat files
/cache
/artifacts

# TypeChain files
/typechain
/typechain-types

# solidity-coverage files
/coverage
/coverage.json

# Hardhat Ignition default folder for deployments against a local node
ignition/deployments/chain-31337
29 changes: 29 additions & 0 deletions contracts/Diamond.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.0;

import { IDiamond } from "./interfaces/IDiamond.sol";

contract Diamond {
// A mapping to store which facet handles which function selectors
mapping(bytes4 => address) public facets;

event FacetUpdated(address indexed facet, bytes4[] selectors);

// Function to add or replace a facet and its function selectors
function updateFacet(address _facet, bytes4[] calldata _selectors) external {
for (uint256 i = 0; i < _selectors.length; i++) {
facets[_selectors[i]] = _facet;
}
emit FacetUpdated(_facet, _selectors);
}

// Thisi fallback function is used to delegate calls to the appropriate facet
fallback() external payable {
address facet = facets[msg.sig];
require(facet != address(0), "Diamond: Function does not exist");
(bool success, ) = facet.delegatecall(msg.data);
require(success, "Diamond: Delegatecall failed");
}

receive() external payable {}
}
34 changes: 34 additions & 0 deletions contracts/Lock.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.24;

// Uncomment this line to use console.log
// import "hardhat/console.sol";

contract Lock {
uint public unlockTime;
address payable public owner;

event Withdrawal(uint amount, uint when);

constructor(uint _unlockTime) payable {
require(
block.timestamp < _unlockTime,
"Unlock time should be in the future"
);

unlockTime = _unlockTime;
owner = payable(msg.sender);
}

function withdraw() public {
// Uncomment this line, and the import of "hardhat/console.sol", to print a log in your terminal
// console.log("Unlock time is %o and block timestamp is %o", unlockTime, block.timestamp);

require(block.timestamp >= unlockTime, "You can't withdraw yet");
require(msg.sender == owner, "You aren't the owner");

emit Withdrawal(address(this).balance, block.timestamp);

owner.transfer(address(this).balance);
}
}
26 changes: 26 additions & 0 deletions contracts/facets/OwnershipFacet.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.0;

import { IOwnershipFacet } from "../interfaces/IOwnershipFacet.sol";

contract OwnershipFacet is IOwnershipFacet {
address public owner;

modifier onlyOwner() {
require(msg.sender == owner, "Not the owner");
_;
}

constructor() {
owner = msg.sender;
}

function transferOwnership(address _newOwner) external override onlyOwner {
require(_newOwner != address(0), "Invalid new owner");
owner = _newOwner;
}

function getOwner() external view override returns (address) {
return owner;
}
}
22 changes: 22 additions & 0 deletions contracts/facets/TokenFacet.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.0;

import { ITokenFacet } from "../interfaces/ITokenFacet.sol";

contract TokenFacet is ITokenFacet {
mapping(address => uint256) public balances;

function mint(address _to, uint256 _amount) external override {
balances[_to] += _amount;
}

function transfer(address _to, uint256 _amount) external override {
require(balances[msg.sender] >= _amount, "Not enough balance");
balances[msg.sender] -= _amount;
balances[_to] += _amount;
}

function balanceOf(address _account) external view override returns (uint256) {
return balances[_account];
}
}
6 changes: 6 additions & 0 deletions contracts/interfaces/IDiamond.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.0;

interface IDiamond {
function updateFacet(address _facet, bytes4[] calldata _selectors) external;
}
7 changes: 7 additions & 0 deletions contracts/interfaces/IOwnershipFacet.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.0;

interface IOwnershipFacet {
function transferOwnership(address _newOwner) external;
function getOwner() external view returns (address);
}
8 changes: 8 additions & 0 deletions contracts/interfaces/ITokenFacet.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.0;

interface ITokenFacet {
function mint(address _to, uint256 _amount) external;
function transfer(address _to, uint256 _amount) external;
function balanceOf(address _account) external view returns (uint256);
}
6 changes: 6 additions & 0 deletions hardhat.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
require("@nomicfoundation/hardhat-toolbox");

/** @type import('hardhat/config').HardhatUserConfig */
module.exports = {
solidity: "0.8.24",
};
15 changes: 15 additions & 0 deletions ignition/modules/Lock.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
const { buildModule } = require("@nomicfoundation/hardhat-ignition/modules");

const JAN_1ST_2030 = 1893456000;
const ONE_GWEI = 1_000_000_000n;

module.exports = buildModule("LockModule", (m) => {
const unlockTime = m.getParameter("unlockTime", JAN_1ST_2030);
const lockedAmount = m.getParameter("lockedAmount", ONE_GWEI);

const lock = m.contract("Lock", [unlockTime], {
value: lockedAmount,
});

return { lock };
});
Loading

0 comments on commit 9fc3df2

Please sign in to comment.