@gardenfi/orderbook 3.0.4 → 3.0.5-beta.1
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 +36 -35
- package/dist/index10.cjs +1 -1
- package/dist/index10.js +30 -42
- package/dist/index11.cjs +1 -1
- package/dist/index11.js +54 -56
- package/dist/index12.cjs +1 -1
- package/dist/index12.js +14 -24
- package/dist/index3.cjs +1 -1
- package/dist/index3.js +1 -1
- package/dist/index4.cjs +1 -1
- package/dist/index4.js +79 -105
- package/dist/src/lib/assetManager/AssetManager.d.ts +3 -9
- package/dist/src/lib/assetManager/utils.d.ts +0 -3
- package/dist/src/lib/constants/asset.d.ts +51 -104
- package/dist/src/lib/constants/asset.types.d.ts +3 -1
- package/dist/src/lib/orderbook/orderbook.types.d.ts +3 -6
- package/dist/src/lib/utils.d.ts +5 -1
- package/package.json +2 -2
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"),s=require("./index5.cjs"),
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const r=require("./index2.cjs"),n=require("./index3.cjs"),e=require("./index4.cjs"),s=require("./index5.cjs"),o=require("./index6.cjs"),t=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.isBitcoin=e.isBitcoin;exports.isEVM=e.isEVM;exports.isEvmNativeToken=e.isEvmNativeToken;exports.isMainnet=e.isMainnet;exports.isNativeToken=e.isNativeToken;exports.isSolana=e.isSolana;exports.isSolanaNativeToken=e.isSolanaNativeToken;exports.isStarknet=e.isStarknet;exports.isSui=e.isSui;exports.isSuiNativeToken=e.isSuiNativeToken;exports.isTron=e.isTron;exports.ArbitrumLocalnet=s.ArbitrumLocalnet;exports.ETHStarknetLocalnetAsset=s.ETHStarknetLocalnetAsset;exports.EthereumLocalnet=s.EthereumLocalnet;exports.SOLSolanaLocalnetAsset=s.SOLSolanaLocalnetAsset;exports.STRKStarknetLocalnetAsset=s.STRKStarknetLocalnetAsset;exports.StarknetLocalnet=s.StarknetLocalnet;exports.WBTCArbitrumLocalnetAsset=s.WBTCArbitrumLocalnetAsset;exports.WBTCEthereumLocalnetAsset=s.WBTCEthereumLocalnetAsset;exports.bitcoinRegtestAsset=s.bitcoinRegtestAsset;exports.buildAssetsWithChain=o.buildAssetsWithChain;exports.ConstructUrl=t.ConstructUrl;exports.discriminateOrderResponse=t.discriminateOrderResponse;exports.getOrderResponseType=t.getOrderResponseType;exports.isBitcoinOrderResponse=t.isBitcoinOrderResponse;exports.isEvmOrderResponse=t.isEvmOrderResponse;exports.isOrder=t.isOrder;exports.isSolanaOrderResponse=t.isSolanaOrderResponse;exports.isStarknetOrderResponse=t.isStarknetOrderResponse;exports.isSuiOrderResponse=t.isSuiOrderResponse;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;
|
package/dist/index.js
CHANGED
|
@@ -1,59 +1,60 @@
|
|
|
1
1
|
import { Orderbook as s } from "./index2.js";
|
|
2
2
|
import { BlockchainType as o } from "./index3.js";
|
|
3
|
-
import { Assets as n, Chains as a, Config as p, NATIVE_TOKENS as c, OrderLifecycle as l, OrderStatus as d, getBlockchainType as m, is as S, isBitcoin as A, isEVM as O, isEvmNativeToken as T, isMainnet as f, isNativeToken as k, isSolana as u, isSolanaNativeToken as x, isStarknet as L, isSui as R, isSuiNativeToken as h } from "./index4.js";
|
|
4
|
-
import { ArbitrumLocalnet as
|
|
5
|
-
import { buildAssetsWithChain as
|
|
6
|
-
import { ConstructUrl as
|
|
7
|
-
import { ChainAsset as
|
|
8
|
-
import { OrderAction as
|
|
9
|
-
import { AssetManager as
|
|
10
|
-
import { RouteValidator as
|
|
3
|
+
import { Assets as n, Chains as a, Config as p, NATIVE_TOKENS as c, OrderLifecycle as l, OrderStatus as d, getBlockchainType as m, is as S, isBitcoin as A, isEVM as O, isEvmNativeToken as T, isMainnet as f, isNativeToken as k, isSolana as u, isSolanaNativeToken as x, isStarknet as L, isSui as R, isSuiNativeToken as h, isTron as C } from "./index4.js";
|
|
4
|
+
import { ArbitrumLocalnet as v, ETHStarknetLocalnetAsset as B, EthereumLocalnet as N, SOLSolanaLocalnetAsset as b, STRKStarknetLocalnetAsset as g, StarknetLocalnet as y, WBTCArbitrumLocalnetAsset as M, WBTCEthereumLocalnetAsset as V, bitcoinRegtestAsset as W } from "./index5.js";
|
|
5
|
+
import { buildAssetsWithChain as P } from "./index6.js";
|
|
6
|
+
import { ConstructUrl as H, discriminateOrderResponse as I, getOrderResponseType as U, isBitcoinOrderResponse as _, isEvmOrderResponse as j, isOrder as q, isSolanaOrderResponse as w, isStarknetOrderResponse as z, isSuiOrderResponse as F } from "./index7.js";
|
|
7
|
+
import { ChainAsset as J } from "./index8.js";
|
|
8
|
+
import { OrderAction as X, ParseOrderStatus as Y, isCompleted as Z, isDeadlinePassed as $, parseAction as ee } from "./index9.js";
|
|
9
|
+
import { AssetManager as se } from "./index10.js";
|
|
10
|
+
import { RouteValidator as oe } from "./index11.js";
|
|
11
11
|
export {
|
|
12
|
-
|
|
13
|
-
|
|
12
|
+
v as ArbitrumLocalnet,
|
|
13
|
+
se as AssetManager,
|
|
14
14
|
n as Assets,
|
|
15
15
|
o as BlockchainType,
|
|
16
|
-
|
|
16
|
+
J as ChainAsset,
|
|
17
17
|
a as Chains,
|
|
18
18
|
p as Config,
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
19
|
+
H as ConstructUrl,
|
|
20
|
+
B as ETHStarknetLocalnetAsset,
|
|
21
|
+
N as EthereumLocalnet,
|
|
22
22
|
c as NATIVE_TOKENS,
|
|
23
|
-
|
|
23
|
+
X as OrderAction,
|
|
24
24
|
l as OrderLifecycle,
|
|
25
25
|
d as OrderStatus,
|
|
26
26
|
s as Orderbook,
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
27
|
+
Y as ParseOrderStatus,
|
|
28
|
+
oe as RouteValidator,
|
|
29
|
+
b as SOLSolanaLocalnetAsset,
|
|
30
|
+
g as STRKStarknetLocalnetAsset,
|
|
31
|
+
y as StarknetLocalnet,
|
|
32
|
+
M as WBTCArbitrumLocalnetAsset,
|
|
33
|
+
V as WBTCEthereumLocalnetAsset,
|
|
34
|
+
W as bitcoinRegtestAsset,
|
|
35
|
+
P as buildAssetsWithChain,
|
|
36
|
+
I as discriminateOrderResponse,
|
|
37
37
|
m as getBlockchainType,
|
|
38
|
-
|
|
38
|
+
U as getOrderResponseType,
|
|
39
39
|
S as is,
|
|
40
40
|
A as isBitcoin,
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
41
|
+
_ as isBitcoinOrderResponse,
|
|
42
|
+
Z as isCompleted,
|
|
43
|
+
$ as isDeadlinePassed,
|
|
44
44
|
O as isEVM,
|
|
45
45
|
T as isEvmNativeToken,
|
|
46
|
-
|
|
46
|
+
j as isEvmOrderResponse,
|
|
47
47
|
f as isMainnet,
|
|
48
48
|
k as isNativeToken,
|
|
49
|
-
|
|
49
|
+
q as isOrder,
|
|
50
50
|
u as isSolana,
|
|
51
51
|
x as isSolanaNativeToken,
|
|
52
|
-
|
|
52
|
+
w as isSolanaOrderResponse,
|
|
53
53
|
L as isStarknet,
|
|
54
|
-
|
|
54
|
+
z as isStarknetOrderResponse,
|
|
55
55
|
R as isSui,
|
|
56
56
|
h as isSuiNativeToken,
|
|
57
|
-
|
|
58
|
-
|
|
57
|
+
F as isSuiOrderResponse,
|
|
58
|
+
C as isTron,
|
|
59
|
+
ee as parseAction
|
|
59
60
|
};
|
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 r=require("@gardenfi/utils"),A=require("./index11.cjs"),g=require("./index4.cjs"),n=require("./index8.cjs"),C=require("./index12.cjs"),u=Object.keys(g.Config);class m{constructor(t,s){this.assets=null,this.chains=null,this.routeMatrix=null,this.isLoading=!1,this.error=null,this.url=new r.Url(t),typeof s=="string"?this.auth=new r.ApiKey(s):this.auth=s,this.routeValidator=new A.RouteValidator(this.url.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 r.Err("Failed to get auth headers");const s=this.url.endpoint("/v2/chains"),e=await r.Fetcher.get(s,{headers:{...t.val}});if(!e.result)return r.Err(`Failed to fetch chains data: ${e.error}`);const{assets:a,chains:i}=this.processApiData(e.result);return this.assets=a,this.chains=i,await this.buildRouteMatrix(),r.Ok("AssetManager initialized successfully")}catch(t){return r.Err(`Failed to fetch assets data: ${t}`)}finally{this.isLoading=!1}}async isRouteValid(t,s){return await this.routeValidator.isValidRoute(n.ChainAsset.from(t.id),n.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=>n.ChainAsset.from(i).toString()===a)}).filter(Boolean):[]}getAsset(t){if(!this.assets)return;const s=n.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 n.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||!u.includes(i))continue;const h={...a,name:this.formatChainName(a.chain),chain:i};for(const o of a.assets){const l=n.ChainAsset.from(o.id).toString(),{name:c,symbol:d}=C.parseAssetNameSymbol(o.name,o.id),f={...o,id:n.ChainAsset.from(o.id),chain:i,name:c,symbol:d};s[l]=f,e[i]||(e[i]=h)}}return{assets:s,chains:e}}parseChainIdentifier(t){return u.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=m;
|
package/dist/index10.js
CHANGED
|
@@ -1,21 +1,12 @@
|
|
|
1
|
-
import { ApiKey as A, Err as o, Fetcher as
|
|
1
|
+
import { Url as m, ApiKey as A, Err as o, Fetcher as g, Ok as p } from "@gardenfi/utils";
|
|
2
2
|
import { RouteValidator as C } from "./index11.js";
|
|
3
|
-
import { Config as
|
|
3
|
+
import { Config as y } from "./index4.js";
|
|
4
4
|
import { ChainAsset as r } from "./index8.js";
|
|
5
|
-
import {
|
|
6
|
-
const
|
|
7
|
-
class
|
|
5
|
+
import { parseAssetNameSymbol as w } from "./index12.js";
|
|
6
|
+
const u = Object.keys(y);
|
|
7
|
+
class R {
|
|
8
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 i = l(t.baseUrl), e = u(i, "/v2/chains"), a = u(i, "/v2/policy");
|
|
11
|
-
this.urls = {
|
|
12
|
-
base: i,
|
|
13
|
-
assets: l(t.assetsUrl ?? e),
|
|
14
|
-
policy: l(t.policyUrl ?? a)
|
|
15
|
-
}, this.routeValidator = new C(
|
|
16
|
-
this.urls.policy.toString(),
|
|
17
|
-
this.auth
|
|
18
|
-
);
|
|
9
|
+
this.assets = null, this.chains = null, this.routeMatrix = null, this.isLoading = !1, this.error = null, this.url = new m(t), typeof s == "string" ? this.auth = new A(s) : this.auth = s, this.routeValidator = new C(this.url.toString(), this.auth);
|
|
19
10
|
}
|
|
20
11
|
// ============================================
|
|
21
12
|
// Initialization Methods
|
|
@@ -34,17 +25,14 @@ class I {
|
|
|
34
25
|
this.isLoading = !0, this.error = null;
|
|
35
26
|
const t = await this.auth.getAuthHeaders();
|
|
36
27
|
if (!t.ok) return o("Failed to get auth headers");
|
|
37
|
-
const s = await
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
headers: {
|
|
41
|
-
...t.val
|
|
42
|
-
}
|
|
28
|
+
const s = this.url.endpoint("/v2/chains"), i = await g.get(s, {
|
|
29
|
+
headers: {
|
|
30
|
+
...t.val
|
|
43
31
|
}
|
|
44
|
-
);
|
|
45
|
-
if (!
|
|
46
|
-
const { assets:
|
|
47
|
-
return this.assets =
|
|
32
|
+
});
|
|
33
|
+
if (!i.result) return o(`Failed to fetch chains data: ${i.error}`);
|
|
34
|
+
const { assets: a, chains: e } = this.processApiData(i.result);
|
|
35
|
+
return this.assets = a, this.chains = e, await this.buildRouteMatrix(), p("AssetManager initialized successfully");
|
|
48
36
|
} catch (t) {
|
|
49
37
|
return o(`Failed to fetch assets data: ${t}`);
|
|
50
38
|
} finally {
|
|
@@ -73,8 +61,8 @@ class I {
|
|
|
73
61
|
), [];
|
|
74
62
|
const s = this.routeMatrix[t.id.toString().toLowerCase()];
|
|
75
63
|
return s ? s.map((i) => {
|
|
76
|
-
const
|
|
77
|
-
return Object.values(this.assets).find((
|
|
64
|
+
const a = i.toString();
|
|
65
|
+
return Object.values(this.assets).find((e) => r.from(e).toString() === a);
|
|
78
66
|
}).filter(Boolean) : [];
|
|
79
67
|
}
|
|
80
68
|
// ============================================
|
|
@@ -136,27 +124,27 @@ class I {
|
|
|
136
124
|
*/
|
|
137
125
|
processApiData(t) {
|
|
138
126
|
const s = {}, i = {};
|
|
139
|
-
for (const
|
|
140
|
-
const
|
|
141
|
-
if (!
|
|
127
|
+
for (const a of t) {
|
|
128
|
+
const e = this.parseChainIdentifier(a.chain);
|
|
129
|
+
if (!e || !u.includes(e))
|
|
142
130
|
continue;
|
|
143
131
|
const h = {
|
|
144
|
-
...
|
|
145
|
-
name: this.formatChainName(
|
|
146
|
-
chain:
|
|
132
|
+
...a,
|
|
133
|
+
name: this.formatChainName(a.chain),
|
|
134
|
+
chain: e
|
|
147
135
|
};
|
|
148
|
-
for (const n of
|
|
149
|
-
const
|
|
136
|
+
for (const n of a.assets) {
|
|
137
|
+
const l = r.from(n.id).toString(), { name: c, symbol: d } = w(
|
|
150
138
|
n.name,
|
|
151
139
|
n.id
|
|
152
|
-
),
|
|
140
|
+
), f = {
|
|
153
141
|
...n,
|
|
154
142
|
id: r.from(n.id),
|
|
155
|
-
chain:
|
|
156
|
-
name:
|
|
157
|
-
symbol:
|
|
143
|
+
chain: e,
|
|
144
|
+
name: c,
|
|
145
|
+
symbol: d
|
|
158
146
|
};
|
|
159
|
-
s[
|
|
147
|
+
s[l] = f, i[e] || (i[e] = h);
|
|
160
148
|
}
|
|
161
149
|
}
|
|
162
150
|
return { assets: s, chains: i };
|
|
@@ -165,7 +153,7 @@ class I {
|
|
|
165
153
|
* Parse chain identifier from string
|
|
166
154
|
*/
|
|
167
155
|
parseChainIdentifier(t) {
|
|
168
|
-
return
|
|
156
|
+
return u.includes(t) ? t : null;
|
|
169
157
|
}
|
|
170
158
|
/**
|
|
171
159
|
* Format chain name for display
|
|
@@ -184,5 +172,5 @@ class I {
|
|
|
184
172
|
}
|
|
185
173
|
}
|
|
186
174
|
export {
|
|
187
|
-
|
|
175
|
+
R as AssetManager
|
|
188
176
|
};
|
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 o=require("@gardenfi/utils"),h=require("node-cache"),a=class a{constructor(e,t){this.policy=new h({stdTTL:36e3,checkperiod:3600}),typeof t=="string"?this.auth=new o.ApiKey(t):this.auth=t,typeof e=="string"?this.apiBaseUrl=new o.Url(e):this.apiBaseUrl=e}getPolicy(){return this.policy.get(a.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(a.POLICY_CACHE_KEY,s),s}async loadPolicy(){const e=this.getPolicy();if(e)return o.Ok(e);try{const t=await this.auth.getAuthHeaders();if(!t.ok)return o.Err(`Failed to get auth headers: ${t.error}`);const s=await o.Fetcher.get(this.apiBaseUrl.endpoint("v2/policy"),{headers:{"Content-Type":"application/json",...t.val}});return s.result?o.Ok(this.setPolicy(s.result)):o.Err(`API Error: ${s.error}`)}catch(t){return o.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:i,sortedBlacklistRules:r,sortedWhitelistRules:n}=this.preprocessRules(s.val),l=this.findMatchingRule(e,i,"from");if(l&&!this.matchesRuleDestination(t,l))return!1;const c=this.findMatchingRule(t,i,"to");return c&&c.direction==="<->"&&!this.matchesRuleSource(e,c)?!1:this.matchesRuleList(e,t,r)?!!this.matchesRuleList(e,t,n):s.val.default==="open"}async getValidDestinations(e,t){const s=[];for(const i of t)await this.isValidRoute(e,i)&&s.push(i);return s}async getAllValidRoutes(e){const t=[];for(const s of e)for(const i of e)await this.isValidRoute(s,i)&&t.push({from:s,to:i});return t}preprocessRules(e){const t=e.isolation_groups.map(r=>this.parseRule(r)).sort((r,n)=>n.specificity-r.specificity),s=e.blacklist_pairs.map(r=>this.parseRule(r)).sort((r,n)=>n.specificity-r.specificity),i=e.whitelist_overrides.map(r=>this.parseRule(r)).sort((r,n)=>n.specificity-r.specificity);return{sortedIsolationRules:t,sortedBlacklistRules:s,sortedWhitelistRules:i}}parseRule(e){const s=e.includes("<->")?"<->":"->",[i,r]=e.split(s).map(n=>n.trim());return{pattern:e,fromPattern:i,toPattern:r,direction:s,specificity:this.calculateSpecificity(i,r)}}calculateSpecificity(e,t){const s=i=>{const r=i.toLowerCase();return r==="*"?0:r.includes("*")?1:2};return s(e)*10+s(t)}findMatchingRule(e,t,s){for(const i of t){const r=s==="from"?i.fromPattern:i.toPattern;if(this.matchesAssetPattern(e,r))return i;if(i.direction==="<->"){const n=s==="from"?i.toPattern:i.fromPattern;if(this.matchesAssetPattern(e,n))return i}}return null}matchesRuleDestination(e,t){return!!(this.matchesAssetPattern(e,t.toPattern)||t.direction==="<->"&&this.matchesAssetPattern(e,t.fromPattern))}matchesRuleSource(e,t){return!!(this.matchesAssetPattern(e,t.fromPattern)||t.direction==="<->"&&this.matchesAssetPattern(e,t.toPattern))}matchesRuleList(e,t,s){return s.some(i=>this.matchesRule(e,t,i))}matchesRule(e,t,s){const i=this.matchesAssetPattern(e,s.fromPattern)&&this.matchesAssetPattern(t,s.toPattern);if(s.direction==="<->"){const r=this.matchesAssetPattern(e,s.toPattern)&&this.matchesAssetPattern(t,s.fromPattern);return i||r}return i}matchesAssetPattern(e,t){const[s,i]=t.split(":"),r=s==="*"||s===e.chain,n=i.toLowerCase()==="*"||i.toLowerCase()===e.symbol.toLowerCase();return r&&n}async buildRouteMatrix(e){const t={};for(const s of e){const i=[];for(const r of e)await this.isValidRoute(s,r)&&i.push(r);t[s.toString()]=i}return t}};a.POLICY_CACHE_KEY="route_policy";let u=a;exports.RouteValidator=u;
|
package/dist/index11.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { ApiKey as f, Url as p, Ok as u, Err as
|
|
1
|
+
import { ApiKey as f, Url as p, Ok as u, Err as c, Fetcher as d } from "@gardenfi/utils";
|
|
2
2
|
import m from "node-cache";
|
|
3
|
-
const
|
|
3
|
+
const o = class o {
|
|
4
4
|
constructor(e, t) {
|
|
5
5
|
this.policy = new m({
|
|
6
6
|
stdTTL: 36e3,
|
|
@@ -10,7 +10,7 @@ const a = class a {
|
|
|
10
10
|
}), typeof t == "string" ? this.auth = new f(t) : this.auth = t, typeof e == "string" ? this.apiBaseUrl = new p(e) : this.apiBaseUrl = e;
|
|
11
11
|
}
|
|
12
12
|
getPolicy() {
|
|
13
|
-
return this.policy.get(
|
|
13
|
+
return this.policy.get(o.POLICY_CACHE_KEY);
|
|
14
14
|
}
|
|
15
15
|
setPolicy(e) {
|
|
16
16
|
const t = this.getPolicy(), s = {
|
|
@@ -34,7 +34,7 @@ const a = class a {
|
|
|
34
34
|
])
|
|
35
35
|
]
|
|
36
36
|
};
|
|
37
|
-
return this.policy.set(
|
|
37
|
+
return this.policy.set(o.POLICY_CACHE_KEY, s), s;
|
|
38
38
|
}
|
|
39
39
|
/**
|
|
40
40
|
* Loads the current route policy, cached or via API.
|
|
@@ -45,9 +45,9 @@ const a = class a {
|
|
|
45
45
|
try {
|
|
46
46
|
const t = await this.auth.getAuthHeaders();
|
|
47
47
|
if (!t.ok)
|
|
48
|
-
return
|
|
48
|
+
return c(`Failed to get auth headers: ${t.error}`);
|
|
49
49
|
const s = await d.get(
|
|
50
|
-
this.apiBaseUrl,
|
|
50
|
+
this.apiBaseUrl.endpoint("v2/policy"),
|
|
51
51
|
{
|
|
52
52
|
headers: {
|
|
53
53
|
"Content-Type": "application/json",
|
|
@@ -55,9 +55,9 @@ const a = class a {
|
|
|
55
55
|
}
|
|
56
56
|
}
|
|
57
57
|
);
|
|
58
|
-
return s.result ? u(this.setPolicy(s.result)) :
|
|
58
|
+
return s.result ? u(this.setPolicy(s.result)) : c(`API Error: ${s.error}`);
|
|
59
59
|
} catch (t) {
|
|
60
|
-
return
|
|
60
|
+
return c(`Failed to load policy: ${t}`);
|
|
61
61
|
}
|
|
62
62
|
}
|
|
63
63
|
/**
|
|
@@ -66,25 +66,29 @@ const a = class a {
|
|
|
66
66
|
async isValidRoute(e, t) {
|
|
67
67
|
const s = await this.loadPolicy();
|
|
68
68
|
if (!s.ok || e.toString() === t.toString()) return !1;
|
|
69
|
-
const { sortedIsolationRules:
|
|
69
|
+
const { sortedIsolationRules: i, sortedBlacklistRules: r, sortedWhitelistRules: n } = this.preprocessRules(s.val), l = this.findMatchingRule(
|
|
70
70
|
e,
|
|
71
|
-
|
|
71
|
+
i,
|
|
72
|
+
"from"
|
|
73
|
+
/* From */
|
|
72
74
|
);
|
|
73
|
-
if (
|
|
75
|
+
if (l && !this.matchesRuleDestination(t, l))
|
|
74
76
|
return !1;
|
|
75
|
-
const
|
|
77
|
+
const a = this.findMatchingRule(
|
|
76
78
|
t,
|
|
77
|
-
|
|
79
|
+
i,
|
|
80
|
+
"to"
|
|
81
|
+
/* To */
|
|
78
82
|
);
|
|
79
|
-
return
|
|
83
|
+
return a && a.direction === "<->" && !this.matchesRuleSource(e, a) ? !1 : this.matchesRuleList(e, t, r) ? !!this.matchesRuleList(e, t, n) : s.val.default === "open";
|
|
80
84
|
}
|
|
81
85
|
/**
|
|
82
86
|
* Returns all valid destinations for a given source asset.
|
|
83
87
|
*/
|
|
84
88
|
async getValidDestinations(e, t) {
|
|
85
89
|
const s = [];
|
|
86
|
-
for (const
|
|
87
|
-
await this.isValidRoute(e,
|
|
90
|
+
for (const i of t)
|
|
91
|
+
await this.isValidRoute(e, i) && s.push(i);
|
|
88
92
|
return s;
|
|
89
93
|
}
|
|
90
94
|
/**
|
|
@@ -93,16 +97,16 @@ const a = class a {
|
|
|
93
97
|
async getAllValidRoutes(e) {
|
|
94
98
|
const t = [];
|
|
95
99
|
for (const s of e)
|
|
96
|
-
for (const
|
|
97
|
-
await this.isValidRoute(s,
|
|
100
|
+
for (const i of e)
|
|
101
|
+
await this.isValidRoute(s, i) && t.push({ from: s, to: i });
|
|
98
102
|
return t;
|
|
99
103
|
}
|
|
100
104
|
/**
|
|
101
105
|
* Turns policy string rules into sorted ParsedRule objects.
|
|
102
106
|
*/
|
|
103
107
|
preprocessRules(e) {
|
|
104
|
-
const t = e.isolation_groups.map((
|
|
105
|
-
return { sortedIsolationRules: t, sortedBlacklistRules: s, sortedWhitelistRules:
|
|
108
|
+
const t = e.isolation_groups.map((r) => this.parseRule(r)).sort((r, n) => n.specificity - r.specificity), s = e.blacklist_pairs.map((r) => this.parseRule(r)).sort((r, n) => n.specificity - r.specificity), i = e.whitelist_overrides.map((r) => this.parseRule(r)).sort((r, n) => n.specificity - r.specificity);
|
|
109
|
+
return { sortedIsolationRules: t, sortedBlacklistRules: s, sortedWhitelistRules: i };
|
|
106
110
|
}
|
|
107
111
|
/**
|
|
108
112
|
* Parses a rule string (e.g. "ethereum:usdc -> base:usdt") into parts.
|
|
@@ -111,22 +115,22 @@ const a = class a {
|
|
|
111
115
|
const s = e.includes(
|
|
112
116
|
"<->"
|
|
113
117
|
/* Bidirectional */
|
|
114
|
-
) ? "<->" : "->", [
|
|
118
|
+
) ? "<->" : "->", [i, r] = e.split(s).map((n) => n.trim());
|
|
115
119
|
return {
|
|
116
120
|
pattern: e,
|
|
117
|
-
fromPattern:
|
|
118
|
-
toPattern:
|
|
121
|
+
fromPattern: i,
|
|
122
|
+
toPattern: r,
|
|
119
123
|
direction: s,
|
|
120
|
-
specificity: this.calculateSpecificity(
|
|
124
|
+
specificity: this.calculateSpecificity(i, r)
|
|
121
125
|
};
|
|
122
126
|
}
|
|
123
127
|
/**
|
|
124
128
|
* Gives a specificity score to a rule pattern for prioritizing.
|
|
125
129
|
*/
|
|
126
130
|
calculateSpecificity(e, t) {
|
|
127
|
-
const s = (
|
|
128
|
-
const
|
|
129
|
-
return
|
|
131
|
+
const s = (i) => {
|
|
132
|
+
const r = i.toLowerCase();
|
|
133
|
+
return r === "*" ? 0 : r.includes(
|
|
130
134
|
"*"
|
|
131
135
|
/* Any */
|
|
132
136
|
) ? 1 : 2;
|
|
@@ -136,20 +140,14 @@ const a = class a {
|
|
|
136
140
|
/**
|
|
137
141
|
* Finds the first matching rule for an asset given the rule "side".
|
|
138
142
|
*/
|
|
139
|
-
findMatchingRule(e, t) {
|
|
140
|
-
for (const
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
if (s.direction === "<->" && this.matchesAssetPattern(e, s.toPattern))
|
|
148
|
-
return {
|
|
149
|
-
rule: s,
|
|
150
|
-
matchedAs: "to"
|
|
151
|
-
/* To */
|
|
152
|
-
};
|
|
143
|
+
findMatchingRule(e, t, s) {
|
|
144
|
+
for (const i of t) {
|
|
145
|
+
const r = s === "from" ? i.fromPattern : i.toPattern;
|
|
146
|
+
if (this.matchesAssetPattern(e, r)) return i;
|
|
147
|
+
if (i.direction === "<->") {
|
|
148
|
+
const n = s === "from" ? i.toPattern : i.fromPattern;
|
|
149
|
+
if (this.matchesAssetPattern(e, n)) return i;
|
|
150
|
+
}
|
|
153
151
|
}
|
|
154
152
|
return null;
|
|
155
153
|
}
|
|
@@ -157,51 +155,51 @@ const a = class a {
|
|
|
157
155
|
* Checks if a given toAsset matches the rule's destination side.
|
|
158
156
|
*/
|
|
159
157
|
matchesRuleDestination(e, t) {
|
|
160
|
-
return
|
|
158
|
+
return !!(this.matchesAssetPattern(e, t.toPattern) || t.direction === "<->" && this.matchesAssetPattern(e, t.fromPattern));
|
|
161
159
|
}
|
|
162
160
|
/**
|
|
163
161
|
* Checks if a given fromAsset matches the rule's source side.
|
|
164
162
|
*/
|
|
165
163
|
matchesRuleSource(e, t) {
|
|
166
|
-
return
|
|
164
|
+
return !!(this.matchesAssetPattern(e, t.fromPattern) || t.direction === "<->" && this.matchesAssetPattern(e, t.toPattern));
|
|
167
165
|
}
|
|
168
166
|
/**
|
|
169
167
|
* Checks if any rule in a list matches fromAsset and toAsset.
|
|
170
168
|
*/
|
|
171
169
|
matchesRuleList(e, t, s) {
|
|
172
|
-
return s.some((
|
|
170
|
+
return s.some((i) => this.matchesRule(e, t, i));
|
|
173
171
|
}
|
|
174
172
|
/**
|
|
175
173
|
* Checks if a rule matches fromAsset and toAsset (forward or backward if bidirectional).
|
|
176
174
|
*/
|
|
177
175
|
matchesRule(e, t, s) {
|
|
178
|
-
const
|
|
176
|
+
const i = this.matchesAssetPattern(e, s.fromPattern) && this.matchesAssetPattern(t, s.toPattern);
|
|
179
177
|
if (s.direction === "<->") {
|
|
180
|
-
const
|
|
181
|
-
return
|
|
178
|
+
const r = this.matchesAssetPattern(e, s.toPattern) && this.matchesAssetPattern(t, s.fromPattern);
|
|
179
|
+
return i || r;
|
|
182
180
|
}
|
|
183
|
-
return
|
|
181
|
+
return i;
|
|
184
182
|
}
|
|
185
183
|
/**
|
|
186
184
|
* Checks if an asset string matches a rule's pattern (wildcards supported).
|
|
187
185
|
*/
|
|
188
186
|
matchesAssetPattern(e, t) {
|
|
189
|
-
const [s
|
|
190
|
-
return
|
|
187
|
+
const [s, i] = t.split(":"), r = s === "*" || s === e.chain, n = i.toLowerCase() === "*" || i.toLowerCase() === e.symbol.toLowerCase();
|
|
188
|
+
return r && n;
|
|
191
189
|
}
|
|
192
190
|
async buildRouteMatrix(e) {
|
|
193
191
|
const t = {};
|
|
194
192
|
for (const s of e) {
|
|
195
|
-
const
|
|
196
|
-
for (const
|
|
197
|
-
await this.isValidRoute(s,
|
|
198
|
-
t[s.toString()] =
|
|
193
|
+
const i = [];
|
|
194
|
+
for (const r of e)
|
|
195
|
+
await this.isValidRoute(s, r) && i.push(r);
|
|
196
|
+
t[s.toString()] = i;
|
|
199
197
|
}
|
|
200
198
|
return t;
|
|
201
199
|
}
|
|
202
200
|
};
|
|
203
|
-
|
|
204
|
-
let h =
|
|
201
|
+
o.POLICY_CACHE_KEY = "route_policy";
|
|
202
|
+
let h = o;
|
|
205
203
|
export {
|
|
206
204
|
h as RouteValidator
|
|
207
205
|
};
|
package/dist/index12.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 u=require("./index8.cjs");function c(m,e,t){var i;const r=(m??"").trim();if(!r)return{name:"",symbol:""};const s=r.split(":");if(s.length>=2){const o=((i=s[0])==null?void 0:i.trim())||"",p=s.slice(1).join(":").trim()||(t==null?void 0:t.trim())||"";return{name:o,symbol:p}}let n="";if(e)try{n=(typeof e=="string"?u.ChainAsset.from(e):e).symbol.toUpperCase()}catch{}return{name:r,symbol:n||(t==null?void 0:t.trim())||""}}exports.parseAssetNameSymbol=c;
|
package/dist/index12.js
CHANGED
|
@@ -1,31 +1,21 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
return { name: m, symbol: u };
|
|
1
|
+
import { ChainAsset as h } from "./index8.js";
|
|
2
|
+
function A(m, r, t) {
|
|
3
|
+
var s;
|
|
4
|
+
const n = (m ?? "").trim();
|
|
5
|
+
if (!n) return { name: "", symbol: "" };
|
|
6
|
+
const e = n.split(":");
|
|
7
|
+
if (e.length >= 2) {
|
|
8
|
+
const o = ((s = e[0]) == null ? void 0 : s.trim()) || "", p = e.slice(1).join(":").trim() || (t == null ? void 0 : t.trim()) || "";
|
|
9
|
+
return { name: o, symbol: p };
|
|
11
10
|
}
|
|
12
|
-
let
|
|
13
|
-
if (
|
|
11
|
+
let i = "";
|
|
12
|
+
if (r)
|
|
14
13
|
try {
|
|
15
|
-
|
|
14
|
+
i = (typeof r == "string" ? h.from(r) : r).symbol.toUpperCase();
|
|
16
15
|
} catch {
|
|
17
16
|
}
|
|
18
|
-
return { name:
|
|
19
|
-
}
|
|
20
|
-
function d(r) {
|
|
21
|
-
return r instanceof p ? r : new p(r);
|
|
22
|
-
}
|
|
23
|
-
function f(r, t) {
|
|
24
|
-
const n = r.toString().replace(/\/+$/, ""), e = t.startsWith("/") ? t : `/${t}`;
|
|
25
|
-
return `${n}${e}`;
|
|
17
|
+
return { name: n, symbol: i || (t == null ? void 0 : t.trim()) || "" };
|
|
26
18
|
}
|
|
27
19
|
export {
|
|
28
|
-
|
|
29
|
-
d as ensureUrl,
|
|
30
|
-
$ as parseAssetNameSymbol
|
|
20
|
+
A as parseAssetNameSymbol
|
|
31
21
|
};
|
package/dist/index3.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});var
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});var r=(t=>(t.bitcoin="bitcoin",t.evm="evm",t.solana="solana",t.starknet="starknet",t.sui="sui",t.tron="tron",t))(r||{});exports.BlockchainType=r;
|
package/dist/index3.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
var r = /* @__PURE__ */ ((t) => (t.bitcoin = "bitcoin", t.evm = "evm", t.solana = "solana", t.starknet = "starknet", t.sui = "sui", t))(r || {});
|
|
1
|
+
var r = /* @__PURE__ */ ((t) => (t.bitcoin = "bitcoin", t.evm = "evm", t.solana = "solana", t.starknet = "starknet", t.sui = "sui", t.tron = "tron", t))(r || {});
|
|
2
2
|
export {
|
|
3
3
|
r as BlockchainType
|
|
4
4
|
};
|