@gardenfi/core 0.3.0-beta.2 → 0.3.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.
@@ -1,38 +1,36 @@
1
1
  import { ISecretManager } from './../secretManager/secretManager.types';
2
2
  import { AsyncResult } from '@catalogfi/utils';
3
- import { GardenEvents, IGardenJS, SwapParams } from './garden.types';
4
- import { MatchedOrder } from '@gardenfi/orderbook';
5
- import { IAuth } from '@gardenfi/utils';
3
+ import { GardenEvents, GardenProps, IGardenJS, SwapParams } from './garden.types';
4
+ import { IOrderbook, MatchedOrder } from '@gardenfi/orderbook';
5
+ import { EventBroker } from '@gardenfi/utils';
6
6
  import { IQuote } from '../quote/quote.types';
7
- import { WalletClient } from 'viem';
8
7
  import { IBitcoinWallet } from '@catalogfi/wallets';
9
- import { IBlockNumberFetcher } from './blockNumber';
8
+ import { IBlockNumberFetcher } from '../blockNumberFetcher/blockNumber';
9
+ import { IEVMRelay } from '../evm/relay/evmRelay.types';
10
10
 
11
- export declare class Garden implements IGardenJS {
12
- private secretManager;
13
- private readonly eventListeners;
14
- private orderBook;
15
- private quote;
11
+ export declare class Garden extends EventBroker<GardenEvents> implements IGardenJS {
12
+ private environment;
13
+ private _secretManager;
14
+ private _orderBook;
15
+ private _quote;
16
16
  private getOrderThreshold;
17
- private orderbookUrl;
18
- private auth;
19
- private useRelay;
20
- private wallets;
21
- private evmAddress;
17
+ private _orderbookUrl;
18
+ private _auth;
22
19
  private orderExecutorCache;
23
- private blockNumberFetcher;
24
- constructor(config: {
25
- orderbookURl: string;
26
- secretManager: ISecretManager;
27
- quote: IQuote;
28
- auth: IAuth;
29
- wallets: {
30
- evmWallet: WalletClient;
31
- btcWallet?: IBitcoinWallet;
32
- };
33
- blockNumberFetcher?: IBlockNumberFetcher;
34
- });
35
- setUseRelay(useRelay: boolean): void;
20
+ private _blockNumberFetcher;
21
+ private refundSacpCache;
22
+ private _evmRelay;
23
+ private _evmWallet;
24
+ private _btcWallet;
25
+ constructor(config: GardenProps);
26
+ get orderbookUrl(): string;
27
+ get evmRelay(): IEVMRelay;
28
+ get quote(): IQuote;
29
+ get btcWallet(): IBitcoinWallet | undefined;
30
+ get orderbook(): IOrderbook;
31
+ get blockNumberFetcher(): IBlockNumberFetcher;
32
+ get secretManager(): ISecretManager;
33
+ private initializeSMandBTCWallet;
36
34
  swap(params: SwapParams): AsyncResult<MatchedOrder, string>;
37
35
  private validateAndFillParams;
38
36
  private getAddresses;
@@ -43,9 +41,7 @@ export declare class Garden implements IGardenJS {
43
41
  private btcRedeem;
44
42
  private btcRefund;
45
43
  private getWallet;
46
- private emit;
47
- on<E extends keyof GardenEvents>(event: E, cb: GardenEvents[E]): void;
48
- off<E extends keyof GardenEvents>(event: E, cb: GardenEvents[E]): void;
44
+ private postRefundSACP;
49
45
  private fetchCurrentBlockNumbers;
50
- private assignOrderStatus;
46
+ private filterExpiredAndAssignStatus;
51
47
  }
@@ -1,6 +1,13 @@
1
1
  import { AsyncResult } from '@catalogfi/utils';
2
- import { Asset, MatchedOrder } from '@gardenfi/orderbook';
2
+ import { Asset, IOrderbook, MatchedOrder } from '@gardenfi/orderbook';
3
3
  import { OrderStatus } from '../status';
4
+ import { Environment, EventBroker, SiweOpts } from '@gardenfi/utils';
5
+ import { WalletClient } from 'viem';
6
+ import { ISecretManager } from '../secretManager/secretManager.types';
7
+ import { IQuote } from '../quote/quote.types';
8
+ import { IBlockNumberFetcher } from '../blockNumberFetcher/blockNumber';
9
+ import { IEVMRelay } from '../evm/relay/evmRelay.types';
10
+ import { IBitcoinWallet } from '@catalogfi/wallets';
4
11
 
5
12
  export type SwapParams = {
6
13
  /**
@@ -55,20 +62,56 @@ export type GardenEvents = {
55
62
  log: (id: string, message: string) => void;
56
63
  };
57
64
  export type EventCallback = (...args: any[]) => void;
58
- export interface IGardenJS {
65
+ /**
66
+ * Interface representing the GardenJS library.
67
+ */
68
+ export interface IGardenJS extends EventBroker<GardenEvents> {
59
69
  /**
60
70
  * Create Order
61
- * @param {SwapParams} - The parameters for creating the order.
71
+ * @param {SwapParams} params - The parameters for creating the order.
72
+ * @returns {AsyncResult<MatchedOrder, string>} The result of the swap operation.
62
73
  */
63
74
  swap(params: SwapParams): AsyncResult<MatchedOrder, string>;
64
75
  /**
65
- * Subscribe to orders. This will poll the orderbook and call callback for each order.
66
- * @param cb - Callback function to be called for each order. This callback will take orderExecutor as an argument.
67
- * @param interval - Polling interval in milliseconds.
76
+ * Execute an action.
77
+ * @returns {Promise<() => void>} A promise that resolves to a function to cancel the execution.
68
78
  */
69
79
  execute(): Promise<() => void>;
70
- on<E extends keyof GardenEvents>(event: E, cb: GardenEvents[E]): void;
71
- off<E extends keyof GardenEvents>(event: E, cb: GardenEvents[E]): void;
80
+ /**
81
+ * The URL of the orderbook.
82
+ * @readonly
83
+ */
84
+ get orderbookUrl(): string;
85
+ /**
86
+ * The EVM relay.
87
+ * @readonly
88
+ */
89
+ get evmRelay(): IEVMRelay;
90
+ /**
91
+ * The current quote.
92
+ * @readonly
93
+ */
94
+ get quote(): IQuote;
95
+ /**
96
+ * The BTC wallet.
97
+ * @readonly
98
+ */
99
+ get btcWallet(): IBitcoinWallet | undefined;
100
+ /**
101
+ * The orderbook.
102
+ * @readonly
103
+ */
104
+ get orderbook(): IOrderbook;
105
+ /**
106
+ * The block number fetcher.
107
+ * @readonly
108
+ */
109
+ get blockNumberFetcher(): IBlockNumberFetcher;
110
+ /**
111
+ * The secret manager.
112
+ * @readonly
113
+ */
114
+ get secretManager(): ISecretManager;
72
115
  }
73
116
  export type OrderCacheValue = {
74
117
  txHash: string;
@@ -80,6 +123,15 @@ export interface IOrderExecutorCache {
80
123
  get(order: MatchedOrder, action: OrderActions): OrderCacheValue | null;
81
124
  remove(order: MatchedOrder, action: OrderActions): void;
82
125
  }
126
+ export type GardenProps = {
127
+ environment: Environment;
128
+ evmWallet: WalletClient;
129
+ secretManager?: ISecretManager;
130
+ siweOpts?: SiweOpts;
131
+ orderbookURl?: string;
132
+ quote?: IQuote;
133
+ blockNumberFetcher?: IBlockNumberFetcher;
134
+ };
83
135
  /**
84
136
  * Actions that can be performed on the order.
85
137
  */
@@ -1,6 +1,6 @@
1
1
  import { MatchedOrder, Swap } from '@gardenfi/orderbook';
2
- import { OrderActions } from './garden.types';
3
- import { OrderStatus, SwapStatus } from '../status';
2
+ import { OrderActions } from './garden/garden.types';
3
+ import { OrderStatus, SwapStatus } from './status';
4
4
 
5
5
  /**
6
6
  * Parse the order status based on the current block number and checks if its expired or initiated or redeemed or refunded
@@ -34,3 +34,4 @@ export declare const parseAction: (order: MatchedOrder, sourceChainCurrentBlockN
34
34
  export declare const parseActionFromStatus: (status: OrderStatus) => OrderActions;
35
35
  export declare const isExpired: (unixTime: number, tillHours?: number) => boolean;
36
36
  export declare const filterDeadlineExpiredOrders: (orders: MatchedOrder[]) => MatchedOrder[];
37
+ export declare const isOrderExpired: (order: MatchedOrder) => boolean;
@@ -1,15 +1,20 @@
1
1
  import { WalletClient } from 'viem';
2
- import { ISecretManager } from './secretManager.types';
2
+ import { EventBroker } from '@gardenfi/utils';
3
+ import { ISecretManager, SecretManagerEvents } from './secretManager.types';
3
4
 
4
- export declare class SecretManager implements ISecretManager {
5
- private privKey;
5
+ export declare class SecretManager extends EventBroker<SecretManagerEvents> implements ISecretManager {
6
+ private privKey?;
7
+ private walletClient?;
8
+ get isInitialized(): boolean;
6
9
  private constructor();
7
10
  static fromPrivKey(privKey: string): SecretManager;
8
- static fromWalletClient(walletClient: WalletClient): Promise<import('@catalogfi/utils').Result<never, string> | import('@catalogfi/utils').Result<SecretManager, never>>;
9
- getMasterPrivKey(): string;
10
- generateSecret(nonce: number): import('@catalogfi/utils').Result<{
11
+ static fromWalletClient(walletClient: WalletClient): SecretManager;
12
+ initialize(): Promise<import('@catalogfi/utils').Result<never, string> | import('@catalogfi/utils').Result<string, never>>;
13
+ private derivePrivKeyFromWalletClient;
14
+ getMasterPrivKey(): Promise<import('@catalogfi/utils').Result<never, string> | import('@catalogfi/utils').Result<string, never>>;
15
+ generateSecret(nonce: number): Promise<import('@catalogfi/utils').Result<never, string> | import('@catalogfi/utils').Result<{
11
16
  secret: `0x${string}`;
12
17
  secretHash: `0x${string}`;
13
- }, never>;
18
+ }, never>>;
14
19
  private signMessage;
15
20
  }
@@ -1,4 +1,5 @@
1
- import { Result } from '@catalogfi/utils';
1
+ import { AsyncResult } from '@catalogfi/utils';
2
+ import { EventBroker } from '@gardenfi/utils';
2
3
 
3
4
  export type Secret = {
4
5
  secretHash: string;
@@ -9,18 +10,31 @@ export type Secret = {
9
10
  * This interface is used to generate and retrieve secrets and secretHashes.
10
11
  * The secret is generated by hashing the signature and the secretHash is generated by hashing the secret.
11
12
  */
12
- export interface ISecretManager {
13
+ export interface ISecretManager extends EventBroker<SecretManagerEvents> {
14
+ /**
15
+ * Initializes the SecretManager with the wallet client.
16
+ */
17
+ initialize: () => AsyncResult<string, string>;
13
18
  /**
14
19
  * Generates a new master private key.
15
20
  * If the master private key already exists, it will return the existing master private key.
16
21
  * Else, it will generate a new master private key by hashing a signature generated by signing a message with the wallet client.
17
22
  * @returns Master private key
18
23
  */
19
- getMasterPrivKey: () => string;
24
+ getMasterPrivKey: () => AsyncResult<string, string>;
20
25
  /**
21
26
  * Generates secret and secretHash by signing the nonce. Secret is generated by hashing the signature and secretHash is generated by hashing the secret.
22
27
  * @param nonce
23
28
  * @returns {AsyncResult<Secret, string>} secret,secretHash
24
29
  */
25
- generateSecret: (nonce: number) => Result<Secret, string>;
30
+ generateSecret: (nonce: number) => AsyncResult<Secret, string>;
31
+ /**
32
+ * Checks if the SecretManager is initialized with a private key.
33
+ * @returns boolean
34
+ */
35
+ readonly isInitialized: boolean;
26
36
  }
37
+ export type SecretManagerEvents = {
38
+ error: (error: string) => void;
39
+ initialized: (init: boolean) => void;
40
+ };
@@ -0,0 +1,53 @@
1
+ import { Chain as viemChain } from 'viem/chains';
2
+ import { Chain, EvmChain } from '@gardenfi/orderbook';
3
+ import { WalletClient } from 'viem';
4
+ import { AsyncResult } from '@catalogfi/utils';
5
+
6
+ export declare const citreaTestnet: {
7
+ blockExplorers: {
8
+ readonly default: {
9
+ readonly name: "Citrea Explorer";
10
+ readonly url: "https://explorer.testnet.citrea.xyz";
11
+ readonly apiUrl: "https://explorer.testnet.citrea.xyz/api/v2/";
12
+ };
13
+ };
14
+ contracts?: import('viem').Prettify<{
15
+ [key: string]: import('viem').ChainContract | {
16
+ [sourceId: number]: import('viem').ChainContract | undefined;
17
+ } | undefined;
18
+ } & {
19
+ ensRegistry?: import('viem').ChainContract | undefined;
20
+ ensUniversalResolver?: import('viem').ChainContract | undefined;
21
+ multicall3?: import('viem').ChainContract | undefined;
22
+ universalSignatureVerifier?: import('viem').ChainContract | undefined;
23
+ }> | undefined;
24
+ id: 5115;
25
+ name: "Citrea Testnet";
26
+ nativeCurrency: {
27
+ readonly name: "";
28
+ readonly symbol: "cBTC";
29
+ readonly decimals: 18;
30
+ };
31
+ rpcUrls: {
32
+ readonly default: {
33
+ readonly http: readonly ["https://rpc.testnet.citrea.xyz"];
34
+ };
35
+ };
36
+ sourceId?: number | undefined;
37
+ testnet: true;
38
+ custom?: Record<string, unknown> | undefined;
39
+ fees?: import('viem').ChainFees<undefined> | undefined;
40
+ formatters?: undefined;
41
+ serializers?: import('viem').ChainSerializers<undefined, import('viem').TransactionSerializable> | undefined;
42
+ };
43
+ export declare const evmToViemChainMap: Record<EvmChain, viemChain>;
44
+ /**
45
+ * Switches or adds a network to the wallet
46
+ * @param chain Garden supported chain
47
+ * @param walletClient
48
+ * @returns new walletClient with updated chain
49
+ */
50
+ export declare const switchOrAddNetwork: (chain: Chain, walletClient: WalletClient) => AsyncResult<{
51
+ message: string;
52
+ walletClient: WalletClient;
53
+ }, string>;
@@ -1,8 +1,6 @@
1
- import { Chain as viemChain } from 'viem/chains';
2
- import { IBaseWallet } from '@catalogfi/wallets';
3
- import { Chain, EvmChain, NetworkType } from '@gardenfi/orderbook';
4
- import { WalletClient } from 'viem';
5
- import { AsyncResult } from '@catalogfi/utils';
1
+ import { BitcoinNetwork, IBaseWallet } from '@catalogfi/wallets';
2
+ import { Environment } from '@gardenfi/utils';
3
+ import { Chain, NetworkType } from '@gardenfi/orderbook';
6
4
 
7
5
  export declare const computeSecret: (fromChain: Chain, toChain: Chain, wallets: Partial<Record<Chain, IBaseWallet>>, nonce: number) => Promise<string>;
8
6
  export declare const isFromChainBitcoin: (chain: Chain) => chain is "bitcoin" | "bitcoin_testnet" | "bitcoin_regtest";
@@ -22,16 +20,6 @@ export declare function prefixScriptLength(s: Buffer): Buffer;
22
20
  export declare function sortLeaves(leaf1: Buffer, leaf2: Buffer): Buffer[];
23
21
  export declare const toXOnly: (pubKey: string) => string;
24
22
  export declare const isValidBitcoinPubKey: (pubKey: string) => boolean;
25
- export declare const evmToViemChainMap: 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>;
36
23
  export declare const constructOrderPair: (sourceChain: Chain, sourceAsset: string, destChain: Chain, destAsset: string) => string;
37
24
  export declare function validateBTCAddress(address: string, networkType: NetworkType): boolean;
25
+ export declare const getBitcoinNetwork: (network: Environment) => BitcoinNetwork;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gardenfi/core",
3
- "version": "0.3.0-beta.2",
3
+ "version": "0.3.0-beta.21",
4
4
  "type": "module",
5
5
  "files": [
6
6
  "dist"
@@ -28,8 +28,8 @@
28
28
  "dependencies": {
29
29
  "@catalogfi/utils": "^0.1.6",
30
30
  "@catalogfi/wallets": "^0.2.51",
31
- "@gardenfi/orderbook": "^0.2.0-beta.34",
32
- "@gardenfi/utils": "^0.0.1-beta.18",
31
+ "@gardenfi/orderbook": "^0.2.0-beta.44",
32
+ "@gardenfi/utils": "^0.0.1-beta.22",
33
33
  "bignumber.js": "^9.1.2",
34
34
  "bitcoinjs-lib": "^6.1.6",
35
35
  "tiny-secp256k1": "^2.2.3",