Developers Forum for XinFin XDC Network

Atul Khekade
Atul Khekade

Posted on • Updated on

[Informative] Smart contract for charging station reservation system for tesla cars using their developer documentation?

Here's an example for smart contract for a charging station reservation system, tailored for the XinFin XDC Network, which is suitable for managing reservations for Tesla car charging stations. This contract will be written in Solidity, compatible with the XDC Network's EVM.

Solidity

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

contract ChargingStationReservation {
    address public owner;
    uint256 public reservationFee = 0.01 ether;

    struct Reservation {
        uint256 id;
        address user;
        uint256 startTime;
        uint256 endTime;
    }

    Reservation[] public reservations;

    mapping(uint256 => bool) public occupied;
    mapping(address => uint256) public userBalances;

    event Reserved(uint256 indexed id, address indexed user, uint256 startTime, uint256 endTime);
    event Cancelled(uint256 indexed id, address indexed user);

    constructor() {
        owner = msg.sender;
    }

    modifier onlyOwner() {
        require(msg.sender == owner, "Only owner can perform this action");
        _;
    }

    function setReservationFee(uint256 _fee) external onlyOwner {
        reservationFee = _fee;
    }

    function reserve(uint256 _id, uint256 _startTime, uint256 _endTime) external payable {
        require(msg.value == reservationFee, "Incorrect reservation fee");
        require(!occupied[_id], "Slot already reserved");
        require(_startTime < _endTime, "Invalid time frame");

        reservations.push(Reservation(_id, msg.sender, _startTime, _endTime));
        occupied[_id] = true;
        userBalances[msg.sender] += msg.value;

        emit Reserved(_id, msg.sender, _startTime, _endTime);
    }

    function cancelReservation(uint256 _id) external {
        Reservation memory reservation = reservations[_id];
        require(msg.sender == reservation.user, "Only the user who reserved can cancel");
        require(block.timestamp < reservation.startTime, "Cancellation not allowed after start time");

        occupied[_id] = false;
        payable(msg.sender).transfer(reservationFee);
        emit Cancelled(_id, msg.sender);
    }

    function withdrawFunds() external onlyOwner {
        uint256 balance = address(this).balance;
        require(balance > 0, "No funds available");

        payable(owner).transfer(balance);
    }
Enter fullscreen mode Exit fullscreen mode

Details:
- Contract Ownership: Only the owner (deployer initially) can modify critical settings like the reservation fee.
- Reservations: Users can make reservations by specifying a slot ID, start time, and end time, accompanied by the reservation fee.
- Cancellations: Reservations can be cancelled before they begin.
- Fee Handling: Users pay a flat fee to reserve a slot. This fee is stored in the contract and can be withdrawn by the owner.
- Events: Events are emitted for reservations and cancellations for off-chain tracking.

This contract can be deployed on the XinFin XDC Network, leveraging its fast transaction times and low gas costs. Ensure to test thoroughly in a test environment before deploying on the main network. Adjust the reservationFee as necessary to match economic conditions.

Discussion (0)