@vleap/warps-adapter-near 0.1.0-beta.2
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/dist/index.d.cts +155 -0
- package/dist/index.d.ts +155 -0
- package/dist/index.js +1171 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +1145 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +45 -0
package/dist/index.js
ADDED
|
@@ -0,0 +1,1171 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
|
|
30
|
+
// src/index.ts
|
|
31
|
+
var index_exports = {};
|
|
32
|
+
__export(index_exports, {
|
|
33
|
+
ExplorerUrls: () => ExplorerUrls,
|
|
34
|
+
KnownTokens: () => KnownTokens,
|
|
35
|
+
NativeTokenNear: () => NativeTokenNear,
|
|
36
|
+
NearExplorerMap: () => NearExplorerMap,
|
|
37
|
+
NearExplorerNames: () => NearExplorerNames,
|
|
38
|
+
NearExplorerUrls: () => NearExplorerUrls,
|
|
39
|
+
NearExplorers: () => NearExplorers,
|
|
40
|
+
NearTokens: () => NearTokens,
|
|
41
|
+
WarpNearConstants: () => WarpNearConstants,
|
|
42
|
+
WarpNearDataLoader: () => WarpNearDataLoader,
|
|
43
|
+
WarpNearExecutor: () => WarpNearExecutor,
|
|
44
|
+
WarpNearExplorer: () => WarpNearExplorer,
|
|
45
|
+
WarpNearOutput: () => WarpNearOutput,
|
|
46
|
+
WarpNearSerializer: () => WarpNearSerializer,
|
|
47
|
+
WarpNearWallet: () => WarpNearWallet,
|
|
48
|
+
findKnownTokenById: () => findKnownTokenById,
|
|
49
|
+
getKnownTokensForChain: () => getKnownTokensForChain,
|
|
50
|
+
getNearAdapter: () => getNearAdapter
|
|
51
|
+
});
|
|
52
|
+
module.exports = __toCommonJS(index_exports);
|
|
53
|
+
|
|
54
|
+
// src/WarpNearDataLoader.ts
|
|
55
|
+
var import_warps = require("@vleap/warps");
|
|
56
|
+
var import_near_api_js = require("near-api-js");
|
|
57
|
+
|
|
58
|
+
// src/constants.ts
|
|
59
|
+
var WarpNearConstants = {
|
|
60
|
+
NativeToken: {
|
|
61
|
+
Identifier: "NEAR",
|
|
62
|
+
Decimals: 24
|
|
63
|
+
},
|
|
64
|
+
Gas: {
|
|
65
|
+
Default: "300000000000000",
|
|
66
|
+
Transfer: "100000000000000",
|
|
67
|
+
FunctionCall: "300000000000000"
|
|
68
|
+
}
|
|
69
|
+
};
|
|
70
|
+
var NearExplorers = /* @__PURE__ */ ((NearExplorers2) => {
|
|
71
|
+
NearExplorers2["NearExplorer"] = "near_explorer";
|
|
72
|
+
NearExplorers2["NearBlocks"] = "near_blocks";
|
|
73
|
+
NearExplorers2["NearScan"] = "nearscan";
|
|
74
|
+
return NearExplorers2;
|
|
75
|
+
})(NearExplorers || {});
|
|
76
|
+
var NearExplorerMap = {
|
|
77
|
+
near: {
|
|
78
|
+
mainnet: ["near_explorer" /* NearExplorer */, "near_blocks" /* NearBlocks */, "nearscan" /* NearScan */],
|
|
79
|
+
testnet: ["near_explorer" /* NearExplorer */, "near_blocks" /* NearBlocks */, "nearscan" /* NearScan */],
|
|
80
|
+
devnet: ["near_explorer" /* NearExplorer */, "near_blocks" /* NearBlocks */, "nearscan" /* NearScan */]
|
|
81
|
+
}
|
|
82
|
+
};
|
|
83
|
+
var ExplorerUrls = {
|
|
84
|
+
["near_explorer" /* NearExplorer */]: "https://explorer.near.org",
|
|
85
|
+
["near_blocks" /* NearBlocks */]: "https://nearblocks.io",
|
|
86
|
+
["nearscan" /* NearScan */]: "https://nearscan.io"
|
|
87
|
+
};
|
|
88
|
+
var NearExplorerNames = {
|
|
89
|
+
mainnet: ["near_explorer" /* NearExplorer */, "near_blocks" /* NearBlocks */, "nearscan" /* NearScan */],
|
|
90
|
+
testnet: ["near_explorer" /* NearExplorer */, "near_blocks" /* NearBlocks */, "nearscan" /* NearScan */],
|
|
91
|
+
devnet: ["near_explorer" /* NearExplorer */, "near_blocks" /* NearBlocks */, "nearscan" /* NearScan */]
|
|
92
|
+
};
|
|
93
|
+
var NearExplorerUrls = ExplorerUrls;
|
|
94
|
+
|
|
95
|
+
// src/tokens.ts
|
|
96
|
+
var NearTokens = [];
|
|
97
|
+
var KnownTokens = {
|
|
98
|
+
near: {
|
|
99
|
+
mainnet: NearTokens,
|
|
100
|
+
testnet: NearTokens,
|
|
101
|
+
devnet: NearTokens
|
|
102
|
+
}
|
|
103
|
+
};
|
|
104
|
+
var findKnownTokenById = (chain, env, id) => {
|
|
105
|
+
const chainTokens = KnownTokens[chain]?.[env] || [];
|
|
106
|
+
return chainTokens.find((token) => token.identifier === id) || null;
|
|
107
|
+
};
|
|
108
|
+
var getKnownTokensForChain = (chainName, env = "mainnet") => {
|
|
109
|
+
return KnownTokens[chainName]?.[env] || [];
|
|
110
|
+
};
|
|
111
|
+
|
|
112
|
+
// src/WarpNearDataLoader.ts
|
|
113
|
+
var WarpNearDataLoader = class {
|
|
114
|
+
constructor(config, chain) {
|
|
115
|
+
this.config = config;
|
|
116
|
+
this.chain = chain;
|
|
117
|
+
this.cache = new import_warps.WarpCache(config.cache?.type);
|
|
118
|
+
const providerConfig = (0, import_warps.getProviderConfig)(this.config, this.chain.name, this.config.env, this.chain.defaultApiUrl);
|
|
119
|
+
this.nearConfig = {
|
|
120
|
+
networkId: this.config.env === "mainnet" ? "mainnet" : this.config.env === "testnet" ? "testnet" : "testnet",
|
|
121
|
+
nodeUrl: providerConfig.url,
|
|
122
|
+
keyStore: new import_near_api_js.keyStores.InMemoryKeyStore()
|
|
123
|
+
};
|
|
124
|
+
}
|
|
125
|
+
async getAccount(address) {
|
|
126
|
+
try {
|
|
127
|
+
const near = await (0, import_near_api_js.connect)(this.nearConfig);
|
|
128
|
+
const account = await near.account(address);
|
|
129
|
+
const balance = await account.getAccountBalance();
|
|
130
|
+
const balanceBigInt = BigInt(balance.total);
|
|
131
|
+
return {
|
|
132
|
+
chain: this.chain.name,
|
|
133
|
+
address,
|
|
134
|
+
balance: balanceBigInt
|
|
135
|
+
};
|
|
136
|
+
} catch (error) {
|
|
137
|
+
throw new Error(`Failed to get account: ${error}`);
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
async getAccountAssets(address) {
|
|
141
|
+
try {
|
|
142
|
+
const account = await this.getAccount(address);
|
|
143
|
+
const assets = account.balance > 0n ? [{ ...this.chain.nativeToken, amount: account.balance }] : [];
|
|
144
|
+
const env = this.config.env === "mainnet" ? "mainnet" : this.config.env === "devnet" ? "devnet" : "testnet";
|
|
145
|
+
const knownTokens = getKnownTokensForChain(this.chain.name, env);
|
|
146
|
+
for (const token of knownTokens) {
|
|
147
|
+
try {
|
|
148
|
+
const near = await (0, import_near_api_js.connect)(this.nearConfig);
|
|
149
|
+
const accountObj = await near.account(address);
|
|
150
|
+
const balance = await accountObj.viewFunction({
|
|
151
|
+
contractId: token.identifier,
|
|
152
|
+
methodName: "ft_balance_of",
|
|
153
|
+
args: { account_id: address }
|
|
154
|
+
});
|
|
155
|
+
if (balance && BigInt(balance) > 0n) {
|
|
156
|
+
assets.push({
|
|
157
|
+
...token,
|
|
158
|
+
amount: BigInt(balance)
|
|
159
|
+
});
|
|
160
|
+
}
|
|
161
|
+
} catch {
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
return assets;
|
|
165
|
+
} catch (error) {
|
|
166
|
+
return [];
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
async getAsset(identifier) {
|
|
170
|
+
try {
|
|
171
|
+
if (identifier === this.chain.nativeToken.identifier || identifier === WarpNearConstants.NativeToken.Identifier) {
|
|
172
|
+
return this.chain.nativeToken;
|
|
173
|
+
}
|
|
174
|
+
const cacheKey = import_warps.WarpCacheKey.Asset(this.config.env, this.chain.name, identifier);
|
|
175
|
+
const cachedAsset = this.cache.get(cacheKey);
|
|
176
|
+
if (cachedAsset) {
|
|
177
|
+
return cachedAsset;
|
|
178
|
+
}
|
|
179
|
+
const env = this.config.env === "mainnet" ? "mainnet" : this.config.env === "devnet" ? "devnet" : "testnet";
|
|
180
|
+
const knownToken = findKnownTokenById(this.chain.name, env, identifier);
|
|
181
|
+
if (knownToken) {
|
|
182
|
+
return {
|
|
183
|
+
chain: this.chain.name,
|
|
184
|
+
identifier,
|
|
185
|
+
name: knownToken.name,
|
|
186
|
+
symbol: knownToken.symbol,
|
|
187
|
+
amount: 0n,
|
|
188
|
+
decimals: knownToken.decimals,
|
|
189
|
+
logoUrl: knownToken.logoUrl
|
|
190
|
+
};
|
|
191
|
+
}
|
|
192
|
+
try {
|
|
193
|
+
const near = await (0, import_near_api_js.connect)(this.nearConfig);
|
|
194
|
+
const account = await near.account(identifier);
|
|
195
|
+
const metadata = await account.viewFunction({
|
|
196
|
+
contractId: identifier,
|
|
197
|
+
methodName: "ft_metadata",
|
|
198
|
+
args: {}
|
|
199
|
+
});
|
|
200
|
+
const asset = {
|
|
201
|
+
chain: this.chain.name,
|
|
202
|
+
identifier,
|
|
203
|
+
name: metadata.name || "Unknown Token",
|
|
204
|
+
symbol: metadata.symbol || "UNKNOWN",
|
|
205
|
+
amount: 0n,
|
|
206
|
+
decimals: metadata.decimals || WarpNearConstants.NativeToken.Decimals,
|
|
207
|
+
logoUrl: metadata.icon || ""
|
|
208
|
+
};
|
|
209
|
+
this.cache.set(cacheKey, asset, import_warps.CacheTtl.OneHour);
|
|
210
|
+
return asset;
|
|
211
|
+
} catch {
|
|
212
|
+
const asset = {
|
|
213
|
+
chain: this.chain.name,
|
|
214
|
+
identifier,
|
|
215
|
+
name: "Unknown Token",
|
|
216
|
+
symbol: "UNKNOWN",
|
|
217
|
+
amount: 0n,
|
|
218
|
+
decimals: WarpNearConstants.NativeToken.Decimals,
|
|
219
|
+
logoUrl: ""
|
|
220
|
+
};
|
|
221
|
+
return asset;
|
|
222
|
+
}
|
|
223
|
+
} catch (error) {
|
|
224
|
+
return null;
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
async getAction(identifier, awaitCompleted = false) {
|
|
228
|
+
try {
|
|
229
|
+
const near = await (0, import_near_api_js.connect)(this.nearConfig);
|
|
230
|
+
const txStatus = await near.connection.provider.txStatus(identifier, this.nearConfig.networkId);
|
|
231
|
+
if (!txStatus) return null;
|
|
232
|
+
const statusObj = txStatus.status;
|
|
233
|
+
const isSuccess = statusObj && ("SuccessValue" in statusObj || "SuccessReceiptId" in statusObj);
|
|
234
|
+
const status = isSuccess ? "success" : "failed";
|
|
235
|
+
const transaction = txStatus.transaction;
|
|
236
|
+
const receipt = txStatus.receipts?.[0];
|
|
237
|
+
const sender = transaction.signer_id;
|
|
238
|
+
const receiver = transaction.receiver_id;
|
|
239
|
+
const value = transaction.actions?.[0]?.Transfer?.deposit ? BigInt(transaction.actions[0].Transfer.deposit) : 0n;
|
|
240
|
+
return {
|
|
241
|
+
chain: this.chain.name,
|
|
242
|
+
id: identifier,
|
|
243
|
+
receiver,
|
|
244
|
+
sender,
|
|
245
|
+
value,
|
|
246
|
+
function: transaction.actions?.[0]?.FunctionCall?.method_name || "transfer",
|
|
247
|
+
status,
|
|
248
|
+
createdAt: (/* @__PURE__ */ new Date()).toISOString(),
|
|
249
|
+
error: status === "failed" ? JSON.stringify(txStatus.status) : null,
|
|
250
|
+
tx: txStatus
|
|
251
|
+
};
|
|
252
|
+
} catch (error) {
|
|
253
|
+
return null;
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
async getAccountActions(address, options) {
|
|
257
|
+
return [];
|
|
258
|
+
}
|
|
259
|
+
};
|
|
260
|
+
|
|
261
|
+
// src/WarpNearExecutor.ts
|
|
262
|
+
var import_warps4 = require("@vleap/warps");
|
|
263
|
+
var import_near_api_js3 = require("near-api-js");
|
|
264
|
+
|
|
265
|
+
// src/WarpNearOutput.ts
|
|
266
|
+
var import_warps3 = require("@vleap/warps");
|
|
267
|
+
var import_near_api_js2 = require("near-api-js");
|
|
268
|
+
|
|
269
|
+
// src/WarpNearSerializer.ts
|
|
270
|
+
var import_warps2 = require("@vleap/warps");
|
|
271
|
+
var WarpNearSerializer = class {
|
|
272
|
+
constructor() {
|
|
273
|
+
this.coreSerializer = new import_warps2.WarpSerializer();
|
|
274
|
+
}
|
|
275
|
+
typedToString(value) {
|
|
276
|
+
if (typeof value === "string") {
|
|
277
|
+
if (value.endsWith(".near") || value.length > 2 && value.length < 65 && /^[a-z0-9._-]+$/.test(value) && !value.includes(" ")) {
|
|
278
|
+
return `address:${value}`;
|
|
279
|
+
}
|
|
280
|
+
if (value.startsWith("0x") || /^[0-9a-fA-F]+$/.test(value)) {
|
|
281
|
+
return `hex:${value}`;
|
|
282
|
+
}
|
|
283
|
+
return `string:${value}`;
|
|
284
|
+
}
|
|
285
|
+
if (typeof value === "number") {
|
|
286
|
+
if (Number.isInteger(value)) {
|
|
287
|
+
if (value >= 0 && value <= 255) return `uint8:${value}`;
|
|
288
|
+
if (value >= 0 && value <= 65535) return `uint16:${value}`;
|
|
289
|
+
if (value >= 0 && value <= 4294967295) return `uint32:${value}`;
|
|
290
|
+
return `uint64:${value}`;
|
|
291
|
+
}
|
|
292
|
+
return `string:${value}`;
|
|
293
|
+
}
|
|
294
|
+
if (typeof value === "bigint") {
|
|
295
|
+
return `biguint:${value.toString()}`;
|
|
296
|
+
}
|
|
297
|
+
if (typeof value === "boolean") {
|
|
298
|
+
return `boolean:${value}`;
|
|
299
|
+
}
|
|
300
|
+
if (value instanceof Uint8Array) {
|
|
301
|
+
return `hex:${Buffer.from(value).toString("hex")}`;
|
|
302
|
+
}
|
|
303
|
+
if (typeof value === "object" && value !== null && "identifier" in value && "amount" in value) {
|
|
304
|
+
const asset = value;
|
|
305
|
+
if (asset.decimals !== void 0) {
|
|
306
|
+
return `asset:${asset.identifier}${import_warps2.WarpConstants.ArgCompositeSeparator}${asset.amount.toString()}${import_warps2.WarpConstants.ArgCompositeSeparator}${asset.decimals}`;
|
|
307
|
+
}
|
|
308
|
+
return `asset:${asset.identifier}${import_warps2.WarpConstants.ArgCompositeSeparator}${asset.amount.toString()}`;
|
|
309
|
+
}
|
|
310
|
+
if (Array.isArray(value)) {
|
|
311
|
+
if (value.length === 0) return `list:string:`;
|
|
312
|
+
const types = value.map((item) => this.typedToString(item).split(import_warps2.WarpConstants.ArgParamsSeparator)[0]);
|
|
313
|
+
const type = types[0];
|
|
314
|
+
const values = value.map((item) => this.typedToString(item).split(import_warps2.WarpConstants.ArgParamsSeparator)[1]);
|
|
315
|
+
return `list:${type}:${values.join(",")}`;
|
|
316
|
+
}
|
|
317
|
+
if (value === null || value === void 0) {
|
|
318
|
+
return `string:null`;
|
|
319
|
+
}
|
|
320
|
+
return `string:${String(value)}`;
|
|
321
|
+
}
|
|
322
|
+
typedToNative(value) {
|
|
323
|
+
const stringValue = this.typedToString(value);
|
|
324
|
+
const [type, ...valueParts] = stringValue.split(import_warps2.WarpConstants.ArgParamsSeparator);
|
|
325
|
+
const nativeValue = valueParts.join(import_warps2.WarpConstants.ArgParamsSeparator);
|
|
326
|
+
return [type, this.parseNativeValue(type, nativeValue)];
|
|
327
|
+
}
|
|
328
|
+
nativeToTyped(type, value) {
|
|
329
|
+
switch (type) {
|
|
330
|
+
case "string":
|
|
331
|
+
return String(value);
|
|
332
|
+
case "uint8":
|
|
333
|
+
case "uint16":
|
|
334
|
+
case "uint32":
|
|
335
|
+
case "uint64":
|
|
336
|
+
return BigInt(value);
|
|
337
|
+
case "biguint":
|
|
338
|
+
return BigInt(value);
|
|
339
|
+
case "boolean":
|
|
340
|
+
return Boolean(value);
|
|
341
|
+
case "address":
|
|
342
|
+
return String(value);
|
|
343
|
+
case "hex":
|
|
344
|
+
return String(value);
|
|
345
|
+
case "asset":
|
|
346
|
+
if (typeof value === "object" && value !== null && "identifier" in value && "amount" in value) {
|
|
347
|
+
return value;
|
|
348
|
+
}
|
|
349
|
+
return value;
|
|
350
|
+
default:
|
|
351
|
+
if (type.startsWith("list:")) {
|
|
352
|
+
const [, itemType, itemsStr] = type.split(":");
|
|
353
|
+
if (!itemsStr) return [];
|
|
354
|
+
const items = itemsStr.split(",");
|
|
355
|
+
return items.map((item) => this.nativeToTyped(itemType, item));
|
|
356
|
+
}
|
|
357
|
+
return String(value);
|
|
358
|
+
}
|
|
359
|
+
}
|
|
360
|
+
nativeToType(type) {
|
|
361
|
+
switch (type) {
|
|
362
|
+
case "string":
|
|
363
|
+
return "string";
|
|
364
|
+
case "uint8":
|
|
365
|
+
case "uint16":
|
|
366
|
+
case "uint32":
|
|
367
|
+
case "uint64":
|
|
368
|
+
case "biguint":
|
|
369
|
+
return "bigint";
|
|
370
|
+
case "boolean":
|
|
371
|
+
return "boolean";
|
|
372
|
+
case "address":
|
|
373
|
+
return "string";
|
|
374
|
+
case "hex":
|
|
375
|
+
return "string";
|
|
376
|
+
default:
|
|
377
|
+
return "string";
|
|
378
|
+
}
|
|
379
|
+
}
|
|
380
|
+
stringToTyped(value) {
|
|
381
|
+
const parts = value.split(import_warps2.WarpConstants.ArgParamsSeparator, 2);
|
|
382
|
+
if (parts.length < 2) {
|
|
383
|
+
return value;
|
|
384
|
+
}
|
|
385
|
+
const [type, stringValue] = parts;
|
|
386
|
+
switch (type) {
|
|
387
|
+
case "string":
|
|
388
|
+
return stringValue;
|
|
389
|
+
case "uint8":
|
|
390
|
+
case "uint16":
|
|
391
|
+
case "uint32":
|
|
392
|
+
case "uint64":
|
|
393
|
+
return BigInt(stringValue);
|
|
394
|
+
case "biguint":
|
|
395
|
+
return BigInt(stringValue);
|
|
396
|
+
case "boolean":
|
|
397
|
+
return stringValue === "true";
|
|
398
|
+
case "address":
|
|
399
|
+
return stringValue;
|
|
400
|
+
case "hex":
|
|
401
|
+
return stringValue;
|
|
402
|
+
case "asset":
|
|
403
|
+
const assetParts = stringValue.split(import_warps2.WarpConstants.ArgCompositeSeparator);
|
|
404
|
+
const identifier = assetParts[0] || "";
|
|
405
|
+
const amount = assetParts[1] ? BigInt(assetParts[1]) : 0n;
|
|
406
|
+
const decimals = assetParts[2] ? parseInt(assetParts[2], 10) : void 0;
|
|
407
|
+
return { identifier, amount, decimals };
|
|
408
|
+
default:
|
|
409
|
+
if (type.startsWith("list:")) {
|
|
410
|
+
const [, itemType, itemsStr] = type.split(":");
|
|
411
|
+
if (!itemsStr) return [];
|
|
412
|
+
const items = itemsStr.split(",");
|
|
413
|
+
return items.map((item) => this.stringToTyped(`${itemType}:${item}`));
|
|
414
|
+
}
|
|
415
|
+
return stringValue;
|
|
416
|
+
}
|
|
417
|
+
}
|
|
418
|
+
parseNativeValue(type, value) {
|
|
419
|
+
switch (type) {
|
|
420
|
+
case "string":
|
|
421
|
+
return value;
|
|
422
|
+
case "uint8":
|
|
423
|
+
case "uint16":
|
|
424
|
+
case "uint32":
|
|
425
|
+
case "uint64":
|
|
426
|
+
case "biguint":
|
|
427
|
+
return BigInt(value);
|
|
428
|
+
case "boolean":
|
|
429
|
+
return value === "true";
|
|
430
|
+
case "address":
|
|
431
|
+
return value;
|
|
432
|
+
case "hex":
|
|
433
|
+
return value;
|
|
434
|
+
default:
|
|
435
|
+
return value;
|
|
436
|
+
}
|
|
437
|
+
}
|
|
438
|
+
};
|
|
439
|
+
|
|
440
|
+
// src/WarpNearOutput.ts
|
|
441
|
+
var WarpNearOutput = class {
|
|
442
|
+
constructor(config, chain) {
|
|
443
|
+
this.config = config;
|
|
444
|
+
this.chain = chain;
|
|
445
|
+
this.serializer = new WarpNearSerializer();
|
|
446
|
+
const providerConfig = (0, import_warps3.getProviderConfig)(this.config, this.chain.name, this.config.env, this.chain.defaultApiUrl);
|
|
447
|
+
this.nearConfig = {
|
|
448
|
+
networkId: this.config.env === "mainnet" ? "mainnet" : this.config.env === "testnet" ? "testnet" : "testnet",
|
|
449
|
+
nodeUrl: providerConfig.url,
|
|
450
|
+
keyStore: new import_near_api_js2.keyStores.InMemoryKeyStore()
|
|
451
|
+
};
|
|
452
|
+
this.cache = new import_warps3.WarpCache(config.cache?.type);
|
|
453
|
+
}
|
|
454
|
+
async getActionExecution(warp, actionIndex, tx) {
|
|
455
|
+
const inputs = this.cache.get(import_warps3.WarpCacheKey.WarpExecutable(this.config.env, warp.meta?.hash || "", actionIndex)) ?? [];
|
|
456
|
+
const resolvedInputs = (0, import_warps3.extractResolvedInputValues)(inputs);
|
|
457
|
+
if (!tx) {
|
|
458
|
+
return this.createFailedExecution(warp, actionIndex, resolvedInputs);
|
|
459
|
+
}
|
|
460
|
+
if ("status" in tx && typeof tx.status === "string") {
|
|
461
|
+
return this.handleWarpChainAction(warp, actionIndex, tx, resolvedInputs);
|
|
462
|
+
}
|
|
463
|
+
if (typeof tx === "string") {
|
|
464
|
+
return this.handleTransactionHash(warp, actionIndex, tx, resolvedInputs);
|
|
465
|
+
}
|
|
466
|
+
return this.createFailedExecution(warp, actionIndex, resolvedInputs);
|
|
467
|
+
}
|
|
468
|
+
createFailedExecution(warp, actionIndex, resolvedInputs = []) {
|
|
469
|
+
return {
|
|
470
|
+
status: "error",
|
|
471
|
+
warp,
|
|
472
|
+
action: actionIndex,
|
|
473
|
+
user: (0, import_warps3.getWarpWalletAddressFromConfig)(this.config, this.chain.name),
|
|
474
|
+
txHash: "",
|
|
475
|
+
tx: null,
|
|
476
|
+
next: null,
|
|
477
|
+
values: { string: [], native: [], mapped: {} },
|
|
478
|
+
output: {},
|
|
479
|
+
messages: {},
|
|
480
|
+
destination: null,
|
|
481
|
+
resolvedInputs
|
|
482
|
+
};
|
|
483
|
+
}
|
|
484
|
+
handleWarpChainAction(warp, actionIndex, tx, resolvedInputs = []) {
|
|
485
|
+
const success = tx.status === "success";
|
|
486
|
+
const transactionHash = tx.id || tx.tx?.hash || "";
|
|
487
|
+
const rawValues = [transactionHash];
|
|
488
|
+
const stringValues = rawValues.map(String);
|
|
489
|
+
return {
|
|
490
|
+
status: success ? "success" : "error",
|
|
491
|
+
warp,
|
|
492
|
+
action: actionIndex,
|
|
493
|
+
user: (0, import_warps3.getWarpWalletAddressFromConfig)(this.config, this.chain.name),
|
|
494
|
+
txHash: transactionHash,
|
|
495
|
+
tx,
|
|
496
|
+
next: null,
|
|
497
|
+
values: { string: stringValues, native: rawValues, mapped: {} },
|
|
498
|
+
output: {},
|
|
499
|
+
messages: {},
|
|
500
|
+
destination: null,
|
|
501
|
+
resolvedInputs
|
|
502
|
+
};
|
|
503
|
+
}
|
|
504
|
+
async handleTransactionHash(warp, actionIndex, hash, resolvedInputs = []) {
|
|
505
|
+
try {
|
|
506
|
+
const near = await (0, import_near_api_js2.connect)(this.nearConfig);
|
|
507
|
+
const txStatus = await near.connection.provider.txStatus(hash, this.nearConfig.networkId);
|
|
508
|
+
if (!txStatus) {
|
|
509
|
+
return this.createFailedExecution(warp, actionIndex, resolvedInputs);
|
|
510
|
+
}
|
|
511
|
+
const statusObj = txStatus.status;
|
|
512
|
+
const success = statusObj && ("SuccessValue" in statusObj || "SuccessReceiptId" in statusObj);
|
|
513
|
+
const transaction = txStatus.transaction;
|
|
514
|
+
const rawValues = [hash];
|
|
515
|
+
const stringValues = rawValues.map(String);
|
|
516
|
+
return {
|
|
517
|
+
status: success ? "success" : "error",
|
|
518
|
+
warp,
|
|
519
|
+
action: actionIndex,
|
|
520
|
+
user: (0, import_warps3.getWarpWalletAddressFromConfig)(this.config, this.chain.name),
|
|
521
|
+
txHash: hash,
|
|
522
|
+
tx: {
|
|
523
|
+
hash,
|
|
524
|
+
transaction,
|
|
525
|
+
status: txStatus.status
|
|
526
|
+
},
|
|
527
|
+
next: null,
|
|
528
|
+
values: { string: stringValues, native: rawValues, mapped: {} },
|
|
529
|
+
output: {},
|
|
530
|
+
messages: {},
|
|
531
|
+
destination: null,
|
|
532
|
+
resolvedInputs
|
|
533
|
+
};
|
|
534
|
+
} catch (error) {
|
|
535
|
+
return this.createFailedExecution(warp, actionIndex, resolvedInputs);
|
|
536
|
+
}
|
|
537
|
+
}
|
|
538
|
+
async extractQueryOutput(warp, typedValues, actionIndex, inputs) {
|
|
539
|
+
const stringValues = typedValues.map((t) => this.serializer.typedToString(t));
|
|
540
|
+
const nativeValues = typedValues.map((t) => this.serializer.typedToNative(t)[1]);
|
|
541
|
+
const values = { string: stringValues, native: nativeValues, mapped: {} };
|
|
542
|
+
let output = {};
|
|
543
|
+
if (!warp.output) return { values, output };
|
|
544
|
+
const getNestedValue = (path) => {
|
|
545
|
+
const indices = path.split(".").slice(1).map((i) => parseInt(i) - 1);
|
|
546
|
+
if (indices.length === 0) return void 0;
|
|
547
|
+
let value = nativeValues[indices[0]];
|
|
548
|
+
for (let i = 1; i < indices.length; i++) {
|
|
549
|
+
if (value === void 0 || value === null) return void 0;
|
|
550
|
+
value = value[indices[i]];
|
|
551
|
+
}
|
|
552
|
+
return value;
|
|
553
|
+
};
|
|
554
|
+
for (const [key, path] of Object.entries(warp.output)) {
|
|
555
|
+
if (path.startsWith(import_warps3.WarpConstants.Transform.Prefix)) continue;
|
|
556
|
+
const currentActionIndex = (0, import_warps3.parseOutputOutIndex)(path);
|
|
557
|
+
if (currentActionIndex !== null && currentActionIndex !== actionIndex) {
|
|
558
|
+
output[key] = null;
|
|
559
|
+
continue;
|
|
560
|
+
}
|
|
561
|
+
if (path.startsWith("out.") || path === "out" || path.startsWith("out[")) {
|
|
562
|
+
output[key] = getNestedValue(path) || null;
|
|
563
|
+
} else {
|
|
564
|
+
output[key] = path;
|
|
565
|
+
}
|
|
566
|
+
}
|
|
567
|
+
return { values, output: await (0, import_warps3.evaluateOutputCommon)(warp, output, actionIndex, inputs, this.serializer.coreSerializer, this.config) };
|
|
568
|
+
}
|
|
569
|
+
async getTransactionStatus(txHash) {
|
|
570
|
+
try {
|
|
571
|
+
const near = await (0, import_near_api_js2.connect)(this.nearConfig);
|
|
572
|
+
const txStatus = await near.connection.provider.txStatus(txHash, this.nearConfig.networkId);
|
|
573
|
+
if (!txStatus) {
|
|
574
|
+
return { status: "pending" };
|
|
575
|
+
}
|
|
576
|
+
const statusObj = txStatus.status;
|
|
577
|
+
const isSuccess = statusObj && ("SuccessValue" in statusObj || "SuccessReceiptId" in statusObj);
|
|
578
|
+
return {
|
|
579
|
+
status: isSuccess ? "confirmed" : "failed",
|
|
580
|
+
blockNumber: txStatus.transaction_outcome?.block_hash ? 0 : void 0,
|
|
581
|
+
gasUsed: BigInt(0)
|
|
582
|
+
};
|
|
583
|
+
} catch (error) {
|
|
584
|
+
throw new Error(`Failed to get transaction status: ${error}`);
|
|
585
|
+
}
|
|
586
|
+
}
|
|
587
|
+
async getTransactionReceipt(txHash) {
|
|
588
|
+
try {
|
|
589
|
+
const near = await (0, import_near_api_js2.connect)(this.nearConfig);
|
|
590
|
+
const txStatus = await near.connection.provider.txStatus(txHash, this.nearConfig.networkId);
|
|
591
|
+
if (!txStatus) {
|
|
592
|
+
return null;
|
|
593
|
+
}
|
|
594
|
+
return {
|
|
595
|
+
hash: txHash,
|
|
596
|
+
transaction: txStatus.transaction,
|
|
597
|
+
status: txStatus.status
|
|
598
|
+
};
|
|
599
|
+
} catch (error) {
|
|
600
|
+
return null;
|
|
601
|
+
}
|
|
602
|
+
}
|
|
603
|
+
};
|
|
604
|
+
|
|
605
|
+
// src/WarpNearExecutor.ts
|
|
606
|
+
var WarpNearExecutor = class {
|
|
607
|
+
constructor(config, chain) {
|
|
608
|
+
this.config = config;
|
|
609
|
+
this.chain = chain;
|
|
610
|
+
this.serializer = new WarpNearSerializer();
|
|
611
|
+
const providerConfig = (0, import_warps4.getProviderConfig)(this.config, chain.name, this.config.env, this.chain.defaultApiUrl);
|
|
612
|
+
this.nearConfig = {
|
|
613
|
+
networkId: this.config.env === "mainnet" ? "mainnet" : this.config.env === "testnet" ? "testnet" : "testnet",
|
|
614
|
+
nodeUrl: providerConfig.url,
|
|
615
|
+
keyStore: new import_near_api_js3.keyStores.InMemoryKeyStore()
|
|
616
|
+
};
|
|
617
|
+
this.output = new WarpNearOutput(config, this.chain);
|
|
618
|
+
}
|
|
619
|
+
async createTransaction(executable) {
|
|
620
|
+
const action = (0, import_warps4.getWarpActionByIndex)(executable.warp, executable.action);
|
|
621
|
+
let tx = null;
|
|
622
|
+
if (action.type === "transfer") {
|
|
623
|
+
tx = await this.createTransferTransaction(executable);
|
|
624
|
+
} else if (action.type === "contract") {
|
|
625
|
+
tx = await this.createContractCallTransaction(executable);
|
|
626
|
+
} else if (action.type === "query") {
|
|
627
|
+
throw new Error("WarpNearExecutor: Invalid action type for createTransaction; Use executeQuery instead");
|
|
628
|
+
} else if (action.type === "collect") {
|
|
629
|
+
throw new Error("WarpNearExecutor: Invalid action type for createTransaction; Use executeCollect instead");
|
|
630
|
+
}
|
|
631
|
+
if (!tx) throw new Error(`WarpNearExecutor: Invalid action type (${action.type})`);
|
|
632
|
+
return tx;
|
|
633
|
+
}
|
|
634
|
+
async createTransferTransaction(executable) {
|
|
635
|
+
const userWallet = (0, import_warps4.getWarpWalletAddressFromConfig)(this.config, executable.chain.name);
|
|
636
|
+
if (!userWallet) throw new Error("WarpNearExecutor: createTransfer - user address not set");
|
|
637
|
+
if (!executable.destination) throw new Error("WarpNearExecutor: Destination address is required");
|
|
638
|
+
if (executable.transfers && executable.transfers.length > 0) {
|
|
639
|
+
return this.createTokenTransferTransaction(executable, userWallet);
|
|
640
|
+
}
|
|
641
|
+
const amount = executable.value > 0n ? executable.value : 0n;
|
|
642
|
+
const amountInYoctoNear = amount.toString();
|
|
643
|
+
return {
|
|
644
|
+
receiverId: executable.destination,
|
|
645
|
+
actions: [
|
|
646
|
+
{
|
|
647
|
+
type: "Transfer",
|
|
648
|
+
params: {
|
|
649
|
+
deposit: amountInYoctoNear
|
|
650
|
+
}
|
|
651
|
+
}
|
|
652
|
+
]
|
|
653
|
+
};
|
|
654
|
+
}
|
|
655
|
+
async createContractCallTransaction(executable) {
|
|
656
|
+
const userWallet = (0, import_warps4.getWarpWalletAddressFromConfig)(this.config, executable.chain.name);
|
|
657
|
+
if (!userWallet) throw new Error("WarpNearExecutor: createContractCall - user address not set");
|
|
658
|
+
const action = (0, import_warps4.getWarpActionByIndex)(executable.warp, executable.action);
|
|
659
|
+
if (!action || !("func" in action) || !action.func) throw new Error("WarpNearExecutor: Contract action must have a function name");
|
|
660
|
+
if (!executable.destination) throw new Error("WarpNearExecutor: Contract address is required");
|
|
661
|
+
try {
|
|
662
|
+
const nativeArgs = executable.args.map((arg) => this.serializer.coreSerializer.stringToNative(arg)[1]);
|
|
663
|
+
const args = nativeArgs.length > 0 ? nativeArgs : {};
|
|
664
|
+
const gas = WarpNearConstants.Gas.FunctionCall;
|
|
665
|
+
const deposit = executable.value > 0n ? executable.value.toString() : "0";
|
|
666
|
+
return {
|
|
667
|
+
receiverId: executable.destination,
|
|
668
|
+
actions: [
|
|
669
|
+
{
|
|
670
|
+
type: "FunctionCall",
|
|
671
|
+
params: {
|
|
672
|
+
methodName: action.func,
|
|
673
|
+
args,
|
|
674
|
+
gas,
|
|
675
|
+
deposit
|
|
676
|
+
}
|
|
677
|
+
}
|
|
678
|
+
]
|
|
679
|
+
};
|
|
680
|
+
} catch (error) {
|
|
681
|
+
const errorMessage = error instanceof Error ? error.message : String(error);
|
|
682
|
+
throw new Error(`WarpNearExecutor: Failed to create contract call: ${errorMessage}`);
|
|
683
|
+
}
|
|
684
|
+
}
|
|
685
|
+
async createTokenTransferTransaction(executable, userWallet) {
|
|
686
|
+
if (executable.transfers.length === 0) throw new Error("WarpNearExecutor: No transfers provided");
|
|
687
|
+
if (!this.chain.nativeToken?.identifier) throw new Error("WarpNearExecutor: No native token defined for this chain");
|
|
688
|
+
const nativeTokenTransfers = executable.transfers.filter(
|
|
689
|
+
(transfer) => transfer.identifier === this.chain.nativeToken.identifier || transfer.identifier === WarpNearConstants.NativeToken.Identifier
|
|
690
|
+
);
|
|
691
|
+
const ftTokenTransfers = executable.transfers.filter(
|
|
692
|
+
(transfer) => transfer.identifier !== this.chain.nativeToken.identifier && transfer.identifier !== WarpNearConstants.NativeToken.Identifier
|
|
693
|
+
);
|
|
694
|
+
if (nativeTokenTransfers.length === 1 && ftTokenTransfers.length === 0) {
|
|
695
|
+
const transfer = nativeTokenTransfers[0];
|
|
696
|
+
if (transfer.amount <= 0n) throw new Error("WarpNearExecutor: Native token transfer amount must be positive");
|
|
697
|
+
return {
|
|
698
|
+
receiverId: executable.destination,
|
|
699
|
+
actions: [
|
|
700
|
+
{
|
|
701
|
+
type: "Transfer",
|
|
702
|
+
params: {
|
|
703
|
+
deposit: transfer.amount.toString()
|
|
704
|
+
}
|
|
705
|
+
}
|
|
706
|
+
]
|
|
707
|
+
};
|
|
708
|
+
}
|
|
709
|
+
if (nativeTokenTransfers.length === 0 && ftTokenTransfers.length === 1) {
|
|
710
|
+
return this.createSingleTokenTransfer(executable, ftTokenTransfers[0]);
|
|
711
|
+
}
|
|
712
|
+
if (executable.transfers.length > 1) throw new Error("WarpNearExecutor: Multiple token transfers not yet supported");
|
|
713
|
+
throw new Error("WarpNearExecutor: Invalid transfer configuration");
|
|
714
|
+
}
|
|
715
|
+
async createSingleTokenTransfer(executable, transfer) {
|
|
716
|
+
if (!executable.destination) throw new Error("WarpNearExecutor: Destination address is required");
|
|
717
|
+
const userWallet = (0, import_warps4.getWarpWalletAddressFromConfig)(this.config, executable.chain.name);
|
|
718
|
+
if (!userWallet) throw new Error("WarpNearExecutor: User wallet not set");
|
|
719
|
+
const args = {
|
|
720
|
+
receiver_id: executable.destination,
|
|
721
|
+
amount: transfer.amount.toString()
|
|
722
|
+
};
|
|
723
|
+
return {
|
|
724
|
+
receiverId: transfer.identifier,
|
|
725
|
+
actions: [
|
|
726
|
+
{
|
|
727
|
+
type: "FunctionCall",
|
|
728
|
+
params: {
|
|
729
|
+
methodName: "ft_transfer",
|
|
730
|
+
args,
|
|
731
|
+
gas: WarpNearConstants.Gas.FunctionCall,
|
|
732
|
+
deposit: "1"
|
|
733
|
+
}
|
|
734
|
+
}
|
|
735
|
+
]
|
|
736
|
+
};
|
|
737
|
+
}
|
|
738
|
+
async executeQuery(executable) {
|
|
739
|
+
const action = (0, import_warps4.getWarpActionByIndex)(executable.warp, executable.action);
|
|
740
|
+
if (action.type !== "query") throw new Error(`WarpNearExecutor: Invalid action type for executeQuery: ${action.type}`);
|
|
741
|
+
if (!action.func) throw new Error("WarpNearExecutor: Query action must have a function name");
|
|
742
|
+
let queryAddress;
|
|
743
|
+
try {
|
|
744
|
+
if (!executable.destination) throw new Error("WarpNearExecutor: Query address is required");
|
|
745
|
+
queryAddress = executable.destination;
|
|
746
|
+
} catch {
|
|
747
|
+
throw new Error(`WarpNearExecutor: Invalid address for query: ${executable.destination}`);
|
|
748
|
+
}
|
|
749
|
+
try {
|
|
750
|
+
const nativeArgs = executable.args.map((arg) => this.serializer.coreSerializer.stringToNative(arg)[1]);
|
|
751
|
+
const args = nativeArgs.length > 0 ? nativeArgs : {};
|
|
752
|
+
const near = await (0, import_near_api_js3.connect)(this.nearConfig);
|
|
753
|
+
const account = await near.account(queryAddress);
|
|
754
|
+
const result = await account.viewFunction({
|
|
755
|
+
contractId: queryAddress,
|
|
756
|
+
methodName: action.func,
|
|
757
|
+
args
|
|
758
|
+
});
|
|
759
|
+
const { values, output } = await this.output.extractQueryOutput(
|
|
760
|
+
executable.warp,
|
|
761
|
+
Array.isArray(result) ? result : [result],
|
|
762
|
+
executable.action,
|
|
763
|
+
executable.resolvedInputs
|
|
764
|
+
);
|
|
765
|
+
const next = (0, import_warps4.getNextInfo)(this.config, [], executable.warp, executable.action, output);
|
|
766
|
+
const destinationInput = executable.resolvedInputs.find((i) => i.input.position === "receiver" || i.input.position === "destination");
|
|
767
|
+
const destination = destinationInput?.value || executable.destination;
|
|
768
|
+
const resolvedInputs = (0, import_warps4.extractResolvedInputValues)(executable.resolvedInputs);
|
|
769
|
+
return {
|
|
770
|
+
status: "success",
|
|
771
|
+
warp: executable.warp,
|
|
772
|
+
action: executable.action,
|
|
773
|
+
user: (0, import_warps4.getWarpWalletAddressFromConfig)(this.config, executable.chain.name),
|
|
774
|
+
txHash: null,
|
|
775
|
+
tx: null,
|
|
776
|
+
next,
|
|
777
|
+
values,
|
|
778
|
+
output: { ...output, _DATA: result },
|
|
779
|
+
messages: (0, import_warps4.applyOutputToMessages)(executable.warp, output, this.config),
|
|
780
|
+
destination,
|
|
781
|
+
resolvedInputs
|
|
782
|
+
};
|
|
783
|
+
} catch (error) {
|
|
784
|
+
const errorMessage = error instanceof Error ? error.message : String(error);
|
|
785
|
+
const destinationInput = executable.resolvedInputs.find((i) => i.input.position === "receiver" || i.input.position === "destination");
|
|
786
|
+
const destination = destinationInput?.value || executable.destination;
|
|
787
|
+
const resolvedInputs = (0, import_warps4.extractResolvedInputValues)(executable.resolvedInputs);
|
|
788
|
+
return {
|
|
789
|
+
status: "error",
|
|
790
|
+
warp: executable.warp,
|
|
791
|
+
action: executable.action,
|
|
792
|
+
user: (0, import_warps4.getWarpWalletAddressFromConfig)(this.config, executable.chain.name),
|
|
793
|
+
txHash: null,
|
|
794
|
+
tx: null,
|
|
795
|
+
next: null,
|
|
796
|
+
values: { string: [], native: [], mapped: {} },
|
|
797
|
+
output: { _DATA: errorMessage, _ERROR: errorMessage },
|
|
798
|
+
messages: {},
|
|
799
|
+
destination,
|
|
800
|
+
resolvedInputs
|
|
801
|
+
};
|
|
802
|
+
}
|
|
803
|
+
}
|
|
804
|
+
};
|
|
805
|
+
|
|
806
|
+
// src/WarpNearExplorer.ts
|
|
807
|
+
var WarpNearExplorer = class {
|
|
808
|
+
constructor(chain, config) {
|
|
809
|
+
this.chain = chain;
|
|
810
|
+
this.config = config;
|
|
811
|
+
}
|
|
812
|
+
getExplorers() {
|
|
813
|
+
const explorers = NearExplorerNames[this.config.env];
|
|
814
|
+
if (!explorers) {
|
|
815
|
+
return ["near_explorer" /* NearExplorer */];
|
|
816
|
+
}
|
|
817
|
+
return explorers;
|
|
818
|
+
}
|
|
819
|
+
getPrimaryExplorer() {
|
|
820
|
+
const explorers = this.getExplorers();
|
|
821
|
+
return explorers[0];
|
|
822
|
+
}
|
|
823
|
+
getExplorerUrlByName(explorer) {
|
|
824
|
+
const userPreference = this.config.preferences?.explorers?.[this.chain.name];
|
|
825
|
+
if (userPreference && !explorer) {
|
|
826
|
+
const url2 = NearExplorerUrls[userPreference];
|
|
827
|
+
if (url2) return url2;
|
|
828
|
+
}
|
|
829
|
+
if (explorer) {
|
|
830
|
+
const url2 = NearExplorerUrls[explorer];
|
|
831
|
+
if (url2) return url2;
|
|
832
|
+
}
|
|
833
|
+
const primaryExplorer = this.getPrimaryExplorer();
|
|
834
|
+
const url = NearExplorerUrls[primaryExplorer];
|
|
835
|
+
return url || NearExplorerUrls[primaryExplorer];
|
|
836
|
+
}
|
|
837
|
+
getAccountUrl(address, explorer) {
|
|
838
|
+
const baseUrl = this.getExplorerUrlByName(explorer);
|
|
839
|
+
const network = this.config.env === "mainnet" ? "" : `/${this.config.env}`;
|
|
840
|
+
return `${baseUrl}${network}/accounts/${address}`;
|
|
841
|
+
}
|
|
842
|
+
getTransactionUrl(hash, explorer) {
|
|
843
|
+
const baseUrl = this.getExplorerUrlByName(explorer);
|
|
844
|
+
const network = this.config.env === "mainnet" ? "" : `/${this.config.env}`;
|
|
845
|
+
return `${baseUrl}${network}/transactions/${hash}`;
|
|
846
|
+
}
|
|
847
|
+
getBlockUrl(blockNumber, explorer) {
|
|
848
|
+
const baseUrl = this.getExplorerUrlByName(explorer);
|
|
849
|
+
const network = this.config.env === "mainnet" ? "" : `/${this.config.env}`;
|
|
850
|
+
return `${baseUrl}${network}/blocks/${blockNumber}`;
|
|
851
|
+
}
|
|
852
|
+
getAssetUrl(identifier, explorer) {
|
|
853
|
+
const baseUrl = this.getExplorerUrlByName(explorer);
|
|
854
|
+
const network = this.config.env === "mainnet" ? "" : `/${this.config.env}`;
|
|
855
|
+
return `${baseUrl}${network}/tokens/${identifier}`;
|
|
856
|
+
}
|
|
857
|
+
getContractUrl(address, explorer) {
|
|
858
|
+
const baseUrl = this.getExplorerUrlByName(explorer);
|
|
859
|
+
const network = this.config.env === "mainnet" ? "" : `/${this.config.env}`;
|
|
860
|
+
return `${baseUrl}${network}/accounts/${address}`;
|
|
861
|
+
}
|
|
862
|
+
getAllExplorers() {
|
|
863
|
+
return this.getExplorers();
|
|
864
|
+
}
|
|
865
|
+
getExplorerByName(name) {
|
|
866
|
+
const explorers = this.getExplorers();
|
|
867
|
+
return explorers.find((explorer) => explorer.toLowerCase() === name.toLowerCase());
|
|
868
|
+
}
|
|
869
|
+
getAccountUrls(address) {
|
|
870
|
+
const explorers = this.getAllExplorers();
|
|
871
|
+
const urls = {};
|
|
872
|
+
const network = this.config.env === "mainnet" ? "" : `/${this.config.env}`;
|
|
873
|
+
explorers.forEach((explorer) => {
|
|
874
|
+
const url = NearExplorerUrls[explorer];
|
|
875
|
+
if (url) {
|
|
876
|
+
urls[explorer] = `${url}${network}/accounts/${address}`;
|
|
877
|
+
}
|
|
878
|
+
});
|
|
879
|
+
return urls;
|
|
880
|
+
}
|
|
881
|
+
getTransactionUrls(hash) {
|
|
882
|
+
const explorers = this.getAllExplorers();
|
|
883
|
+
const urls = {};
|
|
884
|
+
const network = this.config.env === "mainnet" ? "" : `/${this.config.env}`;
|
|
885
|
+
explorers.forEach((explorer) => {
|
|
886
|
+
const url = NearExplorerUrls[explorer];
|
|
887
|
+
if (url) {
|
|
888
|
+
urls[explorer] = `${url}${network}/transactions/${hash}`;
|
|
889
|
+
}
|
|
890
|
+
});
|
|
891
|
+
return urls;
|
|
892
|
+
}
|
|
893
|
+
getAssetUrls(identifier) {
|
|
894
|
+
const explorers = this.getAllExplorers();
|
|
895
|
+
const urls = {};
|
|
896
|
+
const network = this.config.env === "mainnet" ? "" : `/${this.config.env}`;
|
|
897
|
+
explorers.forEach((explorer) => {
|
|
898
|
+
const url = NearExplorerUrls[explorer];
|
|
899
|
+
if (url) {
|
|
900
|
+
urls[explorer] = `${url}${network}/tokens/${identifier}`;
|
|
901
|
+
}
|
|
902
|
+
});
|
|
903
|
+
return urls;
|
|
904
|
+
}
|
|
905
|
+
getContractUrls(address) {
|
|
906
|
+
const explorers = this.getAllExplorers();
|
|
907
|
+
const urls = {};
|
|
908
|
+
const network = this.config.env === "mainnet" ? "" : `/${this.config.env}`;
|
|
909
|
+
explorers.forEach((explorer) => {
|
|
910
|
+
const url = NearExplorerUrls[explorer];
|
|
911
|
+
if (url) {
|
|
912
|
+
urls[explorer] = `${url}${network}/accounts/${address}`;
|
|
913
|
+
}
|
|
914
|
+
});
|
|
915
|
+
return urls;
|
|
916
|
+
}
|
|
917
|
+
getBlockUrls(blockNumber) {
|
|
918
|
+
const explorers = this.getAllExplorers();
|
|
919
|
+
const urls = {};
|
|
920
|
+
const network = this.config.env === "mainnet" ? "" : `/${this.config.env}`;
|
|
921
|
+
explorers.forEach((explorer) => {
|
|
922
|
+
const url = NearExplorerUrls[explorer];
|
|
923
|
+
if (url) {
|
|
924
|
+
urls[explorer] = `${url}${network}/blocks/${blockNumber}`;
|
|
925
|
+
}
|
|
926
|
+
});
|
|
927
|
+
return urls;
|
|
928
|
+
}
|
|
929
|
+
};
|
|
930
|
+
|
|
931
|
+
// src/WarpNearWallet.ts
|
|
932
|
+
var bip39 = __toESM(require("@scure/bip39"), 1);
|
|
933
|
+
var import_english = require("@scure/bip39/wordlists/english.js");
|
|
934
|
+
var import_warps5 = require("@vleap/warps");
|
|
935
|
+
var import_bs58 = __toESM(require("bs58"), 1);
|
|
936
|
+
var import_near_api_js4 = require("near-api-js");
|
|
937
|
+
var WarpNearWallet = class {
|
|
938
|
+
constructor(config, chain) {
|
|
939
|
+
this.config = config;
|
|
940
|
+
this.chain = chain;
|
|
941
|
+
const providerConfig = (0, import_warps5.getProviderConfig)(config, chain.name, config.env, chain.defaultApiUrl);
|
|
942
|
+
this.nearConfig = {
|
|
943
|
+
networkId: this.config.env === "mainnet" ? "mainnet" : this.config.env === "testnet" ? "testnet" : "testnet",
|
|
944
|
+
nodeUrl: providerConfig.url,
|
|
945
|
+
keyStore: new import_near_api_js4.keyStores.InMemoryKeyStore()
|
|
946
|
+
};
|
|
947
|
+
}
|
|
948
|
+
async signTransaction(tx) {
|
|
949
|
+
if (!tx || typeof tx !== "object") throw new Error("Invalid transaction object");
|
|
950
|
+
const keyPair = this.getKeyPair();
|
|
951
|
+
const accountId = this.getAddress();
|
|
952
|
+
if (!accountId) throw new Error("No account ID available");
|
|
953
|
+
await this.nearConfig.keyStore.setKey(this.nearConfig.networkId, accountId, keyPair);
|
|
954
|
+
const near = await (0, import_near_api_js4.connect)(this.nearConfig);
|
|
955
|
+
const account = await near.account(accountId);
|
|
956
|
+
if (tx.signature) {
|
|
957
|
+
return tx;
|
|
958
|
+
}
|
|
959
|
+
const signedTx = await account.signAndSendTransaction({
|
|
960
|
+
receiverId: tx.receiverId,
|
|
961
|
+
actions: tx.actions
|
|
962
|
+
});
|
|
963
|
+
return {
|
|
964
|
+
...tx,
|
|
965
|
+
signature: signedTx.transaction.hash,
|
|
966
|
+
transactionHash: signedTx.transaction.hash
|
|
967
|
+
};
|
|
968
|
+
}
|
|
969
|
+
async signTransactions(txs) {
|
|
970
|
+
if (txs.length === 0) return [];
|
|
971
|
+
return Promise.all(txs.map(async (tx) => this.signTransaction(tx)));
|
|
972
|
+
}
|
|
973
|
+
async signMessage(message) {
|
|
974
|
+
const keyPair = this.getKeyPair();
|
|
975
|
+
const messageBytes = new TextEncoder().encode(message);
|
|
976
|
+
const signature = keyPair.sign(messageBytes);
|
|
977
|
+
return import_bs58.default.encode(signature.signature);
|
|
978
|
+
}
|
|
979
|
+
async sendTransaction(tx) {
|
|
980
|
+
if (!tx || typeof tx !== "object") throw new Error("Invalid transaction object");
|
|
981
|
+
const keyPair = this.getKeyPair();
|
|
982
|
+
const accountId = this.getAddress();
|
|
983
|
+
if (!accountId) throw new Error("No account ID available");
|
|
984
|
+
await this.nearConfig.keyStore.setKey(this.nearConfig.networkId, accountId, keyPair);
|
|
985
|
+
const near = await (0, import_near_api_js4.connect)(this.nearConfig);
|
|
986
|
+
const account = await near.account(accountId);
|
|
987
|
+
if (tx.transactionHash) {
|
|
988
|
+
return tx.transactionHash;
|
|
989
|
+
}
|
|
990
|
+
const result = await account.signAndSendTransaction({
|
|
991
|
+
receiverId: tx.receiverId,
|
|
992
|
+
actions: tx.actions
|
|
993
|
+
});
|
|
994
|
+
return result.transaction.hash;
|
|
995
|
+
}
|
|
996
|
+
async sendTransactions(txs) {
|
|
997
|
+
return Promise.all(txs.map(async (tx) => this.sendTransaction(tx)));
|
|
998
|
+
}
|
|
999
|
+
create(mnemonic) {
|
|
1000
|
+
const seed = bip39.mnemonicToSeedSync(mnemonic);
|
|
1001
|
+
const keyPair = import_near_api_js4.KeyPair.fromRandom("ed25519");
|
|
1002
|
+
const publicKey = keyPair.getPublicKey();
|
|
1003
|
+
const accountId = publicKey.toString().split(":")[1] || publicKey.toString();
|
|
1004
|
+
return {
|
|
1005
|
+
address: accountId,
|
|
1006
|
+
privateKey: keyPair.toString(),
|
|
1007
|
+
mnemonic
|
|
1008
|
+
};
|
|
1009
|
+
}
|
|
1010
|
+
generate() {
|
|
1011
|
+
const mnemonic = bip39.generateMnemonic(import_english.wordlist);
|
|
1012
|
+
const seed = bip39.mnemonicToSeedSync(mnemonic);
|
|
1013
|
+
const keyPair = import_near_api_js4.KeyPair.fromRandom("ed25519");
|
|
1014
|
+
const publicKey = keyPair.getPublicKey();
|
|
1015
|
+
const accountId = publicKey.toString().split(":")[1] || publicKey.toString();
|
|
1016
|
+
return {
|
|
1017
|
+
address: accountId,
|
|
1018
|
+
privateKey: keyPair.toString(),
|
|
1019
|
+
mnemonic
|
|
1020
|
+
};
|
|
1021
|
+
}
|
|
1022
|
+
getAddress() {
|
|
1023
|
+
try {
|
|
1024
|
+
const privateKey = (0, import_warps5.getWarpWalletPrivateKeyFromConfig)(this.config, this.chain.name);
|
|
1025
|
+
if (privateKey) {
|
|
1026
|
+
try {
|
|
1027
|
+
const keyPair = import_near_api_js4.KeyPair.fromString(privateKey);
|
|
1028
|
+
const publicKey = keyPair.getPublicKey();
|
|
1029
|
+
return publicKey.toString().split(":")[1] || publicKey.toString();
|
|
1030
|
+
} catch {
|
|
1031
|
+
return null;
|
|
1032
|
+
}
|
|
1033
|
+
}
|
|
1034
|
+
const mnemonic = (0, import_warps5.getWarpWalletMnemonicFromConfig)(this.config, this.chain.name);
|
|
1035
|
+
if (mnemonic) {
|
|
1036
|
+
const seed = bip39.mnemonicToSeedSync(mnemonic);
|
|
1037
|
+
const keyPair = import_near_api_js4.KeyPair.fromRandom("ed25519");
|
|
1038
|
+
const publicKey = keyPair.getPublicKey();
|
|
1039
|
+
return publicKey.toString().split(":")[1] || publicKey.toString();
|
|
1040
|
+
}
|
|
1041
|
+
const wallet = this.config.user?.wallets?.[this.chain.name];
|
|
1042
|
+
if (wallet && typeof wallet === "object" && "address" in wallet) {
|
|
1043
|
+
return wallet.address;
|
|
1044
|
+
}
|
|
1045
|
+
if (wallet && typeof wallet === "string") {
|
|
1046
|
+
return wallet;
|
|
1047
|
+
}
|
|
1048
|
+
return null;
|
|
1049
|
+
} catch {
|
|
1050
|
+
return null;
|
|
1051
|
+
}
|
|
1052
|
+
}
|
|
1053
|
+
getPublicKey() {
|
|
1054
|
+
try {
|
|
1055
|
+
const keyPair = this.getKeyPair();
|
|
1056
|
+
const publicKey = keyPair.getPublicKey();
|
|
1057
|
+
return publicKey.toString();
|
|
1058
|
+
} catch {
|
|
1059
|
+
return null;
|
|
1060
|
+
}
|
|
1061
|
+
}
|
|
1062
|
+
getKeyPair() {
|
|
1063
|
+
const privateKey = (0, import_warps5.getWarpWalletPrivateKeyFromConfig)(this.config, this.chain.name);
|
|
1064
|
+
if (privateKey) {
|
|
1065
|
+
try {
|
|
1066
|
+
try {
|
|
1067
|
+
return import_near_api_js4.KeyPair.fromString(privateKey);
|
|
1068
|
+
} catch {
|
|
1069
|
+
}
|
|
1070
|
+
const keyPair = import_near_api_js4.KeyPair.fromRandom("ed25519");
|
|
1071
|
+
return keyPair;
|
|
1072
|
+
} catch (error) {
|
|
1073
|
+
if (error instanceof Error) {
|
|
1074
|
+
throw new Error(`Invalid private key format: ${error.message}`);
|
|
1075
|
+
}
|
|
1076
|
+
throw new Error("Invalid private key format");
|
|
1077
|
+
}
|
|
1078
|
+
}
|
|
1079
|
+
const mnemonic = (0, import_warps5.getWarpWalletMnemonicFromConfig)(this.config, this.chain.name);
|
|
1080
|
+
if (mnemonic) {
|
|
1081
|
+
const seed = bip39.mnemonicToSeedSync(mnemonic);
|
|
1082
|
+
const keyPair = import_near_api_js4.KeyPair.fromRandom("ed25519");
|
|
1083
|
+
return keyPair;
|
|
1084
|
+
}
|
|
1085
|
+
throw new Error("No private key or mnemonic provided");
|
|
1086
|
+
}
|
|
1087
|
+
};
|
|
1088
|
+
|
|
1089
|
+
// src/chains/common.ts
|
|
1090
|
+
var createNearAdapter = (chainName, chainInfos) => {
|
|
1091
|
+
return (config, fallback) => {
|
|
1092
|
+
if (!fallback) throw new Error(`${chainName} adapter requires a fallback adapter`);
|
|
1093
|
+
return {
|
|
1094
|
+
chainInfo: chainInfos[config.env],
|
|
1095
|
+
builder: () => fallback.builder(),
|
|
1096
|
+
executor: new WarpNearExecutor(config, chainInfos[config.env]),
|
|
1097
|
+
output: new WarpNearOutput(config, chainInfos[config.env]),
|
|
1098
|
+
serializer: new WarpNearSerializer(),
|
|
1099
|
+
registry: fallback.registry,
|
|
1100
|
+
explorer: new WarpNearExplorer(chainInfos[config.env], config),
|
|
1101
|
+
abiBuilder: () => fallback.abiBuilder(),
|
|
1102
|
+
brandBuilder: () => fallback.brandBuilder(),
|
|
1103
|
+
dataLoader: new WarpNearDataLoader(config, chainInfos[config.env]),
|
|
1104
|
+
wallet: new WarpNearWallet(config, chainInfos[config.env])
|
|
1105
|
+
};
|
|
1106
|
+
};
|
|
1107
|
+
};
|
|
1108
|
+
|
|
1109
|
+
// src/chains/near.ts
|
|
1110
|
+
var NativeTokenNear = {
|
|
1111
|
+
chain: "near",
|
|
1112
|
+
identifier: "NEAR",
|
|
1113
|
+
symbol: "NEAR",
|
|
1114
|
+
name: "NEAR",
|
|
1115
|
+
decimals: 24,
|
|
1116
|
+
logoUrl: "https://vleap.ai/images/tokens/near.svg"
|
|
1117
|
+
};
|
|
1118
|
+
var getNearAdapter = createNearAdapter("near", {
|
|
1119
|
+
mainnet: {
|
|
1120
|
+
name: "near",
|
|
1121
|
+
displayName: "NEAR Mainnet",
|
|
1122
|
+
chainId: "mainnet",
|
|
1123
|
+
blockTime: 1200,
|
|
1124
|
+
addressHrp: "",
|
|
1125
|
+
defaultApiUrl: "https://rpc.mainnet.near.org",
|
|
1126
|
+
logoUrl: "https://vleap.ai/images/chains/near.svg",
|
|
1127
|
+
nativeToken: NativeTokenNear
|
|
1128
|
+
},
|
|
1129
|
+
testnet: {
|
|
1130
|
+
name: "near",
|
|
1131
|
+
displayName: "NEAR Testnet",
|
|
1132
|
+
chainId: "testnet",
|
|
1133
|
+
blockTime: 1200,
|
|
1134
|
+
addressHrp: "",
|
|
1135
|
+
defaultApiUrl: "https://rpc.testnet.near.org",
|
|
1136
|
+
logoUrl: "https://vleap.ai/images/chains/near.svg",
|
|
1137
|
+
nativeToken: NativeTokenNear
|
|
1138
|
+
},
|
|
1139
|
+
devnet: {
|
|
1140
|
+
name: "near",
|
|
1141
|
+
displayName: "NEAR Devnet",
|
|
1142
|
+
chainId: "testnet",
|
|
1143
|
+
blockTime: 1200,
|
|
1144
|
+
addressHrp: "",
|
|
1145
|
+
defaultApiUrl: "https://rpc.testnet.near.org",
|
|
1146
|
+
logoUrl: "https://vleap.ai/images/chains/near.svg",
|
|
1147
|
+
nativeToken: NativeTokenNear
|
|
1148
|
+
}
|
|
1149
|
+
});
|
|
1150
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
1151
|
+
0 && (module.exports = {
|
|
1152
|
+
ExplorerUrls,
|
|
1153
|
+
KnownTokens,
|
|
1154
|
+
NativeTokenNear,
|
|
1155
|
+
NearExplorerMap,
|
|
1156
|
+
NearExplorerNames,
|
|
1157
|
+
NearExplorerUrls,
|
|
1158
|
+
NearExplorers,
|
|
1159
|
+
NearTokens,
|
|
1160
|
+
WarpNearConstants,
|
|
1161
|
+
WarpNearDataLoader,
|
|
1162
|
+
WarpNearExecutor,
|
|
1163
|
+
WarpNearExplorer,
|
|
1164
|
+
WarpNearOutput,
|
|
1165
|
+
WarpNearSerializer,
|
|
1166
|
+
WarpNearWallet,
|
|
1167
|
+
findKnownTokenById,
|
|
1168
|
+
getKnownTokensForChain,
|
|
1169
|
+
getNearAdapter
|
|
1170
|
+
});
|
|
1171
|
+
//# sourceMappingURL=index.js.map
|