enable 2 users to use my contract at the same time with different states in Solidity

Viewed 40

I'm new in solidity, and I was trying to create a simple purchase contract between 2 users, with different states as below:

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;

contract purchase {

    uint public value;
    struct Users {
        address payable seller;
        address payable buyer;
    }
   Users public users;
    uint256 public contractID;

    enum State{Created, Locked, Release, Inactive}
    State public state;

    constructor() payable {
        users.seller = payable(msg.sender);
        value = msg.value / 2;
    }
    
    ///The function can't be called at the current state.
    error InvalidState();
    ///Only buyer/buyer can call this function
    error Onlybuyer();
    ///Only Seller can call this function
    error OnlySeller();

    modifier inState(State state_){
        if (state != state_) {
            revert InvalidState();
        }
        _;
    }

    modifier onlybuyer(){
        if (msg.sender != users.buyer) {
            revert Onlybuyer();
        }
        _;
    }

     modifier onlySeller(){
        if (msg.sender != users.seller) {
            revert OnlySeller();
        }
        _;
    }

    mapping(uint => Users) public contractUsers;
    mapping(uint => State) public contractState;

    function confirmPurchase() external inState(State.Created) payable {
        contractID ++;
        require(msg.value == (2*value), "Please send in 2X the purchase amount");
        users.buyer = payable(msg.sender);
        state = State.Locked;
    }

    function confirmRecieved() external onlybuyer inState(State.Locked) {
        state = State.Release;
        users.buyer.transfer(value);
    }

    function paySeller() external onlySeller inState(State.Release){
        state = State.Inactive;
        users.seller.transfer(3 * value);
    }

    function abort() external onlySeller inState(State.Created){
        state = State.Inactive;
        users.seller.transfer(address(this).balance);
    }
}

I would like to ask, how I can enable 2 sellers to use the contract at the same time with different states? or how I enable many users to use my contract at the same time?

for example:let's say we have seller 1, buyer 1 with price 2 ETH at state 1. at the same time seller 2, buyer 2 with price 3 ETH at state 0. each one of them is using the contract at the same time and they can view their contract details using the contract ID. how I can do that?

I thought about creating a function to set the state to initial state 0,then a new user can use the contract, and retrieve their data from the mapping using contract ID. but I'm not sure if this is the best practice. Can anyone advice please!

Thank you in advance.

0 Answers
Related