-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEscrowContract.sol
56 lines (46 loc) · 1.74 KB
/
EscrowContract.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;
// Escrow contract
contract Escrow {
// Mapping of trade IDs to trade details
mapping(bytes32 => Trade) public trades;
// Struct to represent a trade
struct Trade {
// Address of the buyer
address payable buyer;
// Address of the seller (change type to "address payable")
address payable seller;
// Amount of ether paid by the buyer
uint256 value;
// ID of the trade
bytes32 tradeId;
// Flag to indicate if the trade is complete
bool isComplete;
}
// Function to create a new trade
function createTrade(bytes32 _tradeId, address payable _buyer, address payable _seller, uint256 _value) public payable {
// Check if the caller is the seller
if (msg.sender != _seller) revert("Only the seller can create a trade");
// Check the contract's balance
if (address(this).balance < _value) revert("Insufficient funds");
// Check the seller's address
// Create a new trade with the specified details
Trade memory trade = Trade(_buyer, _seller, _value, _tradeId, false);
// Store the trade in the mapping
trades[_tradeId] = trade;
}
// Function to release the payment to the seller
function releasePayment(bytes32 _tradeId) public {
// Get the trade details from storage
Trade storage trade = trades[_tradeId];
// Check if the trade is complete
if (trade.isComplete) revert("Trade already complete");
// Check if the caller is the seller
if (msg.sender != trade.seller) revert("Only the seller can release payment");
// Transfer the payment to the buyer
bool success = trade.buyer.send(trade.value);
if (!success) revert("Transfer failed");
// Mark the trade as complete
trade.isComplete = true;
}
}