This repository was archived by the owner on Mar 16, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathuseAgentPermissions.ts
More file actions
57 lines (50 loc) · 1.86 KB
/
useAgentPermissions.ts
File metadata and controls
57 lines (50 loc) · 1.86 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
import { useQuery } from "@tanstack/react-query";
interface AgentPermissions {
canUseProposals: boolean;
canApproveRevokeContracts: boolean;
canBuySell: boolean;
canDeposit: boolean;
}
export function useAgentPermissions(agentAddress: string | null) {
const fetchPermissions = async (): Promise<AgentPermissions> => {
if (!agentAddress) throw new Error("No agent address");
const [contractAddress, contractName] = agentAddress.split(".");
const res = await fetch(
// `${process.env.NEXT_PUBLIC_CACHE_URL}/contract-calls/read-only/${contractAddress}/${contractName}/get-agent-permissions`,
`https://aibtcdev-cache-preview.hosting-962.workers.dev/contract-calls/read-only/${contractAddress}/${contractName}/get-agent-permissions`,
{
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
sender: contractAddress,
arguments: [],
network: process.env.NEXT_PUBLIC_STACKS_NETWORK,
cacheControl: {
bustCache: true,
ttl: 0,
},
}),
}
);
if (!res.ok) throw new Error("Failed to fetch permissions");
const response = await res.json();
const apiData = response?.data;
// Map API response to expected interface
return {
canUseProposals: apiData?.canUseProposals ?? true,
canApproveRevokeContracts: apiData?.canApproveRevokeContracts ?? true,
canBuySell: apiData?.canBuySell ?? false,
canDeposit: apiData?.canManageAssets ?? true, // Map canManageAssets to canDeposit
};
};
return useQuery({
queryKey: ["agent-permissions", agentAddress], // Stable key for proper caching
queryFn: fetchPermissions,
enabled: !!agentAddress,
staleTime: 0, // Always consider data stale
refetchOnMount: true,
refetchOnWindowFocus: true,
});
}