Skip to content

Instantly share code, notes, and snippets.

@depresto
Created July 26, 2022 08:19
Show Gist options
  • Save depresto/cebc5a983ccfd8747ca0516a11a20f06 to your computer and use it in GitHub Desktop.
Save depresto/cebc5a983ccfd8747ca0516a11a20f06 to your computer and use it in GitHub Desktop.
// SPDX-License-Identifier: GPL-3.0
pragma solidity 0.8.7;
contract Multisig {
uint256 minSignatures = 3;
address private contractOwner;
mapping(address => bool) private owners;
struct Transaction {
address to;
uint256 amount;
uint256 createdAt;
uint256 sentAt;
uint256 signatureCount;
mapping(address => bool) signatures;
}
mapping(uint256 => Transaction) transactions;
uint256[] public pendingTransactionIds;
uint256 private currentTransactionId = 0;
constructor() payable {
contractOwner = msg.sender;
owners[msg.sender] = true;
}
modifier isOwner() {
require(owners[msg.sender], "You are not the owner");
_;
}
function addOwner(address newOwner) public {
require(contractOwner == msg.sender, "You are not the owner");
owners[newOwner] = true;
}
function removeOwner(address addr) public isOwner() {
require(contractOwner == msg.sender, "You are not the owner");
owners[addr] = false;
}
function createTransaction(address to, uint256 amount) public isOwner() returns(uint256) {
uint256 currentBalance = address(this).balance;
require(amount <= currentBalance, "Account balance is not enough");
uint256 transactionId = currentTransactionId;
Transaction storage newTransaction = transactions[transactionId];
newTransaction.to = to;
newTransaction.amount = amount;
newTransaction.createdAt = block.timestamp;
newTransaction.sentAt = 0;
newTransaction.signatureCount = 0;
pendingTransactionIds.push(transactionId);
currentTransactionId += 1;
return transactionId;
}
function signTransaction(uint256 tranactionId) public isOwner() {
Transaction storage currentTransaction = transactions[tranactionId];
require(currentTransaction.sentAt == 0, "This transaction has executed");
require(currentTransaction.signatures[msg.sender] == false, "You have signed the transaction");
currentTransaction.signatures[msg.sender] = true;
currentTransaction.signatureCount += 1;
if (currentTransaction.signatureCount >= minSignatures) {
payable(currentTransaction.to).transfer(currentTransaction.amount);
currentTransaction.sentAt = block.timestamp;
for (uint256 i=0; i<pendingTransactionIds.length; i++) {
if (pendingTransactionIds[i] == tranactionId) {
delete pendingTransactionIds[i];
break;
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment