Skip to content

Commit

Permalink
payment escrow contract updated
Browse files Browse the repository at this point in the history
  • Loading branch information
ejim11 committed Mar 27, 2023
0 parents commit 57c6670
Show file tree
Hide file tree
Showing 15 changed files with 16,579 additions and 0 deletions.
11 changes: 11 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
node_modules
.env
coverage
coverage.json
typechain
typechain-types

# Hardhat files
cache
artifacts

10 changes: 10 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
node_modules
package.json
img
artifacts
cache
coverage
.env
.*
README.md
coverage.json
4 changes: 4 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"tabWidth": 4,
"singleQuote": false
}
7 changes: 7 additions & 0 deletions .solhint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"extends": "solhint:recommended",
"rules": {
"compiler-version": ["error", "^0.8.0"],
"func-visibility": ["warn", { "ignoreConstructors": false }]
}
}
10 changes: 10 additions & 0 deletions .solhintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
node_modules
package.json
img
artifacts
cache
coverage
.env
.*
README.md
coverage.json
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Sample Hardhat Project

This project demonstrates a basic Hardhat use case. It comes with a sample contract, a test for that contract, and a script that deploys that contract.

Try running some of the following tasks:

```shell
npx hardhat help
npx hardhat test
REPORT_GAS=true npx hardhat test
npx hardhat node
npx hardhat run scripts/deploy.js
```
63 changes: 63 additions & 0 deletions contracts/paymentEscrow.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.7;

// errors
error PaymentEscrow__NotArbiter();
error PaymentEscrow__TransactionUnsuccesful();

/**@title A Payment Escrow Contract
* @author Ejim favour
* @notice This contract ensures secure and credible payment for goods and services.
*/

contract PaymentEscrow {
// state variables
address public s_arbiter;
address public s_beneficiary;
address public s_payer;
bool public s_isApproved;

// events
event Approved(
address indexed payer,
address indexed beneficiary,
uint amount
);

// modifiers
modifier onlyArbiter() {
if (msg.sender != s_arbiter) {
revert PaymentEscrow__NotArbiter();
}
_;
}

// functions

receive() external payable {}

function pay(address _beneficiary, address _arbiter) external payable {
s_payer = msg.sender;
s_beneficiary = _beneficiary;
s_arbiter = _arbiter;
(bool callSuccess, ) = address(this).call{value: msg.value}("");

if (!callSuccess) revert PaymentEscrow__TransactionUnsuccesful();
}

function approve() external onlyArbiter {
uint balance = address(this).balance;

uint payedAmount = balance - (address(this).balance / 1000) * 5;

(bool callSuccess, ) = s_beneficiary.call{value: payedAmount}("");

if (!callSuccess) revert PaymentEscrow__TransactionUnsuccesful();

emit Approved(s_payer, s_beneficiary, payedAmount);

s_isApproved = true;
}

function revertTransaction() external {}
}
29 changes: 29 additions & 0 deletions deploy/01-deploy-payment-escrow.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
const { network, ethers } = require("hardhat");
const { verify } = require("../utils/verify");

module.exports = async ({ getNamedAccounts, deployments }) => {
const { payer, arbiter, beneficiary } = await getNamedAccounts();
const { deploy, log } = deployments;

const amount = ethers.utils.parseEther("0.4");

const args = [arbiter, beneficiary];

const paymentEscrowContract = await deploy("PaymentEscrow", {
from: payer,
log: true,
args,
waitConfirmations: network.config.blockConfirmation || 1,
value: amount,
});

log("Contract deployed");
log("-------------------------");

// verify
if (network.config.chainId === 11155111 && process.env.ETHERSCAN_API_KEY) {
await verify(paymentEscrowContract.address, args);
}
};

module.exports.tags = ["all", "paymentEscrow"];
13 changes: 13 additions & 0 deletions gas-reporter.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
·-----------------------------|----------------------------|-------------|-----------------------------·
| Solc version: 0.8.18 · Optimizer enabled: false · Runs: 200 · Block limit: 30000000 gas │
······························|····························|·············|······························
| Methods · 15 gwei/gas · 1.11 usd/matic │
··················|···········|··············|·············|·············|···············|··············
| Contract · Method · Min · Max · Avg · # calls · usd (avg) │
··················|···········|··············|·············|·············|···············|··············
| PaymentEscrow · approve · - · - · 62756 · 6 · 0.00 │
··················|···········|··············|·············|·············|···············|··············
| Deployments · · % of limit · │
······························|··············|·············|·············|···············|··············
| PaymentEscrow · - · - · 437471 · 1.5 % · 0.01 │
·-----------------------------|--------------|-------------|-------------|---------------|-------------·
53 changes: 53 additions & 0 deletions hardhat.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
require("@nomicfoundation/hardhat-toolbox");
require("@nomiclabs/hardhat-ethers");
require("hardhat-deploy");
require("solidity-coverage");
require("dotenv").config();
require("hardhat-deploy-ethers");
require("hardhat-gas-reporter");

/** @type import('hardhat/config').HardhatUserConfig */

const TEST_SEPOLIA_URL =
process.env.TEST_SEPOLIA_URL || "https:\\eth-sepolia/example";
const TEST_PRIVATE_KEY = process.env.TEST_PRIVATE_KEY || "key";
const ETHERSCAN_API_KEY = process.env.ETHERSCAN_API_KEY || "key";
const COIN_MARKET_CAP_API_KEY = process.env.COIN_MARKET_CAP_API_KEY || "key";

module.exports = {
solidity: "0.8.18",
networks: {
sepolia: {
url: TEST_SEPOLIA_URL,
accounts: [TEST_PRIVATE_KEY],
chainId: 11155111,
blockConfirmations: 6,
},
localhost: {
url: "http://127.0.0.1:8545",
chainId: 31337,
},
},
etherscan: {
api: ETHERSCAN_API_KEY,
},
gasReporter: {
enabled: true,
outputFile: "gas-reporter.txt",
noColors: true,
currency: "USD",
coinmarketcap: COIN_MARKET_CAP_API_KEY,
token: "MATIC",
},
namedAccounts: {
payer: {
default: 0,
},
arbiter: {
default: 1,
},
beneficiary: {
default: 2,
},
},
};
Loading

0 comments on commit 57c6670

Please sign in to comment.