From e221395e7974ab09753448adf207ba0ae5381355 Mon Sep 17 00:00:00 2001 From: Emmanuel Date: Sat, 16 Oct 2021 21:06:59 +0300 Subject: [PATCH] optimized smart contract and added functionalities --- contract/abis/crowdfund.abi.json | 754 +++++++++++++++++-------------- contract/crowdfund.sol | 86 +++- src/main.js | 2 + 3 files changed, 510 insertions(+), 332 deletions(-) diff --git a/contract/abis/crowdfund.abi.json b/contract/abis/crowdfund.abi.json index 001b2de..75517b7 100644 --- a/contract/abis/crowdfund.abi.json +++ b/contract/abis/crowdfund.abi.json @@ -1,330 +1,428 @@ [ - { - "anonymous": false, - "inputs": [ - { - "indexed": true, - "internalType": "address", - "name": "owner", - "type": "address" - }, - { - "indexed": false, - "internalType": "string", - "name": "name", - "type": "string" - }, - { - "indexed": false, - "internalType": "string", - "name": "image", - "type": "string" - }, - { - "indexed": false, - "internalType": "string", - "name": "description", - "type": "string" - }, - { - "indexed": false, - "internalType": "string", - "name": "website", - "type": "string" - }, - { - "indexed": false, - "internalType": "uint256", - "name": "target", - "type": "uint256" - }, - { - "indexed": false, - "internalType": "uint256", - "name": "minContrib", - "type": "uint256" - }, - { - "indexed": false, - "internalType": "uint256", - "name": "timestamp", - "type": "uint256" - } - ], - "name": "AddProjectEvent", - "type": "event" - }, - { - "anonymous": false, - "inputs": [ - { - "indexed": false, - "internalType": "bool", - "name": "funded", - "type": "bool" - }, - { - "indexed": false, - "internalType": "bool", - "name": "fundingOpen", - "type": "bool" - } - ], - "name": "CloseFundEvent", - "type": "event" - }, - { - "anonymous": false, - "inputs": [ - { - "indexed": false, - "internalType": "uint256", - "name": "totalFunded", - "type": "uint256" - }, - { - "indexed": false, - "internalType": "uint256", - "name": "fundersCount", - "type": "uint256" - } - ], - "name": "FundProjectEvent", - "type": "event" - }, - { - "inputs": [ - { - "internalType": "uint256", - "name": "_index", - "type": "uint256" - } - ], - "name": "closeFund", - "outputs": [], - "stateMutability": "nonpayable", - "type": "function" - }, - { - "inputs": [ - { - "internalType": "string", - "name": "_name", - "type": "string" - }, - { - "internalType": "string", - "name": "_image", - "type": "string" - }, - { - "internalType": "string", - "name": "_description", - "type": "string" - }, - { - "internalType": "string", - "name": "_website", - "type": "string" - }, - { - "internalType": "uint256", - "name": "_target", - "type": "uint256" - }, - { - "internalType": "uint256", - "name": "_minContrib", - "type": "uint256" - } - ], - "name": "createProject", - "outputs": [], - "stateMutability": "nonpayable", - "type": "function" - }, - { - "inputs": [ - { - "internalType": "uint256", - "name": "_index", - "type": "uint256" - }, - { - "internalType": "uint256", - "name": "_amount", - "type": "uint256" - } - ], - "name": "fundProject", - "outputs": [], - "stateMutability": "payable", - "type": "function" - }, - { - "inputs": [ - { - "internalType": "uint256", - "name": "_index", - "type": "uint256" - } - ], - "name": "getContributions", - "outputs": [ - { - "internalType": "uint256[]", - "name": "", - "type": "uint256[]" - }, - { - "internalType": "uint256[]", - "name": "", - "type": "uint256[]" - }, - { - "internalType": "uint256", - "name": "", - "type": "uint256" - }, - { - "internalType": "bool", - "name": "", - "type": "bool" - } - ], - "stateMutability": "view", - "type": "function" - }, - { - "inputs": [ - { - "internalType": "uint256", - "name": "_index", - "type": "uint256" - } - ], - "name": "getProject", - "outputs": [ - { - "internalType": "address payable", - "name": "", - "type": "address" - }, - { - "internalType": "string", - "name": "", - "type": "string" - }, - { - "internalType": "string", - "name": "", - "type": "string" - }, - { - "internalType": "string", - "name": "", - "type": "string" - }, - { - "internalType": "string", - "name": "", - "type": "string" - }, - { - "internalType": "uint256", - "name": "", - "type": "uint256" - }, - { - "internalType": "uint256", - "name": "", - "type": "uint256" - }, - { - "internalType": "uint256", - "name": "", - "type": "uint256" - }, - { - "internalType": "uint256", - "name": "", - "type": "uint256" - }, - { - "internalType": "uint256", - "name": "", - "type": "uint256" - }, - { - "internalType": "bool", - "name": "", - "type": "bool" - }, - { - "internalType": "bool", - "name": "", - "type": "bool" - } - ], - "stateMutability": "view", - "type": "function" - }, - { - "inputs": [], - "name": "getProjectCount", - "outputs": [ - { - "internalType": "uint256", - "name": "", - "type": "uint256" - } - ], - "stateMutability": "view", - "type": "function" - }, - { - "inputs": [ - { - "internalType": "uint256", - "name": "_index", - "type": "uint256" - } - ], - "name": "getRefund", - "outputs": [], - "stateMutability": "payable", - "type": "function" - }, - { - "inputs": [ - { - "internalType": "uint256", - "name": "_index", - "type": "uint256" - } - ], - "name": "requestRefund", - "outputs": [ - { - "internalType": "uint256", - "name": "", - "type": "uint256" - } - ], - "stateMutability": "nonpayable", - "type": "function" - }, - { - "inputs": [ - { - "internalType": "uint256", - "name": "_index", - "type": "uint256" - } - ], - "name": "withdrawFunds", - "outputs": [], - "stateMutability": "payable", - "type": "function" - } + { + "anonymous": false, + "inputs": [ + { + "indexed": true, + "internalType": "address", + "name": "owner", + "type": "address" + }, + { + "indexed": false, + "internalType": "string", + "name": "name", + "type": "string" + }, + { + "indexed": false, + "internalType": "string", + "name": "image", + "type": "string" + }, + { + "indexed": false, + "internalType": "string", + "name": "description", + "type": "string" + }, + { + "indexed": false, + "internalType": "string", + "name": "website", + "type": "string" + }, + { + "indexed": false, + "internalType": "uint256", + "name": "target", + "type": "uint256" + }, + { + "indexed": false, + "internalType": "uint256", + "name": "minContrib", + "type": "uint256" + }, + { + "indexed": false, + "internalType": "uint256", + "name": "timestamp", + "type": "uint256" + } + ], + "name": "AddProjectEvent", + "type": "event" + }, + { + "anonymous": false, + "inputs": [ + { + "indexed": false, + "internalType": "bool", + "name": "funded", + "type": "bool" + }, + { + "indexed": false, + "internalType": "bool", + "name": "fundingOpen", + "type": "bool" + } + ], + "name": "CloseFundEvent", + "type": "event" + }, + { + "anonymous": false, + "inputs": [ + { + "indexed": true, + "internalType": "address", + "name": "owner", + "type": "address" + }, + { + "indexed": false, + "internalType": "string", + "name": "name", + "type": "string" + }, + { + "indexed": false, + "internalType": "string", + "name": "image", + "type": "string" + }, + { + "indexed": false, + "internalType": "string", + "name": "description", + "type": "string" + }, + { + "indexed": false, + "internalType": "string", + "name": "website", + "type": "string" + }, + { + "indexed": false, + "internalType": "uint256", + "name": "target", + "type": "uint256" + }, + { + "indexed": false, + "internalType": "uint256", + "name": "minContrib", + "type": "uint256" + }, + { + "indexed": false, + "internalType": "uint256", + "name": "timestamp", + "type": "uint256" + } + ], + "name": "EditProjectEvent", + "type": "event" + }, + { + "anonymous": false, + "inputs": [ + { + "indexed": false, + "internalType": "uint256", + "name": "totalFunded", + "type": "uint256" + }, + { + "indexed": false, + "internalType": "uint256", + "name": "fundersCount", + "type": "uint256" + } + ], + "name": "FundProjectEvent", + "type": "event" + }, + { + "inputs": [ + { + "internalType": "uint256", + "name": "_index", + "type": "uint256" + } + ], + "name": "closeFund", + "outputs": [], + "stateMutability": "nonpayable", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "string", + "name": "_name", + "type": "string" + }, + { + "internalType": "string", + "name": "_image", + "type": "string" + }, + { + "internalType": "string", + "name": "_description", + "type": "string" + }, + { + "internalType": "string", + "name": "_website", + "type": "string" + }, + { + "internalType": "uint256", + "name": "_target", + "type": "uint256" + }, + { + "internalType": "uint256", + "name": "_minContrib", + "type": "uint256" + } + ], + "name": "createProject", + "outputs": [], + "stateMutability": "nonpayable", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "uint256", + "name": "_index", + "type": "uint256" + }, + { + "internalType": "string", + "name": "_name", + "type": "string" + }, + { + "internalType": "string", + "name": "_image", + "type": "string" + }, + { + "internalType": "string", + "name": "_description", + "type": "string" + }, + { + "internalType": "string", + "name": "_website", + "type": "string" + }, + { + "internalType": "uint256", + "name": "_target", + "type": "uint256" + }, + { + "internalType": "uint256", + "name": "_minContrib", + "type": "uint256" + } + ], + "name": "editProject", + "outputs": [], + "stateMutability": "nonpayable", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "uint256", + "name": "_index", + "type": "uint256" + }, + { + "internalType": "uint256", + "name": "_amount", + "type": "uint256" + } + ], + "name": "fundProject", + "outputs": [], + "stateMutability": "payable", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "uint256", + "name": "_index", + "type": "uint256" + } + ], + "name": "getContributions", + "outputs": [ + { + "internalType": "uint256[]", + "name": "", + "type": "uint256[]" + }, + { + "internalType": "uint256[]", + "name": "", + "type": "uint256[]" + }, + { + "internalType": "uint256", + "name": "", + "type": "uint256" + }, + { + "internalType": "bool", + "name": "", + "type": "bool" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "uint256", + "name": "_index", + "type": "uint256" + } + ], + "name": "getProject", + "outputs": [ + { + "internalType": "address payable", + "name": "", + "type": "address" + }, + { + "internalType": "string", + "name": "", + "type": "string" + }, + { + "internalType": "string", + "name": "", + "type": "string" + }, + { + "internalType": "string", + "name": "", + "type": "string" + }, + { + "internalType": "string", + "name": "", + "type": "string" + }, + { + "internalType": "uint256", + "name": "", + "type": "uint256" + }, + { + "internalType": "uint256", + "name": "", + "type": "uint256" + }, + { + "internalType": "uint256", + "name": "", + "type": "uint256" + }, + { + "internalType": "uint256", + "name": "", + "type": "uint256" + }, + { + "internalType": "uint256", + "name": "", + "type": "uint256" + }, + { + "internalType": "bool", + "name": "", + "type": "bool" + }, + { + "internalType": "bool", + "name": "", + "type": "bool" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [], + "name": "getProjectCount", + "outputs": [ + { + "internalType": "uint256", + "name": "", + "type": "uint256" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "uint256", + "name": "_index", + "type": "uint256" + } + ], + "name": "getRefund", + "outputs": [], + "stateMutability": "payable", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "uint256", + "name": "_index", + "type": "uint256" + } + ], + "name": "requestRefund", + "outputs": [ + { + "internalType": "uint256", + "name": "", + "type": "uint256" + } + ], + "stateMutability": "nonpayable", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "uint256", + "name": "_index", + "type": "uint256" + } + ], + "name": "withdrawFunds", + "outputs": [], + "stateMutability": "payable", + "type": "function" + } ] \ No newline at end of file diff --git a/contract/crowdfund.sol b/contract/crowdfund.sol index d6c83d2..99eb970 100644 --- a/contract/crowdfund.sol +++ b/contract/crowdfund.sol @@ -60,6 +60,17 @@ contract CrowdFund { uint timestamp ); + event EditProjectEvent( + address indexed owner, + string name, + string image, + string description, + string website, + uint target, + uint minContrib, + uint timestamp + ); + event FundProjectEvent( uint totalFunded, uint fundersCount @@ -114,10 +125,14 @@ contract CrowdFund { uint _minContrib ) public { // Error Handling - require(_target > 0, 'Project target must be greater than 0 cUSD'); - require(_minContrib < _target, 'The minimum contribution must be less than the Target!'); - require(bytes(_name).length != 0 && bytes(_description).length != 0 && bytes(_website).length != 0, 'Project Title, description and website must be present!'); - + validateData( + _image, + _description, + _website, + _target, + _minContrib); + + uint _totalFunded = 0; uint _fundersCount = 0; @@ -150,6 +165,58 @@ contract CrowdFund { newProject.timestamp ); } + + +// owner of the project can edit it + function editProject ( + uint _index, + string memory _name, + string memory _image, + string memory _description, + string memory _website, + uint _target, + uint _minContrib + ) public onlyProjectOwner(_index) { + + + Project storage project = projects[_index]; + + // Error Handling + validateData( + _image, + _description, + _website, + _target, + _minContrib); + + + + uint _totalFunded = project.totalFunded; + uint _fundersCount = project.fundersCount; + + require(_target >_totalFunded, "Cannt update target to value less than already raised" ); + + project.name = _name; + project.image = _image; + project.description = _description; + project.website = _website; + project.target = _target; + project.minContrib = _minContrib; + project.totalFunded = _totalFunded; + project.fundersCount = _fundersCount; + + emit EditProjectEvent( + project.owner, + project.name, + project.image, + project.description, + project.website, + project.target, + project.minContrib, + project.timestamp + ); + } + function fundProject (uint _index, uint _amount) public payable { Project storage project = projects[_index]; @@ -300,4 +367,15 @@ contract CrowdFund { function increaseProjectCount() internal { projectCount++; } + function validateData( + string memory _name, + string memory _description, + string memory _website, + uint _target, + uint _minContrib ) internal pure { + require(_target > 0, 'Project target must be greater than 0 cUSD'); + require(_minContrib < _target, 'The minimum contribution must be less than the Target!'); + require(bytes(_name).length != 0 && bytes(_description).length != 0 && bytes(_website).length != 0, 'Project Title, description and website must be present!'); + + } } \ No newline at end of file diff --git a/src/main.js b/src/main.js index e39af90..ac0ef25 100644 --- a/src/main.js +++ b/src/main.js @@ -6,6 +6,8 @@ import erc20Abi from "../contract/abis/erc20.abi.json"; const ERC20_DECIMALS = 18; const CFContractAddress = "0xF7A1502Aa2C3E699Dfc445fFf516Ff6eA08742f7"; +// new deployed adrress +// const CFContractAddress = "0x0a17Acbbfd9ACe7C9e17eFf281b191B80617e739"; const cUSDContractAddress = "0x874069Fa1Eb16D44d622F2e0Ca25eeA172369bC1"; let kit;