Advertisement
Mika_Isaak

Untitled

Feb 18th, 2025 (edited)
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
JavaScript 6.84 KB | Cryptocurrency | 0 0
  1. import "@stdlib/deploy";
  2. import "./messages";
  3. message(0xd53276db) Excesses {
  4. }
  5.  
  6. contract DealJetton with Deployable {
  7.     id: String; // Unique identifier for the deal
  8.     amount: Int; // Total value of the deal in TON
  9.     admin: Address; // Administrator's address who manages the deal
  10.     customer: Address; // Customer's address who created the deal
  11.     freelancer: Address; // Freelancer's address who will fulfill the job
  12.     isActive: Bool; // Boolean flag indicating if the deal is active
  13.     isFinished: Bool; // Boolean flag indicating if the deal is finished
  14.     jettonWalletAddress: Address = newAddress(0, 0);
  15.     /**
  16.     @dev Constructor to initialize the deal contract.
  17.     @param dealId Unique identifier for the deal.
  18.     @param admin The address of the administrator responsible for managing the deal.
  19.     @param amount The total value of the deal in TON.
  20.     @param customer The address of the customer who created the deal.
  21.     @param freelancer The address of the freelancer performing the job in the deal.
  22.     **/
  23.     init(id: String, amount: Int, admin: Address, customer: Address, freelancer: Address){
  24.         require(admin != newAddress(0, 0), "Incorrect admin address"); // Check if the admin address is valid
  25.         require(customer != newAddress(0, 0), "Incorrect customer address"); // Check if the customer address is valid
  26.         require(freelancer != newAddress(0, 0), "Incorrect freelancer address"); // Check if the freelancer address is valid
  27.         require(amount != 0, "Incorrect amount"); // Ensure that the amount is not zero
  28.  
  29.         self.id = id; // Set the deal ID
  30.         self.amount = amount; // Set the deal amount
  31.         self.admin = admin; // Set the administrator's address
  32.         self.customer = customer; // Set the customer's address
  33.         self.freelancer = freelancer; // Set the freelancer's address
  34.         self.isActive = false; // Mark the deal as inactive initially
  35.         self.isFinished = false; // Mark the deal as unfinished initially
  36.     }
  37.  
  38.     receive(msg: InitDataMessage){
  39.         self.jettonWalletAddress = msg.jettonWalletAddress;
  40.     }
  41.  
  42.     receive(msg: Excesses){}
  43.  
  44.     /**
  45.     @dev Notification message receiver.
  46.     **/
  47.     receive(msg: JettonTransferNotification){
  48.         require(self.isActive == false, "Deal is already laucnhed");
  49.         require(self.isFinished == false, "Deal is already finished");
  50.         require(sender() == self.jettonWalletAddress, "Incorrect sender");
  51.         require(msg.amount >= self.amount, "Incorrect amount");
  52.         self.isActive = true;
  53.     }
  54.  
  55.     /**
  56.     @dev Marks the deal as finished by the customer and transfers the remaining balance to the freelancer.
  57.     @param msg The message indicating the deal should be completed.
  58.     **/
  59.     receive(msg: FinishDeal){
  60.         require(context().sender == self.customer, "Incorrect sender"); // Ensure the sender is the customer
  61.         require(self.isActive == true, "Deal isn't active"); // Ensure the deal is active
  62.         require(context().value >= ton("0.05"), "Message value is too low"); // Ensure the message value is at least 0.15 TON
  63.  
  64.         self.isActive = false; // Mark the deal as inactive
  65.         self.isFinished = true; // Mark the deal as finished
  66.  
  67.         send(SendParameters{
  68.                 to: self.freelancer,
  69.                 value: 0,
  70.                 mode: SendRemainingValue,
  71.                 body: JettonTransfer{
  72.                     queryId: 1,
  73.                     amount: self.amount, // jetton amount you want to transfer
  74.                     destination: self.freelancer, // address you want to transfer jettons. Note that this is address of jetton wallet owner, not jetton wallet itself
  75.                     responseDestination: self.customer, //  address where to send a response with confirmation of a successful transfer and the rest of the incoming message Toncoins
  76.                     customPayload: null, // in most cases will be null and can be omitted. Needed for custom logic on Jetton Wallets itself
  77.                     forwardTonAmount: 1, // amount that will be transferred with JettonTransferNotification. Needed for custom logic execution like in example below. If the amount is 0 notification won't be sent
  78.                     forwardPayload: rawSlice("F" // precomputed beginCell().storeUint(0xF, 4).endCell().beginParse(). This works for simple transfer, if needed any struct can be used as `forwardPayload`
  79.                     )
  80.                 }.toCell()
  81.             }
  82.         );
  83.     }
  84.  
  85.     /**
  86.     @dev Allows the admin to forcibly finish the deal and transfer the remaining balance to either the customer or freelancer.
  87.     @param msg The message containing the deal information and the receiver address.
  88.     **/
  89.     receive(msg: AdminForceEnd){
  90.         require(context().sender == self.admin, "Non-admin address can't finish this deal"); // Check if the sender is the admin
  91.         require(msg.receiver == self.customer || msg.receiver == self.freelancer, "Incorrect receiver address"); // Validate the receiver address
  92.         require(self.isActive == true, "Deal isn't active"); // Ensure the deal is active
  93.  
  94.         self.isActive = false; // Mark the deal as inactive
  95.         self.isFinished = true; // Mark the deal as finished
  96.         send(SendParameters{to: msg.receiver, bounce: true, value: 0, mode: SendRemainingBalance + SendIgnoreErrors}); // Transfer the remaining balance to the specified receiver
  97.     }
  98.  
  99.     /**
  100.     @dev Getter function for deal ID.
  101.     @return The deal ID.
  102.     **/
  103.     get fun id(): String {
  104.         return self.id;
  105.     }
  106.  
  107.     /**
  108.     @dev Getter function for deal amount.
  109.     @return The total value of the deal.
  110.     **/
  111.  
  112.     get fun amount(): Int {
  113.         return self.amount;
  114.     }
  115.  
  116.     /**
  117.     @dev Getter function for admin address.
  118.     @return The admin's address.
  119.     **/
  120.  
  121.     get fun admin(): Address {
  122.         return self.admin;
  123.     }
  124.  
  125.     /**
  126.     @dev Getter function for customer address.
  127.     @return The customer's address.
  128.     **/
  129.  
  130.     get fun customer(): Address {
  131.         return self.customer;
  132.     }
  133.  
  134.     /**
  135.     @dev Getter function for freelancer address.
  136.     @return The freelancer's address.
  137.     **/
  138.  
  139.     get fun freelancer(): Address {
  140.         return self.freelancer;
  141.     }
  142.  
  143.     /**
  144.     @dev Getter function for the deal's active status.
  145.     @return True if the deal is active, false otherwise.
  146.     **/
  147.  
  148.     get fun isActive(): Bool {
  149.         return self.isActive;
  150.     }
  151.  
  152.     get fun isFinished(): Bool {
  153.         return self.isFinished;
  154.     }
  155.  
  156.     /**
  157.     @dev Getter function for the contract's balance.
  158.     @return The current balance of the contract.
  159.     **/
  160.  
  161.     get fun contractBalance(): Int {
  162.         return myBalance(); // Return the current balance of the contract
  163.     }
  164.  
  165.     get fun jettonWalletAddress(): Address {
  166.         return self.jettonWalletAddress;
  167.     }
  168. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement