@gardenfi/core 0.2.0-beta.4 → 0.2.0-beta.41

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.
@@ -0,0 +1,29 @@
1
+ import { MarkRequired } from '@catalogfi/utils';
2
+ import { OnChainIdentifier } from '@catalogfi/wallets';
3
+ import { Address } from 'viem';
4
+
5
+ export type AtomicSwapConfig = {
6
+ secretHash: string;
7
+ /**
8
+ * The number of blocks before the swap expires
9
+ */
10
+ expiryBlocks: number;
11
+ /**
12
+ * Amount in it's lowest denomination
13
+ */
14
+ amount: bigint;
15
+ recipientAddress: OnChainIdentifier;
16
+ refundAddress: OnChainIdentifier;
17
+ initiatorAddress: OnChainIdentifier;
18
+ contractAddress?: Address;
19
+ chain?: number;
20
+ };
21
+ export type EVMSwapConfig = MarkRequired<AtomicSwapConfig, 'chain' | 'contractAddress'>;
22
+ export declare const EVMHTLCErrors: {
23
+ INVALID_SECRET_HASH: string;
24
+ INSUFFICIENT_TOKENS: string;
25
+ ORDER_INITIATED: string;
26
+ INVALID_SECRET: string;
27
+ ORDER_NOT_EXPIRED: string;
28
+ INSUFFICIENT_FUNDS: (balance: string, required: string) => string;
29
+ };
@@ -1,13 +1,14 @@
1
1
  import { WalletClient } from 'viem';
2
2
  import { MatchedOrder } from '@gardenfi/orderbook';
3
- import { EVMRelayOpts, IEVMRelay } from './evmRelay.types';
3
+ import { IEVMRelay } from './evmRelay.types';
4
4
  import { AsyncResult } from '@catalogfi/utils';
5
+ import { IAuth } from '@gardenfi/utils';
5
6
 
6
7
  export declare class EvmRelay implements IEVMRelay {
7
- private walletClient;
8
8
  private url;
9
9
  private auth;
10
- constructor(url: string, walletClient: WalletClient, opts?: EVMRelayOpts);
11
- init(order: MatchedOrder, currentL1BlockNumber: number): AsyncResult<string, string>;
10
+ private order;
11
+ constructor(order: MatchedOrder, url: string, auth: IAuth);
12
+ init(walletClient: WalletClient, currentL1BlockNumber?: number): AsyncResult<string, string>;
12
13
  redeem(orderId: string, secret: string): AsyncResult<string, string>;
13
14
  }
@@ -1,6 +1,6 @@
1
1
  import { AsyncResult } from '@catalogfi/utils';
2
- import { MatchedOrder } from '@gardenfi/orderbook';
3
2
  import { IStore } from '@gardenfi/utils';
3
+ import { WalletClient } from 'viem';
4
4
 
5
5
  export type EVMRelayOpts = {
6
6
  store?: IStore;
@@ -12,10 +12,10 @@ export interface IEVMRelay {
12
12
  * Sends the signature to the relay service.
13
13
  * @param order Matched Order
14
14
  * @param currentL1BlockNumber Current L1 block number. Used to calculate the swap expiry.
15
- * @NOTE send the current block number of the L1 chain even if the order is on L2 chain.
15
+ * @NOTE send the current block number of the L1 chain, if the order is on L2 chain (arbitrum).
16
16
  * @returns txHash of Initiation
17
17
  */
18
- init(order: MatchedOrder, currentL1BlockNumber: number): AsyncResult<string, string>;
18
+ init(walletClient: WalletClient, currentL1BlockNumber: number): AsyncResult<string, string>;
19
19
  /**
20
20
  * Redeems funds from the EVM atomic swap contract using relay service.
21
21
  * Sends the secret to the relay service.
@@ -0,0 +1,9 @@
1
+ import { MatchedOrder } from '@gardenfi/orderbook';
2
+ import { IOrderExecutorCache, OrderActions, OrderCacheValue } from '../garden.types';
3
+
4
+ export declare class ExecutorCache implements IOrderExecutorCache {
5
+ private cache;
6
+ set(order: MatchedOrder, action: OrderActions, txHash: string, utxo?: string): void;
7
+ get(order: MatchedOrder, action: OrderActions): OrderCacheValue | null;
8
+ remove(order: MatchedOrder, action: OrderActions): void;
9
+ }
@@ -1,22 +1,50 @@
1
1
  import { ISecretManager } from './../secretManager/secretManager.types';
2
2
  import { AsyncResult } from '@catalogfi/utils';
3
- import { IGardenJS, SwapParams } from './garden.types';
4
- import { IOrderbook, MatchedOrder } from '@gardenfi/orderbook';
5
- import { IStore } from '@gardenfi/utils';
6
- import { IOrderExecutor } from '../orderExecutor/orderExecutor.types';
3
+ import { GardenEvents, IGardenJS, SwapParams } from './garden.types';
4
+ import { MatchedOrder } from '@gardenfi/orderbook';
5
+ import { IAuth } from '@gardenfi/utils';
6
+ import { IQuote } from '../quote/quote.types';
7
+ import { WalletClient } from 'viem';
8
+ import { IBitcoinWallet } from '@catalogfi/wallets';
7
9
 
8
10
  export declare class Garden implements IGardenJS {
9
11
  private secretManager;
12
+ private readonly eventListeners;
10
13
  private orderBook;
11
- private relayURL;
12
14
  private quote;
13
- private opts;
14
15
  private getOrderThreshold;
15
- constructor(orderbook: IOrderbook, secretManager: ISecretManager, relayUrl: string, quoteUrl: string, opts?: {
16
- store?: IStore;
17
- domain?: string;
16
+ private pendingOrdersCount;
17
+ private orderbookUrl;
18
+ private auth;
19
+ private useRelay;
20
+ private wallets;
21
+ private evmAddress;
22
+ private orderExecutorCache;
23
+ constructor(config: {
24
+ orderbookURl: string;
25
+ secretManager: ISecretManager;
26
+ quote: IQuote;
27
+ auth: IAuth;
28
+ wallets: {
29
+ evmWallet: WalletClient;
30
+ btcWallet?: IBitcoinWallet;
31
+ };
18
32
  });
33
+ getPendingOrderCount(): number;
34
+ setUseRelay(useRelay: boolean): void;
19
35
  swap(params: SwapParams): AsyncResult<MatchedOrder, string>;
20
- subscribeOrders(cb: (executor: IOrderExecutor) => Promise<void>, interval?: number): Promise<() => void>;
21
- private validateSwapParams;
36
+ private validateAndFillParams;
37
+ private getAddresses;
38
+ private validateAmount;
39
+ private getTimelock;
40
+ private pollOrder;
41
+ private emit;
42
+ execute(interval?: number): Promise<() => void>;
43
+ private evmRedeem;
44
+ private btcRedeem;
45
+ private btcRefund;
46
+ private getWallet;
47
+ on<E extends keyof GardenEvents>(event: E, cb: GardenEvents[E]): void;
48
+ off<E extends keyof GardenEvents>(event: E, cb: GardenEvents[E]): void;
49
+ private fetchCurrentBlockNumbers;
22
50
  }
@@ -1,6 +1,5 @@
1
1
  import { AsyncResult } from '@catalogfi/utils';
2
2
  import { Asset, MatchedOrder } from '@gardenfi/orderbook';
3
- import { IOrderExecutor } from '../orderExecutor/orderExecutor.types';
4
3
 
5
4
  export type SwapParams = {
6
5
  /**
@@ -19,18 +18,6 @@ export type SwapParams = {
19
18
  * Amount in lowest denomination of the asset.
20
19
  */
21
20
  receiveAmount: string;
22
- /**
23
- * EVM - The address from the which the user is sending funds from.
24
- *
25
- * Bitcoin - Provide public key if the source chain is bitcoin.
26
- */
27
- sendAddress: string;
28
- /**
29
- * EVM - The address at which the user wants to receive funds.
30
- *
31
- * Bitcoin - Provide public key if the destination chain is bitcoin.
32
- */
33
- receiveAddress: string;
34
21
  /**
35
22
  * Time lock for the swap.
36
23
  */
@@ -57,6 +44,13 @@ export declare enum TimeLocks {
57
44
  evm = 14400,
58
45
  btc = 288
59
46
  }
47
+ export type GardenEvents = {
48
+ error: (order: MatchedOrder, error: string) => void;
49
+ success: (order: MatchedOrder, action: OrderActions, result: string) => void;
50
+ onPendingOrdersChanged: (orders: MatchedOrder[]) => void;
51
+ log: (id: string, message: string) => void;
52
+ };
53
+ export type EventCallback = (...args: any[]) => void;
60
54
  export interface IGardenJS {
61
55
  /**
62
56
  * Create Order
@@ -68,5 +62,26 @@ export interface IGardenJS {
68
62
  * @param cb - Callback function to be called for each order. This callback will take orderExecutor as an argument.
69
63
  * @param interval - Polling interval in milliseconds.
70
64
  */
71
- subscribeOrders(cb: (orderExecutor: IOrderExecutor) => Promise<void>, interval?: number): Promise<() => void>;
65
+ execute(): Promise<() => void>;
66
+ getPendingOrderCount(): number;
67
+ on<E extends keyof GardenEvents>(event: E, cb: GardenEvents[E]): void;
68
+ }
69
+ export type OrderCacheValue = {
70
+ txHash: string;
71
+ timeStamp: number;
72
+ btcRedeemUTXO?: string;
73
+ };
74
+ export interface IOrderExecutorCache {
75
+ set(order: MatchedOrder, action: OrderActions, txHash: string, utxo?: string): void;
76
+ get(order: MatchedOrder, action: OrderActions): OrderCacheValue | null;
77
+ remove(order: MatchedOrder, action: OrderActions): void;
78
+ }
79
+ /**
80
+ * Actions that can be performed on the order.
81
+ */
82
+ export declare enum OrderActions {
83
+ Idle = "Idle",
84
+ Initiate = "Initiate",
85
+ Redeem = "Redeem",
86
+ Refund = "Refund"
72
87
  }
@@ -1,5 +1,6 @@
1
1
  import { MatchedOrder, Swap } from '@gardenfi/orderbook';
2
- import { OrderActions, OrderStatus, SwapStatus } from './orderExecutor.types';
2
+ import { OrderActions } from './garden.types';
3
+ import { OrderStatus, SwapStatus } from '../status';
3
4
 
4
5
  /**
5
6
  * Parse the order status based on the current block number and checks if its expired or initiated or redeemed or refunded
@@ -25,3 +26,4 @@ export declare const ParseSwapStatus: (swap: Swap, currentBlockNumber: number) =
25
26
  * @returns {OrderActions} The action to be performed on the order
26
27
  */
27
28
  export declare const parseAction: (order: MatchedOrder, sourceChainCurrentBlockNumber: number, destChainCurrentBlockNumber: number) => OrderActions;
29
+ export declare const isExpired: (unixTime: number, tillHours?: number) => boolean;
@@ -1,8 +1,3 @@
1
- import { AsyncResult } from '@catalogfi/utils';
2
- import { IBitcoinWallet } from '@catalogfi/wallets';
3
- import { WalletClient } from 'viem';
4
- import { MatchedOrder } from '@gardenfi/orderbook';
5
-
6
1
  /**
7
2
  * Order statuses
8
3
  */
@@ -90,11 +85,13 @@ export declare enum OrderStatus {
90
85
  CounterPartyRefunded = "CounterPartyRefunded",
91
86
  /**
92
87
  * - Order is cancelled
93
- * - There are some cases where the order is cancelled:
94
- * 1) Not matched within 1 hour.
95
- * 2) Both parties refund.
96
88
  */
97
- Cancelled = "Cancelled"
89
+ Cancelled = "Cancelled",
90
+ /**
91
+ * - DeadLine exceeded
92
+ * - Initiate not detected before 1 hour of deadline or initiate not confirmed before 12 hours of deadline
93
+ */
94
+ DeadLineExceeded = "DeadLineExceeded"
98
95
  }
99
96
  export declare enum SwapStatus {
100
97
  /**
@@ -131,65 +128,3 @@ export declare enum SwapStatus {
131
128
  */
132
129
  Expired = "Expired"
133
130
  }
134
- export declare enum OrderActions {
135
- Idle = "Idle",
136
- Initiate = "Initiate",
137
- Redeem = "Redeem",
138
- Refund = "Refund"
139
- }
140
- export type ExecuteParams = {
141
- wallets: {
142
- source: IBitcoinWallet | WalletClient;
143
- destination: IBitcoinWallet | WalletClient;
144
- };
145
- blockNumbers?: {
146
- source: number;
147
- destination: number;
148
- };
149
- };
150
- /**
151
- * This is a generic interface for Order. Use this interface to perform operations on the order (init, redeem, refund, execute).
152
- */
153
- export interface IOrderExecutor {
154
- /**
155
- * Get the order details.
156
- * @returns MatchedOrder
157
- */
158
- getOrder(): MatchedOrder;
159
- /**
160
- * Initiate the atomic swap contract.
161
- * @param wallet - Wallet to initiate the swap.
162
- * @param currentBlockNumber - Current block number of the chain. Provide L1 block number if chain is arbitrum or arbitrum_sepolia.
163
- */
164
- init(wallet: WalletClient | IBitcoinWallet, currentBlockNumber?: number): AsyncResult<string, string>;
165
- /**
166
- * Redeem the funds from the atomic swap contract.
167
- */
168
- redeem(wallet: WalletClient | IBitcoinWallet, secret: string): AsyncResult<string, string>;
169
- /**
170
- * Refund the funds from the atomic swap contract after expiry.
171
- */
172
- refund(wallet: IBitcoinWallet): AsyncResult<string, string>;
173
- /**
174
- * This will take care of order execution according to its current status, i.e., redeem or refund.
175
- *
176
- * Redeem:- Both EVM and BTC redeems are done by executor, and only BTC refund is done by executor.
177
- * EVM refund is done automatically by the relayer service.
178
- */
179
- execute(params: ExecuteParams): AsyncResult<string | void, string>;
180
- }
181
- export declare enum OrderCacheAction {
182
- init = "init",
183
- redeem = "redeem",
184
- refund = "refund"
185
- }
186
- export type OrderCacheValue = {
187
- txHash: string;
188
- timeStamp: number;
189
- };
190
- export interface IOrderCache {
191
- getOrder(): MatchedOrder;
192
- set(action: OrderCacheAction, txHash: string): void;
193
- get(action: OrderCacheAction): OrderCacheValue | null;
194
- remove(action: OrderCacheAction): void;
195
- }
@@ -1,5 +1,8 @@
1
+ import { Chain as viemChain } from 'viem/chains';
1
2
  import { IBaseWallet } from '@catalogfi/wallets';
2
- import { Chain } from '@gardenfi/orderbook';
3
+ import { Chain, EvmChain } from '@gardenfi/orderbook';
4
+ import { WalletClient } from 'viem';
5
+ import { AsyncResult } from '@catalogfi/utils';
3
6
 
4
7
  export declare const computeSecret: (fromChain: Chain, toChain: Chain, wallets: Partial<Record<Chain, IBaseWallet>>, nonce: number) => Promise<string>;
5
8
  export declare const isFromChainBitcoin: (chain: Chain) => chain is "bitcoin" | "bitcoin_testnet" | "bitcoin_regtest";
@@ -9,13 +12,24 @@ export declare const isFromChainBitcoin: (chain: Chain) => chain is "bitcoin" |
9
12
  export declare function xOnlyPubkey(pubkey: Buffer | string): Buffer;
10
13
  export declare function assert(condition: boolean, message: string): void;
11
14
  /**
12
- * concats the leaf version, the length of the script, and the script itself
15
+ * concat the leaf version, the length of the script, and the script itself
13
16
  */
14
17
  export declare function serializeScript(leafScript: Buffer): Buffer;
15
18
  /**
16
- * concats the length of the script and the script itself
19
+ * concat the length of the script and the script itself
17
20
  */
18
21
  export declare function prefixScriptLength(s: Buffer): Buffer;
19
22
  export declare function sortLeaves(leaf1: Buffer, leaf2: Buffer): Buffer[];
20
23
  export declare const toXOnly: (pubKey: string) => string;
21
24
  export declare const isValidBitcoinPubKey: (pubKey: string) => boolean;
25
+ export declare const ChainToID: Record<EvmChain, viemChain>;
26
+ /**
27
+ * Switches or adds a network to the wallet
28
+ * @param chain Garden supported chain
29
+ * @param walletClient
30
+ * @returns new walletClient with updated chain
31
+ */
32
+ export declare const switchOrAddNetwork: (chain: Chain, walletClient: WalletClient) => AsyncResult<{
33
+ message: string;
34
+ walletClient: WalletClient;
35
+ }, string>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gardenfi/core",
3
- "version": "0.2.0-beta.4",
3
+ "version": "0.2.0-beta.41",
4
4
  "type": "module",
5
5
  "files": [
6
6
  "dist"
@@ -27,8 +27,10 @@
27
27
  },
28
28
  "dependencies": {
29
29
  "@catalogfi/utils": "^0.1.6",
30
- "@catalogfi/wallets": "^0.2.49",
31
- "@gardenfi/orderbook": "^0.2.0-beta.4",
30
+ "@catalogfi/wallets": "^0.2.50",
31
+ "@gardenfi/orderbook": "^0.2.0-beta.14",
32
+ "@gardenfi/utils": "^0.0.1-beta.18",
33
+ "bignumber.js": "^9.1.2",
32
34
  "bitcoinjs-lib": "^6.1.6",
33
35
  "tiny-secp256k1": "^2.2.3",
34
36
  "varuint-bitcoin": "^1.1.2",
@@ -1,17 +0,0 @@
1
- import { AsyncResult } from '@catalogfi/utils';
2
- import { IBitcoinProvider } from '@catalogfi/wallets';
3
- import { WalletClient, Chain } from 'viem';
4
-
5
- /**
6
- * Fetches the latest block number of EVM chain
7
- * @param walletClient Wallet client
8
- * @returns EVM latest block number
9
- */
10
- export declare const fetchEVMBlockNumber: (walletClient: WalletClient) => AsyncResult<number, string>;
11
- export declare const _fetchEVMBlockNumber: (chain: Chain) => Promise<import('@catalogfi/utils').Result<never, string> | import('@catalogfi/utils').Result<number, never>>;
12
- /**
13
- * Fetches the latest block number of Bitcoin chain
14
- * @param btcProvider Bitcoin provider
15
- * @returns bitcoin latest block number
16
- */
17
- export declare const fetchBitcoinBlockNumber: (btcProvider: IBitcoinProvider) => AsyncResult<number, string>;
@@ -1,13 +0,0 @@
1
- import { MatchedOrder } from '@gardenfi/orderbook';
2
- import { IStore } from '@gardenfi/utils';
3
- import { IOrderCache, OrderCacheAction, OrderCacheValue } from './orderExecutor.types';
4
-
5
- export declare class OrderCache implements IOrderCache {
6
- private order;
7
- private store;
8
- constructor(order: MatchedOrder, store?: IStore);
9
- getOrder(): MatchedOrder;
10
- set(action: OrderCacheAction, txHash: string): void;
11
- get(action: OrderCacheAction): OrderCacheValue | null;
12
- remove(action: OrderCacheAction): void;
13
- }
@@ -1,25 +0,0 @@
1
- import { WalletClient } from 'viem';
2
- import { ExecuteParams, IOrderExecutor } from './orderExecutor.types';
3
- import { IBitcoinWallet } from '@catalogfi/wallets';
4
- import { MatchedOrder } from '@gardenfi/orderbook';
5
- import { IStore } from '@gardenfi/utils';
6
- import { AsyncResult } from '@catalogfi/utils';
7
- import { ISecretManager } from '../secretManager/secretManager.types';
8
-
9
- export declare class OrderExecutor implements IOrderExecutor {
10
- private order;
11
- private relayURL;
12
- private secretManager;
13
- private opts;
14
- private orderCache;
15
- constructor(order: MatchedOrder, relayURL: string, secretManager: ISecretManager, opts?: {
16
- store?: IStore;
17
- domain?: string;
18
- });
19
- getOrder(): MatchedOrder;
20
- init(walletClient: WalletClient, currentBlockNumber?: number): AsyncResult<string, string>;
21
- redeem(wallet: WalletClient | IBitcoinWallet, secret: string): AsyncResult<string, string>;
22
- refund(wallet: IBitcoinWallet): AsyncResult<string, string>;
23
- execute(params: ExecuteParams): AsyncResult<string | void, string>;
24
- private fetchCurrentBlockNumber;
25
- }