// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;
/**
* @title 代际财富管理系统 - 完整版
* @notice 实现22年首代注册、生育控制、赡养义务与跨代继承
*/
contract GenerationalWealth {
// ======================
// 1. 常量定义
// ======================
uint256 private constant SECONDS_PER_YEAR = 31536000;
uint256 public constant GENESIS_PERIOD = 22 * SECONDS_PER_YEAR;
uint256 public constant FERTILITY_WINDOW = 20 * SECONDS_PER_YEAR;
uint256 public constant LIFESPAN = 100 * SECONDS_PER_YEAR;
uint256 public constant SUPPORT_INTERVAL = 30 days;
uint256 public constant SUPPORT_RATE = 5; // 5%
uint256 public constant MAX_CHILDREN = 3;
// ======================
// 2. 数据结构
// ======================
struct Person {
uint256 birthTime;
address[2] parents;
address[] children;
address spouse;
uint256 fertilityEnd;
uint256 lastSupportPaid;
}
// ======================
// 3. 存储变量
// ======================
mapping(address => Person) public people;
address[] public population;
uint256 public immutable genesisEndTime;
// ======================
// 4. 事件定义
// ======================
event Registered(address indexed who);
event Married(address indexed partner1, address indexed partner2);
event ChildBorn(address indexed parent1, address indexed parent2, address child);
event SupportPaid(address indexed from, address indexed to, uint256 amount);
event Inherited(address indexed from, address[] heirs, uint256[] amounts);
// ======================
// 5. 构造函数
// ======================
constructor() {
genesisEndTime = block.timestamp + GENESIS_PERIOD;
}
// ======================
// 6. 外部函数
// ======================
/**
* @dev 首代注册(前22年有效)
*/
function register() external {
require(block.timestamp < genesisEndTime, "GENESIS_PERIOD_ENDED");
require(people[msg.sender].birthTime == 0, "ALREADY_REGISTERED");
people[msg.sender] = Person({
birthTime: block.timestamp,
parents: [address(0), address(0)],
children: new address[](0),
spouse: address(0),
fertilityEnd: block.timestamp + FERTILITY_WINDOW,
lastSupportPaid: 0
});
population.push(msg.sender);
emit Registered(msg.sender);
}
/**
* @dev 创建家庭并生育子代
* @param partner 配偶地址
*/
function createFamily(address partner) external returns (address child) {
require(block.timestamp >= genesisEndTime, "GENESIS_PERIOD_ACTIVE");
require(msg.sender != partner, "SELF_MARRIAGE");
require(people[msg.sender].spouse == address(0), "ALREADY_MARRIED");
require(people[partner].spouse == address(0), "PARTNER_MARRIED");
require(people[msg.sender].children.length < MAX_CHILDREN, "CHILD_LIMIT");
require(block.timestamp < people[msg.sender].fertilityEnd, "FERTILITY_ENDED");
// 使用CREATE2生成确定性子地址
bytes memory creationCode = abi.encodePacked(
type(ChildProxy).creationCode,
abi.encode(msg.sender, partner, address(this))
);
bytes32 salt = keccak256(abi.encodePacked(
msg.sender,
partner,
people[msg.sender].children.length
));
assembly {
child := create2(0, add(creationCode, 0x20), mload(creationCode), salt)
}
// 注册子代
_registerChild(msg.sender, partner, child);
emit Married(msg.sender, partner);
emit ChildBorn(msg.sender, partner, child);
}
/**
* @dev 执行赡养费支付(需定期调用)
*/
function executeSupport() external {
for (uint256 i = 0; i < population.length; i++) {
address person = population[i];
if (_shouldPaySupport(person)) {
_paySupport(person);
}
}
}
/**
* @dev 执行遗产分配(需定期调用)
*/
function executeInheritance() external {
for (uint256 i = 0; i < population.length; i++) {
address person = population[i];
if (_shouldInherit(person)) {
_distributeInheritance(person);
}
}
}
// ======================
// 7. 公开查询函数
// ======================
/**
* @dev 获取地址年龄
*/
function getAge(address who) public view returns (uint256) {
return (block.timestamp - people[who].birthTime) / SECONDS_PER_YEAR;
}
// ======================
// 8. 内部函数
// ======================
/**
* @dev 注册子代(供ChildProxy调用)
*/
function _registerChild(address parent1, address parent2, address child) internal {
people[parent1].spouse = parent2;
people[parent2].spouse = parent1;
people[parent1].children.push(child);
people[parent2].children.push(child);
people[child] = Person({
birthTime: block.timestamp,
parents: [parent1, parent2],
children: new address[](0),
spouse: address(0),
fertilityEnd: block.timestamp + FERTILITY_WINDOW,
lastSupportPaid: 0
});
population.push(child);
}
function _shouldPaySupport(address who) internal view returns (bool) {
Person storage p = people[who];
return getAge(who) >= 40 &&
(p.parents[0] != address(0) || p.parents[1] != address(0)) &&
block.timestamp >= p.lastSupportPaid + SUPPORT_INTERVAL &&
address(who).balance > 0;
}
function _paySupport(address payer) internal {
Person storage p = people[payer];
uint256 paymentAmount = address(payer).balance * SUPPORT_RATE / 100;
for (uint256 i = 0; i < 2; i++) {
if (p.parents[i] != address(0)) {
(bool sent, ) = p.parents[i].call{value: paymentAmount / 2}("");
require(sent, "PAYMENT_FAILED");
p.lastSupportPaid = block.timestamp;
emit SupportPaid(payer, p.parents[i], paymentAmount / 2);
}
}
}
function _shouldInherit(address who) internal view returns (bool) {
return getAge(who) >= 100 &&
people[who].children.length > 0 &&
address(who).balance > 0;
}
function _distributeInheritance(address deceased) internal {
Person storage p = people[deceased];
uint256 totalEstate = address(deceased).balance;
// 计算分配比例
uint256 childrenShare = totalEstate * 80 / 100;
uint256 grandchildrenShare = totalEstate - childrenShare;
// 准备受益人数组
address[] memory heirs = new address[](p.children.length + _countGrandchildren(deceased));
uint256[] memory amounts = new uint256[](heirs.length);
uint256 heirIndex = 0;
// 分配给子女(80%平分)
uint256 perChild = childrenShare / p.children.length;
for (uint256 i = 0; i < p.children.length; i++) {
heirs[heirIndex] = p.children[i];
amounts[heirIndex] = perChild;
heirIndex++;
(bool sent, ) = p.children[i].call{value: perChild}("");
require(sent, "TRANSFER_FAILED");
}
// 分配给孙辈(20%平分)
if (grandchildrenShare > 0) {
uint256 totalGrandchildren = _countGrandchildren(deceased);
if (totalGrandchildren > 0) {
uint256 perGrandchild = grandchildrenShare / totalGrandchildren;
for (uint256 i = 0; i < p.children.length; i++) {
address child = p.children[i];
for (uint256 j = 0; j < people[child].children.length; j++) {
heirs[heirIndex] = people[child].children[j];
amounts[heirIndex] = perGrandchild;
heirIndex++;
(bool sent, ) = people[child].children[j].call{value: perGrandchild}("");
require(sent, "TRANSFER_FAILED");
}
}
}
}
emit Inherited(deceased, heirs, amounts);
}
function _countGrandchildren(address grandparent) internal view returns (uint256) {
uint256 count;
Person storage gp = people[grandparent];
for (uint256 i = 0; i < gp.children.length; i++) {
count += people[gp.children[i]].children.length;
}
return count;
}
// ======================
// 9. 回退函数
// ======================
receive() external payable {}
}
/**
* @title 子地址代理合约
* @dev 通过汇编代码实现确定性子地址生成
*/
contract ChildProxy {
constructor(address parent1, address parent2, address wealthContract) {
// 安全调用主合约
(bool success, ) = wealthContract.call(
abi.encodeWithSignature(
"_registerChild(address,address,address)",
parent1,
parent2,
address(this)
)
);
require(success, "REGISTRATION_FAILED");
}
}