@gardenfi/orderbook 3.1.2 → 3.1.3-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.cjs +1 -1
- package/dist/index.js +35 -33
- package/dist/index10.cjs +1 -1
- package/dist/index10.js +29 -186
- package/dist/index11.cjs +1 -1
- package/dist/index11.js +138 -157
- package/dist/index12.cjs +1 -1
- package/dist/index12.js +203 -27
- package/dist/index13.cjs +1 -0
- package/dist/index13.js +31 -0
- package/dist/index2.cjs +1 -1
- package/dist/index2.js +1 -1
- package/dist/index4.cjs +1 -1
- package/dist/index4.js +25 -5
- package/dist/index9.cjs +1 -1
- package/dist/index9.js +54 -28
- package/dist/src/index.d.ts +1 -0
- package/dist/src/lib/ExplorerUrlBuilder/ExplorerUrlBuilder.d.ts +13 -0
- package/dist/src/lib/constants/asset.d.ts +41 -1
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const i=require("./index2.cjs"),n=require("./index3.cjs"),e=require("./index4.cjs"),t=require("./index5.cjs"),o=require("./index6.cjs"),s=require("./index7.cjs"),a=require("./index8.cjs"),l=require("./index9.cjs"),r=require("./index10.cjs"),c=require("./index11.cjs"),d=require("./index12.cjs");exports.Orderbook=i.Orderbook;exports.BlockchainType=n.BlockchainType;exports.Assets=e.Assets;exports.Chains=e.Chains;exports.Config=e.Config;exports.NATIVE_TOKENS=e.NATIVE_TOKENS;exports.OrderLifecycle=e.OrderLifecycle;exports.OrderStatus=e.OrderStatus;exports.getBlockchainType=e.getBlockchainType;exports.is=e.is;exports.isAlpenSignet=e.isAlpenSignet;exports.isBitcoin=e.isBitcoin;exports.isEVM=e.isEVM;exports.isEvmNativeToken=e.isEvmNativeToken;exports.isLitecoin=e.isLitecoin;exports.isMainnet=e.isMainnet;exports.isNativeToken=e.isNativeToken;exports.isSolana=e.isSolana;exports.isSolanaNativeToken=e.isSolanaNativeToken;exports.isSpark=e.isSpark;exports.isStarknet=e.isStarknet;exports.isSui=e.isSui;exports.isSuiNativeToken=e.isSuiNativeToken;exports.isTron=e.isTron;exports.isXrpl=e.isXrpl;exports.ArbitrumLocalnet=t.ArbitrumLocalnet;exports.ETHStarknetLocalnetAsset=t.ETHStarknetLocalnetAsset;exports.EthereumLocalnet=t.EthereumLocalnet;exports.SOLSolanaLocalnetAsset=t.SOLSolanaLocalnetAsset;exports.STRKStarknetLocalnetAsset=t.STRKStarknetLocalnetAsset;exports.StarknetLocalnet=t.StarknetLocalnet;exports.WBTCArbitrumLocalnetAsset=t.WBTCArbitrumLocalnetAsset;exports.WBTCEthereumLocalnetAsset=t.WBTCEthereumLocalnetAsset;exports.bitcoinRegtestAsset=t.bitcoinRegtestAsset;exports.buildAssetsWithChain=o.buildAssetsWithChain;exports.ConstructUrl=s.ConstructUrl;exports.discriminateOrderResponse=s.discriminateOrderResponse;exports.getOrderResponseType=s.getOrderResponseType;exports.isBitcoinOrderResponse=s.isBitcoinOrderResponse;exports.isEvmOrderResponse=s.isEvmOrderResponse;exports.isOrder=s.isOrder;exports.isSolanaOrderResponse=s.isSolanaOrderResponse;exports.isStarknetOrderResponse=s.isStarknetOrderResponse;exports.isSuiOrderResponse=s.isSuiOrderResponse;exports.isXrplOrderResponse=s.isXrplOrderResponse;exports.ChainAsset=a.ChainAsset;exports.ExplorerUrlBuilder=l.ExplorerUrlBuilder;exports.OrderAction=r.OrderAction;exports.ParseOrderStatus=r.ParseOrderStatus;exports.isCompleted=r.isCompleted;exports.isDeadlinePassed=r.isDeadlinePassed;exports.parseAction=r.parseAction;exports.AssetManager=c.AssetManager;exports.RouteValidator=d.RouteValidator;
|
package/dist/index.js
CHANGED
|
@@ -1,65 +1,67 @@
|
|
|
1
|
-
import { Orderbook as
|
|
2
|
-
import { BlockchainType as
|
|
3
|
-
import { Assets as n, Chains as a, Config as p, NATIVE_TOKENS as l, OrderLifecycle as c, OrderStatus as d, getBlockchainType as
|
|
4
|
-
import { ArbitrumLocalnet as b, ETHStarknetLocalnetAsset as y, EthereumLocalnet as M, SOLSolanaLocalnetAsset as V, STRKStarknetLocalnetAsset as W, StarknetLocalnet as K, WBTCArbitrumLocalnetAsset as P, WBTCEthereumLocalnetAsset as
|
|
5
|
-
import { buildAssetsWithChain as
|
|
1
|
+
import { Orderbook as s } from "./index2.js";
|
|
2
|
+
import { BlockchainType as o } from "./index3.js";
|
|
3
|
+
import { Assets as n, Chains as a, Config as p, NATIVE_TOKENS as l, OrderLifecycle as c, OrderStatus as d, getBlockchainType as m, is as S, isAlpenSignet as A, isBitcoin as O, isEVM as T, isEvmNativeToken as f, isLitecoin as k, isMainnet as u, isNativeToken as x, isSolana as L, isSolanaNativeToken as R, isSpark as E, isStarknet as h, isSui as C, isSuiNativeToken as B, isTron as g, isXrpl as v } from "./index4.js";
|
|
4
|
+
import { ArbitrumLocalnet as b, ETHStarknetLocalnetAsset as y, EthereumLocalnet as M, SOLSolanaLocalnetAsset as V, STRKStarknetLocalnetAsset as W, StarknetLocalnet as K, WBTCArbitrumLocalnetAsset as P, WBTCEthereumLocalnetAsset as U, bitcoinRegtestAsset as X } from "./index5.js";
|
|
5
|
+
import { buildAssetsWithChain as H } from "./index6.js";
|
|
6
6
|
import { ConstructUrl as _, discriminateOrderResponse as j, getOrderResponseType as q, isBitcoinOrderResponse as w, isEvmOrderResponse as z, isOrder as F, isSolanaOrderResponse as G, isStarknetOrderResponse as J, isSuiOrderResponse as Q, isXrplOrderResponse as Y } from "./index7.js";
|
|
7
7
|
import { ChainAsset as $ } from "./index8.js";
|
|
8
|
-
import {
|
|
9
|
-
import {
|
|
10
|
-
import {
|
|
8
|
+
import { ExplorerUrlBuilder as re } from "./index9.js";
|
|
9
|
+
import { OrderAction as te, ParseOrderStatus as oe, isCompleted as ie, isDeadlinePassed as ne, parseAction as ae } from "./index10.js";
|
|
10
|
+
import { AssetManager as le } from "./index11.js";
|
|
11
|
+
import { RouteValidator as de } from "./index12.js";
|
|
11
12
|
export {
|
|
12
13
|
b as ArbitrumLocalnet,
|
|
13
|
-
|
|
14
|
+
le as AssetManager,
|
|
14
15
|
n as Assets,
|
|
15
|
-
|
|
16
|
+
o as BlockchainType,
|
|
16
17
|
$ as ChainAsset,
|
|
17
18
|
a as Chains,
|
|
18
19
|
p as Config,
|
|
19
20
|
_ as ConstructUrl,
|
|
20
21
|
y as ETHStarknetLocalnetAsset,
|
|
21
22
|
M as EthereumLocalnet,
|
|
23
|
+
re as ExplorerUrlBuilder,
|
|
22
24
|
l as NATIVE_TOKENS,
|
|
23
|
-
|
|
25
|
+
te as OrderAction,
|
|
24
26
|
c as OrderLifecycle,
|
|
25
27
|
d as OrderStatus,
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
28
|
+
s as Orderbook,
|
|
29
|
+
oe as ParseOrderStatus,
|
|
30
|
+
de as RouteValidator,
|
|
29
31
|
V as SOLSolanaLocalnetAsset,
|
|
30
32
|
W as STRKStarknetLocalnetAsset,
|
|
31
33
|
K as StarknetLocalnet,
|
|
32
34
|
P as WBTCArbitrumLocalnetAsset,
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
35
|
+
U as WBTCEthereumLocalnetAsset,
|
|
36
|
+
X as bitcoinRegtestAsset,
|
|
37
|
+
H as buildAssetsWithChain,
|
|
36
38
|
j as discriminateOrderResponse,
|
|
37
|
-
|
|
39
|
+
m as getBlockchainType,
|
|
38
40
|
q as getOrderResponseType,
|
|
39
|
-
|
|
41
|
+
S as is,
|
|
40
42
|
A as isAlpenSignet,
|
|
41
43
|
O as isBitcoin,
|
|
42
44
|
w as isBitcoinOrderResponse,
|
|
43
|
-
|
|
44
|
-
|
|
45
|
+
ie as isCompleted,
|
|
46
|
+
ne as isDeadlinePassed,
|
|
45
47
|
T as isEVM,
|
|
46
|
-
|
|
48
|
+
f as isEvmNativeToken,
|
|
47
49
|
z as isEvmOrderResponse,
|
|
48
|
-
|
|
50
|
+
k as isLitecoin,
|
|
49
51
|
u as isMainnet,
|
|
50
|
-
|
|
52
|
+
x as isNativeToken,
|
|
51
53
|
F as isOrder,
|
|
52
|
-
|
|
53
|
-
|
|
54
|
+
L as isSolana,
|
|
55
|
+
R as isSolanaNativeToken,
|
|
54
56
|
G as isSolanaOrderResponse,
|
|
55
|
-
|
|
56
|
-
|
|
57
|
+
E as isSpark,
|
|
58
|
+
h as isStarknet,
|
|
57
59
|
J as isStarknetOrderResponse,
|
|
58
|
-
|
|
59
|
-
|
|
60
|
+
C as isSui,
|
|
61
|
+
B as isSuiNativeToken,
|
|
60
62
|
Q as isSuiOrderResponse,
|
|
61
|
-
|
|
62
|
-
|
|
63
|
+
g as isTron,
|
|
64
|
+
v as isXrpl,
|
|
63
65
|
Y as isXrplOrderResponse,
|
|
64
|
-
|
|
66
|
+
ae as parseAction
|
|
65
67
|
};
|
package/dist/index10.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const e=require("./index4.cjs"),u=t=>{const{created_at:n,source_swap:r,destination_swap:d}=t;if(d.redeem_tx_hash)return Number(d.redeem_block_number)||e.isBitcoin(d.chain)?e.OrderStatus.Redeemed:e.OrderStatus.RedeemDetected;if(e.isBitcoin(d.chain)&&r.redeem_tx_hash&&!d.redeem_tx_hash)return e.OrderStatus.Redeemed;if(r.refund_tx_hash)return Number(r.refund_block_number)?e.OrderStatus.Refunded:e.OrderStatus.RefundDetected;if(d.refund_tx_hash)return e.OrderStatus.AwaitingRefund;const a=e.isBitcoin(r.chain)?s(new Date(n),6):s(new Date(n),1);return d.initiate_tx_hash?e.OrderStatus.AwaitingRedeem:r.initiate_tx_hash?a?e.OrderStatus.AwaitingRefund:Number(r.initiate_block_number)?e.OrderStatus.Initiated:e.OrderStatus.InitiateDetected:a?e.OrderStatus.Expired:e.OrderStatus.Created},s=(t,n=1)=>{const r=new Date,d=new Date(t);return d.setHours(d.getHours()+n),r>d},_=t=>{const n=u(t);return n===e.OrderStatus.Redeemed||n===e.OrderStatus.Refunded||n===e.OrderStatus.Expired};var i=(t=>(t.Initiate="Initiate",t.PostRefundSACP="PostRefundSACP",t.Redeem="Redeem",t.Refund="Refund",t.Idle="Idle",t))(i||{});const o=t=>{const{source_swap:n,destination_swap:r}=t;return r.redeem_tx_hash&&Number(r.redeem_block_number)||n.refund_tx_hash&&Number(n.refund_block_number)?"Idle":!r.refund_tx_hash&&r.initiate_tx_hash?"Redeem":"Idle"};exports.OrderAction=i;exports.ParseOrderStatus=u;exports.isCompleted=_;exports.isDeadlinePassed=s;exports.parseAction=o;
|
package/dist/index10.js
CHANGED
|
@@ -1,188 +1,31 @@
|
|
|
1
|
-
import {
|
|
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
|
-
async initialize() {
|
|
27
|
-
await this.fetchAndSetAssetsAndChains(), await this.routeValidator.loadPolicy();
|
|
28
|
-
}
|
|
29
|
-
/**
|
|
30
|
-
* Fetch and cache asset and chain data from API
|
|
31
|
-
*/
|
|
32
|
-
async fetchAndSetAssetsAndChains() {
|
|
33
|
-
try {
|
|
34
|
-
this.isLoading = !0, this.error = null;
|
|
35
|
-
const t = await this.auth.getAuthHeaders();
|
|
36
|
-
if (!t.ok) return o("Failed to get auth headers");
|
|
37
|
-
const s = await y.get(
|
|
38
|
-
this.urls.assets,
|
|
39
|
-
{
|
|
40
|
-
headers: {
|
|
41
|
-
...t.val
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
);
|
|
45
|
-
if (!s.result) return o(`Failed to fetch chains data: ${s.error}`);
|
|
46
|
-
const { assets: e, chains: a } = this.processApiData(s.result);
|
|
47
|
-
return this.assets = e, this.chains = a, await this.buildRouteMatrix(), g("AssetManager initialized successfully");
|
|
48
|
-
} catch (t) {
|
|
49
|
-
return o(`Failed to fetch assets data: ${t}`);
|
|
50
|
-
} finally {
|
|
51
|
-
this.isLoading = !1;
|
|
52
|
-
}
|
|
53
|
-
}
|
|
54
|
-
// ============================================
|
|
55
|
-
// Route Validation Methods
|
|
56
|
-
// ============================================
|
|
57
|
-
/**
|
|
58
|
-
* Check if a swap route from one asset to another is valid
|
|
59
|
-
*/
|
|
60
|
-
async isRouteValid(t, s) {
|
|
61
|
-
return await this.routeValidator.isValidRoute(
|
|
62
|
-
r.from(t.id),
|
|
63
|
-
r.from(s.id)
|
|
64
|
-
);
|
|
65
|
-
}
|
|
66
|
-
/**
|
|
67
|
-
* Get all valid destination assets for a given source asset
|
|
68
|
-
*/
|
|
69
|
-
getValidDestinations(t) {
|
|
70
|
-
if (!this.routeMatrix || !this.assets || !t.id)
|
|
71
|
-
return console.warn(
|
|
72
|
-
"Missing routeMatrix, assets, or asset. Returning all assets."
|
|
73
|
-
), [];
|
|
74
|
-
const s = this.routeMatrix[t.id.toString().toLowerCase()];
|
|
75
|
-
return s ? s.map((e) => {
|
|
76
|
-
const a = e.toString();
|
|
77
|
-
return Object.values(this.assets).find((i) => r.from(i).toString() === a);
|
|
78
|
-
}).filter(Boolean) : [];
|
|
79
|
-
}
|
|
80
|
-
// ============================================
|
|
81
|
-
// Asset Query Methods
|
|
82
|
-
// ============================================
|
|
83
|
-
/**
|
|
84
|
-
* Get asset by chain and token address
|
|
85
|
-
*/
|
|
86
|
-
getAsset(t) {
|
|
87
|
-
if (!this.assets) return;
|
|
88
|
-
const s = r.from(t).toString();
|
|
89
|
-
return this.assets[s];
|
|
90
|
-
}
|
|
91
|
-
/**
|
|
92
|
-
* Get all assets for a specific chain
|
|
93
|
-
*/
|
|
94
|
-
getAssetsByChain(t) {
|
|
95
|
-
return this.assets ? Object.values(this.assets).filter((s) => s.chain === t) : [];
|
|
96
|
-
}
|
|
97
|
-
/**
|
|
98
|
-
* Search assets by symbol or name
|
|
99
|
-
*/
|
|
100
|
-
searchAssets(t) {
|
|
101
|
-
if (!this.assets) return [];
|
|
102
|
-
const s = t.toLowerCase();
|
|
103
|
-
return Object.values(this.assets).filter(
|
|
104
|
-
(e) => e.symbol.toLowerCase().includes(s) || e.name.toLowerCase().includes(s)
|
|
105
|
-
);
|
|
106
|
-
}
|
|
107
|
-
/**
|
|
108
|
-
* Get chain data by chain identifier
|
|
109
|
-
*/
|
|
110
|
-
getChain(t) {
|
|
111
|
-
if (this.chains)
|
|
112
|
-
return this.chains[t];
|
|
113
|
-
}
|
|
114
|
-
// ============================================
|
|
115
|
-
// Private Helper Methods
|
|
116
|
-
// ============================================
|
|
117
|
-
/**
|
|
118
|
-
* Build route matrix for fast O(1) route lookups
|
|
119
|
-
*/
|
|
120
|
-
async buildRouteMatrix() {
|
|
121
|
-
if (!this.assets || !this.routeValidator) return;
|
|
122
|
-
const t = Object.values(this.assets).map((s) => {
|
|
123
|
-
if (!s.id) return null;
|
|
124
|
-
try {
|
|
125
|
-
return r.from(s.id);
|
|
126
|
-
} catch {
|
|
127
|
-
return null;
|
|
128
|
-
}
|
|
129
|
-
}).filter((s) => s !== null);
|
|
130
|
-
this.routeMatrix = await this.routeValidator.buildRouteMatrix(
|
|
131
|
-
t
|
|
132
|
-
);
|
|
133
|
-
}
|
|
134
|
-
/**
|
|
135
|
-
* Process raw API data into structured format
|
|
136
|
-
*/
|
|
137
|
-
processApiData(t) {
|
|
138
|
-
const s = {}, e = {};
|
|
139
|
-
for (const a of t) {
|
|
140
|
-
const i = this.parseChainIdentifier(a.chain);
|
|
141
|
-
if (!i || !c.includes(i))
|
|
142
|
-
continue;
|
|
143
|
-
const h = {
|
|
144
|
-
...a,
|
|
145
|
-
name: a.name || this.formatChainName(i),
|
|
146
|
-
chain: i
|
|
147
|
-
};
|
|
148
|
-
for (const n of a.assets) {
|
|
149
|
-
const d = r.from(n.id).toString(), { name: f, symbol: m } = w(
|
|
150
|
-
n.name,
|
|
151
|
-
n.id
|
|
152
|
-
), p = {
|
|
153
|
-
...n,
|
|
154
|
-
id: r.from(n.id),
|
|
155
|
-
chain: i,
|
|
156
|
-
name: f,
|
|
157
|
-
symbol: m
|
|
158
|
-
};
|
|
159
|
-
s[d] = p, e[i] || (e[i] = h);
|
|
160
|
-
}
|
|
161
|
-
}
|
|
162
|
-
return { assets: s, chains: e };
|
|
163
|
-
}
|
|
164
|
-
/**
|
|
165
|
-
* Parse chain identifier from string
|
|
166
|
-
*/
|
|
167
|
-
parseChainIdentifier(t) {
|
|
168
|
-
return c.includes(t) ? t : null;
|
|
169
|
-
}
|
|
170
|
-
/**
|
|
171
|
-
* Format chain name for display
|
|
172
|
-
*/
|
|
173
|
-
formatChainName(t) {
|
|
174
|
-
return t.split("_").map((s) => s.charAt(0).toUpperCase() + s.slice(1)).join(" ");
|
|
175
|
-
}
|
|
176
|
-
// ============================================
|
|
177
|
-
// Data Refresh Methods
|
|
178
|
-
// ============================================
|
|
179
|
-
/**
|
|
180
|
-
* Refresh all data
|
|
181
|
-
*/
|
|
182
|
-
async refresh() {
|
|
183
|
-
await this.initialize();
|
|
184
|
-
}
|
|
185
|
-
}
|
|
1
|
+
import { OrderStatus as e, isBitcoin as d } from "./index4.js";
|
|
2
|
+
const s = (t) => {
|
|
3
|
+
const { created_at: r, source_swap: n, destination_swap: i } = t;
|
|
4
|
+
if (i.redeem_tx_hash)
|
|
5
|
+
return Number(i.redeem_block_number) ? e.Redeemed : d(i.chain) ? e.Redeemed : e.RedeemDetected;
|
|
6
|
+
if (d(i.chain) && n.redeem_tx_hash && !i.redeem_tx_hash)
|
|
7
|
+
return e.Redeemed;
|
|
8
|
+
if (n.refund_tx_hash)
|
|
9
|
+
return Number(n.refund_block_number) ? e.Refunded : e.RefundDetected;
|
|
10
|
+
if (i.refund_tx_hash) return e.AwaitingRefund;
|
|
11
|
+
const u = d(n.chain) ? a(new Date(r), 6) : a(new Date(r), 1);
|
|
12
|
+
return i.initiate_tx_hash ? e.AwaitingRedeem : n.initiate_tx_hash ? u ? e.AwaitingRefund : Number(n.initiate_block_number) ? e.Initiated : e.InitiateDetected : u ? e.Expired : e.Created;
|
|
13
|
+
}, a = (t, r = 1) => {
|
|
14
|
+
const n = /* @__PURE__ */ new Date(), i = new Date(t);
|
|
15
|
+
return i.setHours(i.getHours() + r), n > i;
|
|
16
|
+
}, m = (t) => {
|
|
17
|
+
const r = s(t);
|
|
18
|
+
return r === e.Redeemed || r === e.Refunded || r === e.Expired;
|
|
19
|
+
};
|
|
20
|
+
var _ = /* @__PURE__ */ ((t) => (t.Initiate = "Initiate", t.PostRefundSACP = "PostRefundSACP", t.Redeem = "Redeem", t.Refund = "Refund", t.Idle = "Idle", t))(_ || {});
|
|
21
|
+
const o = (t) => {
|
|
22
|
+
const { source_swap: r, destination_swap: n } = t;
|
|
23
|
+
return n.redeem_tx_hash && Number(n.redeem_block_number) || r.refund_tx_hash && Number(r.refund_block_number) ? "Idle" : !n.refund_tx_hash && n.initiate_tx_hash ? "Redeem" : "Idle";
|
|
24
|
+
};
|
|
186
25
|
export {
|
|
187
|
-
|
|
26
|
+
_ as OrderAction,
|
|
27
|
+
s as ParseOrderStatus,
|
|
28
|
+
m as isCompleted,
|
|
29
|
+
a as isDeadlinePassed,
|
|
30
|
+
o as parseAction
|
|
188
31
|
};
|
package/dist/index11.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const n=require("@gardenfi/utils"),y=require("./index12.cjs"),p=require("./index4.cjs"),r=require("./index8.cjs"),o=require("./index13.cjs"),h=Object.keys(p.Config);class g{constructor(t,s){this.assets=null,this.chains=null,this.routeMatrix=null,this.isLoading=!1,this.error=null,typeof s=="string"?this.auth=new n.ApiKey(s):this.auth=s;const e=o.ensureUrl(t.baseUrl),a=o.buildDefaultEndpoint(e,"/v2/chains"),i=o.buildDefaultEndpoint(e,"/v2/policy");this.urls={base:e,assets:o.ensureUrl(t.assetsUrl??a),policy:o.ensureUrl(t.policyUrl??i)},this.routeValidator=new y.RouteValidator(this.urls.policy.toString(),this.auth)}async initialize(){await this.fetchAndSetAssetsAndChains(),await this.routeValidator.loadPolicy()}async fetchAndSetAssetsAndChains(){try{this.isLoading=!0,this.error=null;const t=await this.auth.getAuthHeaders();if(!t.ok)return n.Err("Failed to get auth headers");const s=await n.Fetcher.get(this.urls.assets,{headers:{...t.val}});if(!s.result)return n.Err(`Failed to fetch chains data: ${s.error}`);const{assets:e,chains:a}=this.processApiData(s.result);return this.assets=e,this.chains=a,await this.buildRouteMatrix(),n.Ok("AssetManager initialized successfully")}catch(t){return n.Err(`Failed to fetch assets data: ${t}`)}finally{this.isLoading=!1}}async isRouteValid(t,s){return await this.routeValidator.isValidRoute(r.ChainAsset.from(t.id),r.ChainAsset.from(s.id))}getValidDestinations(t){if(!this.routeMatrix||!this.assets||!t.id)return console.warn("Missing routeMatrix, assets, or asset. Returning all assets."),[];const s=this.routeMatrix[t.id.toString().toLowerCase()];return s?s.map(e=>{const a=e.toString();return Object.values(this.assets).find(i=>r.ChainAsset.from(i).toString()===a)}).filter(Boolean):[]}getAsset(t){if(!this.assets)return;const s=r.ChainAsset.from(t).toString();return this.assets[s]}getAssetsByChain(t){return this.assets?Object.values(this.assets).filter(s=>s.chain===t):[]}searchAssets(t){if(!this.assets)return[];const s=t.toLowerCase();return Object.values(this.assets).filter(e=>e.symbol.toLowerCase().includes(s)||e.name.toLowerCase().includes(s))}getChain(t){if(this.chains)return this.chains[t]}async buildRouteMatrix(){if(!this.assets||!this.routeValidator)return;const t=Object.values(this.assets).map(s=>{if(!s.id)return null;try{return r.ChainAsset.from(s.id)}catch{return null}}).filter(s=>s!==null);this.routeMatrix=await this.routeValidator.buildRouteMatrix(t)}processApiData(t){const s={},e={};for(const a of t){const i=this.parseChainIdentifier(a.chain);if(!i||!h.includes(i))continue;const u={...a,name:a.name||this.formatChainName(i),chain:i};for(const l of a.assets){const c=r.ChainAsset.from(l.id).toString(),{name:d,symbol:f}=o.parseAssetNameSymbol(l.name,l.id),A={...l,id:r.ChainAsset.from(l.id),chain:i,name:d,symbol:f};s[c]=A,e[i]||(e[i]=u)}}return{assets:s,chains:e}}parseChainIdentifier(t){return h.includes(t)?t:null}formatChainName(t){return t.split("_").map(s=>s.charAt(0).toUpperCase()+s.slice(1)).join(" ")}async refresh(){await this.initialize()}}exports.AssetManager=g;
|