Mika_Isaak

Untitled

Feb 18th, 2025
23
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
JavaScript 5.65 KB | Cryptocurrency | 0 0
  1. import "@stdlib/deploy";
  2. import "./messages";
  3.  
  4. contract DealContract with Deployable {
  5.     id: String; // Unique identifier for the deal
  6.     amount: Int as coins; // Total value of the deal in TON
  7.     admin: Address; // Administrator's address who manages the deal
  8.     customer: Address; // Customer's address who created the deal
  9.     freelancer: Address; // Freelancer's address who will fulfill the job
  10.     isActive: Bool; // Boolean flag indicating if the deal is active
  11.     isFinished: Bool; // Boolean flag indicating if the deal is finished
  12.  
  13.     /**
  14.     @dev Constructor to initialize the deal contract.
  15.     @param dealId Unique identifier for the deal.
  16.     @param admin The address of the administrator responsible for managing the deal.
  17.     @param amount The total value of the deal in TON.
  18.     @param customer The address of the customer who created the deal.
  19.     @param freelancer The address of the freelancer performing the job in the deal.
  20.     **/
  21.     init(id: String, amount: Int, admin: Address, customer: Address, freelancer: Address){
  22.         require(admin != newAddress(0, 0), "Incorrect admin address"); // Check if the admin address is valid
  23.         require(customer != newAddress(0, 0), "Incorrect customer address"); // Check if the customer address is valid
  24.         require(freelancer != newAddress(0, 0), "Incorrect freelancer address"); // Check if the freelancer address is valid
  25.         require(amount != 0, "Incorrect amount"); // Ensure that the amount is not zero
  26.  
  27.         self.id = id; // Set the deal ID
  28.         self.amount = amount; // Set the deal amount
  29.         self.admin = admin; // Set the administrator's address
  30.         self.customer = customer; // Set the customer's address
  31.         self.freelancer = freelancer; // Set the freelancer's address
  32.         self.isActive = false; // Mark the deal as inactive initially
  33.         self.isFinished = false; // Mark the deal as unfinished initially
  34.     }
  35.  
  36.     /**
  37.     @dev Handles deposits from the customer and activates the deal if the conditions are met.
  38.     **/
  39.     receive(msg: Deposit){
  40.         require(context().sender == self.customer, "Incorrect sender"); // Ensure the sender is the customer
  41.         require(self.isActive == false, "Deal is already active"); // Ensure the deal isn't already active
  42.         require(self.isFinished == false, "Deal is already finished"); // Ensure the deal isn't already finished
  43.         require(context().value >= self.amount, "Incorrect value of deal"); // Ensure the deposit matches or exceeds the deal value
  44.  
  45.         self.isActive = true;
  46.     }
  47.  
  48.     /**
  49.     @dev Marks the deal as finished by the customer and transfers the remaining balance to the freelancer.
  50.     @param msg The message indicating the deal should be completed.
  51.     **/
  52.     receive(msg: FinishDeal){
  53.         require(context().sender == self.customer, "Incorrect sender"); // Ensure the sender is the customer
  54.         require(self.isActive == true, "Deal isn't active"); // Ensure the deal is active
  55.         require(self.isFinished == false, "Deal is already finished");
  56.         self.isActive = false; // Mark the deal as inactive
  57.         self.isFinished = true; // Mark the deal as finished
  58.         send(SendParameters{to: self.freelancer, bounce: false, value: 0, mode: SendRemainingBalance + SendIgnoreErrors}
  59.         );
  60.     }
  61.  
  62.     /**
  63.     @dev Allows the admin to forcibly finish the deal and transfer the remaining balance to either the customer or freelancer.
  64.     @param msg The message containing the deal information and the receiver address.
  65.     **/
  66.     receive(msg: AdminForceEnd){
  67.         require(context().sender == self.admin, "Non-admin address can't finish this deal"); // Check if the sender is the admin
  68.         require(msg.receiver == self.customer || msg.receiver == self.freelancer, "Incorrect receiver address"); // Validate the receiver address
  69.         require(self.isActive == true, "Deal isn't active"); // Ensure the deal is active
  70.         require(self.isFinished == false, "Deal is already finished");
  71.         self.isActive = false; // Mark the deal as inactive
  72.         self.isFinished = true; // Mark the deal as finished
  73.         send(SendParameters{to: msg.receiver, bounce: false, value: 0, mode: SendRemainingBalance + SendIgnoreErrors}); // Transfer the remaining balance to the specified receiver
  74.     }
  75.  
  76.     /**
  77.     @dev Getter function for deal ID.
  78.     @return The deal ID.
  79.     **/
  80.     get fun id(): String {
  81.         return self.id;
  82.     }
  83.  
  84.     /**
  85.     @dev Getter function for deal amount.
  86.     @return The total value of the deal.
  87.     **/
  88.  
  89.     get fun amount(): Int {
  90.         return self.amount;
  91.     }
  92.  
  93.     /**
  94.     @dev Getter function for admin address.
  95.     @return The admin's address.
  96.     **/
  97.  
  98.     get fun admin(): Address {
  99.         return self.admin;
  100.     }
  101.  
  102.     /**
  103.     @dev Getter function for customer address.
  104.     @return The customer's address.
  105.     **/
  106.  
  107.     get fun customer(): Address {
  108.         return self.customer;
  109.     }
  110.  
  111.     /**
  112.     @dev Getter function for freelancer address.
  113.     @return The freelancer's address.
  114.     **/
  115.  
  116.     get fun freelancer(): Address {
  117.         return self.freelancer;
  118.     }
  119.  
  120.     /**
  121.     @dev Getter function for the deal's active status.
  122.     @return True if the deal is active, false otherwise.
  123.     **/
  124.  
  125.     get fun isActive(): Bool {
  126.         return self.isActive;
  127.     }
  128.  
  129.     get fun isFinished(): Bool {
  130.         return self.isFinished;
  131.     }
  132.  
  133.     /**
  134.     @dev Getter function for the contract's balance.
  135.     @return The current balance of the contract.
  136.     **/
  137.  
  138.     get fun contractBalance(): Int {
  139.         return myBalance(); // Return the current balance of the contract
  140.     }
  141. }
Add Comment
Please, Sign In to add comment