-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsla-client-contract.ts
More file actions
95 lines (73 loc) · 2.71 KB
/
sla-client-contract.ts
File metadata and controls
95 lines (73 loc) · 2.71 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
import { Address } from "viem";
import { SERVICE_CONFIG } from "../config/env.js";
import { baseLogger } from "../utils/logger.js";
import { CLIENT_CONTRACT_ABI } from "./abis/client-abi.js";
import { getRpcClient, getWalletClient } from "./blockchain-client.js";
const childLogger = baseLogger.child(
{ avengers: "assemble" },
{ msgPrefix: "[SLA Client Contract] " },
);
export async function getClientsForSPFromClientContract(
storageProviderId: number,
): Promise<Address[]> {
const rpcClient = getRpcClient();
childLogger.info("Fetching clients for storage provider...");
const spClients = await rpcClient.readContract({
address: SERVICE_CONFIG.CLIENT_CONTRACT_ADDRESS as Address,
abi: CLIENT_CONTRACT_ABI,
functionName: "getSPClients",
args: [BigInt(storageProviderId)],
});
childLogger.info(
`Fetched ${spClients.length} clients for SP ${storageProviderId}`,
);
return spClients as Address[];
}
export async function getClientAllocationIdsPerProvider(
storageProviderId: number,
clientAddresses: Address[],
): Promise<number[]> {
const rpcClient = getRpcClient();
childLogger.info(
`Fetching allocation ID for clients and storage providers ${storageProviderId}...`,
);
const multicallData = clientAddresses.map((clientId) => ({
address: SERVICE_CONFIG.CLIENT_CONTRACT_ADDRESS as Address,
abi: CLIENT_CONTRACT_ABI,
functionName: "getClientAllocationIdsPerProvider",
args: [BigInt(storageProviderId), clientId],
}));
const multicall = await rpcClient.multicall({
contracts: multicallData,
});
childLogger.info(
`Fetched ${multicall.length} allocations for storage provider ${storageProviderId}...`,
);
const ids = multicall
.filter((response) => !response.error)
.flatMap((response) => Number(response.result));
return ids;
}
export async function setClaimTerminatedEarly(allocationIds: bigint[]) {
const rpcClient = getRpcClient();
const walletClient = getWalletClient();
childLogger.info("claimsTerminatedEarly: Simulating request...");
const { request } = await rpcClient.simulateContract({
address: SERVICE_CONFIG.CLIENT_CONTRACT_ADDRESS as Address,
abi: CLIENT_CONTRACT_ABI,
functionName: "claimsTerminatedEarly",
args: [allocationIds],
account: walletClient.account,
});
childLogger.info("claimsTerminatedEarly: Sending transaction...");
const txHash = await walletClient.writeContract(request);
childLogger.info(
`claimsTerminatedEarly: Transaction sent: ${txHash}, waiting for confirmation...`,
);
const receipt = await rpcClient.waitForTransactionReceipt({
hash: txHash,
});
childLogger.info(
`claimsTerminatedEarly: Transaction executed in block ${receipt.blockNumber}`,
);
}