forked from Txio-labs/txio
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmockService.ts
More file actions
126 lines (109 loc) · 3.56 KB
/
Copy pathmockService.ts
File metadata and controls
126 lines (109 loc) · 3.56 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
import { RPCHealthMetric, ObjectSnapshot, DashboardTransaction } from '../types';
import { executeSuiRpc, getSuiRpcHealth } from './suiService';
import { appStore } from '../lib/store';
import { NETWORKS } from '../lib/constants';
export const fetchRPCHealth = async (): Promise<RPCHealthMetric[]> => {
const { network, settings } = appStore.getSnapshot();
const networksToCheck = Object.keys(NETWORKS) as (keyof typeof NETWORKS)[];
const results = await Promise.all(
networksToCheck.map((net) => getSuiRpcHealth(net))
);
return results;
};
export const fetchRecentTransactions = async (): Promise<DashboardTransaction[]> => {
const { network } = appStore.getSnapshot();
try {
const { result } = await executeSuiRpc(
network,
'suix_queryTransactionBlocks',
[
{ filter: null, options: { showInput: true, showEffects: false } },
null,
10,
true
]
);
const blocks: any[] = result?.data ?? [];
return blocks.map((block: any, idx: number) => ({
id: String(idx),
digest: block.digest ?? '',
sender: block.transaction?.data?.sender ?? '',
type: block.transaction?.data?.transaction?.kind ?? 'Unknown',
gas: String(block.effects?.gasUsed?.computationCost ?? 0),
timestamp: block.timestampMs
? Number(block.timestampMs)
: Date.now()
}));
} catch {
return [];
}
};
export const fetchOwnedObjects = async (address: string): Promise<ObjectSnapshot[]> => {
if (!address) return [];
const { network } = appStore.getSnapshot();
try {
const { result } = await executeSuiRpc(
network,
'suix_getOwnedObjects',
[
address,
{
options: {
showType: true,
showContent: false,
showDisplay: false
}
}
]
);
const items: any[] = result?.data ?? [];
return items.map((item: any) => ({
id: item.data?.objectId ?? '',
type: item.data?.type ?? '',
version: String(item.data?.version ?? ''),
owner: address
}));
} catch {
return [];
}
};
export const executeMockTransaction = async (ptbData: any) => {
const { network } = appStore.getSnapshot();
try {
const { result } = await executeSuiRpc(
network,
'sui_dryRunTransactionBlock',
[ptbData]
);
return {
status: 'success',
digest: result?.digest ?? '',
gasUsed: result?.effects?.gasUsed?.computationCost ?? 0,
events: result?.events ?? [],
timestamp: Date.now()
};
} catch (error: any) {
return {
status: 'error',
message: error?.message ?? 'Dry run failed',
timestamp: Date.now()
};
}
};
export const fetchMovePackage = async (id: string) => {
const { network } = appStore.getSnapshot();
try {
const { result } = await executeSuiRpc(
network,
'sui_getNormalizedMoveModule',
[id, 'init']
);
return {
id,
modules: result ? Object.keys(result) : [],
publishedAt: result?.fileFormatVersion ?? ''
};
} catch {
return { id, modules: [], publishedAt: '' };
}
};