本文小編為大家詳細介紹“以太坊DAO時間鎖定Multisig怎么運用”,內容詳細,步驟清晰,細節處理妥當,希望這篇“以太坊DAO時間鎖定Multisig怎么運用”文章能幫助大家解決疑惑,下面跟著小編的思路慢慢深入,一起來學習新知識吧。
Decentralized Autonomous Organization,簡稱DAO,以太坊中重要的概念。一般翻譯為去中心化的自治組織。
有時候,時間也可以用作一種很好的安全機制。以下代碼基于DAO區塊鏈大會,但有不同的變化。不是每個操作需要X個成員批準,而是任何交易都可以由單個成員發起,但它們在執行之前都需要最少的延遲,這取決于交易的支持。提案的批準越多,就越早執行。會員可以對交易進行投票,這意味著它將取消其他一個已批準的簽名。
這意味著如果你沒有緊急程度,則執行任何交易可能只需要一個或兩個簽名。但是,如果單個密鑰被泄露,其他密鑰可以將該交易延遲數月或數年,甚至可以阻止其執行。
所有密鑰都已批準的交易可以在十分鐘后執行(此金額是可配置的),并且每5%未投票的成員每次需要的時間加倍(如果他們主動投票,則為四倍)反對)。如果它是一個簡單的ether交易,只要支持投票將其置于所需的時間內,就會執行交易,但更復雜的交易將要求使用正確的字節碼手動執行交易。這些是默認值,但在創建合約時可以設置不同的值:
批準交易的成員數量:近似時間延遲
100%批準:10分鐘(最低默認值)
90%批準:40分鐘
80%:2小時40分鐘
50%:大約一周
40%:1個月
30%:4個月
20%:超過一年
10%或更少:5年或從不 一旦最短的時間過去,任何人都可以執行交易(參見“國會”以獲得更完整的步行)。這是故意的,因為它允許某人安排交易或雇用其他人來執行交易。
代碼:
pragma solidity >=0.4.22 <0.6.0;
contract owned {
address public owner;
constructor() public {
owner = msg.sender;
}
modifier onlyOwner {
require(msg.sender == owner);
_;
}
function transferOwnership(address newOwner) onlyOwner public {
owner = newOwner;
}
}
contract tokenRecipient {
event receivedEther(address sender, uint amount);
event receivedTokens(address _from, uint256 _value, address _token, bytes _extraData);
function receiveApproval(address _from, uint256 _value, address _token, bytes memory _extraData) public {
Token t = Token(_token);
require(t.transferFrom(_from, address(this), _value));
emit receivedTokens(_from, _value, _token, _extraData);
}
function () payable external {
emit receivedEther(msg.sender, msg.value);
}
}
interface Token {
function transferFrom(address _from, address _to, uint256 _value) external returns (bool success);
}
contract TimeLockMultisig is owned, tokenRecipient {
Proposal[] public proposals;
uint public numProposals;
mapping (address => uint) public memberId;
Member[] public members;
uint minimumTime = 10;
event ProposalAdded(uint proposalID, address recipient, uint amount, string description);
event Voted(uint proposalID, bool position, address voter, string justification);
event ProposalExecuted(uint proposalID, int result, uint deadline);
event MembershipChanged(address member, bool isMember);
struct Proposal {
address recipient;
uint amount;
string description;
bool executed;
int currentResult;
bytes32 proposalHash;
uint creationDate;
Vote[] votes;
mapping (address => bool) voted;
}
struct Member {
address member;
string name;
uint memberSince;
}
struct Vote {
bool inSupport;
address voter;
string justification;
}
// Modifier that allows only shareholders to vote and create new proposals
modifier onlyMembers {
require(memberId[msg.sender] != 0);
_;
}
/**
* Constructor
*
* First time setup
*/
constructor(
address founder,
address[] memory initialMembers,
uint minimumAmountOfMinutes
) payable public {
if (founder != address(0)) owner = founder;
if (minimumAmountOfMinutes !=0) minimumTime = minimumAmountOfMinutes;
// It’s necessary to add an empty first member
addMember(address(0), '');
// and let's add the founder, to save a step later
addMember(owner, 'founder');
changeMembers(initialMembers, true);
}
/**
* Add member
*
* @param targetMember address to add as a member
* @param memberName label to give this member address
*/
function addMember(address targetMember, string memory memberName) onlyOwner public
{
uint id;
if (memberId[targetMember] == 0) {
memberId[targetMember] = members.length;
id = members.length++;
} else {
id = memberId[targetMember];
}
members[id] = Member({member: targetMember, memberSince: now, name: memberName});
emit MembershipChanged(targetMember, true);
}
/**
* Remove member
*
* @param targetMember the member to remove
*/
function removeMember(address targetMember) onlyOwner public {
require(memberId[targetMember] != 0);
for (uint i = memberId[targetMember]; i<members.length-1; i++){
members[i] = members[i+1];
memberId[members[i].member] = i;
}
memberId[targetMember] = 0;
delete members[members.length-1];
members.length--;
}
/**
* Edit existing members
*
* @param newMembers array of addresses to update
* @param canVote new voting value that all the values should be set to
*/
function changeMembers(address[] memory newMembers, bool canVote) public {
for (uint i = 0; i < newMembers.length; i++) {
if (canVote)
addMember(newMembers[i], '');
else
removeMember(newMembers[i]);
}
}
/**
* Add Proposal
*
* Propose to send `weiAmount / 1e18` ether to `beneficiary` for `jobDescription`. `transactionBytecode ? Contains : Does not contain` code.
*
* @param beneficiary who to send the ether to
* @param weiAmount amount of ether to send, in wei
* @param jobDescription Description of job
* @param transactionBytecode bytecode of transaction
*/
function newProposal(
address beneficiary,
uint weiAmount,
string memory jobDescription,
bytes memory transactionBytecode
)
onlyMembers public
returns (uint proposalID)
{
proposalID = proposals.length++;
Proposal storage p = proposals[proposalID];
p.recipient = beneficiary;
p.amount = weiAmount;
p.description = jobDescription;
p.proposalHash = keccak256(abi.encodePacked(beneficiary, weiAmount, transactionBytecode));
p.executed = false;
p.creationDate = now;
emit ProposalAdded(proposalID, beneficiary, weiAmount, jobDescription);
numProposals = proposalID+1;
vote(proposalID, true, '');
return proposalID;
}
/**
* Add proposal in Ether
*
* Propose to send `etherAmount` ether to `beneficiary` for `jobDescription`. `transactionBytecode ? Contains : Does not contain` code.
* This is a convenience function to use if the amount to be given is in round number of ether units.
*
* @param beneficiary who to send the ether to
* @param etherAmount amount of ether to send
* @param jobDescription Description of job
* @param transactionBytecode bytecode of transaction
*/
function newProposalInEther(
address beneficiary,
uint etherAmount,
string memory jobDescription,
bytes memory transactionBytecode
)
onlyMembers public
returns (uint proposalID)
{
return newProposal(beneficiary, etherAmount * 1 ether, jobDescription, transactionBytecode);
}
/**
* Check if a proposal code matches
*
* @param proposalNumber ID number of the proposal to query
* @param beneficiary who to send the ether to
* @param weiAmount amount of ether to send
* @param transactionBytecode bytecode of transaction
*/
function checkProposalCode(
uint proposalNumber,
address beneficiary,
uint weiAmount,
bytes memory transactionBytecode
)
view public
returns (bool codeChecksOut)
{
Proposal storage p = proposals[proposalNumber];
return p.proposalHash == keccak256(abi.encodePacked(beneficiary, weiAmount, transactionBytecode));
}
/**
* Log a vote for a proposal
*
* Vote `supportsProposal? in support of : against` proposal #`proposalNumber`
*
* @param proposalNumber number of proposal
* @param supportsProposal either in favor or against it
* @param justificationText optional justification text
*/
function vote(
uint proposalNumber,
bool supportsProposal,
string memory justificationText
)
onlyMembers public
{
Proposal storage p = proposals[proposalNumber]; // Get the proposal
require(p.voted[msg.sender] != true); // If has already voted, cancel
p.voted[msg.sender] = true; // Set this voter as having voted
if (supportsProposal) { // If they support the proposal
p.currentResult++; // Increase score
} else { // If they don't
p.currentResult--; // Decrease the score
}
// Create a log of this event
emit Voted(proposalNumber, supportsProposal, msg.sender, justificationText);
// If you can execute it now, do it
if ( now > proposalDeadline(proposalNumber)
&& p.currentResult > 0
&& p.proposalHash == keccak256(abi.encodePacked(p.recipient, p.amount, ''))
&& supportsProposal) {
executeProposal(proposalNumber, '');
}
}
function proposalDeadline(uint proposalNumber) public view returns(uint deadline) {
Proposal storage p = proposals[proposalNumber];
uint factor = calculateFactor(uint(p.currentResult), (members.length - 1));
return p.creationDate + uint(factor * minimumTime * 1 minutes);
}
function calculateFactor(uint a, uint b) public pure returns (uint factor) {
return 2**(20 - (20 * a)/b);
}
/**
* Finish vote
*
* Count the votes proposal #`proposalNumber` and execute it if approved
*
* @param proposalNumber proposal number
* @param transactionBytecode optional: if the transaction contained a bytecode, you need to send it
*/
function executeProposal(uint proposalNumber, bytes memory transactionBytecode) public {
Proposal storage p = proposals[proposalNumber];
require(now >= proposalDeadline(proposalNumber) // If it is past the voting deadline
&& p.currentResult > 0 // and a minimum quorum has been reached
&& !p.executed // and it is not currently being executed
&& checkProposalCode(proposalNumber, p.recipient, p.amount, transactionBytecode)); // and the supplied code matches the proposal...
p.executed = true;
(bool success, ) = p.recipient.call.value(p.amount)(transactionBytecode);
require(success);
// Fire Events
emit ProposalExecuted(proposalNumber, p.currentResult, proposalDeadline(proposalNumber));
}
}像以前一樣在這些教程上部署該代碼。在部署參數上,將最小時間留空將默認為30分鐘,如果你想要更快的鎖定時間,則放1分鐘。上傳后,執行“添加成員”功能以添加組的新成員,他們可以是你認識的其他人,也可以是不同計算機上的帳戶或離線存儲。
設置為所有者owner的帳戶非常強大,因為它可以隨意添加或刪除成員。因此,在添加主成員后,我們建議你通過執行Transfer Membership功能將owner設置為另一個帳戶。如果你希望對所有成員的添加或刪除進行投票,則將其設置為multisig本身,就像任何其他交易一樣。另一種方法是將其設置為另一個受信任的multisig錢包,如果你希望永久修復成員數,則可以設置為0x000。請記住,此合約上的資金僅與“所有者”帳戶一樣安全。
與上述任何DAO一樣,此合約可以持有以太幣,任何基于以太坊的代幣并執行任何合約。為此,請檢查如何在國會DAO上執行復雜的提案。
為簡單起見,對提案的投票僅僅算得少一點支持。如果你愿意,你可以玩弄負面投票更重要的想法,但這意味著少數成員可以對任何提議的交易擁有有效的否決權!
讀到這里,這篇“以太坊DAO時間鎖定Multisig怎么運用”文章已經介紹完畢,想要掌握這篇文章的知識點還需要大家自己動手實踐使用過才能領會,如果想了解更多相關內容的文章,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。