Skip to content

Instantly share code, notes, and snippets.

@casweeney
Created August 11, 2022 14:16
Show Gist options
  • Select an option

  • Save casweeney/92b0fc5c06e69be6061f42249cf70748 to your computer and use it in GitHub Desktop.

Select an option

Save casweeney/92b0fc5c06e69be6061f42249cf70748 to your computer and use it in GitHub Desktop.
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract Wallet {
address[] public approvers;
uint256 public quorum;
struct Transfer {
uint256 id;
uint256 amount;
address payable to;
uint256 approvals;
bool sent;
}
Transfer[] public transfers;
mapping(address => mapping(uint256 => bool)) public approvals;
// Set approvers addresses and quorum need to sign a transaction
constructor(address[] memory _approvers, uint256 _quorum) {
approvers = _approvers;
quorum = _quorum;
}
// Get approvers
function getApprovers() external view returns (address[] memory) {
return approvers;
}
function getTransfers() external view returns (Transfer[] memory) {
return transfers;
}
function createTransfer(uint256 amount, address payable to)
external
onlyApprover
{
transfers.push(Transfer(transfers.length, amount, to, 0, false));
}
function approveTransfer(uint256 id) external onlyApprover {
require(transfers[id].sent == false, "transfer has already been sent");
require(
approvals[msg.sender][id] == false,
"cannot approve transfer twice"
);
approvals[msg.sender][id] = true;
transfers[id].approvals++;
if (transfers[id].approvals >= quorum) {
transfers[id].sent = true;
address payable to = transfers[id].to;
uint256 amount = transfers[id].amount;
to.transfer(amount);
}
}
receive() external payable {}
modifier onlyApprover() {
bool allowed = false;
for (uint256 i = 0; i < approvers.length; i++) {
if (approvers[i] == msg.sender) {
allowed = true;
}
}
require(allowed == true, "only approver allowed");
_;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment