文件 1 的 9:AccessControl.sol
pragma solidity ^0.8.0;
import "./IAccessControl.sol";
import "../utils/Context.sol";
import "../utils/Strings.sol";
import "../utils/introspection/ERC165.sol";
abstract contract AccessControl is Context, IAccessControl, ERC165 {
struct RoleData {
mapping(address => bool) members;
bytes32 adminRole;
}
mapping(bytes32 => RoleData) private _roles;
bytes32 public constant DEFAULT_ADMIN_ROLE = 0x00;
modifier onlyRole(bytes32 role) {
_checkRole(role);
_;
}
function supportsInterface(bytes4 interfaceId) public view virtual override returns (bool) {
return interfaceId == type(IAccessControl).interfaceId || super.supportsInterface(interfaceId);
}
function hasRole(bytes32 role, address account) public view virtual override returns (bool) {
return _roles[role].members[account];
}
function _checkRole(bytes32 role) internal view virtual {
_checkRole(role, _msgSender());
}
function _checkRole(bytes32 role, address account) internal view virtual {
if (!hasRole(role, account)) {
revert(
string(
abi.encodePacked(
"AccessControl: account ",
Strings.toHexString(uint160(account), 20),
" is missing role ",
Strings.toHexString(uint256(role), 32)
)
)
);
}
}
function getRoleAdmin(bytes32 role) public view virtual override returns (bytes32) {
return _roles[role].adminRole;
}
function grantRole(bytes32 role, address account) public virtual override onlyRole(getRoleAdmin(role)) {
_grantRole(role, account);
}
function revokeRole(bytes32 role, address account) public virtual override onlyRole(getRoleAdmin(role)) {
_revokeRole(role, account);
}
function renounceRole(bytes32 role, address account) public virtual override {
require(account == _msgSender(), "AccessControl: can only renounce roles for self");
_revokeRole(role, account);
}
function _setupRole(bytes32 role, address account) internal virtual {
_grantRole(role, account);
}
function _setRoleAdmin(bytes32 role, bytes32 adminRole) internal virtual {
bytes32 previousAdminRole = getRoleAdmin(role);
_roles[role].adminRole = adminRole;
emit RoleAdminChanged(role, previousAdminRole, adminRole);
}
function _grantRole(bytes32 role, address account) internal virtual {
if (!hasRole(role, account)) {
_roles[role].members[account] = true;
emit RoleGranted(role, account, _msgSender());
}
}
function _revokeRole(bytes32 role, address account) internal virtual {
if (hasRole(role, account)) {
_roles[role].members[account] = false;
emit RoleRevoked(role, account, _msgSender());
}
}
}
文件 2 的 9:Context.sol
pragma solidity ^0.8.0;
abstract contract Context {
function _msgSender() internal view virtual returns (address) {
return msg.sender;
}
function _msgData() internal view virtual returns (bytes calldata) {
return msg.data;
}
}
文件 3 的 9:ECDSA.sol
pragma solidity ^0.8.0;
import "../Strings.sol";
library ECDSA {
enum RecoverError {
NoError,
InvalidSignature,
InvalidSignatureLength,
InvalidSignatureS,
InvalidSignatureV
}
function _throwError(RecoverError error) private pure {
if (error == RecoverError.NoError) {
return;
} else if (error == RecoverError.InvalidSignature) {
revert("ECDSA: invalid signature");
} else if (error == RecoverError.InvalidSignatureLength) {
revert("ECDSA: invalid signature length");
} else if (error == RecoverError.InvalidSignatureS) {
revert("ECDSA: invalid signature 's' value");
} else if (error == RecoverError.InvalidSignatureV) {
revert("ECDSA: invalid signature 'v' value");
}
}
function tryRecover(bytes32 hash, bytes memory signature) internal pure returns (address, RecoverError) {
if (signature.length == 65) {
bytes32 r;
bytes32 s;
uint8 v;
assembly {
r := mload(add(signature, 0x20))
s := mload(add(signature, 0x40))
v := byte(0, mload(add(signature, 0x60)))
}
return tryRecover(hash, v, r, s);
} else {
return (address(0), RecoverError.InvalidSignatureLength);
}
}
function recover(bytes32 hash, bytes memory signature) internal pure returns (address) {
(address recovered, RecoverError error) = tryRecover(hash, signature);
_throwError(error);
return recovered;
}
function tryRecover(
bytes32 hash,
bytes32 r,
bytes32 vs
) internal pure returns (address, RecoverError) {
bytes32 s = vs & bytes32(0x7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff);
uint8 v = uint8((uint256(vs) >> 255) + 27);
return tryRecover(hash, v, r, s);
}
function recover(
bytes32 hash,
bytes32 r,
bytes32 vs
) internal pure returns (address) {
(address recovered, RecoverError error) = tryRecover(hash, r, vs);
_throwError(error);
return recovered;
}
function tryRecover(
bytes32 hash,
uint8 v,
bytes32 r,
bytes32 s
) internal pure returns (address, RecoverError) {
if (uint256(s) > 0x7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF5D576E7357A4501DDFE92F46681B20A0) {
return (address(0), RecoverError.InvalidSignatureS);
}
if (v != 27 && v != 28) {
return (address(0), RecoverError.InvalidSignatureV);
}
address signer = ecrecover(hash, v, r, s);
if (signer == address(0)) {
return (address(0), RecoverError.InvalidSignature);
}
return (signer, RecoverError.NoError);
}
function recover(
bytes32 hash,
uint8 v,
bytes32 r,
bytes32 s
) internal pure returns (address) {
(address recovered, RecoverError error) = tryRecover(hash, v, r, s);
_throwError(error);
return recovered;
}
function toEthSignedMessageHash(bytes32 hash) internal pure returns (bytes32) {
return keccak256(abi.encodePacked("\x19Ethereum Signed Message:\n32", hash));
}
function toEthSignedMessageHash(bytes memory s) internal pure returns (bytes32) {
return keccak256(abi.encodePacked("\x19Ethereum Signed Message:\n", Strings.toString(s.length), s));
}
function toTypedDataHash(bytes32 domainSeparator, bytes32 structHash) internal pure returns (bytes32) {
return keccak256(abi.encodePacked("\x19\x01", domainSeparator, structHash));
}
}
文件 4 的 9:ERC165.sol
pragma solidity ^0.8.0;
import "./IERC165.sol";
abstract contract ERC165 is IERC165 {
function supportsInterface(bytes4 interfaceId) public view virtual override returns (bool) {
return interfaceId == type(IERC165).interfaceId;
}
}
文件 5 的 9:IAccessControl.sol
pragma solidity ^0.8.0;
interface IAccessControl {
event RoleAdminChanged(bytes32 indexed role, bytes32 indexed previousAdminRole, bytes32 indexed newAdminRole);
event RoleGranted(bytes32 indexed role, address indexed account, address indexed sender);
event RoleRevoked(bytes32 indexed role, address indexed account, address indexed sender);
function hasRole(bytes32 role, address account) external view returns (bool);
function getRoleAdmin(bytes32 role) external view returns (bytes32);
function grantRole(bytes32 role, address account) external;
function revokeRole(bytes32 role, address account) external;
function renounceRole(bytes32 role, address account) external;
}
文件 6 的 9:IERC165.sol
pragma solidity ^0.8.0;
interface IERC165 {
function supportsInterface(bytes4 interfaceId) external view returns (bool);
}
文件 7 的 9:IOraclePoS.sol
pragma solidity 0.8.6;
interface IOraclePoS {
event OracleUpdate(
address indexed caller,
uint32 indexed referenceDay,
uint256 indexed referenceBlock,
uint256 currentSupply,
uint256 supplyCap,
uint256 maxStakingDuration,
uint256 maxConsumptionRate,
uint256 minConsumptionRate,
uint256 mintingPeriod,
uint256 scale,
uint256 timestamp
);
function isDayIndexed(uint256 _referenceDay) external view returns (bool);
function getLastIndexedDay() external view returns (uint32);
function updateIndex(
uint32 _referenceDay,
uint256 _referenceBlock,
uint256 _currentSupply,
uint256 _supplyCap,
uint256 _maxStakingDuration,
uint256 _maxConsumptionRate,
uint256 _minConsumptionRate,
uint256 _mintingPeriod,
uint256 _scale,
bytes memory signature
) external returns (bool success);
function get(uint256 _referenceDay)
external
view
returns (
uint256 referenceDay,
uint256 referenceBlock,
uint256 currentSupply,
uint256 supplyCap,
uint256 maxStakingDuration,
uint256 maxConsumptionRate,
uint256 minConsumptionRate,
uint256 mintingPeriod,
uint256 scale,
uint256 timestamp
);
}
文件 8 的 9:OraclePoS.sol
pragma solidity 0.8.6;
import "@openzeppelin/contracts/access/AccessControl.sol";
import "./interfaces/oracle/IOraclePoS.sol";
import "@openzeppelin/contracts/utils/cryptography/ECDSA.sol";
contract OraclePoS is AccessControl, IOraclePoS {
int8 public constant VERSION = 1;
uint32 public lastIndexedDay;
bytes32 public constant PUBLISHER_ROLE = keccak256("PUBLISHER_ROLE");
bytes32 public constant CALCULATOR_ROLE = keccak256("CALCULATOR_ROLE");
mapping(uint256 => AlkimiyaIndex) private index;
string public name;
struct AlkimiyaIndex {
uint256 referenceBlock;
uint256 currentSupply;
uint256 supplyCap;
uint256 maxStakingDuration;
uint256 maxConsumptionRate;
uint256 minConsumptionRate;
uint256 mintingPeriod;
uint256 scale;
uint256 timestamp;
}
constructor(string memory _name) {
_setupRole(PUBLISHER_ROLE, msg.sender);
_setupRole(DEFAULT_ADMIN_ROLE, msg.sender);
name = _name;
}
function updateIndex(
uint32 _referenceDay,
uint256 _referenceBlock,
uint256 _currentSupply,
uint256 _supplyCap,
uint256 _maxStakingDuration,
uint256 _maxConsumptionRate,
uint256 _minConsumptionRate,
uint256 _mintingPeriod,
uint256 _scale,
bytes memory signature
) external override returns (bool success) {
require(hasRole(PUBLISHER_ROLE, msg.sender), "Update not allowed to everyone");
bytes32 messageHash = keccak256(
abi.encodePacked(
"\x19Ethereum Signed Message:\n32",
keccak256(
abi.encode(
_referenceDay,
_referenceBlock,
_currentSupply,
_supplyCap,
_maxStakingDuration,
_maxConsumptionRate,
_minConsumptionRate,
_mintingPeriod,
_scale
)
)
)
);
require(hasRole(CALCULATOR_ROLE, ECDSA.recover(messageHash, signature)), "Invalid signature");
require(index[_referenceDay].timestamp == 0, "Information cannot be updated.");
index[_referenceDay].timestamp = block.timestamp;
index[_referenceDay].referenceBlock = _referenceBlock;
index[_referenceDay].currentSupply = _currentSupply;
index[_referenceDay].supplyCap = _supplyCap;
index[_referenceDay].maxStakingDuration = _maxStakingDuration;
index[_referenceDay].maxConsumptionRate = _maxConsumptionRate;
index[_referenceDay].minConsumptionRate = _minConsumptionRate;
index[_referenceDay].mintingPeriod = _mintingPeriod;
index[_referenceDay].scale = _scale;
if (_referenceDay > lastIndexedDay) {
lastIndexedDay = _referenceDay;
}
emit OracleUpdate(
msg.sender,
_referenceDay,
_referenceBlock,
_currentSupply,
_supplyCap,
_maxStakingDuration,
_maxConsumptionRate,
_minConsumptionRate,
_mintingPeriod,
_scale,
block.timestamp
);
return true;
}
function get(uint256 _referenceDay)
external
view
override
returns (
uint256 referenceDay,
uint256 referenceBlock,
uint256 currentSupply,
uint256 supplyCap,
uint256 maxStakingDuration,
uint256 maxConsumptionRate,
uint256 minConsumptionRate,
uint256 mintingPeriod,
uint256 scale,
uint256 timestamp
)
{
AlkimiyaIndex memory current = index[_referenceDay];
require(index[_referenceDay].timestamp != 0, "Date not yet indexed");
return (
_referenceDay,
current.referenceBlock,
current.currentSupply,
current.supplyCap,
current.maxStakingDuration,
current.maxConsumptionRate,
current.minConsumptionRate,
current.mintingPeriod,
current.scale,
current.timestamp
);
}
function isDayIndexed(uint256 _referenceDay) external view override returns (bool) {
return index[_referenceDay].timestamp != 0;
}
function getLastIndexedDay() external view override returns (uint32) {
return lastIndexedDay;
}
}
文件 9 的 9:Strings.sol
pragma solidity ^0.8.0;
library Strings {
bytes16 private constant _HEX_SYMBOLS = "0123456789abcdef";
uint8 private constant _ADDRESS_LENGTH = 20;
function toString(uint256 value) internal pure returns (string memory) {
if (value == 0) {
return "0";
}
uint256 temp = value;
uint256 digits;
while (temp != 0) {
digits++;
temp /= 10;
}
bytes memory buffer = new bytes(digits);
while (value != 0) {
digits -= 1;
buffer[digits] = bytes1(uint8(48 + uint256(value % 10)));
value /= 10;
}
return string(buffer);
}
function toHexString(uint256 value) internal pure returns (string memory) {
if (value == 0) {
return "0x00";
}
uint256 temp = value;
uint256 length = 0;
while (temp != 0) {
length++;
temp >>= 8;
}
return toHexString(value, length);
}
function toHexString(uint256 value, uint256 length) internal pure returns (string memory) {
bytes memory buffer = new bytes(2 * length + 2);
buffer[0] = "0";
buffer[1] = "x";
for (uint256 i = 2 * length + 1; i > 1; --i) {
buffer[i] = _HEX_SYMBOLS[value & 0xf];
value >>= 4;
}
require(value == 0, "Strings: hex length insufficient");
return string(buffer);
}
function toHexString(address addr) internal pure returns (string memory) {
return toHexString(uint256(uint160(addr)), _ADDRESS_LENGTH);
}
}
{
"compilationTarget": {
"contracts/OraclePoS.sol": "OraclePoS"
},
"evmVersion": "berlin",
"libraries": {},
"metadata": {
"bytecodeHash": "ipfs"
},
"optimizer": {
"enabled": true,
"runs": 1000
},
"remappings": [
":@alkimiya/=node_modules/@alkimiya/",
":@clones/=lib/clones-with-immutable-args/src/",
":@ds/=lib/ds-test/src/",
":@ensdomains/=node_modules/@ensdomains/",
":@openzeppelin/=node_modules/@openzeppelin/",
":@solmate/=lib/solmate/src/",
":@std/=lib/forge-std/src/",
":@uniswap/=node_modules/@uniswap/",
":base64-sol/=node_modules/base64-sol/",
":ds-test/=lib/solmate/lib/ds-test/src/",
":forge-std/=lib/forge-std/src/",
":hardhat/=node_modules/hardhat/",
":solmate/=node_modules/solmate/"
]
}
[{"inputs":[{"internalType":"string","name":"_name","type":"string"}],"stateMutability":"nonpayable","type":"constructor"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"caller","type":"address"},{"indexed":true,"internalType":"uint32","name":"referenceDay","type":"uint32"},{"indexed":true,"internalType":"uint256","name":"referenceBlock","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"currentSupply","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"supplyCap","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"maxStakingDuration","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"maxConsumptionRate","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"minConsumptionRate","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"mintingPeriod","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"scale","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"timestamp","type":"uint256"}],"name":"OracleUpdate","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"bytes32","name":"role","type":"bytes32"},{"indexed":true,"internalType":"bytes32","name":"previousAdminRole","type":"bytes32"},{"indexed":true,"internalType":"bytes32","name":"newAdminRole","type":"bytes32"}],"name":"RoleAdminChanged","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"bytes32","name":"role","type":"bytes32"},{"indexed":true,"internalType":"address","name":"account","type":"address"},{"indexed":true,"internalType":"address","name":"sender","type":"address"}],"name":"RoleGranted","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"bytes32","name":"role","type":"bytes32"},{"indexed":true,"internalType":"address","name":"account","type":"address"},{"indexed":true,"internalType":"address","name":"sender","type":"address"}],"name":"RoleRevoked","type":"event"},{"inputs":[],"name":"CALCULATOR_ROLE","outputs":[{"internalType":"bytes32","name":"","type":"bytes32"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"DEFAULT_ADMIN_ROLE","outputs":[{"internalType":"bytes32","name":"","type":"bytes32"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"PUBLISHER_ROLE","outputs":[{"internalType":"bytes32","name":"","type":"bytes32"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"VERSION","outputs":[{"internalType":"int8","name":"","type":"int8"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"_referenceDay","type":"uint256"}],"name":"get","outputs":[{"internalType":"uint256","name":"referenceDay","type":"uint256"},{"internalType":"uint256","name":"referenceBlock","type":"uint256"},{"internalType":"uint256","name":"currentSupply","type":"uint256"},{"internalType":"uint256","name":"supplyCap","type":"uint256"},{"internalType":"uint256","name":"maxStakingDuration","type":"uint256"},{"internalType":"uint256","name":"maxConsumptionRate","type":"uint256"},{"internalType":"uint256","name":"minConsumptionRate","type":"uint256"},{"internalType":"uint256","name":"mintingPeriod","type":"uint256"},{"internalType":"uint256","name":"scale","type":"uint256"},{"internalType":"uint256","name":"timestamp","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"getLastIndexedDay","outputs":[{"internalType":"uint32","name":"","type":"uint32"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"bytes32","name":"role","type":"bytes32"}],"name":"getRoleAdmin","outputs":[{"internalType":"bytes32","name":"","type":"bytes32"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"bytes32","name":"role","type":"bytes32"},{"internalType":"address","name":"account","type":"address"}],"name":"grantRole","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"bytes32","name":"role","type":"bytes32"},{"internalType":"address","name":"account","type":"address"}],"name":"hasRole","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"_referenceDay","type":"uint256"}],"name":"isDayIndexed","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"lastIndexedDay","outputs":[{"internalType":"uint32","name":"","type":"uint32"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"name","outputs":[{"internalType":"string","name":"","type":"string"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"bytes32","name":"role","type":"bytes32"},{"internalType":"address","name":"account","type":"address"}],"name":"renounceRole","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"bytes32","name":"role","type":"bytes32"},{"internalType":"address","name":"account","type":"address"}],"name":"revokeRole","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"bytes4","name":"interfaceId","type":"bytes4"}],"name":"supportsInterface","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint32","name":"_referenceDay","type":"uint32"},{"internalType":"uint256","name":"_referenceBlock","type":"uint256"},{"internalType":"uint256","name":"_currentSupply","type":"uint256"},{"internalType":"uint256","name":"_supplyCap","type":"uint256"},{"internalType":"uint256","name":"_maxStakingDuration","type":"uint256"},{"internalType":"uint256","name":"_maxConsumptionRate","type":"uint256"},{"internalType":"uint256","name":"_minConsumptionRate","type":"uint256"},{"internalType":"uint256","name":"_mintingPeriod","type":"uint256"},{"internalType":"uint256","name":"_scale","type":"uint256"},{"internalType":"bytes","name":"signature","type":"bytes"}],"name":"updateIndex","outputs":[{"internalType":"bool","name":"success","type":"bool"}],"stateMutability":"nonpayable","type":"function"}]