@@ -59,10 +59,27 @@ contract XcrowEscrow is IXcrowEscrow, ReentrancyGuard, Pausable, Ownable {
5959 nextJobId = 1 ;
6060 }
6161
62+ // --- Internal Helpers ---
63+
64+ /// @notice Resolve the payout address for a job — always the agent owner's wallet
65+ /// @dev Agent wallet signs completions; owner wallet receives payment
66+ function _resolvePayoutAddress (uint256 agentId ) internal view returns (address ) {
67+ address ownerAddr = identityRegistry.ownerOf (agentId);
68+ require (ownerAddr != address (0 ), "Agent owner not found " );
69+ return ownerAddr;
70+ }
71+
6272 // --- Core Functions ---
6373
64- /// @notice Create a job by specifying the agent's wallet address directly (no ERC-8004 lookup needed)
65- function createJobByWallet (address agentWallet , uint256 amount , bytes32 taskHash , uint256 deadline )
74+ /// @notice Create a job by specifying the agent's wallet address directly
75+ /// @dev agentId is required so payout can be routed to the agent owner
76+ function createJobByWallet (
77+ address agentWallet ,
78+ uint256 amount ,
79+ bytes32 taskHash ,
80+ uint256 deadline ,
81+ uint256 agentId
82+ )
6683 external
6784 nonReentrant
6885 whenNotPaused
@@ -72,13 +89,14 @@ contract XcrowEscrow is IXcrowEscrow, ReentrancyGuard, Pausable, Ownable {
7289 require (deadline > block .timestamp , "Deadline must be future " );
7390 require (taskHash != bytes32 (0 ), "Task hash required " );
7491 require (agentWallet != address (0 ), "Invalid agent wallet " );
92+ require (agentId > 0 , "Agent ID required " );
7593
7694 uint256 platformFee = (amount * protocolFeeBps) / 10000 ;
7795 jobId = nextJobId++ ;
7896
7997 jobs[jobId] = XcrowTypes.Job ({
8098 jobId: jobId,
81- agentId: 0 ,
99+ agentId: agentId ,
82100 agentChainId: uint32 (block .chainid ),
83101 client: msg .sender ,
84102 agentWallet: agentWallet,
@@ -96,10 +114,11 @@ contract XcrowEscrow is IXcrowEscrow, ReentrancyGuard, Pausable, Ownable {
96114 });
97115
98116 clientJobs[msg .sender ].push (jobId);
117+ agentJobs[agentId].push (jobId);
99118 agentWalletJobs[agentWallet].push (jobId);
100119
101120 usdc.safeTransferFrom (msg .sender , address (this ), amount);
102- emit JobCreated (jobId, 0 , msg .sender , amount);
121+ emit JobCreated (jobId, agentId , msg .sender , amount);
103122 }
104123
105124 /// @inheritdoc IXcrowEscrow
@@ -250,12 +269,13 @@ contract XcrowEscrow is IXcrowEscrow, ReentrancyGuard, Pausable, Ownable {
250269 require (block .timestamp >= job.proofSubmittedAt + settlementWindow, "Settlement window not elapsed " );
251270
252271 uint256 agentPayout = job.amount - job.platformFee;
272+ address payoutAddress = _resolvePayoutAddress (job.agentId);
253273
254274 job.status = XcrowTypes.JobStatus.Settled;
255275 job.settledAt = block .timestamp ;
256276 accumulatedFees += job.platformFee;
257277
258- usdc.safeTransfer (job.agentWallet , agentPayout);
278+ usdc.safeTransfer (payoutAddress , agentPayout);
259279
260280 emit JobSettled (jobId, agentPayout, job.platformFee);
261281 }
@@ -267,15 +287,16 @@ contract XcrowEscrow is IXcrowEscrow, ReentrancyGuard, Pausable, Ownable {
267287 require (msg .sender == job.client, "Only client can settle " );
268288
269289 uint256 agentPayout = job.amount - job.platformFee;
290+ address payoutAddress = _resolvePayoutAddress (job.agentId);
270291
271292 job.status = XcrowTypes.JobStatus.Settled;
272293 job.settledAt = block .timestamp ;
273294
274295 // Accumulate protocol fees
275296 accumulatedFees += job.platformFee;
276297
277- // Pay the agent
278- usdc.safeTransfer (job.agentWallet , agentPayout);
298+ // Pay the agent owner
299+ usdc.safeTransfer (payoutAddress , agentPayout);
279300
280301 emit JobSettled (jobId, agentPayout, job.platformFee);
281302 }
@@ -337,10 +358,11 @@ contract XcrowEscrow is IXcrowEscrow, ReentrancyGuard, Pausable, Ownable {
337358
338359 if (favorAgent) {
339360 uint256 agentPayout = job.amount - job.platformFee;
361+ address payoutAddress = _resolvePayoutAddress (job.agentId);
340362 job.status = XcrowTypes.JobStatus.Settled;
341363 job.settledAt = block .timestamp ;
342364 accumulatedFees += job.platformFee;
343- usdc.safeTransfer (job.agentWallet , agentPayout);
365+ usdc.safeTransfer (payoutAddress , agentPayout);
344366 emit JobSettled (jobId, agentPayout, job.platformFee);
345367 } else {
346368 job.status = XcrowTypes.JobStatus.Refunded;
0 commit comments