Guest User

Untitled

a guest
Feb 4th, 2025
248
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.72 KB | None | 0 0
  1. // SPDX-License-Identifier: MIT
  2. pragma solidity ^0.8.0;
  3.  
  4. contract Escrow {
  5. address public buyer;
  6. address public seller;
  7. address public arbitrator;
  8. uint256 public depositAmount;
  9. bool public isBuyerConfirmed;
  10. bool public isSellerConfirmed;
  11. bool private locked;
  12.  
  13. uint256 public transactionFeePercent = 5; // Larger fee, 5%
  14.  
  15. enum EscrowState { NOT_INITIATED, AWAITING_CONFIRMATION, COMPLETE, DISPUTE }
  16. EscrowState public currentState;
  17.  
  18. constructor(address _seller, address _arbitrator) {
  19. buyer = msg.sender;
  20. seller = _seller;
  21. arbitrator = _arbitrator;
  22. currentState = EscrowState.NOT_INITIATED;
  23. locked = false;
  24. }
  25.  
  26. modifier onlyBuyer() {
  27. require(msg.sender == buyer, "Only the buyer can call this function.");
  28. _;
  29. }
  30.  
  31. modifier onlyArbitrator() {
  32. require(msg.sender == arbitrator, "Only the arbitrator can call this function.");
  33. _;
  34. }
  35.  
  36. modifier inState(EscrowState state) {
  37. require(currentState == state, "Invalid escrow state for this action.");
  38. _;
  39. }
  40.  
  41. modifier noReentrancy() {
  42. require(!locked, "Reentrant call detected.");
  43. locked = true;
  44. _;
  45. locked = false;
  46. }
  47.  
  48. function depositFunds() external payable onlyBuyer inState(EscrowState.NOT_INITIATED) {
  49. require(msg.value > 0, "Deposit amount must be greater than 0.");
  50. depositAmount = msg.value;
  51. currentState = EscrowState.AWAITING_CONFIRMATION;
  52. }
  53.  
  54. function confirmReceipt() external onlyBuyer inState(EscrowState.AWAITING_CONFIRMATION) {
  55. isBuyerConfirmed = true;
  56. completeTransaction();
  57. }
  58.  
  59. function releaseFunds() external inState(EscrowState.AWAITING_CONFIRMATION) noReentrancy {
  60. require(isBuyerConfirmed, "Buyer must confirm receipt before funds are released.");
  61. require(msg.sender == buyer || msg.sender == arbitrator, "Only buyer or arbitrator can release funds.");
  62. _withdraw(seller, depositAmount);
  63. currentState = EscrowState.COMPLETE;
  64. }
  65.  
  66. function raiseDispute() external onlyBuyer inState(EscrowState.AWAITING_CONFIRMATION) {
  67. require(!isBuyerConfirmed, "Cannot raise dispute after confirming receipt.");
  68. currentState = EscrowState.DISPUTE;
  69. }
  70.  
  71. function resolveDispute(bool refundBuyer) external onlyArbitrator inState(EscrowState.DISPUTE) noReentrancy {
  72. require(currentState == EscrowState.DISPUTE, "Cannot resolve dispute in the current state.");
  73. require(buyer != address(0) && seller != address(0), "Invalid parties involved.");
  74.  
  75. if (refundBuyer) {
  76. _withdraw(buyer, depositAmount);
  77. } else {
  78. _withdraw(seller, depositAmount);
  79. }
  80.  
  81. currentState = EscrowState.COMPLETE;
  82. }
  83.  
  84. function cancelEscrow() external onlyBuyer inState(EscrowState.NOT_INITIATED) noReentrancy {
  85. _withdraw(buyer, address(this).balance);
  86. currentState = EscrowState.COMPLETE;
  87. }
  88.  
  89. function getContractBalance() external view returns (uint256) {
  90. return address(this).balance;
  91. }
  92.  
  93. function _withdraw(address recipient, uint256 amount) private {
  94. require(address(this).balance >= amount, "Insufficient contract balance.");
  95. uint256 fee = (amount * transactionFeePercent) / 100;
  96. uint256 amountAfterFee = amount - fee;
  97.  
  98. require(fee > 0, "Fee must be greater than 0.");
  99. (bool feeSuccess, ) = payable(arbitrator).call{value: fee}();
  100. require(feeSuccess, "Transaction fee transfer failed.");
  101.  
  102. (bool success, ) = payable(recipient).call{value: amountAfterFee}();
  103. require(success, "Transfer failed.");
  104. }
  105. }
Advertisement
Add Comment
Please, Sign In to add comment