@gardenfi/core 0.2.0-beta.2 → 0.2.0-beta.21

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.
@@ -1,19 +1,47 @@
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 } 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
- private opts;
13
- constructor(orderbook: IOrderbook, relayUrl: string, secretManager: ISecretManager, opts?: {
14
- store?: IStore;
15
- domain?: string;
14
+ private quote;
15
+ private getOrderThreshold;
16
+ private pendingOrdersCount;
17
+ private orderbookUrl;
18
+ private auth;
19
+ private useRelay;
20
+ private wallets;
21
+ private evmAddress;
22
+ private store;
23
+ constructor(config: {
24
+ orderbookURl: string;
25
+ secretManager: ISecretManager;
26
+ quote: IQuote;
27
+ auth: IAuth;
28
+ wallets: {
29
+ evmWallet: WalletClient;
30
+ btcWallet?: IBitcoinWallet;
31
+ };
16
32
  });
17
- swap(params: SwapParams): AsyncResult<string, string>;
18
- subscribeOrders(cb: (executor: IOrderExecutor) => Promise<void>, interval?: number): Promise<() => void>;
33
+ getPendingOrderCount(): number;
34
+ setUseRelay(useRelay: boolean): void;
35
+ swap(params: SwapParams): AsyncResult<MatchedOrder, string>;
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 getWallet;
44
+ on<E extends keyof GardenEvents>(event: E, cb: GardenEvents[E]): void;
45
+ off<E extends keyof GardenEvents>(event: E, cb: GardenEvents[E]): void;
46
+ private fetchCurrentBlockNumbers;
19
47
  }
@@ -1,34 +1,86 @@
1
1
  import { AsyncResult } from '@catalogfi/utils';
2
- import { Asset } from '@gardenfi/orderbook';
3
- import { IOrderExecutor } from '../orderExecutor/orderExecutor.types';
2
+ import { Asset, MatchedOrder } from '@gardenfi/orderbook';
4
3
 
5
4
  export type SwapParams = {
5
+ /**
6
+ * Asset to be sent.
7
+ */
6
8
  fromAsset: Asset;
9
+ /**
10
+ * Asset to be received.
11
+ */
7
12
  toAsset: Asset;
13
+ /**
14
+ * Amount in lowest denomination of the asset.
15
+ */
8
16
  sendAmount: string;
17
+ /**
18
+ * Amount in lowest denomination of the asset.
19
+ */
9
20
  receiveAmount: string;
10
- sendAddress: string;
11
- receiveAddress: string;
21
+ /**
22
+ * Time lock for the swap.
23
+ */
12
24
  timelock?: number;
25
+ /**
26
+ * This will wait for the specified number of confirmations before redeeming the funds.
27
+ */
13
28
  minDestinationConfirmations?: number;
14
- additionalData?: {
15
- btcAddress: string;
29
+ /**
30
+ * Additional data for the order.
31
+ */
32
+ additionalData: {
33
+ /**
34
+ * Get strategy id from the quote
35
+ */
36
+ strategyId: string;
37
+ /**
38
+ * Provide btcAddress if the destination or source chain is bitcoin. This address is used as refund address if source chain is bitcoin, and as redeem address if destination chain is bitcoin.
39
+ */
40
+ btcAddress?: string;
16
41
  };
17
42
  };
18
43
  export declare enum TimeLocks {
19
44
  evm = 14400,
20
45
  btc = 288
21
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
+ };
52
+ export type EventCallback = (...args: any[]) => void;
22
53
  export interface IGardenJS {
23
54
  /**
24
55
  * Create Order
25
56
  * @param {SwapParams} - The parameters for creating the order.
26
57
  */
27
- swap(params: SwapParams): AsyncResult<string, string>;
58
+ swap(params: SwapParams): AsyncResult<MatchedOrder, string>;
28
59
  /**
29
60
  * Subscribe to orders. This will poll the orderbook and call callback for each order.
30
61
  * @param cb - Callback function to be called for each order. This callback will take orderExecutor as an argument.
31
62
  * @param interval - Polling interval in milliseconds.
32
63
  */
33
- subscribeOrders(cb: (orderExecutor: IOrderExecutor) => Promise<void>, interval?: number): Promise<() => void>;
64
+ execute(): Promise<() => void>;
65
+ getPendingOrderCount(): number;
66
+ on<E extends keyof GardenEvents>(event: E, cb: GardenEvents[E]): void;
67
+ }
68
+ export type OrderCacheValue = {
69
+ txHash: string;
70
+ timeStamp: number;
71
+ };
72
+ export interface IOrderCache {
73
+ getOrder(): MatchedOrder;
74
+ set(action: OrderActions, txHash: string): void;
75
+ get(action: OrderActions): OrderCacheValue | null;
76
+ remove(action: OrderActions): void;
77
+ }
78
+ /**
79
+ * Actions that can be performed on the order.
80
+ */
81
+ export declare enum OrderActions {
82
+ Idle = "Idle",
83
+ Initiate = "Initiate",
84
+ Redeem = "Redeem",
85
+ Refund = "Refund"
34
86
  }
@@ -0,0 +1,13 @@
1
+ import { MatchedOrder } from '@gardenfi/orderbook';
2
+ import { IStore } from '@gardenfi/utils';
3
+ import { IOrderCache, OrderActions, OrderCacheValue } from './garden.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: OrderActions, txHash: string): void;
11
+ get(action: OrderActions): OrderCacheValue | null;
12
+ remove(action: OrderActions): void;
13
+ }
@@ -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
@@ -0,0 +1,10 @@
1
+ import { AsyncResult } from '@catalogfi/utils';
2
+ import { IQuote, QuoteResponse } from './quote.types';
3
+ import { CreateOrderRequestWithAdditionalData, CreateOrderReqWithStrategyId } from '@gardenfi/orderbook';
4
+
5
+ export declare class Quote implements IQuote {
6
+ private quoteUrl;
7
+ constructor(quoteUrl: string);
8
+ getQuote(orderpair: string, amount: number, isExactOut?: boolean): Promise<import('@catalogfi/utils').Result<never, string> | import('@catalogfi/utils').Result<QuoteResponse, never>>;
9
+ getAttestedQuote(order: CreateOrderReqWithStrategyId): AsyncResult<CreateOrderRequestWithAdditionalData, string>;
10
+ }
@@ -0,0 +1,29 @@
1
+ import { AsyncResult } from '@catalogfi/utils';
2
+ import { CreateOrderRequestWithAdditionalData, CreateOrderReqWithStrategyId } from '@gardenfi/orderbook';
3
+
4
+ export interface IQuote {
5
+ /**
6
+ * Get a quote for the given orderpair and amount
7
+ * @param orderpair - A string representing the order pair for which the quote is requested.
8
+ *
9
+ * ``eg:- bitcoin_regtest:primary::arbitrum_localnet:0xDc64a140Aa3E981100a9becA4E685f962f0cF6C9``
10
+ *
11
+ * Chain name and asset are separated by a colon(:) and chain pairs are separated by double colon(::)
12
+ * @param amount - The amount to quote
13
+ * @param isExactOut - Whether the amount is exact out
14
+ */
15
+ getQuote(orderpair: string, amount: number, isExactOut: boolean): AsyncResult<QuoteResponse, string>;
16
+ /**
17
+ * Attest the quote, server will return a signature by verifying and signing the quote according to the provided `strategy_id`
18
+ * @param order - The order for which the attestation is requested. Order should include `strategy_id` in the `additional_data` field.
19
+ * @returns {string} The attestation signature
20
+ */
21
+ getAttestedQuote(order: CreateOrderReqWithStrategyId): AsyncResult<CreateOrderRequestWithAdditionalData, string>;
22
+ }
23
+ export type QuoteResponse = {
24
+ quotes: {
25
+ [strategy_id: string]: string;
26
+ };
27
+ input_token_price: number;
28
+ output_token_price: number;
29
+ };
@@ -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
  */
@@ -131,60 +126,3 @@ export declare enum SwapStatus {
131
126
  */
132
127
  Expired = "Expired"
133
128
  }
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
- * Redeem the funds from the atomic swap contract.
161
- */
162
- redeem(wallet: WalletClient | IBitcoinWallet, secret: string): AsyncResult<string, string>;
163
- /**
164
- * Refund the funds from the atomic swap contract after expiry.
165
- */
166
- refund(wallet: IBitcoinWallet): AsyncResult<string, string>;
167
- /**
168
- * This will take care of order execution according to its current status, i.e., redeem or refund.
169
- *
170
- * Redeem:- Both EVM and BTC redeems are done by executor, and only BTC refund is done by executor.
171
- * EVM refund is done automatically by the relayer service.
172
- */
173
- execute(params: ExecuteParams): AsyncResult<string | void, string>;
174
- }
175
- export declare enum OrderCacheAction {
176
- init = "init",
177
- redeem = "redeem",
178
- refund = "refund"
179
- }
180
- export type OrderCacheValue = {
181
- txHash: string;
182
- timeStamp: number;
183
- };
184
- export interface IOrderCache {
185
- getOrder(): MatchedOrder;
186
- set(action: OrderCacheAction, txHash: string): void;
187
- get(action: OrderCacheAction): OrderCacheValue | null;
188
- remove(action: OrderCacheAction): void;
189
- deleteHistory(): void;
190
- }
@@ -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,12 +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;
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.2",
3
+ "version": "0.2.0-beta.21",
4
4
  "type": "module",
5
5
  "files": [
6
6
  "dist"
@@ -27,21 +27,19 @@
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.2",
30
+ "@catalogfi/wallets": "^0.2.50",
31
+ "@gardenfi/orderbook": "^0.2.0-beta.10",
32
+ "@gardenfi/utils": "^0.0.1-beta.17",
32
33
  "bitcoinjs-lib": "^6.1.6",
33
34
  "tiny-secp256k1": "^2.2.3",
34
35
  "varuint-bitcoin": "^1.1.2",
35
- "viem": "^2.21.15",
36
- "vite-plugin-node-polyfills": "^0.22.0"
36
+ "viem": "^2.21.15"
37
37
  },
38
38
  "devDependencies": {
39
39
  "dotenv": "^16.3.1",
40
40
  "typescript": "^5.2.2",
41
41
  "vite": "^5.1.6",
42
42
  "vite-plugin-dts": "^3.7.3",
43
- "vite-plugin-top-level-await": "^1.4.1",
44
- "vite-plugin-wasm": "^3.3.0",
45
43
  "vitest": "^1.6.0"
46
44
  },
47
45
  "sideEffects": false
@@ -1,21 +0,0 @@
1
- import { AsyncResult } from '@catalogfi/utils';
2
- import { IBitcoinProvider } from '@catalogfi/wallets';
3
- import { WalletClient } 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
- /**
12
- * Fetches the latest block number of Bitcoin chain
13
- * @param btcProvider Bitcoin provider
14
- * @returns bitcoin latest block number
15
- */
16
- export declare const fetchBitcoinBlockNumber: (btcProvider: IBitcoinProvider) => AsyncResult<number, string>;
17
- /**
18
- * Returns L1 blockNumber
19
- * @returns Ethereum chain latest block number
20
- */
21
- export declare const fetchL1BlockNumber: () => Promise<import('@catalogfi/utils').Result<never, string> | import('@catalogfi/utils').Result<number, never>>;
@@ -1,15 +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
- private cacheExpirationDuration;
9
- constructor(order: MatchedOrder, store: IStore);
10
- getOrder(): MatchedOrder;
11
- set(action: OrderCacheAction, txHash: string): void;
12
- get(action: OrderCacheAction): OrderCacheValue | null;
13
- remove(action: OrderCacheAction): void;
14
- deleteHistory(): void;
15
- }
@@ -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
- }