Skip to content

Commit dc9bdfd

Browse files
committed
Adjust SLIs from CDP enpoint
1 parent 9052c69 commit dc9bdfd

File tree

5 files changed

+120
-21
lines changed

5 files changed

+120
-21
lines changed
Lines changed: 50 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
import { Address, encodeFunctionData } from "viem";
22
import { SERVICE_CONFIG } from "../config/env.js";
33
import { logger } from "../utils/logger.js";
4-
import { CdpSliResponse } from "../utils/types.js";
4+
import {
5+
CdpSliResponse,
6+
SLIAttestation,
7+
StorageProvidersSLIMetric,
8+
} from "../utils/types.js";
59
import { SLI_ORACLE_ABI } from "./abis/sli-oracle-abi.js";
610
import { getRpcClient, getWalletClient } from "./blockchain-client.js";
711

@@ -14,7 +18,40 @@ export async function setSliOnOracleContract(
1418
const oracleContractAddress =
1519
SERVICE_CONFIG.ORACLE_CONTRACT_ADDRESS as Address;
1620

17-
const encodedCalls = sliDataForProviders.map((req) =>
21+
const buildedSliData: {
22+
provider: bigint;
23+
sli: SLIAttestation;
24+
}[] = sliDataForProviders.map((provider) => {
25+
logger.info(
26+
`Preparing SLI data for provider ${provider.storageProviderId}`,
27+
);
28+
const data = {
29+
provider: provider.storageProviderId.startsWith("f0")
30+
? BigInt(provider.storageProviderId.slice(2))
31+
: BigInt(provider.storageProviderId),
32+
sli: {
33+
availability:
34+
Number(
35+
provider.data
36+
.find(
37+
(d) =>
38+
d.sliMetric === StorageProvidersSLIMetric.RPA_RETRIEVABILITY,
39+
)
40+
?.sliMetricValue?.split(".")[0],
41+
) || 0,
42+
latency: 0,
43+
indexing: 0,
44+
retention: 0,
45+
bandwidth: 0,
46+
stability: 0,
47+
lastUpdate: BigInt(provider.updatedAt.getTime()),
48+
},
49+
};
50+
51+
return data;
52+
});
53+
54+
const encodedCalls = buildedSliData.map((req) =>
1855
encodeFunctionData({
1956
abi: SLI_ORACLE_ABI,
2057
functionName: "setSLI",
@@ -24,23 +61,26 @@ export async function setSliOnOracleContract(
2461

2562
logger.info("Simulating request to oracle contract...");
2663

27-
const { request } = await rpcClient.simulateContract({
64+
// const { request } =
65+
await rpcClient.simulateContract({
2866
address: oracleContractAddress,
2967
abi: SLI_ORACLE_ABI,
3068
functionName: "multicall",
3169
args: [encodedCalls],
3270
account: walletClient.account,
3371
});
3472

35-
logger.info("Sending transaction to oracle contract...");
73+
logger.info("Simulation successful.");
3674

37-
const txHash = await walletClient.writeContract(request);
75+
// logger.info("Sending transaction to oracle contract...");
3876

39-
logger.info(`Transaction sent: ${txHash}, waiting for confirmation...`);
77+
// const txHash = await walletClient.writeContract(request);
4078

41-
const receipt = await rpcClient.waitForTransactionReceipt({
42-
hash: txHash,
43-
});
79+
// logger.info(`Transaction sent: ${txHash}, waiting for confirmation...`);
80+
81+
// const receipt = await rpcClient.waitForTransactionReceipt({
82+
// hash: txHash,
83+
// });
4484

45-
logger.info(`Transaction executed in block ${receipt.blockNumber}`);
85+
// logger.info(`Transaction executed in block ${receipt.blockNumber}`);
4686
}

src/http-server/server.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import express, { Request, Response, NextFunction } from "express";
33
// import { setSliOracleJob } from "../jobs/set-sli-job.js";
44
import { logger } from "../utils/logger.js";
55
import { SERVICE_CONFIG } from "../config/env.js";
6+
import { setSliOracleJob } from "../jobs/set-sli-job.js";
67

78
const app = express();
89

@@ -40,7 +41,7 @@ app.post(
4041
logger.info("Manual trigger received via /trigger-now");
4142

4243
try {
43-
// await setSliOracleJob();
44+
await setSliOracleJob();
4445
res.json({ status: "ok", message: "Job triggered successfully" });
4546
} catch (err) {
4647
const message = err instanceof Error ? err.message : String(err);

src/jobs/set-sli-job.ts

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,51 @@
11
import { getProvidersFromSlaAllocatorContract } from "../blockchain/sla-allocator-contract.js";
2+
import { setSliOnOracleContract } from "../blockchain/sli-oracle-contract.js";
3+
import { getSliForStorageProviders } from "../services/cdp-fetch-service.js";
24
import { logger } from "../utils/logger.js";
35

46
export async function setSliOracleJob() {
57
logger.info("Oracle job started");
68

79
try {
8-
await getProvidersFromSlaAllocatorContract(); // TODO: enable when SLA Allocator is ready
9-
// const sliDataForProviders = await getSliForStorageProviders(
10-
// slaContractProviders.map(String),
11-
// ); // TODO: enable when CDP service is ready
12-
//await setSliOnOracleContract(sliDataForProviders);
10+
const slaContractProviders = await getProvidersFromSlaAllocatorContract();
11+
12+
if (slaContractProviders.length === 0) {
13+
logger.info(
14+
"No storage providers found in SLA Allocator contract, skipping SLI update on oracle contract",
15+
);
16+
return;
17+
}
18+
19+
const sliDataForProviders = await getSliForStorageProviders(
20+
slaContractProviders.map((sp) => `f0${sp.toString()}`),
21+
);
22+
23+
if (sliDataForProviders.length === 0) {
24+
logger.info(
25+
"No SLI data fetched for any provider from CDP, skipping SLI update on oracle contract",
26+
);
27+
return;
28+
}
29+
30+
// const sliDataForProviders: CdpSliResponse[] = [
31+
// {
32+
// storageProviderId: "f03315260",
33+
// storageProviderName: "ProviderOne",
34+
// updatedAt: new Date(),
35+
// data: [
36+
// {
37+
// sliMetric: StorageProvidersSLIMetric.RPA_RETRIEVABILITY,
38+
// sliMetricName: "RPA Retrievability",
39+
// sliMetricValue: "99.12",
40+
// sliMetricDescription: "Retrievability percentage",
41+
// sliMetricUnit: "percent",
42+
// updatedAt: new Date(),
43+
// },
44+
// ],
45+
// },
46+
// ];
47+
48+
await setSliOnOracleContract(sliDataForProviders);
1349
} catch (err) {
1450
logger.error({ err }, "Oracle job failed");
1551
}

src/services/cdp-fetch-service.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,18 @@ async function fetchDataFromCdp(endpoint: string): Promise<CdpSliResponse[]> {
1717
return data;
1818
}
1919

20-
export async function getSliForStorageProviders(storageProviders: string[]) {
20+
export async function getSliForStorageProviders(
21+
storageProviders: string[],
22+
): Promise<CdpSliResponse[]> {
23+
if (storageProviders.length === 0) {
24+
logger.info("No storage providers provided for SLI fetch");
25+
return [];
26+
}
27+
2128
const providersParam = storageProviders.join(",");
22-
const endpoint = `sli-storage-providers/?providers=${encodeURIComponent(
29+
const endpoint = `storage-providers/sli-data?storageProvidersIds=${encodeURIComponent(
2330
providersParam,
24-
)}`; //TODO: adjust enpoint if needed
31+
)}`;
2532

2633
logger.info("Fetching SLI data from CDP service...");
2734

src/utils/types.ts

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,22 @@ export interface SLIAttestation {
88
stability: number;
99
}
1010

11+
export enum StorageProvidersSLIMetric {
12+
RPA_RETRIEVABILITY = "RPA_RETRIEVABILITY",
13+
}
14+
15+
export interface StorageProvidersSLIData {
16+
sliMetric: StorageProvidersSLIMetric;
17+
sliMetricName: string;
18+
sliMetricValue: string;
19+
sliMetricDescription: string;
20+
sliMetricUnit: string;
21+
updatedAt: Date;
22+
}
23+
1124
export interface CdpSliResponse {
12-
provider: bigint;
13-
sli: SLIAttestation;
25+
storageProviderId: string;
26+
storageProviderName: string | null;
27+
updatedAt: Date;
28+
data: StorageProvidersSLIData[];
1429
}

0 commit comments

Comments
 (0)