Skip to content
Open

erc20 #172

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions erc20/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Node modules
/node_modules

# Compilation output
/dist

# pnpm deploy output
/bundle

# Hardhat Build Artifacts
/artifacts

# Hardhat compilation (v2) support directory
/cache

# Typechain output
/types

# Hardhat coverage reports
/coverage
57 changes: 57 additions & 0 deletions erc20/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# Sample Hardhat 3 Beta Project (`mocha` and `ethers`)

This project showcases a Hardhat 3 Beta project using `mocha` for tests and the `ethers` library for Ethereum interactions.

To learn more about the Hardhat 3 Beta, please visit the [Getting Started guide](https://hardhat.org/docs/getting-started#getting-started-with-hardhat-3). To share your feedback, join our [Hardhat 3 Beta](https://hardhat.org/hardhat3-beta-telegram-group) Telegram group or [open an issue](https://github.com/NomicFoundation/hardhat/issues/new) in our GitHub issue tracker.

## Project Overview

This example project includes:

- A simple Hardhat configuration file.
- Foundry-compatible Solidity unit tests.
- TypeScript integration tests using `mocha` and ethers.js
- Examples demonstrating how to connect to different types of networks, including locally simulating OP mainnet.

## Usage

### Running Tests

To run all the tests in the project, execute the following command:

```shell
npx hardhat test
```

You can also selectively run the Solidity or `mocha` tests:

```shell
npx hardhat test solidity
npx hardhat test mocha
```

### Make a deployment to Sepolia

This project includes an example Ignition module to deploy the contract. You can deploy this module to a locally simulated chain or to Sepolia.

To run the deployment to a local chain:

```shell
npx hardhat ignition deploy ignition/modules/Counter.ts
```

To run the deployment to Sepolia, you need an account with funds to send the transaction. The provided Hardhat configuration includes a Configuration Variable called `SEPOLIA_PRIVATE_KEY`, which you can use to set the private key of the account you want to use.

You can set the `SEPOLIA_PRIVATE_KEY` variable using the `hardhat-keystore` plugin or by setting it as an environment variable.

To set the `SEPOLIA_PRIVATE_KEY` config variable using `hardhat-keystore`:

```shell
npx hardhat keystore set SEPOLIA_PRIVATE_KEY
```

After setting the variable, you can run the deployment with the Sepolia network:

```shell
npx hardhat ignition deploy --network sepolia ignition/modules/Counter.ts
```
19 changes: 19 additions & 0 deletions erc20/contracts/Counter.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.28;

contract Counter {
uint public x;

event Increment(uint by);

function inc() public {
x++;
emit Increment(1);
}

function incBy(uint by) public {
require(by > 0, "incBy: increment should be positive");
x += by;
emit Increment(by);
}
}
32 changes: 32 additions & 0 deletions erc20/contracts/Counter.t.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.28;

import {Counter} from "./Counter.sol";
import {Test} from "forge-std/Test.sol";

// Solidity tests are compatible with foundry, so they
// use the same syntax and offer the same functionality.

contract CounterTest is Test {
Counter counter;

function setUp() public {
counter = new Counter();
}

function test_InitialValue() public view {
require(counter.x() == 0, "Initial value should be 0");
}

function testFuzz_Inc(uint8 x) public {
for (uint8 i = 0; i < x; i++) {
counter.inc();
}
require(counter.x() == x, "Value after calling inc x times should be x");
}

function test_IncByZero() public {
vm.expectRevert();
counter.incBy(0);
}
}
109 changes: 109 additions & 0 deletions erc20/contracts/ERC20.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;

import "./IERC20.sol";

contract ERC20 is IERC20 {

event Transfer(address indexed from, address indexed to, uint256 value);
event Approval(address indexed owner, address indexed spender, uint256 value);

string public name;
string public symbol;
uint8 public decimals;
uint256 public totalSupply;

address public owner;

mapping(address => uint256) public balanceOf;
mapping(address => mapping(address => uint256)) public allowance;

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

constructor(
string memory _name,
string memory _symbol,
uint8 _decimals,
uint256 initialSupply
) {
name = _name;
symbol = _symbol;
decimals = _decimals;
owner = msg.sender;

_mint(msg.sender, initialSupply);
}

function transfer(address recipient, uint256 amount)
external
returns (bool)
{
require(balanceOf[msg.sender] >= amount, "Insufficient balance");

balanceOf[msg.sender] -= amount;
balanceOf[recipient] += amount;

emit Transfer(msg.sender, recipient, amount);
return true;
}

function approve(address spender, uint256 amount)
external
returns (bool)
{
allowance[msg.sender][spender] = amount;
emit Approval(msg.sender, spender, amount);
return true;
}

function transferFrom(
address sender,
address recipient,
uint256 amount
) external returns (bool) {

require(balanceOf[sender] >= amount, "Insufficient balance");
require(allowance[sender][msg.sender] >= amount, "Insufficient allowance");

allowance[sender][msg.sender] -= amount;
balanceOf[sender] -= amount;
balanceOf[recipient] += amount;

emit Transfer(sender, recipient, amount);
return true;
}

function _mint(address to, uint256 amount) internal {
totalSupply += amount;
balanceOf[to] += amount;
emit Transfer(address(0), to, amount);
}

function _burn(address from, uint256 amount) internal {
require(balanceOf[from] >= amount, "Insufficient balance");

balanceOf[from] -= amount;
totalSupply -= amount;
emit Transfer(from, address(0), amount);
}

function mint(address to, uint256 amount) external onlyOwner {
_mint(to, amount);
}

function burn(uint256 amount) external {
_burn(msg.sender, amount);
}

// 🔥 IMPORTANT FOR STAKING
function burnFrom(address from, uint256 amount) external onlyOwner {
_burn(from, amount);
}

function transferOwnership(address newOwner) external onlyOwner {
owner = newOwner;
}
}
18 changes: 18 additions & 0 deletions erc20/contracts/IERC20.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.28;

interface IERC20 {
function totalSupply() external view returns (uint256);
function balanceOf(address account) external view returns (uint256);
function transfer(address recipient, uint256 amount)
external
returns (bool);
function allowance(address owner, address spender)
external
view
returns (uint256);
function approve(address spender, uint256 amount) external returns (bool);
function transferFrom(address sender, address recipient, uint256 amount)
external
returns (bool);
}
74 changes: 74 additions & 0 deletions erc20/contracts/ProtocolFactory.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;

import "./ERC20.sol";
import "./Staking.sol";

contract ProtocolFactory {

address[] public allProtocols;

struct Protocol {
address stakeToken;
address rewardToken;
address exchangeToken;
address stakingContract;
}

Protocol[] public protocols;

function createProtocol(
string memory stakeName,
string memory stakeSymbol,
uint256 stakeSupply,
uint256 rewardRate
) external {


ERC20 stakeToken = new ERC20(
stakeName,
stakeSymbol,
18,
stakeSupply
);


ERC20 rewardToken = new ERC20(
"Reward Token",
"RWD",
18,
0
);


ERC20 exchangeToken = new ERC20(
"Exchange Token",
"xSTK",
18,
0
);

Staking staking = new Staking(
address(stakeToken),
address(rewardToken),
address(exchangeToken),
rewardRate
);


rewardToken.transferOwnership(address(staking));
exchangeToken.transferOwnership(address(staking));

//
protocols.push(
Protocol(
address(stakeToken),
address(rewardToken),
address(exchangeToken),
address(staking)
)
);

allProtocols.push(address(staking));
}
}
Loading