// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract WillContract {
address public owner;
mapping(address => uint) public inheritors;
bool public isDeceased;
constructor() {
owner = msg.sender;
isDeceased = false;
}
// 仅合约所有者可以调用的修饰符
modifier onlyOwner() {
require(msg.sender == owner, "Only owner can call this function.");
_;
}
// 仅在所有者去世后可以调用的修饰符
modifier onlyWhenDeceased() {
require(isDeceased == true, "Owner is not deceased.");
_;
}
// 存储遗嘱信息
function setInheritor(address _inheritor, uint _amount) public onlyOwner {
inheritors[_inheritor] = _amount;
}
// 标记所有者去世,触发资产分配
function declareDeceased() public onlyOwner {
isDeceased = true;
distributeInheritance();
}
// 分配遗产
function distributeInheritance() private onlyWhenDeceased {
for (address inheritor : inheritors) {
address payable wallet = address(uint160(inheritor));
wallet.transfer(inheritors[inheritor]);
}
}
// 接收以太币
receive() external payable {}
}