jaspervault_cli 1.0.8 → 1.0.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -10,17 +10,118 @@ function getSubgraphClient(network) {
|
|
|
10
10
|
}
|
|
11
11
|
return new SubgraphClient(subgraphUrl);
|
|
12
12
|
}
|
|
13
|
+
/**
|
|
14
|
+
* Parse a time argument into a Unix timestamp string (seconds).
|
|
15
|
+
* Supported formats:
|
|
16
|
+
* - "7d" / "1w" / "2h" → relative: current time minus N days/weeks/hours
|
|
17
|
+
* - "2025-01-01" → ISO date string parsed via Date.parse
|
|
18
|
+
* - pure integer string → treated as Unix timestamp directly
|
|
19
|
+
*/
|
|
20
|
+
function parseTimeArg(input) {
|
|
21
|
+
const relative = input.match(/^(\d+)(d|w|h)$/i);
|
|
22
|
+
if (relative) {
|
|
23
|
+
const n = parseInt(relative[1], 10);
|
|
24
|
+
const unit = relative[2].toLowerCase();
|
|
25
|
+
const secondsPerUnit = { h: 3600, d: 86400, w: 604800 };
|
|
26
|
+
const nowSec = Math.floor(Date.now() / 1000);
|
|
27
|
+
return String(nowSec - n * secondsPerUnit[unit]);
|
|
28
|
+
}
|
|
29
|
+
if (/^\d+$/.test(input)) {
|
|
30
|
+
return input;
|
|
31
|
+
}
|
|
32
|
+
const ms = Date.parse(input);
|
|
33
|
+
if (isNaN(ms)) {
|
|
34
|
+
throw new Error(`Invalid time format: "${input}". Use "7d", "1w", "2h", a Unix timestamp, or an ISO date like "2025-01-01".`);
|
|
35
|
+
}
|
|
36
|
+
return String(Math.floor(ms / 1000));
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Convert a wei-style integer string to a decimal string with correct precision.
|
|
40
|
+
* Uses BigInt to avoid floating-point precision loss for large numbers.
|
|
41
|
+
*/
|
|
42
|
+
function weiToDecimal(value, decimals) {
|
|
43
|
+
try {
|
|
44
|
+
const bigVal = BigInt(value);
|
|
45
|
+
const negative = bigVal < 0n;
|
|
46
|
+
const absVal = negative ? -bigVal : bigVal;
|
|
47
|
+
const divisor = 10n ** BigInt(decimals);
|
|
48
|
+
const intPart = absVal / divisor;
|
|
49
|
+
const fracPart = absVal % divisor;
|
|
50
|
+
const fracStr = fracPart.toString().padStart(decimals, '0');
|
|
51
|
+
const sign = negative ? '-' : '';
|
|
52
|
+
return `${sign}${intPart}.${fracStr}`;
|
|
53
|
+
}
|
|
54
|
+
catch {
|
|
55
|
+
return value;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Append human-readable formatted fields to a raw SubgraphOrder.
|
|
60
|
+
* Original raw fields are preserved; formatted fields are added alongside.
|
|
61
|
+
* Fields added: margin, size, entryPriceUsd, strikeAmountUsd, leverage, createdTime
|
|
62
|
+
*/
|
|
63
|
+
function formatOrder(order, net) {
|
|
64
|
+
const marginDecimals = net.marginAsset?.decimals ?? 6;
|
|
65
|
+
let tokenDecimals = 8;
|
|
66
|
+
if (net.tokens) {
|
|
67
|
+
for (const tok of Object.values(net.tokens)) {
|
|
68
|
+
if (tok.address.toLowerCase() === order.underlyingAsset.toLowerCase()) {
|
|
69
|
+
tokenDecimals = tok.decimals;
|
|
70
|
+
break;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
const margin = order.marginDeposited
|
|
75
|
+
? weiToDecimal(order.marginDeposited, marginDecimals)
|
|
76
|
+
: undefined;
|
|
77
|
+
const size = order.quantity
|
|
78
|
+
? weiToDecimal(order.quantity, tokenDecimals)
|
|
79
|
+
: undefined;
|
|
80
|
+
const entryPriceUsd = order.entryPrice
|
|
81
|
+
? weiToDecimal(order.entryPrice, 18)
|
|
82
|
+
: undefined;
|
|
83
|
+
const strikeAmountUsd = order.strikeAmount
|
|
84
|
+
? weiToDecimal(order.strikeAmount, 18)
|
|
85
|
+
: undefined;
|
|
86
|
+
let leverage;
|
|
87
|
+
if (order.leverage) {
|
|
88
|
+
leverage = order.leverage;
|
|
89
|
+
}
|
|
90
|
+
else if (order.productType) {
|
|
91
|
+
try {
|
|
92
|
+
leverage = String(BigInt(order.productType) / 10n ** 12n);
|
|
93
|
+
}
|
|
94
|
+
catch {
|
|
95
|
+
leverage = undefined;
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
const createdTime = order.createdAt
|
|
99
|
+
? new Date(Number(order.createdAt) * 1000).toISOString()
|
|
100
|
+
: undefined;
|
|
101
|
+
return {
|
|
102
|
+
...order,
|
|
103
|
+
...(margin !== undefined && { margin }),
|
|
104
|
+
...(size !== undefined && { size }),
|
|
105
|
+
...(entryPriceUsd !== undefined && { entryPriceUsd }),
|
|
106
|
+
...(strikeAmountUsd !== undefined && { strikeAmountUsd }),
|
|
107
|
+
...(leverage !== undefined && { leverage }),
|
|
108
|
+
...(createdTime !== undefined && { createdTime }),
|
|
109
|
+
};
|
|
110
|
+
}
|
|
13
111
|
export function registerOrdersCommand(program) {
|
|
14
112
|
const orders = program
|
|
15
113
|
.command('orders')
|
|
16
114
|
.description('Query positions / orders');
|
|
17
115
|
orders
|
|
18
116
|
.command('list')
|
|
19
|
-
.description('List orders for your position vault (Subgraph)')
|
|
117
|
+
.description('List orders for your position vault (Subgraph). Defaults to active positions only.')
|
|
20
118
|
.option('--network <name>', 'Network name', 'jaspervault')
|
|
21
119
|
.option('--page <number>', 'Page number (1-based)', '1')
|
|
22
120
|
.option('--limit <number>', 'Items per page', '20')
|
|
23
121
|
.option('--position-side <side>', 'Filter by position side (LONG / SHORT)')
|
|
122
|
+
.option('--all', 'Show all orders including closed (default: active only)')
|
|
123
|
+
.option('--since <time>', 'Filter orders created after this time (e.g. "7d", "1w", "2h", "2025-01-01", or Unix timestamp)')
|
|
124
|
+
.option('--until <time>', 'Filter orders created before this time (same formats as --since)')
|
|
24
125
|
.option('--pretty', 'Pretty-print JSON output')
|
|
25
126
|
.action(async (opts) => {
|
|
26
127
|
const profile = loadVaultProfile(opts.network);
|
|
@@ -31,13 +132,39 @@ export function registerOrdersCommand(program) {
|
|
|
31
132
|
const page = parseInt(opts.page ?? '1', 10);
|
|
32
133
|
const skip = (page - 1) * limit;
|
|
33
134
|
const recipient = profile.marginAccount ?? '';
|
|
135
|
+
const filter = { recipient };
|
|
136
|
+
if (!opts.all) {
|
|
137
|
+
filter.isActive = true;
|
|
138
|
+
}
|
|
139
|
+
if (opts.since) {
|
|
140
|
+
try {
|
|
141
|
+
filter.createdAtGte = parseTimeArg(opts.since);
|
|
142
|
+
}
|
|
143
|
+
catch (e) {
|
|
144
|
+
exitWithError(e.message, EXIT_CODES.CONFIG_ERROR);
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
if (opts.until) {
|
|
148
|
+
try {
|
|
149
|
+
filter.createdAtLte = parseTimeArg(opts.until);
|
|
150
|
+
}
|
|
151
|
+
catch (e) {
|
|
152
|
+
exitWithError(e.message, EXIT_CODES.CONFIG_ERROR);
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
const net = getNetworkConfig(opts.network);
|
|
34
156
|
const client = getSubgraphClient(opts.network);
|
|
35
|
-
|
|
157
|
+
const [rawOrders, total] = await Promise.all([
|
|
158
|
+
client.queryOrders(filter, limit, skip),
|
|
159
|
+
client.countOrders(filter),
|
|
160
|
+
]);
|
|
161
|
+
let formattedOrders = rawOrders.map((o) => formatOrder(o, net));
|
|
36
162
|
if (opts.positionSide) {
|
|
37
163
|
const side = opts.positionSide.toUpperCase();
|
|
38
|
-
|
|
164
|
+
formattedOrders = formattedOrders.filter((o) => o.positionSide === side);
|
|
39
165
|
}
|
|
40
|
-
|
|
166
|
+
const totalPages = Math.ceil(total / limit);
|
|
167
|
+
printJson({ orders: formattedOrders, total, page, limit, totalPages }, opts.pretty);
|
|
41
168
|
});
|
|
42
169
|
orders
|
|
43
170
|
.command('get')
|
|
@@ -46,12 +173,13 @@ export function registerOrdersCommand(program) {
|
|
|
46
173
|
.option('--network <name>', 'Network name', 'jaspervault')
|
|
47
174
|
.option('--pretty', 'Pretty-print JSON output')
|
|
48
175
|
.action(async (orderId, opts) => {
|
|
176
|
+
const net = getNetworkConfig(opts.network);
|
|
49
177
|
const client = getSubgraphClient(opts.network);
|
|
50
178
|
const order = await client.queryOrderById(orderId);
|
|
51
179
|
if (!order) {
|
|
52
180
|
exitWithError(`Order ${orderId} not found in Subgraph.`, EXIT_CODES.BUSINESS_ERROR);
|
|
53
181
|
}
|
|
54
|
-
printJson(order, opts.pretty);
|
|
182
|
+
printJson(formatOrder(order, net), opts.pretty);
|
|
55
183
|
});
|
|
56
184
|
orders
|
|
57
185
|
.command('stats')
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"orders.js","sourceRoot":"","sources":["../../../src/commands/orders.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,SAAS,EAAE,aAAa,EAAE,UAAU,EAAE,MAAM,oBAAoB,CAAC;AAC1E,OAAO,EAAE,gBAAgB,
|
|
1
|
+
{"version":3,"file":"orders.js","sourceRoot":"","sources":["../../../src/commands/orders.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,SAAS,EAAE,aAAa,EAAE,UAAU,EAAE,MAAM,oBAAoB,CAAC;AAC1E,OAAO,EAAE,gBAAgB,EAAsB,MAAM,6BAA6B,CAAC;AACnF,OAAO,EAAE,gBAAgB,EAAE,MAAM,8BAA8B,CAAC;AAChE,OAAO,EAAE,cAAc,EAA6C,MAAM,gCAAgC,CAAC;AAE3G,SAAS,iBAAiB,CAAC,OAAe;IACxC,MAAM,GAAG,GAAG,gBAAgB,CAAC,OAAO,CAAC,CAAC;IACtC,MAAM,WAAW,GAAG,OAAO,CAAC,GAAG,CAAC,eAAe,IAAI,GAAG,CAAC,WAAW,CAAC;IACnE,IAAI,CAAC,WAAW,EAAE,CAAC;QACjB,aAAa,CACX,yCAAyC,OAAO,0DAA0D,EAC1G,UAAU,CAAC,YAAY,CACxB,CAAC;IACJ,CAAC;IACD,OAAO,IAAI,cAAc,CAAC,WAAY,CAAC,CAAC;AAC1C,CAA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;SACH,OAAO,CAAC,OAAO,CAAC;SAChB,WAAW,CAAC,yDAAyD,CAAC;SACtE,MAAM,CAAC,kBAAkB,EAAE,cAAc,EAAE,aAAa,CAAC;SACzD,MAAM,CAAC,UAAU,EAAE,0BAA0B,CAAC;SAC9C,MAAM,CAAC,KAAK,EAAE,IAA2C,EAAE,EAAE;QAC5D,MAAM,OAAO,GAAG,gBAAgB,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;QAC/C,IAAI,CAAC,OAAO,EAAE,CAAC;YACb,aAAa,CACX,gCAAgC,IAAI,CAAC,OAAO,kCAAkC,IAAI,CAAC,OAAO,UAAU,EACpG,UAAU,CAAC,YAAY,CACxB,CAAC;QACJ,CAAC;QAED,MAAM,SAAS,GAAG,OAAQ,CAAC,aAAa,IAAI,EAAE,CAAC;QAC/C,MAAM,MAAM,GAAG,iBAAiB,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;QAC/C,MAAM,KAAK,GAAG,MAAM,MAAM,CAAC,eAAe,CAAC,SAAS,CAAC,CAAC;QACtD,SAAS,CAAC,KAAK,EAAE,IAAI,CAAC,MAAM,CAAC,CAAC;IAChC,CAAC,CAAC,CAAC;AACP,CAAC"}
|
|
@@ -39,6 +39,12 @@ export interface SubgraphOrder {
|
|
|
39
39
|
optionType: number;
|
|
40
40
|
}>;
|
|
41
41
|
}
|
|
42
|
+
export interface OrderQueryFilter {
|
|
43
|
+
recipient: string;
|
|
44
|
+
isActive?: boolean;
|
|
45
|
+
createdAtGte?: string;
|
|
46
|
+
createdAtLte?: string;
|
|
47
|
+
}
|
|
42
48
|
export interface OrderStats {
|
|
43
49
|
total: number;
|
|
44
50
|
active: number;
|
|
@@ -48,6 +54,7 @@ export declare class SubgraphClient {
|
|
|
48
54
|
private client;
|
|
49
55
|
constructor(endpoint: string);
|
|
50
56
|
queryOrderById(orderId: string): Promise<SubgraphOrder | null>;
|
|
51
|
-
|
|
57
|
+
queryOrders(filter: OrderQueryFilter, first?: number, skip?: number): Promise<SubgraphOrder[]>;
|
|
58
|
+
countOrders(filter: OrderQueryFilter): Promise<number>;
|
|
52
59
|
queryOrderStats(recipient: string): Promise<OrderStats>;
|
|
53
60
|
}
|
|
@@ -1,4 +1,41 @@
|
|
|
1
1
|
import { GraphQLClient } from 'graphql-request';
|
|
2
|
+
const ORDER_FIELDS = `
|
|
3
|
+
id
|
|
4
|
+
holder
|
|
5
|
+
writer
|
|
6
|
+
recipient
|
|
7
|
+
transactionHash
|
|
8
|
+
lockAmount
|
|
9
|
+
quantity
|
|
10
|
+
initialQuantity
|
|
11
|
+
strikeAmount
|
|
12
|
+
entryPrice
|
|
13
|
+
underlyingAsset
|
|
14
|
+
lockAsset
|
|
15
|
+
strikeAsset
|
|
16
|
+
marginAsset
|
|
17
|
+
positionSide
|
|
18
|
+
productType
|
|
19
|
+
marginDeposited
|
|
20
|
+
totalFreeAmount
|
|
21
|
+
createdAt
|
|
22
|
+
lastModifiedAt
|
|
23
|
+
expirationDate
|
|
24
|
+
lockDate
|
|
25
|
+
leverage
|
|
26
|
+
operationCount
|
|
27
|
+
isActive
|
|
28
|
+
blockNumber
|
|
29
|
+
perpsOptions {
|
|
30
|
+
id
|
|
31
|
+
orderID
|
|
32
|
+
hedgeOrderID
|
|
33
|
+
status
|
|
34
|
+
quantity
|
|
35
|
+
expirationDate
|
|
36
|
+
optionType
|
|
37
|
+
}
|
|
38
|
+
`;
|
|
2
39
|
const GET_ORDER_BY_ID = `
|
|
3
40
|
query GetOrderById($orderID: BigInt!) {
|
|
4
41
|
perpsOrder(id: $orderID) {
|
|
@@ -40,53 +77,6 @@ const GET_ORDER_BY_ID = `
|
|
|
40
77
|
}
|
|
41
78
|
}
|
|
42
79
|
`;
|
|
43
|
-
const GET_ORDERS_BY_RECIPIENT = `
|
|
44
|
-
query GetOrdersByRecipient($recipient: String!, $first: Int!, $skip: Int!) {
|
|
45
|
-
perpsOrders(
|
|
46
|
-
where: { recipient: $recipient }
|
|
47
|
-
first: $first
|
|
48
|
-
skip: $skip
|
|
49
|
-
orderBy: createdAt
|
|
50
|
-
orderDirection: desc
|
|
51
|
-
) {
|
|
52
|
-
id
|
|
53
|
-
holder
|
|
54
|
-
writer
|
|
55
|
-
recipient
|
|
56
|
-
transactionHash
|
|
57
|
-
lockAmount
|
|
58
|
-
quantity
|
|
59
|
-
initialQuantity
|
|
60
|
-
strikeAmount
|
|
61
|
-
entryPrice
|
|
62
|
-
underlyingAsset
|
|
63
|
-
lockAsset
|
|
64
|
-
strikeAsset
|
|
65
|
-
marginAsset
|
|
66
|
-
positionSide
|
|
67
|
-
productType
|
|
68
|
-
marginDeposited
|
|
69
|
-
totalFreeAmount
|
|
70
|
-
createdAt
|
|
71
|
-
lastModifiedAt
|
|
72
|
-
expirationDate
|
|
73
|
-
lockDate
|
|
74
|
-
leverage
|
|
75
|
-
operationCount
|
|
76
|
-
isActive
|
|
77
|
-
blockNumber
|
|
78
|
-
perpsOptions {
|
|
79
|
-
id
|
|
80
|
-
orderID
|
|
81
|
-
hedgeOrderID
|
|
82
|
-
status
|
|
83
|
-
quantity
|
|
84
|
-
expirationDate
|
|
85
|
-
optionType
|
|
86
|
-
}
|
|
87
|
-
}
|
|
88
|
-
}
|
|
89
|
-
`;
|
|
90
80
|
const GET_ORDER_STATS_BY_RECIPIENT = `
|
|
91
81
|
query GetOrderStatsByRecipient($recipient: String!) {
|
|
92
82
|
total: perpsOrders(where: { recipient: $recipient }) {
|
|
@@ -97,6 +87,19 @@ const GET_ORDER_STATS_BY_RECIPIENT = `
|
|
|
97
87
|
}
|
|
98
88
|
}
|
|
99
89
|
`;
|
|
90
|
+
function buildWhereString(filter) {
|
|
91
|
+
const parts = [`recipient: "${filter.recipient}"`];
|
|
92
|
+
if (filter.isActive !== undefined) {
|
|
93
|
+
parts.push(`isActive: ${filter.isActive}`);
|
|
94
|
+
}
|
|
95
|
+
if (filter.createdAtGte !== undefined) {
|
|
96
|
+
parts.push(`createdAt_gte: "${filter.createdAtGte}"`);
|
|
97
|
+
}
|
|
98
|
+
if (filter.createdAtLte !== undefined) {
|
|
99
|
+
parts.push(`createdAt_lte: "${filter.createdAtLte}"`);
|
|
100
|
+
}
|
|
101
|
+
return `{ ${parts.join(', ')} }`;
|
|
102
|
+
}
|
|
100
103
|
export class SubgraphClient {
|
|
101
104
|
client;
|
|
102
105
|
constructor(endpoint) {
|
|
@@ -106,10 +109,40 @@ export class SubgraphClient {
|
|
|
106
109
|
const data = await this.client.request(GET_ORDER_BY_ID, { orderID: orderId });
|
|
107
110
|
return data.perpsOrder;
|
|
108
111
|
}
|
|
109
|
-
async
|
|
110
|
-
const
|
|
112
|
+
async queryOrders(filter, first = 20, skip = 0) {
|
|
113
|
+
const where = buildWhereString(filter);
|
|
114
|
+
const query = `
|
|
115
|
+
query {
|
|
116
|
+
perpsOrders(
|
|
117
|
+
where: ${where}
|
|
118
|
+
first: ${first}
|
|
119
|
+
skip: ${skip}
|
|
120
|
+
orderBy: createdAt
|
|
121
|
+
orderDirection: desc
|
|
122
|
+
) {
|
|
123
|
+
${ORDER_FIELDS}
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
`;
|
|
127
|
+
const data = await this.client.request(query);
|
|
111
128
|
return data.perpsOrders;
|
|
112
129
|
}
|
|
130
|
+
async countOrders(filter) {
|
|
131
|
+
const where = buildWhereString(filter);
|
|
132
|
+
const query = `
|
|
133
|
+
query {
|
|
134
|
+
perpsOrders(
|
|
135
|
+
where: ${where}
|
|
136
|
+
first: 1000
|
|
137
|
+
skip: 0
|
|
138
|
+
) {
|
|
139
|
+
id
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
`;
|
|
143
|
+
const data = await this.client.request(query);
|
|
144
|
+
return data.perpsOrders.length;
|
|
145
|
+
}
|
|
113
146
|
async queryOrderStats(recipient) {
|
|
114
147
|
const data = await this.client.request(GET_ORDER_STATS_BY_RECIPIENT, { recipient });
|
|
115
148
|
const total = data.total.length;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"subgraph-client.js","sourceRoot":"","sources":["../../../src/services/subgraph-client.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,iBAAiB,CAAC;
|
|
1
|
+
{"version":3,"file":"subgraph-client.js","sourceRoot":"","sources":["../../../src/services/subgraph-client.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,iBAAiB,CAAC;AAmDhD,MAAM,YAAY,GAAG;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CAoCpB,CAAC;AAEF,MAAM,eAAe,GAAG;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CAwCvB,CAAC;AAEF,MAAM,4BAA4B,GAAG;;;;;;;;;CASpC,CAAC;AAQF,SAAS,gBAAgB,CAAC,MAAwB;IAChD,MAAM,KAAK,GAAa,CAAC,eAAe,MAAM,CAAC,SAAS,GAAG,CAAC,CAAC;IAC7D,IAAI,MAAM,CAAC,QAAQ,KAAK,SAAS,EAAE,CAAC;QAClC,KAAK,CAAC,IAAI,CAAC,aAAa,MAAM,CAAC,QAAQ,EAAE,CAAC,CAAC;IAC7C,CAAC;IACD,IAAI,MAAM,CAAC,YAAY,KAAK,SAAS,EAAE,CAAC;QACtC,KAAK,CAAC,IAAI,CAAC,mBAAmB,MAAM,CAAC,YAAY,GAAG,CAAC,CAAC;IACxD,CAAC;IACD,IAAI,MAAM,CAAC,YAAY,KAAK,SAAS,EAAE,CAAC;QACtC,KAAK,CAAC,IAAI,CAAC,mBAAmB,MAAM,CAAC,YAAY,GAAG,CAAC,CAAC;IACxD,CAAC;IACD,OAAO,KAAK,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC;AACnC,CAAC;AAED,MAAM,OAAO,cAAc;IACjB,MAAM,CAAgB;IAE9B,YAAY,QAAgB;QAC1B,IAAI,CAAC,MAAM,GAAG,IAAI,aAAa,CAAC,QAAQ,CAAC,CAAC;IAC5C,CAAC;IAED,K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