Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Node modules
/node_modules

# Compilation output
/dist

# pnpm deploy output
/bundle

# Hardhat Build Artifacts
/artifacts

# Hardhat compilation (v2) support directory
/cache

# Typechain output
/types

# Hardhat coverage reports
/coverage
57 changes: 57 additions & 0 deletions assignments/savage_assignment/Solidity_Assignment/escrow/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# Sample Hardhat 3 Beta Project (`mocha` and `ethers`)

This project showcases a Hardhat 3 Beta project using `mocha` for tests and the `ethers` library for Ethereum interactions.

To learn more about the Hardhat 3 Beta, please visit the [Getting Started guide](https://hardhat.org/docs/getting-started#getting-started-with-hardhat-3). To share your feedback, join our [Hardhat 3 Beta](https://hardhat.org/hardhat3-beta-telegram-group) Telegram group or [open an issue](https://github.com/NomicFoundation/hardhat/issues/new) in our GitHub issue tracker.

## Project Overview

This example project includes:

- A simple Hardhat configuration file.
- Foundry-compatible Solidity unit tests.
- TypeScript integration tests using `mocha` and ethers.js
- Examples demonstrating how to connect to different types of networks, including locally simulating OP mainnet.

## Usage

### Running Tests

To run all the tests in the project, execute the following command:

```shell
npx hardhat test
```

You can also selectively run the Solidity or `mocha` tests:

```shell
npx hardhat test solidity
npx hardhat test mocha
```

### Make a deployment to Sepolia

This project includes an example Ignition module to deploy the contract. You can deploy this module to a locally simulated chain or to Sepolia.

To run the deployment to a local chain:

```shell
npx hardhat ignition deploy ignition/modules/Counter.ts
```

To run the deployment to Sepolia, you need an account with funds to send the transaction. The provided Hardhat configuration includes a Configuration Variable called `SEPOLIA_PRIVATE_KEY`, which you can use to set the private key of the account you want to use.

You can set the `SEPOLIA_PRIVATE_KEY` variable using the `hardhat-keystore` plugin or by setting it as an environment variable.

To set the `SEPOLIA_PRIVATE_KEY` config variable using `hardhat-keystore`:

```shell
npx hardhat keystore set SEPOLIA_PRIVATE_KEY
```

After setting the variable, you can run the deployment with the Sepolia network:

```shell
npx hardhat ignition deploy --network sepolia ignition/modules/Counter.ts
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.28;

contract Counter {
uint public x;

event Increment(uint by);

function inc() public {
x++;
emit Increment(1);
}

function incBy(uint by) public {
require(by > 0, "incBy: increment should be positive");
x += by;
emit Increment(by);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.28;

import {Counter} from "./Counter.sol";
import {Test} from "forge-std/Test.sol";

// Solidity tests are compatible with foundry, so they
// use the same syntax and offer the same functionality.

contract CounterTest is Test {
Counter counter;

function setUp() public {
counter = new Counter();
}

function test_InitialValue() public view {
require(counter.x() == 0, "Initial value should be 0");
}

function testFuzz_Inc(uint8 x) public {
for (uint8 i = 0; i < x; i++) {
counter.inc();
}
require(counter.x() == x, "Value after calling inc x times should be x");
}

function test_IncByZero() public {
vm.expectRevert();
counter.incBy(0);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.28;

contract EscrowSystem {

// Define State
enum SystemStatus {
AWAITING_DEPOSIT,
ALREADY_DEPOSITED,
ALREADY_DELIVERED,
COMPLETED
}

struct Transactions {
address buyer;
address seller;
uint amount;
SystemStatus status;
bool exists;
}

// Create Storage Sysytem: Building a system where each transaction has a unique number, and the number let's you check your transaction in storage.
mapping (uint => Transactions) public txStorage;


// Creating a counter: Tracking how many transactions gave been created so each gets a unique ID
uint public txCounter;


// This function creates a new transaction when ever it is called.
function newTransactions(address _buyer, address _seller, uint _amount) public returns (uint) {
txCounter++; // This creates new IDs for transactions
txStorage[txCounter] = Transactions({ // Store new transactions at that ID
buyer: _buyer,
seller: _seller,
amount: _amount,
status: SystemStatus.AWAITING_DEPOSIT,
exists: true

});


return txCounter; // Return the ID that was used
}


// this function is recieves the payment from the buyer.
function deposit(uint _id) public payable {
// this line is going into mapping/storage, to get a transaction and store it within storedTx.
// This helps us to get every data within the transaction struct by using the variable storedTx.
Transactions storage storedTx = txStorage[_id];
require(_id > 0 && _id <= txCounter, "Invalid ID");
require(storedTx.exists, " Transaction doesn't exist");
require(msg.sender == storedTx.buyer, "Only buyer can make deposit");
require(msg.value == storedTx.amount, "Wrong amount sent");
require(storedTx.status == SystemStatus.AWAITING_DEPOSIT, "Already Deposited");

// changing status to already deposited. for the buyer has already sent the money.
storedTx.status = SystemStatus.ALREADY_DEPOSITED;
}


// this function is to confrim that the delievery has been made
function confirmDelivery(uint _id) public {
Transactions storage storedTx = txStorage[_id];
require(_id > 0 && _id <= txCounter, "Invalid ID");
require(storedTx.exists, "Transaction doesn't exist");
require(msg.sender == storedTx.buyer, "Only buyer can confirrm delivery");
require(storedTx.status == SystemStatus.ALREADY_DEPOSITED, "Awaiting deposit");

storedTx.status = SystemStatus.ALREADY_DELIVERED;
}


//

function releaseFunds(uint _id) public{
Transactions storage storedTx = txStorage[_id];
require(_id > 0 && _id <= txCounter, "Invalid ID");
require(storedTx.exists, "Transaction doesn't exist");
require(storedTx.status == SystemStatus.ALREADY_DELIVERED, "Awaiting delievery");
require(msg.sender == storedTx.seller, "Only seller can receive payment");

// Change state before sending money
storedTx.status = SystemStatus.COMPLETED;

// Seller recieves payment
(bool success,) = storedTx.seller.call{value: storedTx.amount}("");
require(success, "Transafer failed");

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import "dotenv/config";
import hardhatToolboxMochaEthersPlugin from "@nomicfoundation/hardhat-toolbox-mocha-ethers";
import { configVariable, defineConfig } from "hardhat/config";

export default defineConfig({
plugins: [hardhatToolboxMochaEthersPlugin],
solidity: {
profiles: {
default: {
version: "0.8.28",
},
production: {
version: "0.8.28",
settings: {
optimizer: {
enabled: true,
runs: 200,
},
},
},
},
},
networks: {
hardhatMainnet: {
type: "edr-simulated",
chainType: "l1",
},
hardhatOp: {
type: "edr-simulated",
chainType: "op",
},
sepolia: {
type: "http",
chainType: "l1",
url: configVariable("SEPOLIA_RPC_URL"),
accounts: [configVariable("SEPOLIA_PRIVATE_KEY")],
},
},
verify: {
etherscan: {
apiKey: configVariable("ETHERSCAN_API_KEY"),
},
},
});
Loading