@gardenfi/orderbook 2.1.8-beta.2 → 2.1.8
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 +22 -18
- package/dist/index2.cjs +1 -1
- package/dist/index2.js +72 -65
- package/dist/index3.cjs +1 -1
- package/dist/index3.js +45 -16
- package/dist/index4.cjs +1 -1
- package/dist/index4.js +82 -342
- package/dist/index5.cjs +1 -1
- package/dist/index5.js +346 -186
- package/dist/index6.cjs +1 -1
- package/dist/index6.js +187 -6
- package/dist/index7.cjs +1 -0
- package/dist/index7.js +9 -0
- package/dist/src/index.d.ts +3 -1
- package/dist/src/lib/asset.d.ts +2 -0
- package/dist/src/lib/orderbook/orderbook.d.ts +2 -2
- package/dist/src/lib/orderbook/orderbook.types.d.ts +8 -8
- package/dist/src/lib/orders/orders.types.d.ts +60 -0
- package/dist/src/lib/orders/ordersProvider.d.ts +15 -0
- package/package.json +1 -1
package/dist/index6.js
CHANGED
|
@@ -1,9 +1,190 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
1
|
+
var p = (e, t, a) => {
|
|
2
|
+
if (!t.has(e))
|
|
3
|
+
throw TypeError("Cannot " + a);
|
|
4
|
+
}, h = (e, t, a) => (p(e, t, "read from private field"), a ? a.call(e) : t.get(e)), u = (e, t, a) => {
|
|
5
|
+
if (t.has(e))
|
|
6
|
+
throw TypeError("Cannot add the same private member more than once");
|
|
7
|
+
t instanceof WeakSet ? t.add(e) : t.set(e, a);
|
|
8
|
+
}, w = (e, t, a, r) => (p(e, t, "write to private field"), t.set(e, a), a);
|
|
9
|
+
const d = (e) => new Promise((t) => setTimeout(t, e));
|
|
10
|
+
class g {
|
|
11
|
+
/**
|
|
12
|
+
* @param {number} maxRetries - The maximum number of retries, if less < 0 then it is set to 0
|
|
13
|
+
* @param {number} delay - The delay between retries
|
|
14
|
+
*/
|
|
15
|
+
constructor(t, a) {
|
|
16
|
+
this.maxRetries = Math.max(t, 0), this.delay = a;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Retries a function until it succeeds or the max number of retries is reached
|
|
20
|
+
*
|
|
21
|
+
* @param {() => Promise<T>} fn - The function to retry
|
|
22
|
+
* @return {Promise<T>} a Promise that resolves to the result of the function
|
|
23
|
+
*/
|
|
24
|
+
async retry(t) {
|
|
25
|
+
let a = 0, r;
|
|
26
|
+
for (; a < this.maxRetries + 1; )
|
|
27
|
+
try {
|
|
28
|
+
return await t();
|
|
29
|
+
} catch (i) {
|
|
30
|
+
a++, r = i, await d(this.delay * a);
|
|
31
|
+
}
|
|
32
|
+
throw r;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
const v = (e) => {
|
|
36
|
+
try {
|
|
37
|
+
return JSON.parse(e);
|
|
38
|
+
} catch {
|
|
39
|
+
return e;
|
|
40
|
+
}
|
|
41
|
+
};
|
|
42
|
+
class m {
|
|
43
|
+
static async _postWithFallback(t, a) {
|
|
44
|
+
let r = "";
|
|
45
|
+
for (const i of t)
|
|
46
|
+
try {
|
|
47
|
+
const s = await fetch(i, {
|
|
48
|
+
method: "POST",
|
|
49
|
+
...a
|
|
50
|
+
});
|
|
51
|
+
if (s.status >= 500) {
|
|
52
|
+
r = await (s.text() || s.json());
|
|
53
|
+
continue;
|
|
54
|
+
}
|
|
55
|
+
return await this.parse(s);
|
|
56
|
+
} catch (s) {
|
|
57
|
+
r = y(s);
|
|
58
|
+
continue;
|
|
59
|
+
}
|
|
60
|
+
throw new Error(r || "All APIs failed");
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Asynchronously sends a POST request to multiple URLs with fallback logic.
|
|
64
|
+
*
|
|
65
|
+
* @param {string[]} input - array of URLs to send the POST request to
|
|
66
|
+
* @param {RequestInit} [init] - optional request initialization options
|
|
67
|
+
* @return {Promise<T>} a Promise that resolves to the parsed response data
|
|
68
|
+
*/
|
|
69
|
+
static async postWithFallback(t, a) {
|
|
70
|
+
return await n(a).retry(
|
|
71
|
+
() => this._postWithFallback(t, a)
|
|
72
|
+
);
|
|
73
|
+
}
|
|
74
|
+
static async _getWithFallback(t, a) {
|
|
75
|
+
let r = "";
|
|
76
|
+
for (const i of t)
|
|
77
|
+
try {
|
|
78
|
+
const s = await fetch(i, a);
|
|
79
|
+
if (s.status >= 500) {
|
|
80
|
+
r = await (s.text() || s.json());
|
|
81
|
+
continue;
|
|
82
|
+
}
|
|
83
|
+
return await this.parse(s);
|
|
84
|
+
} catch (s) {
|
|
85
|
+
r = y(s);
|
|
86
|
+
continue;
|
|
87
|
+
}
|
|
88
|
+
throw new Error(r || "All APIs failed");
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* Asynchronously sends a POST request to multiple URLs with fallback mechanism.
|
|
92
|
+
*
|
|
93
|
+
* @param {string[]} input - An array of URLs to retrieve data from.
|
|
94
|
+
* @param {RequestInit} [init] - Optional request options.
|
|
95
|
+
* @return {Promise<T>} A promise that resolves to the retrieved data.
|
|
96
|
+
*/
|
|
97
|
+
static async getWithFallback(t, a) {
|
|
98
|
+
return await n(a).retry(
|
|
99
|
+
() => this._getWithFallback(t, a)
|
|
100
|
+
);
|
|
101
|
+
}
|
|
102
|
+
static async _get(t, a) {
|
|
103
|
+
return await this.parse(await fetch(t, a));
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* Asynchronously retrieves data of type T from the specified URL or RequestInfo, with optional initialization options.
|
|
107
|
+
*
|
|
108
|
+
* @param {RequestInfo | URL} input - The URL or RequestInfo to fetch data from
|
|
109
|
+
* @param {RequestInit} init - Optional initialization options for the fetch request
|
|
110
|
+
* @return {Promise<T>} The retrieved data of type T
|
|
111
|
+
*/
|
|
112
|
+
static async get(t, a) {
|
|
113
|
+
return await n(a).retry(() => this._get(t, a));
|
|
114
|
+
}
|
|
115
|
+
static async _post(t, a) {
|
|
116
|
+
return await this.parse(
|
|
117
|
+
await fetch(t, {
|
|
118
|
+
method: "POST",
|
|
119
|
+
...a
|
|
120
|
+
})
|
|
121
|
+
);
|
|
122
|
+
}
|
|
123
|
+
/**
|
|
124
|
+
* Asynchronously sends a POST request to the specified URL or RequestInfo, with optional initialization options.
|
|
125
|
+
*/
|
|
126
|
+
static async post(t, a) {
|
|
127
|
+
return await n(a).retry(() => this._post(t, a));
|
|
128
|
+
}
|
|
129
|
+
/**
|
|
130
|
+
* Parses the response and returns the result as the specified type.
|
|
131
|
+
*
|
|
132
|
+
* @param {Response} res - the response object to be parsed
|
|
133
|
+
* @return {Promise<T>} the parsed result of type T
|
|
134
|
+
*/
|
|
135
|
+
static async parse(t) {
|
|
136
|
+
const a = await t.text();
|
|
137
|
+
if (t.status >= 200 && t.status < 300)
|
|
138
|
+
return v(a);
|
|
139
|
+
throw new Error(a);
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
function y(e) {
|
|
143
|
+
return (e == null ? void 0 : e.message) || (e == null ? void 0 : e.toString()) || "unknown error";
|
|
144
|
+
}
|
|
145
|
+
const n = (e) => new g((e == null ? void 0 : e.retryCount) ?? 2, (e == null ? void 0 : e.retryDelay) ?? 1e3);
|
|
146
|
+
var o, c, l;
|
|
147
|
+
class f {
|
|
148
|
+
constructor(t, a, r = void 0) {
|
|
149
|
+
u(this, o, void 0), u(this, c, void 0), u(this, l, void 0), w(this, o, t), w(this, l, r), w(this, c, a);
|
|
150
|
+
}
|
|
151
|
+
get ok() {
|
|
152
|
+
return h(this, o);
|
|
153
|
+
}
|
|
154
|
+
get error() {
|
|
155
|
+
return h(this, l);
|
|
156
|
+
}
|
|
157
|
+
get val() {
|
|
158
|
+
return h(this, c);
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
o = /* @__PURE__ */ new WeakMap(), c = /* @__PURE__ */ new WeakMap(), l = /* @__PURE__ */ new WeakMap();
|
|
162
|
+
const k = (e) => new f(!0, e), W = (e, ...t) => {
|
|
163
|
+
if (typeof e == "string" && t && t.length > 0) {
|
|
164
|
+
let a = [e, ...t].map((r) => {
|
|
165
|
+
if (r) {
|
|
166
|
+
if (r instanceof Error)
|
|
167
|
+
return r.message;
|
|
168
|
+
if (typeof r == "string")
|
|
169
|
+
return r;
|
|
170
|
+
if (r != null && r.toString)
|
|
171
|
+
return r.toString();
|
|
172
|
+
}
|
|
173
|
+
});
|
|
174
|
+
return new f(
|
|
175
|
+
!1,
|
|
176
|
+
null,
|
|
177
|
+
a.filter((r) => r !== void 0).join(" ")
|
|
178
|
+
);
|
|
179
|
+
}
|
|
180
|
+
return new f(!1, null, e);
|
|
6
181
|
};
|
|
7
182
|
export {
|
|
8
|
-
|
|
183
|
+
W as Err,
|
|
184
|
+
m as Fetcher,
|
|
185
|
+
k as Ok,
|
|
186
|
+
f as Result,
|
|
187
|
+
g as Retry,
|
|
188
|
+
v as safeParseJson,
|
|
189
|
+
d as sleep
|
|
9
190
|
};
|
package/dist/index7.cjs
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const i=(n,o,t)=>{const r=n.endpoint(o);return t&&Object.entries(t).forEach(([c,e])=>{e!==void 0&&r.searchParams.append(c,e.toString())}),r};exports.ConstructUrl=i;
|
package/dist/index7.js
ADDED
package/dist/src/index.d.ts
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
export { Orderbook } from './lib/orderbook/orderbook';
|
|
2
2
|
export type * from './lib/orderbook/orderbook.types';
|
|
3
3
|
export type { Chain, EvmChain, Asset } from './lib/asset';
|
|
4
|
-
export { Chains, BlockchainType, isMainnet, isBitcoin, isEVM, isStarknet, getBlockchainType, NetworkType, NativeTokenAddress, isEvmNativeToken, } from './lib/asset';
|
|
4
|
+
export { Chains, BlockchainType, isMainnet, isBitcoin, isEVM, isStarknet, getBlockchainType, NetworkType, TimeLocks, getTimeLock, NativeTokenAddress, isEvmNativeToken, } from './lib/asset';
|
|
5
|
+
export { OrdersProvider } from './lib/orders/ordersProvider';
|
|
6
|
+
export type { IOrderProvider } from './lib/orders/orders.types';
|
|
5
7
|
export { WBTCArbitrumLocalnetAsset, WBTCEthereumLocalnetAsset, ArbitrumLocalnet, EthereumLocalnet, bitcoinRegtestAsset, SupportedAssets, StarknetLocalnet, STRKStarknetLocalnetAsset, } from './lib/constants';
|
package/dist/src/lib/asset.d.ts
CHANGED
|
@@ -61,6 +61,8 @@ export declare const isMainnet: (chain: Chain) => chain is "bitcoin" | "ethereum
|
|
|
61
61
|
export declare const isBitcoin: (chain: Chain) => chain is "bitcoin" | "bitcoin_testnet" | "bitcoin_regtest";
|
|
62
62
|
export declare const isEVM: (chain: Chain) => chain is "ethereum" | "base" | "arbitrum" | "ethereum_sepolia" | "arbitrum_localnet" | "arbitrum_sepolia" | "ethereum_localnet" | "base_sepolia" | "bera_testnet" | "citrea_testnet" | "bera" | "monad_testnet" | "hyperliquid_testnet" | "hyperliquid" | "unichain" | "corn" | "botanix";
|
|
63
63
|
export declare const isStarknet: (chain: Chain) => chain is "starknet" | "starknet_sepolia" | "starknet_devnet";
|
|
64
|
+
export declare const TimeLocks: Record<Chain, number>;
|
|
64
65
|
export declare const getBlockchainType: (chain: Chain) => BlockchainType;
|
|
66
|
+
export declare const getTimeLock: (chain: Chain) => number;
|
|
65
67
|
export declare const NativeTokenAddress = "0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE";
|
|
66
68
|
export declare const isEvmNativeToken: (chain: Chain, tokenAddress: string) => boolean;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { AsyncResult } from '@catalogfi/utils';
|
|
2
|
-
import { CreateOrder,
|
|
2
|
+
import { CreateOrder, CreateOrderRequestWithAdditionalData, IOrderbook, MatchedOrder, PaginatedData, PaginationConfig, Status } from './orderbook.types';
|
|
3
3
|
import { IAuth, Url } from '@gardenfi/utils';
|
|
4
4
|
|
|
5
5
|
/**
|
|
@@ -16,7 +16,7 @@ export declare class Orderbook implements IOrderbook {
|
|
|
16
16
|
* @param {IAuth} auth - The auth object.
|
|
17
17
|
* @returns {string} The create order ID.
|
|
18
18
|
*/
|
|
19
|
-
createOrder(order:
|
|
19
|
+
createOrder(order: CreateOrderRequestWithAdditionalData, auth: IAuth): AsyncResult<string, string>;
|
|
20
20
|
getOrder<T extends boolean>(id: string, matched: T): AsyncResult<T extends true ? MatchedOrder : CreateOrder, string>;
|
|
21
21
|
getMatchedOrders(address: string, status: Status, paginationConfig?: PaginationConfig): AsyncResult<PaginatedData<MatchedOrder>, string>;
|
|
22
22
|
getUnMatchedOrders(address: string, paginationConfig?: PaginationConfig): AsyncResult<PaginatedData<CreateOrder>, string>;
|
|
@@ -89,7 +89,7 @@ export interface IOrderbook {
|
|
|
89
89
|
* @param {CreateOrderConfig} orderConfig - The configuration for the creating the order.
|
|
90
90
|
* @returns {number} The create order ID.
|
|
91
91
|
*/
|
|
92
|
-
createOrder(order:
|
|
92
|
+
createOrder(order: CreateOrderRequestWithAdditionalData, auth: IAuth): AsyncResult<string, string>;
|
|
93
93
|
/**
|
|
94
94
|
* Get the order from orderbook based on provided Id and match status.
|
|
95
95
|
* @param id - The create Id of the order
|
|
@@ -192,18 +192,18 @@ export type CreateOrderRequest = {
|
|
|
192
192
|
destination_chain: string;
|
|
193
193
|
source_asset: string;
|
|
194
194
|
destination_asset: string;
|
|
195
|
+
initiator_source_address: string;
|
|
196
|
+
initiator_destination_address: string;
|
|
195
197
|
source_amount: string;
|
|
196
198
|
destination_amount: string;
|
|
199
|
+
fee: string;
|
|
197
200
|
nonce: string;
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
secret_hash
|
|
201
|
+
min_destination_confirmations: number;
|
|
202
|
+
timelock: number;
|
|
203
|
+
secret_hash: string;
|
|
201
204
|
};
|
|
202
205
|
export type CreateOrderRequestWithAdditionalData = CreateOrderRequest & AdditionalData & AffiliateFeeList<AffiliateFeeWithAmount>;
|
|
203
206
|
export type CreateOrder = CreateOrderRequestWithAdditionalData & {
|
|
204
|
-
fee: string;
|
|
205
|
-
min_destination_confirmations: number;
|
|
206
|
-
timelock: number;
|
|
207
207
|
created_at: string;
|
|
208
208
|
updated_at: string;
|
|
209
209
|
deleted_at: string | null;
|
|
@@ -247,7 +247,7 @@ export type PaginatedData<T> = {
|
|
|
247
247
|
total_items: number;
|
|
248
248
|
per_page: number;
|
|
249
249
|
};
|
|
250
|
-
export type CreateOrderResponse = APIResponse<
|
|
250
|
+
export type CreateOrderResponse = APIResponse<string>;
|
|
251
251
|
export type PaginationConfig = {
|
|
252
252
|
page?: number;
|
|
253
253
|
per_page?: number;
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { AsyncResult } from '@catalogfi/utils';
|
|
2
|
+
import { CreateOrder, MatchedOrder, PaginatedData, PaginationConfig } from '../orderbook/orderbook.types';
|
|
3
|
+
|
|
4
|
+
export interface IOrderProvider {
|
|
5
|
+
/**
|
|
6
|
+
* Get the order from orderbook based on provided Id and match status.
|
|
7
|
+
* @param id - The create Id of the order
|
|
8
|
+
* @template T - If true, returns matched order, else returns create order (unmatched Order).
|
|
9
|
+
* @returns {AsyncResult<T extends true ? MatchedOrder : CreateOrder, string>} A promise that resolves to the order.
|
|
10
|
+
*/
|
|
11
|
+
getOrder<T extends boolean>(id: string, matched: T): AsyncResult<T extends true ? MatchedOrder : CreateOrder, string>;
|
|
12
|
+
/**
|
|
13
|
+
* Get all matched orders from the orderbook associated with the `address`.
|
|
14
|
+
* @param address The address to get the orders for.
|
|
15
|
+
* @param pending If true, returns pending orders, else returns all matched orders.
|
|
16
|
+
* @param paginationConfig - The configuration for the pagination.
|
|
17
|
+
* @returns {AsyncResult<PaginatedData<MatchedOrder>, string>} A promise that resolves to the orders.
|
|
18
|
+
*/
|
|
19
|
+
getMatchedOrders(address: string, pending: boolean, paginationConfig?: PaginationConfig): AsyncResult<PaginatedData<MatchedOrder>, string>;
|
|
20
|
+
/**
|
|
21
|
+
* Get all unmatched orders from the orderbook associated with the `address`.
|
|
22
|
+
* @param address The address to get the orders for.
|
|
23
|
+
* @param paginationConfig - The configuration for the pagination.
|
|
24
|
+
* @returns {AsyncResult<PaginatedData<CreateOrder>, string>} A promise that resolves to the orders.
|
|
25
|
+
*/
|
|
26
|
+
getUnMatchedOrders(address: string, paginationConfig?: PaginationConfig): AsyncResult<PaginatedData<CreateOrder>, string>;
|
|
27
|
+
/**
|
|
28
|
+
* Get all orders from the orderbook based on the match status.
|
|
29
|
+
* @param matched - If true, returns matched orders, else returns unmatched orders.
|
|
30
|
+
* @param paginationConfig - The configuration for the pagination.
|
|
31
|
+
* @returns {AsyncResult<PaginatedData<T extends true ? MatchedOrder : CreateOrder>, string>} A promise that resolves to the orders.
|
|
32
|
+
*/
|
|
33
|
+
getOrders<T extends boolean>(matched: T, paginationConfig?: PaginationConfig): AsyncResult<PaginatedData<T extends true ? MatchedOrder : CreateOrder>, string>;
|
|
34
|
+
/**
|
|
35
|
+
* Polls for every provided interval and returns matched and unmatched orders associated on the account.
|
|
36
|
+
* @param account The account to subscribe to
|
|
37
|
+
* @param matched If true, returns matched orders, else returns unmatched orders
|
|
38
|
+
* @param cb Th ack to be called when the orders are updated
|
|
39
|
+
* @param interval The interval to poll for updates
|
|
40
|
+
*
|
|
41
|
+
* Example usage:
|
|
42
|
+
*
|
|
43
|
+
* ```js
|
|
44
|
+
* const unsubscribe =await orderbook.subscribeOrders(account, matched, interval, handleOrders, paginationConfig);
|
|
45
|
+
*
|
|
46
|
+
* // Unsubscribe after 20 seconds
|
|
47
|
+
* setTimeout(() => {
|
|
48
|
+
* unsubscribe();
|
|
49
|
+
* console.log('Unsubscribed from orders');
|
|
50
|
+
* }, 20000);
|
|
51
|
+
* ```
|
|
52
|
+
*/
|
|
53
|
+
subscribeOrders<T extends boolean>(account: string, matched: T, interval: number, cb: (orders: PaginatedData<T extends true ? MatchedOrder : CreateOrder>) => Promise<void>, pending?: boolean, paginationConfig?: PaginationConfig): Promise<() => void>;
|
|
54
|
+
/**
|
|
55
|
+
* Returns the current orders count associated with the provided address. Used to calculate nonce for secret generation.
|
|
56
|
+
* @param address The address to get the orders count for.
|
|
57
|
+
* @returns {AsyncResult<number, string>} A promise that resolves to the orders count.
|
|
58
|
+
*/
|
|
59
|
+
getOrdersCount(address: string): AsyncResult<number, string>;
|
|
60
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { AsyncResult } from '@catalogfi/utils';
|
|
2
|
+
import { IOrderProvider } from './orders.types';
|
|
3
|
+
import { CreateOrder, MatchedOrder, PaginatedData, PaginationConfig } from '../orderbook/orderbook.types';
|
|
4
|
+
import { Url } from '@gardenfi/utils';
|
|
5
|
+
|
|
6
|
+
export declare class OrdersProvider implements IOrderProvider {
|
|
7
|
+
private url;
|
|
8
|
+
constructor(url: string | Url);
|
|
9
|
+
getOrder<T extends boolean>(id: string, matched: T): AsyncResult<T extends true ? MatchedOrder : CreateOrder, string>;
|
|
10
|
+
getMatchedOrders(address: string, pending: boolean, paginationConfig?: PaginationConfig): AsyncResult<PaginatedData<MatchedOrder>, string>;
|
|
11
|
+
getUnMatchedOrders(address: string, paginationConfig?: PaginationConfig): AsyncResult<PaginatedData<CreateOrder>, string>;
|
|
12
|
+
getOrders<T extends boolean>(matched: T, paginationConfig?: PaginationConfig): AsyncResult<PaginatedData<T extends true ? MatchedOrder : CreateOrder>, string>;
|
|
13
|
+
subscribeOrders<T extends boolean>(account: string, matched: T, interval: number, cb: (orders: PaginatedData<T extends true ? MatchedOrder : CreateOrder>) => Promise<void>, pending?: boolean, paginationConfig?: PaginationConfig): Promise<() => void>;
|
|
14
|
+
getOrdersCount(address: string): AsyncResult<number, string>;
|
|
15
|
+
}
|