const SmartWeddingContract = artifacts.require("./contracts/SmartWeddingContract.sol"); const husbandAddress = require("../config.js").husbandAddress; const wifeAddress = require("../config.js").wifeAddress; module.exports = function(deployer) { deployer.deploy(SmartWeddingContract, husbandAddress, wifeAddress); };
modifier isSigned() { require(signed == true, "Contract has not been signed by both spouses yet!"); _; }
function signContract() external onlySpouse { require(isSameString(writtenContractIpfsHash, ""), "Written contract ipfs hash has been proposed yet!"); require(hasSigned[msg.sender] == false, "Spouse has already signed the contract!"); // Sender签名 hasSigned[msg.sender] = true; emit Signed(now, msg.sender); // 检查夫妻双方是否都已经签名 if (hasSigned[husbandAddress] && hasSigned[wifeAddress]) { signed = true; emit ContractSigned(now); } }
signContract函数只能由夫妻调用。 “神奇”部分是最后的multisig。 if-clause 检查丈夫和妻子是否签署了合同。 如果是,则全局签名状态更改为true。
function proposeAsset(string _data, uint _husbandAllocation, uint _wifeAllocation) external onlySpouse isSigned isNotDivorced { require(isSameString(_data, ""), "No asset data provided!"); require(_husbandAllocation >= 0, "Husband allocation invalid!"); require(_wifeAllocation >= 0, "Wife allocation invalid!"); require((_husbandAllocation + _wifeAllocation) == 100, "Total allocation must be equal to 100%!"); // 添加新资产 Asset memory newAsset = Asset({ data: _data, husbandAllocation: _husbandAllocation, wifeAllocation: _wifeAllocation, added: false, removed: false }); uint newAssetId = assets.push(newAsset); emit AssetProposed(now, _data, msg.sender); // 映射到存储对象(否则无法访问映射) Asset storage asset = assets[newAssetId - 1]; // Sender立刻批准 asset.hasApprovedAdd[msg.sender] = true; emit AssetAddApproved(now, _data, msg.sender); }
function approveAsset(uint _assetId) external onlySpouse isSigned isNotDivorced { require(_assetId > 0 && _assetId <= assets.length, "Invalid asset id!"); Asset storage asset = assets[_assetId - 1]; require(asset.added == false, "Asset has already been added!"); require(asset.removed == false, "Asset has already been removed!"); require(asset.hasApprovedAdd[msg.sender] == false, "Asset has already approved by sender!"); // Sender批准 asset.hasApprovedAdd[msg.sender] = true; emit AssetAddApproved(now, asset.data, msg.sender); // 检查夫妻双方是否都已批准 if (asset.hasApprovedAdd[husbandAddress] && asset.hasApprovedAdd[wifeAddress]) { asset.added = true; emit AssetAdded(now, asset.data); } }
function removeAsset(uint _assetId) external onlySpouse isSigned isNotDivorced { require(_assetId > 0 && _assetId <= assets.length, "Invalid asset id!"); Asset storage asset = assets[_assetId - 1]; require(asset.added, "Asset has not been added yet!"); require(asset.removed == false, "Asset has already been removed!"); require(asset.hasApprovedRemove[msg.sender] == false, "Removing the asset has already been approved by the sender!"); // 批准 Sender 删除 asset.hasApprovedRemove[msg.sender] = true; emit AssetRemoveApproved(now, asset.data, msg.sender); // 检查夫妻双方是否都批准移除资产 if (asset.hasApprovedRemove[husbandAddress] && asset.hasApprovedRemove[wifeAddress]) { asset.removed = true; emit AssetRemoved(now, asset.data); } }
function() external payable isSigned isNotDivorced { emit FundsReceived(now, msg.sender, msg.value); }
这是默认功能。 重要的关键词是可支付。 它使智能合约能够获得资金(ETH)。
function pay(address _to, uint _amount) external onlySpouse isSigned isNotDivorced { require(_to != address(0), "Sending funds to address zero is prohibited!"); require(_amount <= address(this).balance, "Not enough balance available!"); // 将资金发送到目的地地址 _to.transfer(_amount); emit FundsSent(now, _to, _amount); }
付费功能仅获取目的地地址和要发送的金额。
function divorce() external onlySpouse isSigned isNotDivorced { require(hasDivorced[msg.sender] == false, "Sender has already approved to divorce!"); // Sender批准 hasDivorced[msg.sender] = true; emit DivorceApproved(now, msg.sender); // 检查夫妻双方是否都同意离婚 if (hasDivorced[husbandAddress] && hasDivorced[wifeAddress]) { divorced = true; emit Divorced(now); // 获取合约的余额 uint balance = address(this).balance; // 将余额分成两半 if (balance != 0) { uint balancePerSpouse = balance / 2; // 转账给原丈夫方 husbandAddress.transfer(balancePerSpouse); emit FundsSent(now, husbandAddress, balancePerSpouse); // 转账给原妻子方 wifeAddress.transfer(balancePerSpouse); emit FundsSent(now, wifeAddress, balancePerSpouse); } } }
modifier isNotDivorced() { require(divorced == false, "Can not be called after spouses agreed to divorce!"); _; }
event WrittenContractProposed(uint timestamp, string ipfsHash, address wallet);
event Signed(uint timestamp, address wallet);
event ContractSigned(uint timestamp);
event AssetProposed(uint timestamp, string asset, address wallet);
event AssetAddApproved(uint timestamp, string asset, address wallet);
event AssetAdded(uint timestamp, string asset);
event AssetRemoveApproved(uint timestamp, string asset, address wallet);
event AssetRemoved(uint timestamp, string asset);
event DivorceApproved(uint timestamp, address wallet);
event Divorced(uint timestamp);
event FundsSent(uint timestamp, address wallet, uint amount);
event FundsReceived(uint timestamp, address wallet, uint amount);
原文:https://www.cnblogs.com/elninowang/p/10115200.html