Skip to content

Commit b0cf794

Browse files
committed
registry: emit index/update from escrow
1 parent 40c7cc1 commit b0cf794

3 files changed

Lines changed: 90 additions & 0 deletions

File tree

src/escrow/SafeBaseEscrowV1.sol

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,18 @@ import {PausableUpgradeable} from "@openzeppelin/contracts-upgradeable/utils/Pau
88
import {Treasury} from "../Treasury.sol";
99
import {IERC20} from "../IERC20.sol";
1010

11+
interface IRegistry {
12+
function indexEscrow(
13+
uint256 escrowId,
14+
address buyer,
15+
address seller,
16+
uint256 amount,
17+
uint256 createdAt
18+
) external;
19+
20+
function updateEscrowState(uint256 escrowId, uint8 state) external;
21+
}
22+
1123
interface IRulesEngine {
1224
function canRelease(
1325
uint256 ruleSetId,
@@ -143,6 +155,10 @@ contract SafeBaseEscrowV1 is
143155

144156
emit EscrowCreated(escrowId, msg.sender, _seller, _token, _amount, _deadline);
145157

158+
if (registry != address(0)) {
159+
IRegistry(registry).indexEscrow(escrowId, msg.sender, _seller, _amount, block.timestamp);
160+
}
161+
146162
return escrowId;
147163
}
148164

@@ -162,6 +178,7 @@ contract SafeBaseEscrowV1 is
162178
}
163179

164180
escrow.state = EscrowState.Funded;
181+
_updateRegistryState(_escrowId);
165182
emit EscrowFunded(_escrowId, escrow.amount);
166183
}
167184

@@ -173,6 +190,7 @@ contract SafeBaseEscrowV1 is
173190
escrow.paymentId = _paymentId;
174191
paymentIdToEscrow[_paymentId] = _escrowId;
175192

193+
_updateRegistryState(_escrowId);
176194
emit EscrowFunded(_escrowId, escrow.amount);
177195
emit BasePayFundingReceived(_escrowId, _paymentId);
178196
}
@@ -235,6 +253,7 @@ contract SafeBaseEscrowV1 is
235253
treasury.approveWithdrawal(requestId);
236254
treasury.executeWithdrawal(requestId);
237255

256+
_updateRegistryState(_escrowId);
238257
emit EscrowReleased(_escrowId, escrow.seller);
239258
}
240259

@@ -276,6 +295,7 @@ contract SafeBaseEscrowV1 is
276295
treasury.approveWithdrawal(requestId);
277296
treasury.executeWithdrawal(requestId);
278297

298+
_updateRegistryState(_escrowId);
279299
emit EscrowPartialReleased(_escrowId, escrow.seller, _amount);
280300
}
281301

@@ -309,6 +329,7 @@ contract SafeBaseEscrowV1 is
309329
treasury.approveWithdrawal(requestId);
310330
treasury.executeWithdrawal(requestId);
311331

332+
_updateRegistryState(_escrowId);
312333
emit EscrowRefunded(_escrowId, escrow.buyer);
313334
}
314335

@@ -319,6 +340,7 @@ contract SafeBaseEscrowV1 is
319340
if (escrow.mediator == address(0)) revert Unauthorized();
320341

321342
escrow.state = EscrowState.Disputed;
343+
_updateRegistryState(_escrowId);
322344
emit EscrowDisputed(_escrowId, msg.sender);
323345
}
324346

@@ -328,6 +350,7 @@ contract SafeBaseEscrowV1 is
328350
if (msg.sender != escrow.buyer) revert Unauthorized();
329351

330352
escrow.state = EscrowState.Cancelled;
353+
_updateRegistryState(_escrowId);
331354
emit EscrowCancelled(_escrowId);
332355
}
333356

@@ -345,5 +368,11 @@ contract SafeBaseEscrowV1 is
345368

346369
function _authorizeUpgrade(address newImplementation) internal override onlyOwner {}
347370

371+
function _updateRegistryState(uint256 _escrowId) internal {
372+
if (registry != address(0)) {
373+
IRegistry(registry).updateEscrowState(_escrowId, uint8(escrows[_escrowId].state));
374+
}
375+
}
376+
348377
uint256[50] private __gap;
349378
}

test/SafeBaseEscrowV1.t.sol

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,13 @@ import {SafeBaseEscrowV1} from "../src/escrow/SafeBaseEscrowV1.sol";
66
import {Treasury} from "../src/Treasury.sol";
77
import {ERC1967Proxy} from "@openzeppelin/contracts/proxy/ERC1967/ERC1967Proxy.sol";
88
import {MockERC20} from "./mocks/MockERC20.sol";
9+
import {MockRegistry} from "./mocks/MockRegistry.sol";
910

1011
contract SafeBaseEscrowV1Test is Test {
1112
SafeBaseEscrowV1 public escrow;
1213
Treasury public treasury;
1314
MockERC20 public token;
15+
MockRegistry public registry;
1416

1517
address public owner = address(1);
1618
address public buyer = address(2);
@@ -36,6 +38,7 @@ contract SafeBaseEscrowV1Test is Test {
3638

3739
function setUp() public {
3840
token = new MockERC20("Test Token", "TEST", 18);
41+
registry = new MockRegistry();
3942

4043
Treasury treasuryImpl = new Treasury();
4144
bytes memory treasuryData = abi.encodeWithSelector(
@@ -58,6 +61,7 @@ contract SafeBaseEscrowV1Test is Test {
5861
vm.startPrank(owner);
5962
treasury.addAdmin(address(escrow));
6063
treasury.addExecutor(address(escrow));
64+
escrow.setRegistry(address(registry));
6165
vm.stopPrank();
6266

6367
vm.deal(buyer, 100 ether);
@@ -209,6 +213,30 @@ contract SafeBaseEscrowV1Test is Test {
209213
escrow.fundEscrow{value: 0.5 ether}(escrowId);
210214
}
211215

216+
function testRegistryIndexAndUpdateOnFund() public {
217+
vm.prank(buyer);
218+
uint256 escrowId = escrow.createEscrow(
219+
seller,
220+
mediator,
221+
address(0),
222+
1 ether,
223+
block.timestamp + 1 days,
224+
0
225+
);
226+
227+
MockRegistry.Indexed memory meta = registry.indexed(escrowId);
228+
assertEq(meta.buyer, buyer);
229+
assertEq(meta.seller, seller);
230+
assertEq(meta.amount, 1 ether);
231+
assertEq(meta.createdAt, block.timestamp);
232+
233+
vm.prank(buyer);
234+
escrow.fundEscrow{value: 1 ether}(escrowId);
235+
236+
assertEq(registry.lastEscrowId(), escrowId);
237+
assertEq(registry.lastState(), uint8(SafeBaseEscrowV1.EscrowState.Funded));
238+
}
239+
212240
function testFundEscrowERC20() public {
213241
vm.prank(buyer);
214242
uint256 escrowId = escrow.createEscrow(
@@ -568,6 +596,8 @@ contract SafeBaseEscrowV1Test is Test {
568596
SafeBaseEscrowV1.EscrowData memory escrowData = escrow.getEscrow(escrowId);
569597
assertTrue(escrowData.state == SafeBaseEscrowV1.EscrowState.Refunded);
570598
assertEq(buyer.balance, buyerBalanceBefore + 1 ether);
599+
assertEq(registry.lastEscrowId(), escrowId);
600+
assertEq(registry.lastState(), uint8(SafeBaseEscrowV1.EscrowState.Refunded));
571601
}
572602

573603
function testRefundToBuyerMediator() public {

test/mocks/MockRegistry.sol

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// SPDX-License-Identifier: MIT
2+
pragma solidity ^0.8.28;
3+
4+
contract MockRegistry {
5+
uint256 public lastEscrowId;
6+
uint8 public lastState;
7+
struct Indexed {
8+
address buyer;
9+
address seller;
10+
uint256 amount;
11+
uint256 createdAt;
12+
}
13+
mapping(uint256 => Indexed) public indexed;
14+
15+
function indexEscrow(
16+
uint256 escrowId,
17+
address buyer,
18+
address seller,
19+
uint256 amount,
20+
uint256 createdAt
21+
) external {
22+
indexed[escrowId] = Indexed(buyer, seller, amount, createdAt);
23+
lastEscrowId = escrowId;
24+
}
25+
26+
function updateEscrowState(uint256 escrowId, uint8 state) external {
27+
lastEscrowId = escrowId;
28+
lastState = state;
29+
}
30+
}
31+

0 commit comments

Comments
 (0)