Skip to content

MasterPrem001/Forward_smart_contract

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

🧾 Forward Smart Contract

📖 Overview

ForwardContract.sol is a decentralized forward trading contract written in Solidity. It enables a buyer and seller to enter a forward agreement to exchange ETH for USDC at a future date for a fixed price.

This smart contract ensures:

  • Secure escrow of ETH and USDC between both parties
  • Mutual rejection before settlement date
  • Automatic settlement after expiry
  • Strict access control for buyer and seller actions

👨‍💻 Author

MasterPrem 📜 SPDX-License-Identifier: MIT 💡 Solidity Version: ^0.8.18


⚙️ Features

  • 🧱 Forward Contract Setup: Define buyer, seller, strike price, ETH amount, and settlement date
  • 💰 Deposit System: Seller deposits ETH, Buyer deposits USDC (ERC20 token)
  • Timed Settlement: Automatically settles after the specified settlement date
  • Mutual Rejection: Both parties can cancel before settlement date
  • 🔒 Access Control: Only involved parties can interact with trade functions
  • 🧮 Detailed Getters: View contract details, balances, and contract state

🧩 Smart Contract Architecture

Key Functions:

Function Description
setUp() Initializes the forward contract between buyer and seller
depositEther() Allows seller to deposit ETH
depositUSDC() Allows buyer to deposit USDC tokens
executeForward() Executes trade once settlement date is reached
rejectContract() Allows mutual rejection before settlement date
getContractDetails() Returns buyer, seller, strike price, and ETH amount
getUSDCBalance() Returns contract’s current USDC balance
getForwardContractState() Returns the contract status (Ongoing / Closed)

🧠 Contract Workflow

flowchart TD
A[Set Up Contract] --> B[Seller Deposits ETH]
B --> C[Buyer Deposits USDC]
C --> H[Before Settlement Date]
C --> D[Wait Until Settlement Date]
D -->|If Both Agree| E[Execute Forward - Trade Happens]
H -->|If both Rejects| F[Reject Contract - Funds Returned]
E --> G[Contract Closed]
F --> G
Loading

🧰 Tech Stack

  • Solidity: Smart Contract Language
  • Hardhat / Foundry: For testing and deployment
  • OpenZeppelin (optional): For ERC20 token interface
  • FakeUSDC Token (Test ERC20): Simulated USDC token

🚀 Getting Started

1️⃣ Prerequisites

Ensure you have the following installed:

  • Node.js (>= 16.x)
  • npm or yarn
  • Hardhat or Foundry
  • MetaMask (for interacting on testnet)
  • FakeUSDC token contract deployed for testing

2️⃣ Clone the Repository

git clone https://github.com/<your-username>/ForwardSmartContract.git
cd ForwardSmartContract

3️⃣ Install Dependencies

If using Hardhat:

npm install --save-dev hardhat @nomicfoundation/hardhat-toolbox

If using Foundry:

forge init

4️⃣ Compile the Contract

For Hardhat:

npx hardhat compile

For Foundry:

forge build

5️⃣ Deploy the Contract

Edit the deployment script (deploy.js or DeployForward.s.sol) with your constructor parameters, then run:

For Hardhat (using Sepolia):

npx hardhat run scripts/deploy.js --network sepolia

For Foundry:

forge script script/DeployForward.s.sol --rpc-url $SEPOLIA_RPC_URL --private-key $PRIVATE_KEY --broadcast

6️⃣ Run Tests

If you have test cases (e.g., ForwardContractTest.t.sol), run:

forge test

or with Hardhat:

npx hardhat test

Perfect 👍 — here’s your Remix-friendly version of the Forward Contract setup and execution steps — rewritten clearly so you can copy and follow them directly in Remix IDE without confusion. Each step includes exact actions, function calls, and example input values for a smooth walkthrough.


💵 Steps to Run the Forward Contract


🧾 Step 1: Initialize the Contract (setUp())

After deploying both contracts:

  1. Copy the address of:

    • The deployed FakeUSDC token (e.g., 0x...FakeUSDC)
    • The buyer (use another Remix account)
    • The seller (use your first Remix account)
  2. Select the deployed ForwardContract in Remix.

  3. In the setUp function, enter the parameters:

_buyer:        0xBuyerAddress
_seller:       0xSellerAddress
_usdcToken:    0xFakeUSDCAddress
_strikePrice:  200000000    // 200 * 1e6 (for USDC with 6 decimals)
_ethAmount_in_wei: 10000000000000000000   // 1 ETH = 1e18 wei
_settlementDate:  <current timestamp + 600>  // e.g., 10 mins later for testing 
this is [UNIX Timestamp](https://www.unixtimestamp.com/)

📘 Example:

setUp(
  0x5B38Da6a701c568545dCfcB03FcB875f56beddC4,
  0xAb8483F64d9C6d1EcF9b849Ae677dD3315835cb2,
  0x8A791620dd6260079BF849Dc5567aDC3F2FdC318,
  2000000000,
  1000000000000000000,
  1728660000
)

✅ This sets up the forward agreement between buyer and seller.


🪙 Step 2: Seller Deposits ETH (depositEther())

Now, from the seller’s account (the one you used in setUp):

  1. Switch Remix account to the seller.
  2. Select the depositEther function.
  3. In the Value (ETH) field (top of Remix panel), enter 10 (to send 10 ETH).
  4. Click transact.

✅ Expected result:

  • Contract now holds 10 ETH.
  • Seller’s deposit recorded successfully.

⚠️ If you enter the wrong ETH amount → transaction will revert with ForwardContract_IncorrectEthAmount().


💵 Step 3: Buyer Approves and Deposits USDC

Now, switch to the buyer’s account in Remix.

  1. Open the deployed FakeUSDC contract.
  2. Call the approve() function:
spender:  <ForwardContract address>
amount:   200000000   // 200 * 1e6 (for 6-decimal USDC)
  1. Click transact to allow the ForwardContract to spend the buyer’s USDC.

✅ Once approval succeeds, open the ForwardContract again and call:

depositUSDC()

✅ Expected result:

  • Contract now holds 200 USDC.
  • Buyer’s payment is locked in escrow.

⚠️ If transfer fails → ForwardContract_TransferFailed() error.


⏳ Step 4: Wait Until Settlement Date

Both ETH and USDC are now locked inside the contract.

You can check:

  • getUSDCBalance() → should show 200000000
  • getContractDetails() → confirm buyer/seller/amounts

⏱ Wait until the settlement date (timestamp) is reached. In Remix VM, you can simulate this by:

  • Clicking the "Increase Time" button under Remix Debugger > Time Travel plugin (optional), or
  • Simply redeploy with a small wait time (e.g., +2 minutes).

⚠️ If you call executeForward() before the time → ForwardContract_SettlementDateHasYetToArrive() error.


🔁 Step 5: Execute Forward Trade (executeForward())

Once the settlement date is reached:

  1. Either buyer or seller can call:

    executeForward()
    
  2. Click transact.

✅ Expected result:

  • 200 USDC sent → Seller
  • 10 ETH sent → Buyer
  • Contract marked as Closed

⚠️ If called twice → ForwardContract_ContractIsSettled() error.


❌ Step 6: Reject Contract Before Settlement (rejectContract())

If both parties want to cancel the trade before settlement date:

  1. Buyer calls:

    rejectContract()
    
  2. Then Seller calls:

    rejectContract()
    

✅ Once both have called:

  • ETH returned to Seller
  • USDC returned to Buyer
  • Contract marked as Closed

⚠️ If rejected after expiry → ForwardContract_CannotRejectAfterSettlementDate() error.


🧪 Quick Recap of Remix Flow

Step Role Function Remix Action
1 Deployer setUp() Enter all parameters
2 Seller depositEther() Send 10 ETH as value
3 Buyer approve() in FakeUSDC Allow spending by contract
4 Buyer depositUSDC() Deposit 200 USDC
5 Either executeForward() After settlement date
6 Buyer + Seller rejectContract() Both reject before expiry

🧾 Example Deployment Parameters

setUp(
    0xBuyerAddress,
    0xSellerAddress,
    0xFakeUSDCAddress,
    200 * 1e6,     // Strike price in USDC (2,000 USDC)
    10 ether,        // ETH Amount
    block.timestamp + 7 days // Settlement date
);

🔐 Security Considerations

  • Contract supports mutual rejection only before settlement date.
  • Only buyer and seller can interact with deposit and settlement functions.
  • Funds are held securely in escrow until settlement or rejection.

📊 Future Enhancements

  • ⛓️ Chainlink Oracle integration for real ETH/USD prices
  • 🧠 Automated settlement via off-chain bot
  • 🪙 Support for multiple ERC20 stablecoins
  • 📅 Partial settlements or rolling forward contracts

🏁 License

This project is licensed under the MIT License. See the LICENSE file for details.


🌐 Connect

💼 Author: @MasterPrem 📧 Email: masterprem001@egmail.com 🔗 GitHub Repo: Forward Smart Contract


Would you like me to:

  • Add a FakeUSDC.sol mock token section in the README (for testing)?
  • Or make this README GitHub-styled with emojis, code formatting, and a deployment guide using Hardhat commands and example scripts?

About

a forward smart contract made to exchange eth with fakeusdc token

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors