-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(misc): examples: validator rewards with ERC20 minting (#1200)
Co-authored-by: cryptoAtwill <[email protected]> Co-authored-by: raulk <[email protected]> Co-authored-by: raulk <[email protected]>
- Loading branch information
1 parent
4243ff9
commit ed602ce
Showing
2 changed files
with
156 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
// SPDX-License-Identifier: MIT OR Apache-2.0 | ||
pragma solidity ^0.8.23; | ||
|
||
import {IValidatorRewarder} from "../interfaces/IValidatorRewarder.sol"; | ||
import {Consensus} from "../structs/Activity.sol"; | ||
import {SubnetID} from "../structs/Subnet.sol"; | ||
import {ERC20} from "@openzeppelin/contracts/token/ERC20/ERC20.sol"; | ||
import {IERC20} from "@openzeppelin/contracts/token/ERC20/IERC20.sol"; | ||
import {Ownable} from "@openzeppelin/contracts/access/Ownable.sol"; | ||
|
||
contract MintableERC20 is ERC20, Ownable { | ||
constructor(string memory name, string memory symbol) ERC20(name, symbol) Ownable(msg.sender) {} | ||
|
||
function mint(address recipient, uint256 amount) external onlyOwner { | ||
_mint(recipient, amount); | ||
} | ||
} | ||
|
||
/// An example validator rewarder implementation that mint ERC20 token for valiator | ||
contract MintingValidatorRewarder is IValidatorRewarder, Ownable { | ||
SubnetID public subnetId; | ||
MintableERC20 public token; | ||
|
||
constructor() Ownable(msg.sender) { | ||
// We can also pass this address as a constructor parameter or update | ||
// using a setter as well. | ||
token = new MintableERC20("test", "TST"); | ||
} | ||
|
||
function setSubnet(SubnetID calldata id) external onlyOwner { | ||
require(id.route.length > 0, "root not allowed"); | ||
|
||
subnetId = id; | ||
} | ||
|
||
function notifyValidClaim( | ||
SubnetID calldata id, | ||
uint64 checkpointHeight, | ||
Consensus.ValidatorData calldata data | ||
) external override { | ||
require(keccak256(abi.encode(id)) == keccak256(abi.encode(subnetId)), "not my subnet"); | ||
|
||
address actor = id.route[id.route.length - 1]; | ||
require(actor == msg.sender, "not from subnet"); | ||
|
||
uint256 reward = calculateReward(data, checkpointHeight); | ||
|
||
token.mint(data.validator, reward); | ||
} | ||
|
||
/// @notice The internal method to derive the amount of reward that each validator should receive | ||
/// based on their subnet activities | ||
function calculateReward(Consensus.ValidatorData calldata data, uint64) internal pure returns (uint256) { | ||
// Reward is the same as blocks mined for convenience. | ||
return data.blocksCommitted; | ||
} | ||
} |
This file contains 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