文件 1 的 5: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;
}
}
文件 2 的 5:ILand.sol
pragma solidity ^0.8.0;
interface ILand {
function maximumSupply() external view returns (uint256);
function balanceOf(address owner) external view returns (uint256);
function tokenOfOwnerByIndex(address owner, uint256 index) external view returns (uint256);
}
文件 3 的 5:LandYield.sol
pragma solidity ^0.8.0;
import "./Ownable.sol";
import "./ReentrancyGuard.sol";
import "./ILand.sol";
contract LandYield is Ownable, ReentrancyGuard {
ILand public landContract;
address private _treasury;
address private _admin;
uint256 private _totalYieldPerLand;
uint256 private _totalReleasedYield;
uint256 private _owedTreasuryYield;
mapping (uint256 => uint256) private _releasedYield;
mapping (address => uint256) private _landYieldRatios;
modifier onlyManager() {
require(msg.sender == owner() || msg.sender == _admin, "Unauthorized Access");
_;
}
constructor(address _landContractAddress) {
landContract = ILand(_landContractAddress);
_treasury = msg.sender;
_admin = msg.sender;
}
function admin() external view returns (address) {
return _admin;
}
function setAdmin(address _address) external onlyOwner {
require(_address != address(0), "Invalid Address");
_admin = _address;
}
function treasury() external view returns (address) {
return _treasury;
}
function setTreasury(address _address) external onlyManager {
require(_address != address(0), "Invalid Address");
_treasury = _address;
}
function totalYieldPerLand() external view returns (uint256) {
return _totalYieldPerLand;
}
function totalReleasedYield() external view returns (uint256) {
return _totalReleasedYield;
}
function owedTreasuryYield() external view returns (uint256) {
return _owedTreasuryYield;
}
function landYieldRatio(address _address) external view returns (uint256) {
return _landYieldRatios[_address];
}
function setLandYieldRatio(address _address, uint256 _ratio) external onlyManager {
require(_address != address(0), "Invalid Address");
require(_ratio > 0 && _ratio < 100, "Invalid Yield Ratio");
_landYieldRatios[_address] = _ratio;
}
function distributeSalesYield() external payable nonReentrant {
require(msg.value > 0, "Insufficient Yield");
uint256 yieldRatio = _landYieldRatios[msg.sender];
require(yieldRatio > 0 && yieldRatio < 100, "Unregistered Yield Source");
uint256 landCount = landContract.maximumSupply();
uint256 landYield = (msg.value * yieldRatio) / 100;
_totalYieldPerLand += landYield / landCount;
_owedTreasuryYield += (msg.value - landYield) + (landYield % landCount);
}
function releasedYieldByTokenId(uint256 _tokenId) external view returns (uint256) {
return _releasedYield[_tokenId];
}
function totalOwedYieldByAccount(address _account) external view returns (uint256) {
uint256 landOwned = landContract.balanceOf(_account);
uint256 totalOwedYield = 0;
for (uint256 i = 0; i < landOwned; i++) {
uint256 tokenId = landContract.tokenOfOwnerByIndex(_account, i);
uint256 owedYield = _totalYieldPerLand - _releasedYield[tokenId];
if (owedYield > 0) {
totalOwedYield += owedYield;
}
}
return totalOwedYield;
}
function releaseForLandOwner() external nonReentrant {
uint256 landOwned = landContract.balanceOf(msg.sender);
require(landOwned > 0, "Reserved For Land Owners");
uint256 totalOwedYield = 0;
for (uint256 i = 0; i < landOwned; i++) {
uint256 tokenId = landContract.tokenOfOwnerByIndex(msg.sender, i);
uint256 owedYield = _totalYieldPerLand - _releasedYield[tokenId];
if (owedYield > 0) {
totalOwedYield += owedYield;
_releasedYield[tokenId] = _totalYieldPerLand;
}
}
require(totalOwedYield > 0, "Insufficient Yield");
_totalReleasedYield += totalOwedYield;
payable(msg.sender).transfer(totalOwedYield);
}
receive() external payable {
require(msg.value > 0, "Insufficient Yield");
uint256 landCount = landContract.maximumSupply();
_totalYieldPerLand += msg.value / landCount;
_owedTreasuryYield += msg.value % landCount;
}
function releaseForTreasury() external onlyManager nonReentrant {
require(_owedTreasuryYield > 0, "Insufficient Yield");
uint256 totalOwedYield = _owedTreasuryYield;
_owedTreasuryYield = 0;
_totalReleasedYield += totalOwedYield;
payable(_treasury).transfer(totalOwedYield);
}
}
文件 4 的 5:Ownable.sol
pragma solidity ^0.8.0;
import "./Context.sol";
abstract contract Ownable is Context {
address private _owner;
event OwnershipTransferred(address indexed previousOwner, address indexed newOwner);
constructor() {
_setOwner(_msgSender());
}
function owner() public view virtual returns (address) {
return _owner;
}
modifier onlyOwner() {
require(owner() == _msgSender(), "Ownable: caller is not the owner");
_;
}
function renounceOwnership() public virtual onlyOwner {
_setOwner(address(0));
}
function transferOwnership(address newOwner) public virtual onlyOwner {
require(newOwner != address(0), "Ownable: new owner is the zero address");
_setOwner(newOwner);
}
function _setOwner(address newOwner) private {
address oldOwner = _owner;
_owner = newOwner;
emit OwnershipTransferred(oldOwner, newOwner);
}
}
文件 5 的 5:ReentrancyGuard.sol
pragma solidity ^0.8.0;
abstract contract ReentrancyGuard {
uint256 private constant _NOT_ENTERED = 1;
uint256 private constant _ENTERED = 2;
uint256 private _status;
constructor() {
_status = _NOT_ENTERED;
}
modifier nonReentrant() {
require(_status != _ENTERED, "ReentrancyGuard: reentrant call");
_status = _ENTERED;
_;
_status = _NOT_ENTERED;
}
}
{
"compilationTarget": {
"LandYield.sol": "LandYield"
},
"evmVersion": "london",
"libraries": {},
"metadata": {
"bytecodeHash": "ipfs"
},
"optimizer": {
"enabled": true,
"runs": 1000
},
"remappings": []
}
[{"inputs":[{"internalType":"address","name":"_landContractAddress","type":"address"}],"stateMutability":"nonpayable","type":"constructor"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"previousOwner","type":"address"},{"indexed":true,"internalType":"address","name":"newOwner","type":"address"}],"name":"OwnershipTransferred","type":"event"},{"inputs":[],"name":"admin","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"distributeSalesYield","outputs":[],"stateMutability":"payable","type":"function"},{"inputs":[],"name":"landContract","outputs":[{"internalType":"contract ILand","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"_address","type":"address"}],"name":"landYieldRatio","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"owedTreasuryYield","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"owner","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"releaseForLandOwner","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"releaseForTreasury","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"_tokenId","type":"uint256"}],"name":"releasedYieldByTokenId","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"renounceOwnership","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"_address","type":"address"}],"name":"setAdmin","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"_address","type":"address"},{"internalType":"uint256","name":"_ratio","type":"uint256"}],"name":"setLandYieldRatio","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"_address","type":"address"}],"name":"setTreasury","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"_account","type":"address"}],"name":"totalOwedYieldByAccount","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"totalReleasedYield","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"totalYieldPerLand","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"newOwner","type":"address"}],"name":"transferOwnership","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"treasury","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"stateMutability":"payable","type":"receive"}]