Skip to content

Instantly share code, notes, and snippets.

@Falilah
Created August 11, 2022 19:22
Show Gist options
  • Save Falilah/91f570afe0c5e45de70e3079966eb2a8 to your computer and use it in GitHub Desktop.
Save Falilah/91f570afe0c5e45de70e3079966eb2a8 to your computer and use it in GitHub Desktop.
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.9;
import "./multisig.sol";
contract MultiSigFactory {
//a factory contract that create multiple clones of multisig.sol
//a function that create a new multisig
//an array that holds contract addresses created
//a function that calls the approve function in multisig.sol
// a function that calls the withdraw function in multisig.sol
MultiSig[] multiSigAddresses;
event newClone(MultiSig indexed, uint256 indexed position);
function cloneMultiSig(address[] memory _validOwners)
external
returns (MultiSig NewMS, uint256 _length)
{
NewMS = new MultiSig(_validOwners);
multiSigAddresses.push(NewMS);
_length = multiSigAddresses.length;
emit newClone(NewMS, _length);
}
function ClonedAddresses()
external
view
returns (MultiSig[] memory _multisig)
{
_multisig = multiSigAddresses;
}
}
@Falilah
Copy link
Author

Falilah commented Aug 11, 2022

import { ethers } from "hardhat";

async function main() {
// const lockedAmount = ethers.utils.parseEther("1");
// let valid1, valid2, valid3, valid4;
let [valid1, valid2, valid3, valid4, valid5] = await ethers.getSigners();

const MultisigFactory = await ethers.getContractFactory("MultiSigFactory");
const multisigFactory = await MultisigFactory.deploy();

await multisigFactory.deployed();

console.log(
"factory contract deployed to this address",
multisigFactory.address
);
let cloned = await multisigFactory.cloneMultiSig([
valid1.address,
valid2.address,
valid3.address,
valid4.address,
valid5.address,
]);

const clone2 = await multisigFactory
.connect(valid2)
.cloneMultiSig([
valid1.address,
valid2.address,
valid3.address,
valid4.address,
valid5.address,
]);
let result = (await clone2.wait()).logs[0].topics.length;
let result1 = (await clone2.wait()).logs[0].topics[0];
let result2 = (await clone2.wait()).logs[0].topics[1];
let result3 = (await clone2.wait()).logs[0].topics[2];

console.log(result, "factory cloned successfully");
console.log("we are the logger", result1, result2, result3);
}

// We recommend this pattern to be able to use async/await everywhere
// and properly handle errors.
main().catch((error) => {
console.error(error);
process.exitCode = 1;
});

@Falilah
Copy link
Author

Falilah commented Aug 11, 2022

// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.9;

contract MultiSig {
///A contract that allows 70% of validSigner to Approve before a withdrawal can be succesful

address[] validSigner;
uint256 ID = 1;
uint256 public Quorum = 3;
//maping of trnsaction Id to number of approval to status
mapping(uint256 => mapping(uint256 => bool)) _approved;
//mapping of transactionId to number of approval
mapping(uint256 => uint256) public noOfApproval;
//checking if an address has approved a particular transactionID
mapping(address => mapping(uint256 => bool)) signed;
mapping(uint256 => address) beneficiary;
mapping(uint256 => uint256) amount;
address owner;

constructor(address[] memory _validSigner) {
    validSigner = _validSigner;
}

function withdrawEther(uint256 _amount) external {
    bool _valid = validOwner();
    Approve(ID);
    beneficiary[ID] = msg.sender;
    amount[ID] = _amount;
    ID++;
}

function validOwner() private view returns (bool success) {
    address valid;

    for (uint256 i = 0; i < validSigner.length; i++) {
        if (msg.sender == validSigner[i]) {
            valid = msg.sender;
        }
    }
    assert(valid != address(0));
    success = true;
}

function Approve(uint256 id) public {
    bool valid = validOwner();
    uint256 value = amount[id];
    address _ben = beneficiary[id];
    assert(signed[msg.sender][id] == false);
    signed[msg.sender][id] = true;
    noOfApproval[id] = noOfApproval[id] + 1;
    if (noOfApproval[id] >= Quorum) {
        _approved[id][noOfApproval[id]] = true;
        payable(_ben).transfer(value);
    }
}

function contractBalance() external view returns (uint256) {
    return address(this).balance;
}

receive() external payable {}

fallback() external payable {}

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment