-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathapi.ts
More file actions
189 lines (175 loc) · 5.59 KB
/
api.ts
File metadata and controls
189 lines (175 loc) · 5.59 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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
import type { AgentConfig, Api, KnownChainId } from "@polkadot-agent-kit/common"
import { getAllSupportedChains, getChainById } from "@polkadot-agent-kit/common"
import type { IPolkadotApi } from "@polkadot-agent-kit/core"
import { PolkadotApi } from "@polkadot-agent-kit/core"
import type {
BalanceTool,
IPolkadotAgentApi,
TransferTool,
XcmTransferNativeAssetTool
} from "@polkadot-agent-kit/llm"
import { PolkadotAgentApi } from "@polkadot-agent-kit/llm"
import { ed25519CreateDerive, sr25519CreateDerive } from "@polkadot-labs/hdkd"
import * as ss58 from "@subsquid/ss58"
import { getPolkadotSigner, type PolkadotSigner } from "polkadot-api/signer"
export class PolkadotAgentKit implements IPolkadotApi, IPolkadotAgentApi {
private polkadotApi: PolkadotApi
private agentApi: PolkadotAgentApi
public wallet: string
public config: AgentConfig
constructor(wallet: string, config: AgentConfig) {
this.polkadotApi = new PolkadotApi()
this.agentApi = new PolkadotAgentApi(this.polkadotApi)
this.wallet = wallet
this.config = config
}
setApi(chainId: KnownChainId, api?: Api<KnownChainId>) {
this.polkadotApi.setApi(chainId, api)
}
getApi(chainId: KnownChainId): Api<KnownChainId> {
return this.polkadotApi.getApi(chainId)
}
async initializeApi(): Promise<void> {
try {
await this.polkadotApi.initializeApi()
} catch (error) {
throw new Error(
`PolkadotAgentKit API initialization failed: ${error instanceof Error ? error.message : String(error)}`
)
}
}
disconnect(): Promise<void> {
return this.polkadotApi.disconnect()
}
/**
* Get Native Balance Tool
* Creates a tool for checking native token balance of an address
*
* @param address - The address to check balance for
* @returns DynamicStructuredTool for checking native token balance
*
* @example
* ```typescript
* // Create a balance checking tool
* const balanceTool = agent.getNativeBalanceTool("5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY");
*
* // Tool can be used with LangChain
* const result = await balanceTool.call(\{ address \});
* ```
*/
getNativeBalanceTool(): BalanceTool {
const address = this.getCurrentAddress()
return this.agentApi.getNativeBalanceTool(address)
}
/**
* Get Native Transfer Tool
* Creates a tool for transferring native tokens to an address
*
* @returns DynamicStructuredTool for transferring native tokens
*
* @example
* ```typescript
* // Create a transfer tool
* const transferTool = agent.transferNativeTool();
*
* // Tool can be used with LangChain
* const result = await transferTool.call({
* amount: "1",
* to: "5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY",
* chain: "polkadot"
* });
* ```
*
* @throws \{Error\} If the transfer fails or parameters are invalid
*/
transferNativeTool(): TransferTool {
return this.agentApi.transferNativeTool(this.getSigner())
}
/**
* Get XCM Transfer Native Tool
* Creates a tool for transferring native tokens across chains using XCM (Cross-Consensus Messaging)
*
* @returns DynamicStructuredTool for cross-chain native token transfers
*
* @example
* ```typescript
* // Create an XCM transfer tool
* const xcmTransferTool = agent.xcmTransferNativeTool();
*
* // Tool can be used with LangChain for cross-chain transfers
* const result = await xcmTransferTool.call(\{
* amount: "1",
* to: "5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY",
* sourceChain: "polkadot",
* destChain: "polkadot_asset_hub"
* \});
* ```
*
* @throws \{Error\} If the XCM transfer fails or parameters are invalid
*/
xcmTransferNativeTool(): XcmTransferNativeAssetTool {
return this.agentApi.xcmTransferNativeTool(this.getSigner(), this.getCurrentAddress())
}
/**
* Get Address
*
* @returns The address as string
* @throws Error if no main private key is available
*
* @example
* ```typescript
* // Get the main account address
* const address = agent.getCurrentAddress('polkadot');
* ```
*/
public getCurrentAddress(): string {
// get chain default address polkadot
const chain = getChainById("polkadot", getAllSupportedChains())
const keypair = this.getKeypair()
const value = keypair.publicKey
if (!value) {
return ""
}
return ss58.codec(chain.prefix).encode(value)
}
/**
* Get main account public key
*
* @returns The public key as Uint8Array
* @throws Error if no main private key is available
*
* @example
* ```typescript
* // Get the main account public key
* const publicKey = agent.getPublicKey();
* ```
*/
private getKeypair() {
if (this.config.keyType === "Sr25519") {
// For Sr25519, use the derive function to get the public key
const derive = sr25519CreateDerive(this.wallet)
return derive(this.config.derivationPath || "")
} else {
// For Ed25519, use the ed25519 lib
const derive = ed25519CreateDerive(this.wallet)
return derive(this.config.derivationPath || "")
}
}
private getSigner(): PolkadotSigner {
if (this.config.keyType === "Sr25519") {
const signer = getPolkadotSigner(
this.getKeypair().publicKey,
this.config.keyType as "Sr25519" | "Ed25519" | "Ecdsa",
input => this.getKeypair().sign(input)
)
return signer
} else {
const signer = getPolkadotSigner(
this.getKeypair().publicKey,
this.config.keyType as "Sr25519" | "Ed25519" | "Ecdsa",
input => this.getKeypair().sign(input)
)
return signer
}
}
}