@arcadia-finance/mcp-server 0.2.0 → 0.3.0
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.
- package/README.md +61 -53
- package/dist/clients/api.js +2 -2
- package/dist/config/addresses.js +59 -4
- package/dist/index.js +38 -1
- package/dist/prompts/workflows.js +16 -13
- package/dist/tools/{write/sign-and-send.js → dev/send.js} +20 -15
- package/dist/tools/index.js +42 -32
- package/dist/tools/output-schemas.js +128 -0
- package/dist/tools/read/account.js +288 -0
- package/dist/tools/read/asset-managers.js +158 -0
- package/dist/tools/read/assets.js +52 -16
- package/dist/tools/read/guides.js +25 -4
- package/dist/tools/read/points.js +14 -20
- package/dist/tools/read/pools.js +72 -33
- package/dist/tools/read/strategy.js +162 -0
- package/dist/tools/read/wallet.js +222 -0
- package/dist/tools/{advanced → write/account}/add-liquidity.js +19 -13
- package/dist/tools/write/{borrow.js → account/borrow.js} +19 -15
- package/dist/tools/{advanced/close-position.js → write/account/close.js} +20 -17
- package/dist/tools/write/{create-account.js → account/create.js} +21 -20
- package/dist/tools/{advanced/repay-with-collateral.js → write/account/deleverage.js} +15 -8
- package/dist/tools/write/{deposit.js → account/deposit.js} +19 -15
- package/dist/tools/{advanced → write/account}/format-response.js +6 -5
- package/dist/tools/{advanced/account-metadata.js → write/account/metadata.js} +1 -1
- package/dist/tools/{advanced → write/account}/remove-liquidity.js +15 -12
- package/dist/tools/write/{repay.js → account/repay.js} +29 -14
- package/dist/tools/{advanced/position-actions.js → write/account/stake.js} +18 -13
- package/dist/tools/{advanced → write/account}/swap.js +9 -5
- package/dist/tools/write/{withdraw.js → account/withdraw.js} +20 -16
- package/dist/tools/write/asset-managers/compounder.js +111 -0
- package/dist/tools/write/asset-managers/cow-swapper.js +49 -0
- package/dist/tools/write/asset-managers/encoding.js +162 -0
- package/dist/tools/write/asset-managers/merkl-operator.js +51 -0
- package/dist/tools/write/asset-managers/rebalancer.js +108 -0
- package/dist/tools/write/asset-managers/set-asset-managers.js +75 -0
- package/dist/tools/write/asset-managers/shared.js +29 -0
- package/dist/tools/write/asset-managers/yield-claimer.js +115 -0
- package/dist/tools/write/{approve.js → wallet/approve.js} +18 -14
- package/package.json +3 -2
- package/server.json +26 -8
- package/skills/clamm-liquidity/SKILL.md +53 -47
- package/skills/clamm-liquidity/automation.md +83 -73
- package/skills/clamm-liquidity/selection.md +13 -13
- package/skills/clamm-liquidity/strategies.md +69 -71
- package/dist/tools/read/accounts.js +0 -265
- package/dist/tools/read/allowances.js +0 -72
- package/dist/tools/read/balances.js +0 -79
- package/dist/tools/read/protocol.js +0 -117
- package/dist/tools/write/configure-asset-manager.js +0 -254
- package/dist/tools/write/set-asset-manager.js +0 -62
|
@@ -2,6 +2,7 @@ import { readFileSync } from "node:fs";
|
|
|
2
2
|
import { fileURLToPath } from "node:url";
|
|
3
3
|
import { dirname, resolve } from "node:path";
|
|
4
4
|
import { z } from "zod";
|
|
5
|
+
import { GuideOutput } from "../output-schemas.js";
|
|
5
6
|
const TOPICS = {
|
|
6
7
|
overview: {
|
|
7
8
|
file: "SKILL.md",
|
|
@@ -33,7 +34,7 @@ export function registerGuideTools(server) {
|
|
|
33
34
|
guides.set(topic, `Guide "${topic}" is not available in this installation.`);
|
|
34
35
|
}
|
|
35
36
|
}
|
|
36
|
-
server.registerTool("read.
|
|
37
|
+
server.registerTool("read.guides", {
|
|
37
38
|
annotations: {
|
|
38
39
|
title: "Get Guide",
|
|
39
40
|
readOnlyHint: true,
|
|
@@ -45,9 +46,29 @@ export function registerGuideTools(server) {
|
|
|
45
46
|
inputSchema: {
|
|
46
47
|
topic: z
|
|
47
48
|
.enum(TOPIC_KEYS)
|
|
49
|
+
.optional()
|
|
48
50
|
.describe("overview = addresses + tool catalog, automation = rebalancer/compounder/claimer setup, strategies = step-by-step LP templates, selection = pool evaluation + leverage sizing"),
|
|
49
51
|
},
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
52
|
+
outputSchema: GuideOutput,
|
|
53
|
+
}, async ({ topic }) => {
|
|
54
|
+
if (!topic) {
|
|
55
|
+
const topicList = Object.entries(TOPICS)
|
|
56
|
+
.map(([key, { summary }]) => ` ${key}: ${summary}`)
|
|
57
|
+
.join("\n");
|
|
58
|
+
const result = {
|
|
59
|
+
topic: "",
|
|
60
|
+
content: `topic is required. Valid topics:\n${topicList}`,
|
|
61
|
+
};
|
|
62
|
+
return {
|
|
63
|
+
content: [{ type: "text", text: JSON.stringify(result, null, 2) }],
|
|
64
|
+
structuredContent: result,
|
|
65
|
+
isError: true,
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
const result = { topic, content: guides.get(topic) };
|
|
69
|
+
return {
|
|
70
|
+
content: [{ type: "text", text: JSON.stringify(result, null, 2) }],
|
|
71
|
+
structuredContent: result,
|
|
72
|
+
};
|
|
73
|
+
});
|
|
53
74
|
}
|
|
@@ -1,44 +1,38 @@
|
|
|
1
1
|
import { z } from "zod";
|
|
2
|
+
import { PointsLeaderboardOutput } from "../output-schemas.js";
|
|
2
3
|
export function registerPointsTools(server, api) {
|
|
3
|
-
server.registerTool("read.
|
|
4
|
+
server.registerTool("read.point_leaderboard", {
|
|
4
5
|
annotations: {
|
|
5
|
-
title: "Get Points",
|
|
6
|
+
title: "Get Points Leaderboard",
|
|
6
7
|
readOnlyHint: true,
|
|
7
8
|
destructiveHint: false,
|
|
8
9
|
idempotentHint: true,
|
|
9
10
|
openWorldHint: true,
|
|
10
11
|
},
|
|
11
|
-
description: "Get Arcadia points
|
|
12
|
+
description: "Get the Arcadia points leaderboard (paginated). For a specific wallet's points balance, use read.wallet.points.",
|
|
12
13
|
inputSchema: {
|
|
13
|
-
|
|
14
|
-
limit: z
|
|
15
|
-
.number()
|
|
16
|
-
.default(25)
|
|
17
|
-
.describe("Max leaderboard entries to return (default 25, ignored when wallet_address is provided)"),
|
|
14
|
+
limit: z.number().default(25).describe("Max leaderboard entries to return (default 25)"),
|
|
18
15
|
offset: z.number().default(0).describe("Skip first N leaderboard entries for pagination"),
|
|
19
16
|
},
|
|
20
|
-
|
|
17
|
+
outputSchema: PointsLeaderboardOutput,
|
|
18
|
+
}, async ({ limit, offset }) => {
|
|
21
19
|
try {
|
|
22
|
-
if (wallet_address) {
|
|
23
|
-
const result = await api.getPoints(wallet_address);
|
|
24
|
-
return { content: [{ type: "text", text: JSON.stringify(result, null, 2) }] };
|
|
25
|
-
}
|
|
26
20
|
const all = await api.getPointsLeaderboard();
|
|
27
21
|
const list = Array.isArray(all)
|
|
28
22
|
? all
|
|
29
23
|
: all.data;
|
|
30
24
|
if (Array.isArray(list)) {
|
|
31
25
|
const page = list.slice(offset, offset + limit);
|
|
26
|
+
const result = { total: list.length, offset, limit, leaderboard: page };
|
|
32
27
|
return {
|
|
33
|
-
content: [
|
|
34
|
-
|
|
35
|
-
type: "text",
|
|
36
|
-
text: JSON.stringify({ total: list.length, offset, limit, leaderboard: page }, null, 2),
|
|
37
|
-
},
|
|
38
|
-
],
|
|
28
|
+
content: [{ type: "text", text: JSON.stringify(result, null, 2) }],
|
|
29
|
+
structuredContent: result,
|
|
39
30
|
};
|
|
40
31
|
}
|
|
41
|
-
return {
|
|
32
|
+
return {
|
|
33
|
+
content: [{ type: "text", text: JSON.stringify(all, null, 2) }],
|
|
34
|
+
structuredContent: all,
|
|
35
|
+
};
|
|
42
36
|
}
|
|
43
37
|
catch (err) {
|
|
44
38
|
return {
|
package/dist/tools/read/pools.js
CHANGED
|
@@ -1,55 +1,94 @@
|
|
|
1
1
|
import { z } from "zod";
|
|
2
|
+
import { PoolListOutput, PoolDetailOutput } from "../output-schemas.js";
|
|
2
3
|
export function registerPoolTools(server, api) {
|
|
3
|
-
server.registerTool("read.
|
|
4
|
+
server.registerTool("read.pool.list", {
|
|
4
5
|
annotations: {
|
|
5
|
-
title: "
|
|
6
|
+
title: "List Lending Pools",
|
|
6
7
|
readOnlyHint: true,
|
|
7
8
|
destructiveHint: false,
|
|
8
9
|
idempotentHint: true,
|
|
9
10
|
openWorldHint: true,
|
|
10
11
|
},
|
|
11
|
-
description: "
|
|
12
|
+
description: "List all Arcadia lending pools: TVL, utilization, available liquidity. Key fields: interest_rate = current borrow cost, lending_apy = lender yield. All rates are decimal fractions (1.0 = 100%, 0.06 = 6%). For APY history on a specific pool, use read.pool.info.",
|
|
12
13
|
inputSchema: {
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
14
|
+
chain_id: z.number().default(8453).describe("Chain ID: 8453 (Base) or 130 (Unichain)"),
|
|
15
|
+
},
|
|
16
|
+
outputSchema: PoolListOutput,
|
|
17
|
+
}, async ({ chain_id }) => {
|
|
18
|
+
try {
|
|
19
|
+
const result = await api.getPools(chain_id);
|
|
20
|
+
if (Array.isArray(result) && result.length === 0) {
|
|
21
|
+
const empty = {
|
|
22
|
+
pools: [],
|
|
23
|
+
note: `No lending pools found on chain ${chain_id}.`,
|
|
24
|
+
};
|
|
25
|
+
return {
|
|
26
|
+
content: [{ type: "text", text: JSON.stringify(empty, null, 2) }],
|
|
27
|
+
structuredContent: empty,
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
const wrapped = { pools: Array.isArray(result) ? result : [] };
|
|
31
|
+
return {
|
|
32
|
+
content: [{ type: "text", text: JSON.stringify(wrapped, null, 2) }],
|
|
33
|
+
structuredContent: wrapped,
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
catch (err) {
|
|
37
|
+
return {
|
|
38
|
+
content: [
|
|
39
|
+
{
|
|
40
|
+
type: "text",
|
|
41
|
+
text: `Error: ${err instanceof Error ? err.message : String(err)}`,
|
|
42
|
+
},
|
|
43
|
+
],
|
|
44
|
+
isError: true,
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
});
|
|
48
|
+
server.registerTool("read.pool.info", {
|
|
49
|
+
annotations: {
|
|
50
|
+
title: "Get Pool Info",
|
|
51
|
+
readOnlyHint: true,
|
|
52
|
+
destructiveHint: false,
|
|
53
|
+
idempotentHint: true,
|
|
54
|
+
openWorldHint: true,
|
|
55
|
+
},
|
|
56
|
+
description: "Get detailed info for a single lending pool including APY history over time. Useful for analyzing rate trends and comparing pools. Use read.pool.list to discover pool addresses.",
|
|
57
|
+
inputSchema: {
|
|
58
|
+
pool_address: z.string().describe("Pool address"),
|
|
17
59
|
days: z.number().default(14).describe("Number of days of APY history"),
|
|
18
60
|
chain_id: z.number().default(8453).describe("Chain ID: 8453 (Base) or 130 (Unichain)"),
|
|
19
61
|
},
|
|
62
|
+
outputSchema: PoolDetailOutput,
|
|
20
63
|
}, async ({ pool_address, days, chain_id }) => {
|
|
21
64
|
try {
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
if (!pool) {
|
|
35
|
-
return {
|
|
36
|
-
content: [
|
|
37
|
-
{
|
|
38
|
-
type: "text",
|
|
39
|
-
text: `Error: Pool ${pool_address} not found on chain ${chain_id}.`,
|
|
40
|
-
},
|
|
41
|
-
],
|
|
42
|
-
isError: true,
|
|
43
|
-
};
|
|
44
|
-
}
|
|
65
|
+
const [allPools, apy_history] = await Promise.all([
|
|
66
|
+
api.getPools(chain_id),
|
|
67
|
+
api.getPoolApyHistory(chain_id, pool_address, days),
|
|
68
|
+
]);
|
|
69
|
+
const poolList = Array.isArray(allPools)
|
|
70
|
+
? allPools
|
|
71
|
+
: allPools.data;
|
|
72
|
+
const pool = Array.isArray(poolList)
|
|
73
|
+
? poolList.find((p) => (p.address ?? p.pool_address)?.toLowerCase() ===
|
|
74
|
+
pool_address.toLowerCase())
|
|
75
|
+
: null;
|
|
76
|
+
if (!pool) {
|
|
45
77
|
return {
|
|
46
78
|
content: [
|
|
47
|
-
{
|
|
79
|
+
{
|
|
80
|
+
type: "text",
|
|
81
|
+
text: `Error: Pool ${pool_address} not found on chain ${chain_id}.`,
|
|
82
|
+
},
|
|
48
83
|
],
|
|
84
|
+
isError: true,
|
|
49
85
|
};
|
|
50
86
|
}
|
|
51
|
-
const
|
|
52
|
-
return {
|
|
87
|
+
const detail = { pool, apy_history };
|
|
88
|
+
return {
|
|
89
|
+
content: [{ type: "text", text: JSON.stringify(detail, null, 2) }],
|
|
90
|
+
structuredContent: detail,
|
|
91
|
+
};
|
|
53
92
|
}
|
|
54
93
|
catch (err) {
|
|
55
94
|
return {
|
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
import { StrategyListOutput, StrategyDetailOutput, StrategyRecommendationOutput, } from "../output-schemas.js";
|
|
3
|
+
export function registerStrategyTools(server, api) {
|
|
4
|
+
server.registerTool("read.strategy.list", {
|
|
5
|
+
annotations: {
|
|
6
|
+
title: "Get Strategies",
|
|
7
|
+
readOnlyHint: true,
|
|
8
|
+
destructiveHint: false,
|
|
9
|
+
idempotentHint: true,
|
|
10
|
+
openWorldHint: true,
|
|
11
|
+
},
|
|
12
|
+
description: "Get Arcadia LP strategies. Use featured_only=true for curated top strategies (recommended first call). Returns a paginated list with 7d avg APY for each strategy's default range. Increase limit or use offset for pagination. All APY values are decimal fractions (1.0 = 100%, 0.05 = 5%). For full detail on a specific strategy (APY per range width), use read.strategy.info.",
|
|
13
|
+
inputSchema: {
|
|
14
|
+
featured_only: z
|
|
15
|
+
.boolean()
|
|
16
|
+
.default(false)
|
|
17
|
+
.describe("Return only featured/curated strategies (recommended)"),
|
|
18
|
+
limit: z.number().default(25).describe("Max strategies to return (default 25)"),
|
|
19
|
+
offset: z.number().default(0).describe("Skip first N strategies for pagination"),
|
|
20
|
+
chain_id: z.number().default(8453).describe("Chain ID: 8453 (Base) or 130 (Unichain)"),
|
|
21
|
+
},
|
|
22
|
+
outputSchema: StrategyListOutput,
|
|
23
|
+
}, async ({ featured_only, limit, offset, chain_id }) => {
|
|
24
|
+
try {
|
|
25
|
+
let strategies;
|
|
26
|
+
let total;
|
|
27
|
+
if (featured_only) {
|
|
28
|
+
const raw = await api.getFeatured(chain_id);
|
|
29
|
+
strategies = Array.isArray(raw) ? raw : [];
|
|
30
|
+
total = strategies.length;
|
|
31
|
+
const result = { total, offset: 0, limit: total, strategies };
|
|
32
|
+
return {
|
|
33
|
+
content: [{ type: "text", text: JSON.stringify(result, null, 2) }],
|
|
34
|
+
structuredContent: result,
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
const all = await api.getStrategies(chain_id);
|
|
38
|
+
const list = Array.isArray(all) ? all : all.data;
|
|
39
|
+
if (Array.isArray(list)) {
|
|
40
|
+
total = list.length;
|
|
41
|
+
const effectiveOffset = offset ?? 0;
|
|
42
|
+
const effectiveLimit = limit ?? 25;
|
|
43
|
+
const page = list.slice(effectiveOffset, effectiveOffset + effectiveLimit);
|
|
44
|
+
strategies = page.map((s) => ({
|
|
45
|
+
strategy_id: s.strategy_id,
|
|
46
|
+
name: s.strategy_name,
|
|
47
|
+
strategy_apy_7d: s.strategy_apy,
|
|
48
|
+
tvl: s.tvl,
|
|
49
|
+
underlyings: s.underlyings?.map((u) => ({
|
|
50
|
+
address: u.underlying_address,
|
|
51
|
+
symbol: u.underlying_symbol,
|
|
52
|
+
})),
|
|
53
|
+
max_leverage: s.max_leverage,
|
|
54
|
+
}));
|
|
55
|
+
const result = {
|
|
56
|
+
total,
|
|
57
|
+
offset: effectiveOffset,
|
|
58
|
+
limit: effectiveLimit,
|
|
59
|
+
strategies,
|
|
60
|
+
};
|
|
61
|
+
return {
|
|
62
|
+
content: [{ type: "text", text: JSON.stringify(result, null, 2) }],
|
|
63
|
+
structuredContent: result,
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
const fallback = { total: 0, offset: 0, limit, strategies: [] };
|
|
67
|
+
return {
|
|
68
|
+
content: [{ type: "text", text: JSON.stringify(fallback, null, 2) }],
|
|
69
|
+
structuredContent: fallback,
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
catch (err) {
|
|
73
|
+
return {
|
|
74
|
+
content: [
|
|
75
|
+
{
|
|
76
|
+
type: "text",
|
|
77
|
+
text: `Error: ${err instanceof Error ? err.message : String(err)}`,
|
|
78
|
+
},
|
|
79
|
+
],
|
|
80
|
+
isError: true,
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
});
|
|
84
|
+
server.registerTool("read.strategy.info", {
|
|
85
|
+
annotations: {
|
|
86
|
+
title: "Get Strategy Info",
|
|
87
|
+
readOnlyHint: true,
|
|
88
|
+
destructiveHint: false,
|
|
89
|
+
idempotentHint: true,
|
|
90
|
+
openWorldHint: true,
|
|
91
|
+
},
|
|
92
|
+
description: "Get full detail for a specific LP strategy by ID — includes APY per range width (narrower range = higher APY but more rebalancing cost/risk), pool info, and configuration. Use read.strategy.list to discover strategy IDs. All APY values are decimal fractions (1.0 = 100%, 0.05 = 5%).",
|
|
93
|
+
inputSchema: {
|
|
94
|
+
strategy_id: z.number().describe("Strategy ID"),
|
|
95
|
+
chain_id: z.number().default(8453).describe("Chain ID: 8453 (Base) or 130 (Unichain)"),
|
|
96
|
+
},
|
|
97
|
+
outputSchema: StrategyDetailOutput,
|
|
98
|
+
}, async ({ strategy_id, chain_id }) => {
|
|
99
|
+
try {
|
|
100
|
+
const result = await api.getStrategyInfo(chain_id, strategy_id);
|
|
101
|
+
return {
|
|
102
|
+
content: [{ type: "text", text: JSON.stringify(result, null, 2) }],
|
|
103
|
+
structuredContent: result,
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
catch (err) {
|
|
107
|
+
return {
|
|
108
|
+
content: [
|
|
109
|
+
{
|
|
110
|
+
type: "text",
|
|
111
|
+
text: `Error: ${err instanceof Error ? err.message : String(err)}`,
|
|
112
|
+
},
|
|
113
|
+
],
|
|
114
|
+
isError: true,
|
|
115
|
+
};
|
|
116
|
+
}
|
|
117
|
+
});
|
|
118
|
+
server.registerTool("read.strategy.recommendation", {
|
|
119
|
+
annotations: {
|
|
120
|
+
title: "Get Recommendation",
|
|
121
|
+
readOnlyHint: true,
|
|
122
|
+
destructiveHint: false,
|
|
123
|
+
idempotentHint: true,
|
|
124
|
+
openWorldHint: true,
|
|
125
|
+
},
|
|
126
|
+
description: "Get a rebalancing recommendation for an Arcadia account — suggests asset changes to optimize yield. Uses 1d APY (not 7d like read.strategy.list), so recommended strategies may differ from the list ranking. APY values are decimal fractions (0.05 = 5%). weekly_earning_difference is in USD.",
|
|
127
|
+
inputSchema: {
|
|
128
|
+
account_address: z.string().describe("Arcadia account address"),
|
|
129
|
+
chain_id: z.number().default(8453).describe("Chain ID: 8453 (Base) or 130 (Unichain)"),
|
|
130
|
+
},
|
|
131
|
+
outputSchema: StrategyRecommendationOutput,
|
|
132
|
+
}, async ({ account_address, chain_id }) => {
|
|
133
|
+
try {
|
|
134
|
+
const result = await api.getRecommendation(chain_id, account_address);
|
|
135
|
+
const rec = result;
|
|
136
|
+
if (Array.isArray(rec.added_assets) &&
|
|
137
|
+
rec.added_assets.length === 0 &&
|
|
138
|
+
Array.isArray(rec.removed_assets) &&
|
|
139
|
+
rec.removed_assets.length === 0 &&
|
|
140
|
+
Number(rec.current_apy ?? 0) === 0 &&
|
|
141
|
+
Number(rec.proposed_apy ?? 0) === 0) {
|
|
142
|
+
rec.context_note =
|
|
143
|
+
"Account has no active positions — no rebalancing recommendations available.";
|
|
144
|
+
}
|
|
145
|
+
return {
|
|
146
|
+
content: [{ type: "text", text: JSON.stringify(rec, null, 2) }],
|
|
147
|
+
structuredContent: rec,
|
|
148
|
+
};
|
|
149
|
+
}
|
|
150
|
+
catch (err) {
|
|
151
|
+
return {
|
|
152
|
+
content: [
|
|
153
|
+
{
|
|
154
|
+
type: "text",
|
|
155
|
+
text: `Error: ${err instanceof Error ? err.message : String(err)}`,
|
|
156
|
+
},
|
|
157
|
+
],
|
|
158
|
+
isError: true,
|
|
159
|
+
};
|
|
160
|
+
}
|
|
161
|
+
});
|
|
162
|
+
}
|
|
@@ -0,0 +1,222 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
import { formatUnits } from "viem";
|
|
3
|
+
import { erc20Abi } from "../../abis/index.js";
|
|
4
|
+
import { getPublicClient } from "../../clients/chain.js";
|
|
5
|
+
import { validateAddress, validateChainId } from "../../utils/validation.js";
|
|
6
|
+
import { WalletBalancesOutput, WalletAllowanceOutput, AccountListOutput, PointsWalletOutput, } from "../output-schemas.js";
|
|
7
|
+
const MAX_UINT256 = 2n ** 256n - 1n;
|
|
8
|
+
export function registerWalletTools(server, chains, api) {
|
|
9
|
+
server.registerTool("read.wallet.balances", {
|
|
10
|
+
annotations: {
|
|
11
|
+
title: "Get Wallet Balances",
|
|
12
|
+
readOnlyHint: true,
|
|
13
|
+
destructiveHint: false,
|
|
14
|
+
idempotentHint: true,
|
|
15
|
+
openWorldHint: true,
|
|
16
|
+
},
|
|
17
|
+
description: "Get native ETH and ERC20 token balances for a wallet address. Reads directly from chain via RPC multicall. Use before write.account.add_liquidity or write.account.deposit to verify the wallet has sufficient tokens. Returns both raw balance (smallest unit/wei) and formatted (human-readable) per token.",
|
|
18
|
+
inputSchema: {
|
|
19
|
+
wallet_address: z.string().describe("Wallet address to check balances for"),
|
|
20
|
+
token_addresses: z.array(z.string()).describe("ERC20 token contract addresses to check"),
|
|
21
|
+
chain_id: z.number().default(8453).describe("Chain ID: 8453 (Base) or 130 (Unichain)"),
|
|
22
|
+
},
|
|
23
|
+
outputSchema: WalletBalancesOutput,
|
|
24
|
+
}, async ({ wallet_address, token_addresses, chain_id }) => {
|
|
25
|
+
try {
|
|
26
|
+
const validChainId = validateChainId(chain_id);
|
|
27
|
+
const wallet = validateAddress(wallet_address, "wallet_address");
|
|
28
|
+
const client = getPublicClient(validChainId, chains);
|
|
29
|
+
// Build multicall: for each token get balanceOf, decimals, symbol
|
|
30
|
+
const calls = token_addresses.flatMap((addr) => [
|
|
31
|
+
{
|
|
32
|
+
address: addr,
|
|
33
|
+
abi: erc20Abi,
|
|
34
|
+
functionName: "balanceOf",
|
|
35
|
+
args: [wallet],
|
|
36
|
+
},
|
|
37
|
+
{ address: addr, abi: erc20Abi, functionName: "decimals" },
|
|
38
|
+
{ address: addr, abi: erc20Abi, functionName: "symbol" },
|
|
39
|
+
]);
|
|
40
|
+
const [nativeBalance, ...multicallResults] = await Promise.all([
|
|
41
|
+
client.getBalance({ address: wallet }),
|
|
42
|
+
...(calls.length > 0 ? [client.multicall({ contracts: calls, allowFailure: true })] : []),
|
|
43
|
+
]);
|
|
44
|
+
const results = (multicallResults[0] ?? []);
|
|
45
|
+
const tokens = token_addresses.map((addr, i) => {
|
|
46
|
+
const balRes = results[i * 3];
|
|
47
|
+
const decRes = results[i * 3 + 1];
|
|
48
|
+
const symRes = results[i * 3 + 2];
|
|
49
|
+
const balance = balRes?.status === "success" ? String(balRes.result) : "0";
|
|
50
|
+
const decimals = decRes?.status === "success" ? Number(decRes.result) : 18;
|
|
51
|
+
const symbol = symRes?.status === "success" ? String(symRes.result) : "???";
|
|
52
|
+
return {
|
|
53
|
+
address: addr,
|
|
54
|
+
symbol,
|
|
55
|
+
decimals,
|
|
56
|
+
balance,
|
|
57
|
+
formatted: formatUnits(BigInt(balance), decimals),
|
|
58
|
+
};
|
|
59
|
+
});
|
|
60
|
+
const result = {
|
|
61
|
+
native: {
|
|
62
|
+
symbol: "ETH",
|
|
63
|
+
balance: String(nativeBalance),
|
|
64
|
+
formatted: formatUnits(nativeBalance, 18),
|
|
65
|
+
},
|
|
66
|
+
tokens,
|
|
67
|
+
};
|
|
68
|
+
return {
|
|
69
|
+
content: [{ type: "text", text: JSON.stringify(result, null, 2) }],
|
|
70
|
+
structuredContent: result,
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
catch (err) {
|
|
74
|
+
return {
|
|
75
|
+
content: [
|
|
76
|
+
{
|
|
77
|
+
type: "text",
|
|
78
|
+
text: `Error: ${err instanceof Error ? err.message : String(err)}`,
|
|
79
|
+
},
|
|
80
|
+
],
|
|
81
|
+
isError: true,
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
});
|
|
85
|
+
server.registerTool("read.wallet.allowances", {
|
|
86
|
+
annotations: {
|
|
87
|
+
title: "Get Token Allowances",
|
|
88
|
+
readOnlyHint: true,
|
|
89
|
+
destructiveHint: false,
|
|
90
|
+
idempotentHint: true,
|
|
91
|
+
openWorldHint: true,
|
|
92
|
+
},
|
|
93
|
+
description: "Check ERC20 token allowances for a spender address. Use before write.wallet.approve to avoid redundant approvals — skip approving if the current allowance is already sufficient.",
|
|
94
|
+
inputSchema: {
|
|
95
|
+
owner_address: z.string().describe("Token owner address (the wallet granting approval)"),
|
|
96
|
+
spender_address: z
|
|
97
|
+
.string()
|
|
98
|
+
.describe("Spender address to check allowance for (e.g. Arcadia account address)"),
|
|
99
|
+
token_addresses: z.array(z.string()).describe("ERC20 token contract addresses to check"),
|
|
100
|
+
chain_id: z.number().default(8453).describe("Chain ID: 8453 (Base) or 130 (Unichain)"),
|
|
101
|
+
},
|
|
102
|
+
outputSchema: WalletAllowanceOutput,
|
|
103
|
+
}, async ({ owner_address, spender_address, token_addresses, chain_id }) => {
|
|
104
|
+
try {
|
|
105
|
+
const validChainId = validateChainId(chain_id);
|
|
106
|
+
const owner = validateAddress(owner_address, "owner_address");
|
|
107
|
+
const spender = validateAddress(spender_address, "spender_address");
|
|
108
|
+
const client = getPublicClient(validChainId, chains);
|
|
109
|
+
const calls = token_addresses.flatMap((addr) => [
|
|
110
|
+
{
|
|
111
|
+
address: addr,
|
|
112
|
+
abi: erc20Abi,
|
|
113
|
+
functionName: "allowance",
|
|
114
|
+
args: [owner, spender],
|
|
115
|
+
},
|
|
116
|
+
{ address: addr, abi: erc20Abi, functionName: "decimals" },
|
|
117
|
+
{ address: addr, abi: erc20Abi, functionName: "symbol" },
|
|
118
|
+
]);
|
|
119
|
+
const results = (await client.multicall({ contracts: calls, allowFailure: true }));
|
|
120
|
+
const tokens = token_addresses.map((addr, i) => {
|
|
121
|
+
const allowRes = results[i * 3];
|
|
122
|
+
const decRes = results[i * 3 + 1];
|
|
123
|
+
const symRes = results[i * 3 + 2];
|
|
124
|
+
const allowance = allowRes?.status === "success" ? BigInt(allowRes.result) : 0n;
|
|
125
|
+
const decimals = decRes?.status === "success" ? Number(decRes.result) : 18;
|
|
126
|
+
const symbol = symRes?.status === "success" ? String(symRes.result) : "???";
|
|
127
|
+
return {
|
|
128
|
+
address: addr,
|
|
129
|
+
symbol,
|
|
130
|
+
decimals,
|
|
131
|
+
allowance: String(allowance),
|
|
132
|
+
formatted: formatUnits(allowance, decimals),
|
|
133
|
+
is_max_approval: allowance === MAX_UINT256,
|
|
134
|
+
};
|
|
135
|
+
});
|
|
136
|
+
return {
|
|
137
|
+
content: [{ type: "text", text: JSON.stringify({ tokens }, null, 2) }],
|
|
138
|
+
structuredContent: { tokens },
|
|
139
|
+
};
|
|
140
|
+
}
|
|
141
|
+
catch (err) {
|
|
142
|
+
return {
|
|
143
|
+
content: [
|
|
144
|
+
{
|
|
145
|
+
type: "text",
|
|
146
|
+
text: `Error: ${err instanceof Error ? err.message : String(err)}`,
|
|
147
|
+
},
|
|
148
|
+
],
|
|
149
|
+
isError: true,
|
|
150
|
+
};
|
|
151
|
+
}
|
|
152
|
+
});
|
|
153
|
+
server.registerTool("read.wallet.accounts", {
|
|
154
|
+
annotations: {
|
|
155
|
+
title: "List Wallet Accounts",
|
|
156
|
+
readOnlyHint: true,
|
|
157
|
+
destructiveHint: false,
|
|
158
|
+
idempotentHint: true,
|
|
159
|
+
openWorldHint: true,
|
|
160
|
+
},
|
|
161
|
+
description: "List all Arcadia accounts owned by a wallet address. Returns a summary of each account (address, name). Call read.account.info with a specific account_address for full details like health factor, collateral, and debt.",
|
|
162
|
+
inputSchema: {
|
|
163
|
+
wallet_address: z.string().describe("Wallet address to list accounts for"),
|
|
164
|
+
chain_id: z.number().default(8453).describe("Chain ID: 8453 (Base) or 130 (Unichain)"),
|
|
165
|
+
},
|
|
166
|
+
outputSchema: AccountListOutput,
|
|
167
|
+
}, async ({ wallet_address, chain_id }) => {
|
|
168
|
+
try {
|
|
169
|
+
const validChainId = validateChainId(chain_id);
|
|
170
|
+
validateAddress(wallet_address, "wallet_address");
|
|
171
|
+
const result = await api.getAccounts(validChainId, wallet_address);
|
|
172
|
+
return {
|
|
173
|
+
content: [{ type: "text", text: JSON.stringify(result, null, 2) }],
|
|
174
|
+
structuredContent: result,
|
|
175
|
+
};
|
|
176
|
+
}
|
|
177
|
+
catch (err) {
|
|
178
|
+
return {
|
|
179
|
+
content: [
|
|
180
|
+
{
|
|
181
|
+
type: "text",
|
|
182
|
+
text: `Error: ${err instanceof Error ? err.message : String(err)}`,
|
|
183
|
+
},
|
|
184
|
+
],
|
|
185
|
+
isError: true,
|
|
186
|
+
};
|
|
187
|
+
}
|
|
188
|
+
});
|
|
189
|
+
server.registerTool("read.wallet.points", {
|
|
190
|
+
annotations: {
|
|
191
|
+
title: "Get Wallet Points",
|
|
192
|
+
readOnlyHint: true,
|
|
193
|
+
destructiveHint: false,
|
|
194
|
+
idempotentHint: true,
|
|
195
|
+
openWorldHint: true,
|
|
196
|
+
},
|
|
197
|
+
description: "Get Arcadia points balance for a specific wallet address.",
|
|
198
|
+
inputSchema: {
|
|
199
|
+
wallet_address: z.string().describe("Wallet address to get points for"),
|
|
200
|
+
},
|
|
201
|
+
outputSchema: PointsWalletOutput,
|
|
202
|
+
}, async ({ wallet_address }) => {
|
|
203
|
+
try {
|
|
204
|
+
const result = await api.getPoints(wallet_address);
|
|
205
|
+
return {
|
|
206
|
+
content: [{ type: "text", text: JSON.stringify(result, null, 2) }],
|
|
207
|
+
structuredContent: result,
|
|
208
|
+
};
|
|
209
|
+
}
|
|
210
|
+
catch (err) {
|
|
211
|
+
return {
|
|
212
|
+
content: [
|
|
213
|
+
{
|
|
214
|
+
type: "text",
|
|
215
|
+
text: `Error: ${err instanceof Error ? err.message : String(err)}`,
|
|
216
|
+
},
|
|
217
|
+
],
|
|
218
|
+
isError: true,
|
|
219
|
+
};
|
|
220
|
+
}
|
|
221
|
+
});
|
|
222
|
+
}
|