-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.ts
More file actions
291 lines (266 loc) · 11.1 KB
/
Copy pathserver.ts
File metadata and controls
291 lines (266 loc) · 11.1 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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
import express from 'express';
import cors from 'cors';
import {fetchTransactionsByChain, fetchChainId} from './transaction/transaction'
import { TrueApi } from '@truenetworkio/sdk'
import { adAttestationSchema, chainActivitySchema } from './true-network/schemas'
import { config, getTrueNetworkInstance } from './true-network/true.config'
import {runAlgo} from '@truenetworkio/sdk/dist/pallets/algorithms/extrinsic'
import { ethers } from 'ethers';
const abi = [
{
anonymous: false,
inputs: [
{ indexed: true, internalType: "address", name: "publisher", type: "address" },
{ indexed: false, internalType: "string", name: "imageIpfsUrl", type: "string" },
{ indexed: false, internalType: "string", name: "name", type: "string" },
{ indexed: false, internalType: "string", name: "text", type: "string" },
{ indexed: false, internalType: "uint256", name: "timestamp", type: "uint256" },
{ indexed: false, internalType: "bool", name: "isActive", type: "bool" },
{ indexed: false, internalType: "uint256", name: "reputationScore", type: "uint256" },
],
name: "AdPublished",
type: "event",
},
{
anonymous: false,
inputs: [
{ indexed: true, internalType: "address", name: "publisher", type: "address" },
{ indexed: false, internalType: "string", name: "imageIpfsUrl", type: "string" },
{ indexed: false, internalType: "string", name: "name", type: "string" },
{ indexed: false, internalType: "string", name: "text", type: "string" },
{ indexed: false, internalType: "uint256", name: "timestamp", type: "uint256" },
{ indexed: false, internalType: "bool", name: "isActive", type: "bool" },
{ indexed: false, internalType: "uint256", name: "reputationScore", type: "uint256" },
],
name: "AdStatusChanged",
type: "event",
},
{
anonymous: false,
inputs: [
{ indexed: true, internalType: "address", name: "publisher", type: "address" },
{ indexed: false, internalType: "string", name: "imageIpfsUrl", type: "string" },
{ indexed: false, internalType: "string", name: "name", type: "string" },
{ indexed: false, internalType: "string", name: "text", type: "string" },
{ indexed: false, internalType: "uint256", name: "timestamp", type: "uint256" },
{ indexed: false, internalType: "bool", name: "isActive", type: "bool" },
{ indexed: false, internalType: "uint256", name: "reputationScore", type: "uint256" },
],
name: "ReputationScoreUpdated",
type: "event",
},
{
inputs: [{ internalType: "uint256", name: "", type: "uint256" }],
name: "ads",
outputs: [
{ internalType: "address", name: "publisher", type: "address" },
{ internalType: "string", name: "imageIpfsUrl", type: "string" },
{ internalType: "string", name: "name", type: "string" },
{ internalType: "string", name: "text", type: "string" },
{ internalType: "uint256", name: "timestamp", type: "uint256" },
{ internalType: "bool", name: "isActive", type: "bool" },
{ internalType: "uint256", name: "reputationScore", type: "uint256" },
],
stateMutability: "view",
type: "function",
},
{
inputs: [{ internalType: "uint256", name: "_adId", type: "uint256" }],
name: "getAd",
outputs: [
{ internalType: "address", name: "publisher", type: "address" },
{ internalType: "string", name: "imageIpfsUrl", type: "string" },
{ internalType: "string", name: "name", type: "string" },
{ internalType: "string", name: "text", type: "string" },
{ internalType: "uint256", name: "timestamp", type: "uint256" },
{ internalType: "bool", name: "isActive", type: "bool" },
],
stateMutability: "view",
type: "function",
},
{
inputs: [],
name: "getAllAds",
outputs: [
{
components: [
{ internalType: "address", name: "publisher", type: "address" },
{ internalType: "string", name: "imageIpfsUrl", type: "string" },
{ internalType: "string", name: "name", type: "string" },
{ internalType: "string", name: "text", type: "string" },
{ internalType: "uint256", name: "timestamp", type: "uint256" },
{ internalType: "bool", name: "isActive", type: "bool" },
{ internalType: "uint256", name: "reputationScore", type: "uint256" },
],
internalType: "struct PlaceHolderAds.Ad[]",
name: "",
type: "tuple[]",
},
],
stateMutability: "view",
type: "function",
},
{
inputs: [{ internalType: "address", name: "_publisher", type: "address" }],
name: "getPublisherAds",
outputs: [{ internalType: "uint256[]", name: "", type: "uint256[]" }],
stateMutability: "view",
type: "function",
},
{
inputs: [
{ internalType: "string", name: "_imageIpfsUrl", type: "string" },
{ internalType: "string", name: "_name", type: "string" },
{ internalType: "string", name: "_text", type: "string" },
],
name: "publishAd",
outputs: [{ internalType: "uint256", name: "", type: "uint256" }],
stateMutability: "nonpayable",
type: "function",
},
{
inputs: [
{ internalType: "address", name: "", type: "address" },
{ internalType: "uint256", name: "", type: "uint256" },
],
name: "publisherAds",
outputs: [{ internalType: "uint256", name: "", type: "uint256" }],
stateMutability: "view",
type: "function",
},
{
inputs: [{ internalType: "address", name: "_publisher", type: "address" }],
name: "toggleAdStatus",
outputs: [],
stateMutability: "nonpayable",
type: "function",
},
{
inputs: [
{ internalType: "address", name: "_publisher", type: "address" },
{ internalType: "uint256", name: "_reputationScore", type: "uint256" },
],
name: "updateReputationScore",
outputs: [],
stateMutability: "nonpayable",
type: "function",
},
];
const contractAddress = "0x61dda741ebc71e5e2230598ff105306a8fc47bcf";
const RPC_URL = process.env.RPC_URL_BASE as string;
const PRIVATE_KEY = process.env.PRIVATE_KEY as string;
// Define interfaces
interface AttestationData {
publisherAddress: string;
rating: number;
userAddress: string;
signature: string;
}
interface ChainData {
totalTransactions : number,
firstTransactionHash : string,
firstTransactionTimestamp : Text,
firstBlockNumber : Text,
accountAgeDays : number,
lastTransactionTimestamp : Text,
lastTransactionHash : string,
lastBlockNumber : Text,
}
interface ApiResponse {
success: boolean;
receivedData?: AttestationData;
error?: string;
}
// Create Express app
const app = express();
const PORT = 4000;
// Middleware
app.use(express.json());
app.use(cors());
// Attestation endpoint
app.post('/api/attestation', async (req, res) => {
try {
const attestationData: AttestationData = req.body;
// Log the received data
console.log('Received attestation:', attestationData);
res.json({
success: true,
message : "Data submitted successully"
});
// Fetch the chainId based on the publisher address
const chainId = fetchChainId(attestationData.publisherAddress);
console.log(`Determined chainId: ${chainId}`);
// Fetch the transactions based on chainId
const txInfo = await fetchTransactionsByChain(attestationData.userAddress, chainId);
const chainActivityData = {
totalTransactions: txInfo.totalTransactions,
// firstTransactionHash: txInfo.firstTransactionHash,
// firstTransactionTimestamp: txInfo.firstTransactionTimestamp,
// firstBlockNumber: txInfo.firstBlockNumber,
accountAgeDays: txInfo.accountAgeDays,
// lastTransactionTimestamp: txInfo.lastTransactionTimestamp,
// lastTransactionHash: txInfo.lastTransactionHash,
// lastBlockNumber: txInfo.lastBlockNumber
};
const api = await getTrueNetworkInstance();
const adAttestationOutput = await adAttestationSchema.attest(api, attestationData.userAddress, {
// publisherAddress: attestationData.publisherAddress,
rating: attestationData.rating,
// signature: attestationData.signature
})
const chainActivitySchemaOutput = await chainActivitySchema.attest(api, attestationData.userAddress, {
...chainActivityData
})
// const result = await getAlgorithmResult(attestationData.userAddress);
const reputationScore = await runAlgo(api.network, config.issuer.hash, api.account, attestationData.userAddress, config.algorithm?.id!);
console.log('reputationScore', reputationScore);
const provider = new ethers.providers.JsonRpcProvider(RPC_URL);
const wallet = new ethers.Wallet(PRIVATE_KEY, provider);
const contract = new ethers.Contract(contractAddress, abi, wallet);
const tx = await contract.updateReputationScore(attestationData.publisherAddress, reputationScore);
const receipt = await tx.wait();
console.log('Updated Reputation Score');
// await updatePublisherScore(attestationData.publisherAddress, reputationScore);
// console.log('Successfully updated reputation score');
let txToggle;
if(receipt.status === 1){
console.log('Updated Reputation Score');
if(reputationScore < 5 ) {
// txToggle = await contract.toggleAdStatus(attestationData.publisherAddress);
// const receiptToggle = await tx.wait();
// if (receiptToggle === 1){
console.log('Toggle called : Ad inactive');
// }
}
}
console.log({
chainId,
transactionInfo: txInfo,
attestationData,
adAttestationOutput,
chainActivitySchemaOutput,
reputationScore,
// updateReputationScoreHash:tx.hash,
// txToggleHash: txToggle.hash
});
await api.network.disconnect()
} catch (error) {
const errorMessage = error instanceof Error ? error.message : 'Unknown error occurred';
console.error('Error processing attestation:', errorMessage);
res.status(500).json({
success: false,
error: errorMessage
});
}
});
// Add a simple GET endpoint for testing
app.get('/api/health', (req, res) => {
res.json({ status: 'Server is running' });
});
// Start server
app.listen(PORT, () => {
console.log(`Server running on http://localhost:${PORT}`);
console.log('Available endpoints:');
console.log(`- POST http://localhost:${PORT}/api/attestation`);
console.log(`- GET http://localhost:${PORT}/api/health`);
});