@swapkit/plugins 4.1.16 → 4.2.3

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.
@@ -0,0 +1,216 @@
1
+ import { AssetValue, Chain, ProviderName, SwapKitError, type SwapParams } from "@swapkit/helpers";
2
+ import type { QuoteResponseRoute } from "@swapkit/helpers/api";
3
+ import type { NearWallet } from "@swapkit/toolboxes/near";
4
+ import { createPlugin } from "../utils";
5
+ import { calculateNearNameCost, validateNearName } from "./nearNames";
6
+ import type { NearAccountInfo, NearNameRegistrationParams } from "./types";
7
+
8
+ export const NearPlugin = createPlugin({
9
+ methods: ({ getWallet }) => ({
10
+ nearNames: {
11
+ async getInfo(name: string): Promise<NearAccountInfo | null> {
12
+ const normalizedName = name.toLowerCase().replace(/\.near$/, "");
13
+
14
+ if (!validateNearName(normalizedName)) {
15
+ throw new SwapKitError("plugin_near_invalid_name");
16
+ }
17
+
18
+ const accountId = `${normalizedName}.near`;
19
+ const wallet = getWallet(Chain.Near);
20
+
21
+ if (!wallet) {
22
+ throw new SwapKitError("plugin_near_no_connection");
23
+ }
24
+
25
+ try {
26
+ // Get account info
27
+ const accountInfo = await wallet.provider.query({
28
+ account_id: accountId,
29
+ finality: "final",
30
+ request_type: "view_account",
31
+ });
32
+
33
+ // Optionally get the account's public keys
34
+ const keysInfo = await wallet.provider.query({
35
+ account_id: accountId,
36
+ finality: "final",
37
+ request_type: "view_access_key_list",
38
+ });
39
+
40
+ return {
41
+ accountId,
42
+ balance: (accountInfo as any).amount,
43
+ codeHash: (accountInfo as any).code_hash,
44
+ publicKeys: (keysInfo as any).keys?.map((k: any) => k.public_key) || [],
45
+ storageUsed: (accountInfo as any).storage_usage,
46
+ };
47
+ } catch (err: any) {
48
+ if (/UNKNOWN_ACCOUNT|does not exist while viewing/.test(err.message)) {
49
+ return null;
50
+ }
51
+ throw err;
52
+ }
53
+ },
54
+
55
+ async isAvailable(name: string) {
56
+ const owner = await this.resolve(name);
57
+ return owner === null;
58
+ },
59
+
60
+ async lookupNames(accountId: string) {
61
+ // NEAR doesn't have a central registry to look up all names owned by an account
62
+ // This would require indexing or an external service
63
+ // For now, we can only check if a specific account exists
64
+ const wallet = getWallet(Chain.Near);
65
+
66
+ if (!wallet) {
67
+ throw new SwapKitError("plugin_near_no_connection");
68
+ }
69
+
70
+ try {
71
+ // Check if the account exists
72
+ await wallet.provider.query({ account_id: accountId, finality: "final", request_type: "view_account" });
73
+
74
+ // If the account ID ends with .near, it's a NEAR name
75
+ if (accountId.endsWith(".near")) {
76
+ return [accountId];
77
+ }
78
+
79
+ // Otherwise, we can't determine what names they own without an indexer
80
+ return [];
81
+ } catch {
82
+ return [];
83
+ }
84
+ },
85
+
86
+ async register(params: NearNameRegistrationParams) {
87
+ const { name, publicKey: publicKeyOverwrite } = params;
88
+ const normalizedName = name.toLowerCase().replace(/\.near$/, "");
89
+
90
+ if (!validateNearName(normalizedName)) {
91
+ throw new SwapKitError("plugin_near_invalid_name");
92
+ }
93
+
94
+ const wallet = getWallet(Chain.Near) as NearWallet;
95
+
96
+ const newPublicKey = publicKeyOverwrite || (await wallet.getPublicKey());
97
+
98
+ const cost = calculateNearNameCost(normalizedName);
99
+
100
+ return wallet.callFunction({
101
+ args: { new_account_id: `${normalizedName}.near`, new_public_key: newPublicKey },
102
+ contractId: "near",
103
+ deposit: cost,
104
+ methodName: "create_account",
105
+ });
106
+ },
107
+ async resolve(name: string) {
108
+ const normalizedName = name.toLowerCase().replace(/\.near$/, "");
109
+
110
+ if (!validateNearName(normalizedName)) {
111
+ throw new SwapKitError("plugin_near_invalid_name");
112
+ }
113
+
114
+ const accountId = `${normalizedName}.near`;
115
+ const wallet = getWallet(Chain.Near);
116
+
117
+ if (!wallet) {
118
+ throw new SwapKitError("plugin_near_no_connection");
119
+ }
120
+
121
+ try {
122
+ // Ask RPC whether the account exists
123
+ await wallet.provider.query({ account_id: accountId, finality: "final", request_type: "view_account" });
124
+ // If no error is thrown, the account exists
125
+ return accountId; // Account is taken, return the account ID as "owner"
126
+ } catch (err: any) {
127
+ // UNKNOWN_ACCOUNT means it hasn't been created yet → available
128
+ if (/UNKNOWN_ACCOUNT|does not exist while viewing/.test(err.message)) {
129
+ return null;
130
+ }
131
+ // Re-throw any unexpected errors
132
+ throw err;
133
+ }
134
+ },
135
+
136
+ transfer(name: string, newOwner: string) {
137
+ const normalizedName = name.toLowerCase().replace(/\.near$/, "");
138
+
139
+ if (!validateNearName(normalizedName)) {
140
+ throw new SwapKitError("plugin_near_invalid_name");
141
+ }
142
+
143
+ const wallet = getWallet(Chain.Near) as NearWallet;
144
+
145
+ return wallet.callFunction({
146
+ args: { name: normalizedName, new_owner: newOwner },
147
+ contractId: "near",
148
+ deposit: "1",
149
+ methodName: "transfer",
150
+ });
151
+ },
152
+ },
153
+ async swap(swapParams: SwapParams<"near", QuoteResponseRoute>) {
154
+ const {
155
+ route: {
156
+ buyAsset: buyAssetString,
157
+ sellAsset: sellAssetString,
158
+ inboundAddress,
159
+ sellAmount,
160
+ meta: { near },
161
+ },
162
+ } = swapParams;
163
+
164
+ if (!(sellAssetString && buyAssetString && near?.sellAsset)) {
165
+ throw new SwapKitError("core_swap_asset_not_recognized");
166
+ }
167
+
168
+ if (!inboundAddress) {
169
+ throw new SwapKitError("core_swap_invalid_params", { missing: ["inboundAddress"] });
170
+ }
171
+
172
+ const sellAsset = await AssetValue.from({ asset: sellAssetString, value: sellAmount });
173
+
174
+ const sellAssetChain = sellAsset.chain;
175
+
176
+ const wallet = getWallet(sellAsset.chain as Exclude<Chain, typeof Chain.Radix>);
177
+
178
+ if (sellAssetChain === Chain.Near && !sellAsset.isGasAsset) {
179
+ const wallet = getWallet(sellAsset.chain as Chain.Near);
180
+ if (!wallet) {
181
+ throw new SwapKitError("core_wallet_connection_not_found");
182
+ }
183
+
184
+ const unsignedTransaction = await wallet.createContractFunctionCall({
185
+ args: {
186
+ amount: sellAsset.getBaseValue("string"),
187
+ msg: JSON.stringify({ receiver_id: inboundAddress }),
188
+ receiver_id: "intents.near",
189
+ },
190
+ attachedDeposit: "1",
191
+ contractId: sellAsset.address as string,
192
+ gas: "250000000000000",
193
+ methodName: "ft_transfer_call",
194
+ sender: wallet.address,
195
+ });
196
+
197
+ return wallet.signAndSendTransaction(unsignedTransaction);
198
+ }
199
+
200
+ if (!wallet) {
201
+ throw new SwapKitError("core_wallet_connection_not_found");
202
+ }
203
+
204
+ const tx = await wallet.transfer({
205
+ assetValue: sellAsset,
206
+ isProgramDerivedAddress: true,
207
+ recipient: inboundAddress,
208
+ sender: wallet.address,
209
+ });
210
+
211
+ return tx as string;
212
+ },
213
+ }),
214
+ name: "near",
215
+ properties: { supportedSwapkitProviders: [ProviderName.NEAR] as const },
216
+ });
@@ -0,0 +1,9 @@
1
+ export type NearAccountInfo = {
2
+ accountId: string;
3
+ balance: string;
4
+ storageUsed: number;
5
+ codeHash: string;
6
+ publicKeys: string[];
7
+ };
8
+
9
+ export type NearNameRegistrationParams = { name: string; publicKey?: string };
@@ -0,0 +1,24 @@
1
+ import { AssetValue, Chain, ProviderName, SwapKitError, type SwapParams } from "@swapkit/helpers";
2
+ import type { QuoteResponseRoute } from "@swapkit/helpers/api";
3
+ import { createPlugin } from "../utils";
4
+
5
+ export const RadixPlugin = createPlugin({
6
+ methods: ({ getWallet }) => ({
7
+ swap: async function radixSwap({ route: { tx, sellAmount, sellAsset } }: SwapParams<"radix", QuoteResponseRoute>) {
8
+ const assetValue = await AssetValue.from({ asset: sellAsset, asyncTokenLookup: true, value: sellAmount });
9
+
10
+ if (Chain.Radix !== assetValue.chain) {
11
+ throw new SwapKitError("core_swap_invalid_params");
12
+ }
13
+
14
+ const wallet = getWallet(assetValue.chain);
15
+ try {
16
+ return wallet.signAndBroadcast({ manifest: tx as string });
17
+ } catch (error) {
18
+ throw new SwapKitError("core_swap_invalid_params", error);
19
+ }
20
+ },
21
+ }),
22
+ name: "radix",
23
+ properties: { supportedSwapkitProviders: [ProviderName.CAVIAR_V1] as const },
24
+ });
@@ -0,0 +1 @@
1
+ export { SolanaPlugin } from "./plugin";
@@ -0,0 +1,26 @@
1
+ import { AssetValue, Chain, ProviderName, SwapKitError, type SwapParams } from "@swapkit/helpers";
2
+ import type { QuoteResponseRoute } from "@swapkit/helpers/api";
3
+ import { createPlugin } from "../utils";
4
+
5
+ export const SolanaPlugin = createPlugin({
6
+ methods: ({ getWallet }) => ({
7
+ swap: async function solanaSwap({ route }: SwapParams<"solana", QuoteResponseRoute>) {
8
+ const { VersionedTransaction } = await import("@solana/web3.js");
9
+ const { tx, sellAsset } = route;
10
+
11
+ const assetValue = await AssetValue.from({ asset: sellAsset });
12
+
13
+ const chain = assetValue.chain;
14
+ if (!(chain === Chain.Solana && tx)) throw new SwapKitError("core_swap_invalid_params");
15
+
16
+ const wallet = getWallet(chain);
17
+ const transaction = VersionedTransaction.deserialize(Buffer.from(tx as string, "base64"));
18
+
19
+ const signedTransaction = await wallet.signTransaction(transaction);
20
+
21
+ return wallet.broadcastTransaction(signedTransaction);
22
+ },
23
+ }),
24
+ name: "solana",
25
+ properties: { supportedSwapkitProviders: [ProviderName.JUPITER] as const },
26
+ });
@@ -0,0 +1,3 @@
1
+ export { MayachainPlugin, ThorchainPlugin } from "./plugin";
2
+ export { validateAddressType } from "./shared";
3
+ export * from "./types";