Sony30 / BlockCart_Site

0 stars 0 forks source link

The contract you use is incorect try this , this can handle multiple order ,try to look deeply before move to presentation , I just quicky fix the contract not fully tested #1

Open Chandu8817 opened 3 months ago

Chandu8817 commented 3 months ago
//SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

contract Ecommerce {
    enum State {
        AWAITING_BUYER_PAYMENT,
        AWAITING_DELIVERY,
        AWAITING_FUND_RELEASE,
        CANCELD,
        COMPLETE
    } //state of the smart contract
    struct item {
        string b_name;
        uint256 no_units;
        uint256 total;
        uint256 price_per_unit;
        address payable buyer;
        address payable seller;
        State current;
        uint256 date;
    }

    mapping(address => item) public product;

    uint256 balance; //escrow balance amount
    uint256 start; //starting time
    uint256 end; //ending time

    event notify(string notification); //notification to the sender

    bool public buyerOK;
    bool public sellerOK;

    function b_Product_details(
        string memory _b_name,
        uint256 units,
        address  payable _seller,
        uint256 p_p_u
    ) public payable {
        item memory _product = product[msg.sender];
        _product.b_name = _b_name;
        _product.no_units = units;
        _product.buyer = payable(msg.sender);
        _product.seller = _seller;
        _product.date = block.timestamp;
        _product.price_per_unit = p_p_u;
        _product.total = p_p_u * units; //total amount to be paid
        _product.current = State.AWAITING_DELIVERY;

        require(msg.sender != _seller, "Can only be accessed by the buyer");

        require(
            msg.value >= _product.total,
            "Entered amount is less that required amount..."
        );
        balance = msg.value;
        start = block.timestamp;

        emit notify(
            "Buyer has deposited the required amount in the Escrow account"
        );
    }

    function escrow_order_balance(address _buyer)
        public
        view
        returns (
            uint256 //returns the balance of the contract
        )
    {
        return product[_buyer].total;
    }

    function seller_deny_service(address _buyer) public {
        require(
            msg.sender == product[_buyer].seller,
            "You cannot hack my contract..."
        );
        require(product[_buyer].current == State.AWAITING_DELIVERY);
        product[_buyer].buyer.transfer(address(this).balance);
        product[_buyer].current = State.CANCELD;
    }

    function seller_send_product(address _buyer) public {
        require(
            msg.sender == product[_buyer].seller,
            "Can be accessed only by sender"
        );
        require(product[_buyer].current == State.AWAITING_DELIVERY);

        sellerOK = true;
    }

    function b_delivery_received() public  {
        require(product[msg.sender].current == State.AWAITING_DELIVERY);
        buyerOK = true;
        product[msg.sender].current = State.AWAITING_FUND_RELEASE;
    }

    function release_fund(address _buyer) public {
        require(
            product[_buyer].current == State.AWAITING_FUND_RELEASE,
            "buyer not confirm yet"
        );
        if (buyerOK && sellerOK)
            product[_buyer].seller.transfer((product[_buyer].total));

        product[_buyer].current = State.COMPLETE;
    }

    function withdraw_amount() public {
        end = block.timestamp;
        require(product[msg.sender].current == State.AWAITING_DELIVERY);

        if (
            !sellerOK && end < start + 172800
        ) //time exceeds 30 days after the buyer has deposited in the escrow contract
        {
            product[msg.sender].buyer.transfer(product[msg.sender].total);
        }
        product[msg.sender].current = State.CANCELD;
        delete product[msg.sender];
    }
}
Chandu8817 commented 3 months ago

remove storage variable
bool public buyerOK; bool public sellerOK; put this inside the struct bool buyerOK; bool sellerOK; and change accordingly in all functions