forked from LeaseFlow-Protocol/LeaseFlow-Protocol-Backend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathworker.js
More file actions
87 lines (70 loc) · 2.69 KB
/
worker.js
File metadata and controls
87 lines (70 loc) · 2.69 KB
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
const {
Contract,
Keypair,
Networks,
TransactionBuilder,
rpc,
} = require('@stellar/stellar-sdk');
const fs = require('fs');
const path = require('path');
const DB_PATH = path.join(__dirname, 'leases.json');
// Mock Database Helper
function getLeases() {
if (!fs.existsSync(DB_PATH)) return {};
return JSON.parse(fs.readFileSync(DB_PATH, 'utf8'));
}
function saveLeases(leases) {
fs.writeFileSync(DB_PATH, JSON.stringify(leases, null, 2));
}
/**
* Coordination Worker: Monitors and triggers lease initialization
*/
async function checkAndInitializeLease(leaseId) {
const leases = getLeases();
const lease = leases[leaseId];
if (!lease) {
console.error(`Lease ${leaseId} not found.`);
return;
}
// check if both parties have signed
if (lease.landlord_signed && lease.tenant_signed && !lease.initialized_on_chain) {
console.log(`[Worker] Coordination triggered for Lease: ${leaseId}. Both parties signed.`);
try {
console.log(`[Worker] Attempting to initialize on-chain for ${leaseId}...`);
await triggerOnChainInitialization(leaseId, lease.contract_data);
// Update local state
lease.initialized_on_chain = true;
lease.status = 'INITIALIZED';
saveLeases(leases);
console.log(`[Worker] Lease ${leaseId} successfully initialized on-chain.`);
} catch (error) {
console.error(`[Worker] CRITICAL FAILURE for lease ${leaseId}:`, error);
}
} else {
console.log(`[Worker] Lease ${leaseId} still pending signatures or already initialized.`);
}
}
async function triggerOnChainInitialization(leaseId, data) {
const server = new rpc.Server('https://soroban-testnet.stellar.org');
const networkPassphrase = Networks.TESTNET;
// Admin key (for demo/simulation, handle dummy values)
const secretKey = process.env.CONTRACT_ADMIN_SECRET || 'S...';
if (secretKey === 'S...' || secretKey === 'SDP...') {
console.log(`[Stellar] Skipping actual transaction building... Simulation mode active.`);
return new Promise((resolve) => setTimeout(resolve, 100));
}
const sourceKey = Keypair.fromSecret(secretKey);
const contractId = process.env.LEASE_CONTRACT_ID || 'CAEGD57WVTVQSYWYB23AISBW334QO7WNA5XQ56S45GH6BP3D2AVHKUG4';
const contract = new Contract(contractId);
// In a real scenario, we'd build and submit the XDR here.
// For the purpose of the coordinating worker, we simulate the submission.
console.log(`[Stellar] Building transaction for contract ${contractId}...`);
console.log(`[Stellar] Calling initialize_lease(${leaseId}, ...)`);
// Simulation:
return new Promise((resolve) => setTimeout(resolve, 1000));
}
module.exports = {
checkAndInitializeLease,
getLeases,
saveLeases
};