-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproperty_transaction.js
More file actions
116 lines (101 loc) · 3.19 KB
/
property_transaction.js
File metadata and controls
116 lines (101 loc) · 3.19 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
const Web3Helper = require('./web3_helper');
const Web5Helper = require('./web5_helper');
const Web4Integration = require('./web4_integration');
class PropertyTransaction {
constructor(web3Provider, web5Options, web4ApiKey) {
this.web3 = new Web3Helper(web3Provider);
this.web5 = Web5Helper.init(web5Options);
this.web4 = new Web4Integration(web4ApiKey);
}
async listProperty(propertyDetails, ownerDID) {
// AI-generated description
const description = await this.web4.generatePropertyDescription(propertyDetails);
// Store on decentralized storage
const propertyRecord = {
...propertyDetails,
description,
ownerDID,
timestamp: new Date().toISOString()
};
const recordId = await this.web5.storeData(
propertyRecord,
'https://schema.org/RealEstateListing'
);
// Create NFT on Ethereum
const txReceipt = await this.web3.sendTransaction(
ownerDID.ethereumAddress,
'0xPropertyContract',
0,
ownerDID.privateKey,
{
data: web3.eth.abi.encodeFunctionCall({
name: 'mintPropertyNFT',
type: 'function',
inputs: [{
type: 'string',
name: 'metadataURI'
}]
}, [`ipfs://${recordId}`])
}
);
return {
web5RecordId: recordId,
nftContractAddress: txReceipt.contractAddress,
tokenId: txReceipt.events.Transfer.returnValues.tokenId
};
}
async executePurchase(buyerDID, propertyRecordId, offerAmount) {
// Verify property details
const property = await this.web5.queryData(propertyRecordId);
// AI-powered market validation
const fairValue = await this.web4.predictMarketValue(property);
if (offerAmount < fairValue * 0.9) {
throw new Error('Offer below fair market value');
}
// Create verifiable credential
const vc = await this.web5.createVC({
id: buyerDID,
financialCapacity: offerAmount * 1.2,
kycStatus: 'verified'
}, 'BuyerCredential');
// Execute blockchain transaction
const tx = await this.web3.sendTransaction(
buyerDID.ethereumAddress,
property.nftContractAddress,
0,
buyerDID.privateKey,
{
data: web3.eth.abi.encodeFunctionCall({
name: 'purchaseProperty',
type: 'function',
inputs: [
{type: 'uint256', name: 'tokenId'},
{type: 'uint256', name: 'amount'}
]
}, [property.tokenId, web3.utils.toWei(offerAmount.toString(), 'ether')])
}
);
// Update property record
await this.web5.updateData(propertyRecordId, {
status: 'sold',
transactionHash: tx.transactionHash,
newOwner: buyerDID
});
return tx;
}
async verifyOwnership(propertyRecordId, did) {
const property = await this.web5.queryData(propertyRecordId);
const nftOwner = await this.web3.callContract(
property.nftContractAddress,
ERC721_ABI,
'ownerOf',
[property.tokenId]
);
return {
web5Owner: property.newOwner,
nftOwner,
verified: property.newOwner === did && nftOwner === did.ethereumAddress
};
}
}
module.exports = PropertyTransaction;