@gardenfi/orderbook 3.1.1 → 3.1.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.
package/dist/index.cjs CHANGED
@@ -1 +1 @@
1
- "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const r=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"),i=require("./index9.cjs"),c=require("./index10.cjs"),l=require("./index11.cjs");exports.Orderbook=r.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.OrderAction=i.OrderAction;exports.ParseOrderStatus=i.ParseOrderStatus;exports.isCompleted=i.isCompleted;exports.isDeadlinePassed=i.isDeadlinePassed;exports.parseAction=i.parseAction;exports.AssetManager=c.AssetManager;exports.RouteValidator=l.RouteValidator;
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 r } from "./index2.js";
2
- import { BlockchainType as i } 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 S, is as m, isAlpenSignet as A, isBitcoin as O, isEVM as T, isEvmNativeToken as k, isLitecoin as f, isMainnet as u, isNativeToken as L, isSolana as R, isSolanaNativeToken as x, isSpark as h, isStarknet as C, isSui as E, isSuiNativeToken as g, isTron as v, isXrpl as B } 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 X, bitcoinRegtestAsset as D } from "./index5.js";
5
- import { buildAssetsWithChain as I } from "./index6.js";
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 { OrderAction as se, ParseOrderStatus as re, isCompleted as te, isDeadlinePassed as ie, parseAction as oe } from "./index9.js";
9
- import { AssetManager as ae } from "./index10.js";
10
- import { RouteValidator as le } from "./index11.js";
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
- ae as AssetManager,
14
+ le as AssetManager,
14
15
  n as Assets,
15
- i as BlockchainType,
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
- se as OrderAction,
25
+ te as OrderAction,
24
26
  c as OrderLifecycle,
25
27
  d as OrderStatus,
26
- r as Orderbook,
27
- re as ParseOrderStatus,
28
- le as RouteValidator,
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
- X as WBTCEthereumLocalnetAsset,
34
- D as bitcoinRegtestAsset,
35
- I as buildAssetsWithChain,
35
+ U as WBTCEthereumLocalnetAsset,
36
+ X as bitcoinRegtestAsset,
37
+ H as buildAssetsWithChain,
36
38
  j as discriminateOrderResponse,
37
- S as getBlockchainType,
39
+ m as getBlockchainType,
38
40
  q as getOrderResponseType,
39
- m as is,
41
+ S as is,
40
42
  A as isAlpenSignet,
41
43
  O as isBitcoin,
42
44
  w as isBitcoinOrderResponse,
43
- te as isCompleted,
44
- ie as isDeadlinePassed,
45
+ ie as isCompleted,
46
+ ne as isDeadlinePassed,
45
47
  T as isEVM,
46
- k as isEvmNativeToken,
48
+ f as isEvmNativeToken,
47
49
  z as isEvmOrderResponse,
48
- f as isLitecoin,
50
+ k as isLitecoin,
49
51
  u as isMainnet,
50
- L as isNativeToken,
52
+ x as isNativeToken,
51
53
  F as isOrder,
52
- R as isSolana,
53
- x as isSolanaNativeToken,
54
+ L as isSolana,
55
+ R as isSolanaNativeToken,
54
56
  G as isSolanaOrderResponse,
55
- h as isSpark,
56
- C as isStarknet,
57
+ E as isSpark,
58
+ h as isStarknet,
57
59
  J as isStarknetOrderResponse,
58
- E as isSui,
59
- g as isSuiNativeToken,
60
+ C as isSui,
61
+ B as isSuiNativeToken,
60
62
  Q as isSuiOrderResponse,
61
- v as isTron,
62
- B as isXrpl,
63
+ g as isTron,
64
+ v as isXrpl,
63
65
  Y as isXrplOrderResponse,
64
- oe as parseAction
66
+ ae as parseAction
65
67
  };
package/dist/index10.cjs CHANGED
@@ -1 +1 @@
1
- "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const n=require("@gardenfi/utils"),y=require("./index11.cjs"),p=require("./index4.cjs"),r=require("./index8.cjs"),o=require("./index12.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;
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 { ApiKey as A, Err as o, Fetcher as y, Ok as g } from "@gardenfi/utils";
2
- import { RouteValidator as C } from "./index11.js";
3
- import { Config as b } from "./index4.js";
4
- import { ChainAsset as r } from "./index8.js";
5
- import { ensureUrl as l, buildDefaultEndpoint as u, parseAssetNameSymbol as w } from "./index12.js";
6
- const c = Object.keys(b);
7
- class I {
8
- constructor(t, s) {
9
- this.assets = null, this.chains = null, this.routeMatrix = null, this.isLoading = !1, this.error = null, typeof s == "string" ? this.auth = new A(s) : this.auth = s;
10
- const e = l(t.baseUrl), a = u(e, "/v2/chains"), i = u(e, "/v2/policy");
11
- this.urls = {
12
- base: e,
13
- assets: l(t.assetsUrl ?? a),
14
- policy: l(t.policyUrl ?? i)
15
- }, this.routeValidator = new C(
16
- this.urls.policy.toString(),
17
- this.auth
18
- );
19
- }
20
- // ============================================
21
- // Initialization Methods
22
- // ============================================
23
- /**
24
- * Initialize the AssetManager by fetching all required data
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
- I as AssetManager
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 a=require("@gardenfi/utils"),h=require("node-cache"),o=class o{constructor(e,t){this.policy=new h({stdTTL:36e3,checkperiod:3600}),typeof t=="string"?this.auth=new a.ApiKey(t):this.auth=t,typeof e=="string"?this.apiBaseUrl=new a.Url(e):this.apiBaseUrl=e}getPolicy(){return this.policy.get(o.POLICY_CACHE_KEY)}setPolicy(e){const t=this.getPolicy(),s={default:e.default||(t==null?void 0:t.default)||"open",isolation_groups:[...new Set([...(t==null?void 0:t.isolation_groups)||[],...e.isolation_groups])],blacklist_pairs:[...new Set([...(t==null?void 0:t.blacklist_pairs)||[],...e.blacklist_pairs])],whitelist_overrides:[...new Set([...(t==null?void 0:t.whitelist_overrides)||[],...e.whitelist_overrides])]};return this.policy.set(o.POLICY_CACHE_KEY,s),s}async loadPolicy(){const e=this.getPolicy();if(e)return a.Ok(e);try{const t=await this.auth.getAuthHeaders();if(!t.ok)return a.Err(`Failed to get auth headers: ${t.error}`);const s=await a.Fetcher.get(this.apiBaseUrl,{headers:{"Content-Type":"application/json",...t.val}});return s.result?a.Ok(this.setPolicy(s.result)):a.Err(`API Error: ${s.error}`)}catch(t){return a.Err(`Failed to load policy: ${t}`)}}async isValidRoute(e,t){const s=await this.loadPolicy();if(!s.ok||e.toString()===t.toString())return!1;const{sortedIsolationRules:r,sortedBlacklistRules:i,sortedWhitelistRules:n}=this.preprocessRules(s.val),c=this.findMatchingRule(e,r);if(c&&!this.matchesRuleDestination(t,c))return!1;const l=this.findMatchingRule(t,r);return l&&l.rule.direction==="<->"&&!this.matchesRuleSource(e,l)?!1:this.matchesRuleList(e,t,i)?!!this.matchesRuleList(e,t,n):s.val.default==="open"}async getValidDestinations(e,t){const s=[];for(const r of t)await this.isValidRoute(e,r)&&s.push(r);return s}async getAllValidRoutes(e){const t=[];for(const s of e)for(const r of e)await this.isValidRoute(s,r)&&t.push({from:s,to:r});return t}preprocessRules(e){const t=e.isolation_groups.map(i=>this.parseRule(i)).sort((i,n)=>n.specificity-i.specificity),s=e.blacklist_pairs.map(i=>this.parseRule(i)).sort((i,n)=>n.specificity-i.specificity),r=e.whitelist_overrides.map(i=>this.parseRule(i)).sort((i,n)=>n.specificity-i.specificity);return{sortedIsolationRules:t,sortedBlacklistRules:s,sortedWhitelistRules:r}}parseRule(e){const s=e.includes("<->")?"<->":"->",[r,i]=e.split(s).map(n=>n.trim());return{pattern:e,fromPattern:r,toPattern:i,direction:s,specificity:this.calculateSpecificity(r,i)}}calculateSpecificity(e,t){const s=r=>{const i=r.toLowerCase();return i==="*"?0:i.includes("*")?1:2};return s(e)*10+s(t)}findMatchingRule(e,t){for(const s of t){if(this.matchesAssetPattern(e,s.fromPattern))return{rule:s,matchedAs:"from"};if(s.direction==="<->"&&this.matchesAssetPattern(e,s.toPattern))return{rule:s,matchedAs:"to"}}return null}matchesRuleDestination(e,t){return t.matchedAs==="from"?this.matchesAssetPattern(e,t.rule.toPattern):t.rule.direction!=="<->"?!1:this.matchesAssetPattern(e,t.rule.fromPattern)}matchesRuleSource(e,t){return t.matchedAs==="to"?this.matchesAssetPattern(e,t.rule.fromPattern):t.rule.direction!=="<->"?!1:this.matchesAssetPattern(e,t.rule.toPattern)}matchesRuleList(e,t,s){return s.some(r=>this.matchesRule(e,t,r))}matchesRule(e,t,s){const r=this.matchesAssetPattern(e,s.fromPattern)&&this.matchesAssetPattern(t,s.toPattern);if(s.direction==="<->"){const i=this.matchesAssetPattern(e,s.toPattern)&&this.matchesAssetPattern(t,s.fromPattern);return r||i}return r}matchesAssetPattern(e,t){const[s="",r=""]=t.split(":").map(c=>c.trim().toLowerCase()),i=s===""||s==="*"||s===e.chain.toLowerCase(),n=r===""||r==="*"||r===e.symbol.toLowerCase();return i&&n}async buildRouteMatrix(e){const t={};for(const s of e){const r=[];for(const i of e)await this.isValidRoute(s,i)&&r.push(i);t[s.toString()]=r}return t}};o.POLICY_CACHE_KEY="route_policy";let u=o;exports.RouteValidator=u;
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;