@gardenfi/core 0.1.14 → 0.2.0-beta.0

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,190 @@
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
+ /**
7
+ * Order statuses
8
+ */
9
+ export declare enum OrderStatus {
10
+ /**
11
+ * - Order is created
12
+ * - Waiting for match
13
+ */
14
+ Created = "Created",
15
+ /**
16
+ * - Order is matched
17
+ * - User has to initiate
18
+ */
19
+ Matched = "Matched",
20
+ /**
21
+ * - User initiate tx detected on-chain but not confirmed
22
+ */
23
+ InitiateDetected = "InitiateDetected",
24
+ /**
25
+ * - User initiated
26
+ * - Waiting for counter party to initiate
27
+ */
28
+ Initiated = "Initiated",
29
+ /**
30
+ * - Counter party initiate tx detected on-chain but not confirmed
31
+ */
32
+ CounterPartyInitiateDetected = "CounterPartyInitiateDetected",
33
+ /**
34
+ * - Counter party initiated
35
+ * - User has to redeem
36
+ */
37
+ CounterPartyInitiated = "CounterPartyInitiated",
38
+ /**
39
+ * - User redeem tx detected on-chain but not confirmed
40
+ */
41
+ RedeemDetected = "RedeemDetected",
42
+ /**
43
+ * - User redeemed
44
+ * - Counter party has to redeem
45
+ * - We also consider user redemption as completed order.
46
+ */
47
+ Redeemed = "Redeemed",
48
+ /**
49
+ * - Counter party redeem tx detected on-chain but not confirmed
50
+ */
51
+ CounterPartyRedeemDetected = "CounterPartyRedeemDetected",
52
+ /**
53
+ * - Counter party redeemed
54
+ * - Order is completed
55
+ */
56
+ CounterPartyRedeemed = "CounterPartyRedeemed",
57
+ /**
58
+ * - Counter party redeemed
59
+ * - Order is completed
60
+ */
61
+ Completed = "Completed",
62
+ /**
63
+ * - The counterparty's swap has expired, and they will refund their funds.
64
+ * - The user can no longer redeem the funds. The user must wait for their swap to expire and then refund their funds.
65
+ * - Typically, in our system, the counterparty's swap expires in 24 hours, and the user's swap expires in 48 hours.
66
+ */
67
+ CounterPartySwapExpired = "CounterPartySwapExpired",
68
+ /**
69
+ * - The user's swap has expired, and they have to refund their funds.
70
+ * - The user's expiry only happens after counterparty's expiry.
71
+ * - Typically, in our system, the counterparty's swap expires in 24 hours, and the user's swap expires in 48 hours.
72
+ */
73
+ Expired = "Expired",
74
+ /**
75
+ * - User refund tx detected on-chain but not confirmed
76
+ */
77
+ RefundDetected = "RefundDetected",
78
+ /**
79
+ * - User refunded
80
+ * - User can only refund if their swap has expired
81
+ */
82
+ Refunded = "Refunded",
83
+ /**
84
+ * - Counter party refund tx detected on-chain but not confirmed
85
+ */
86
+ CounterPartyRefundDetected = "CounterPartyRefundDetected",
87
+ /**
88
+ * - Counter party refunded
89
+ */
90
+ CounterPartyRefunded = "CounterPartyRefunded",
91
+ /**
92
+ * - 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
+ */
97
+ Cancelled = "Cancelled"
98
+ }
99
+ export declare enum SwapStatus {
100
+ /**
101
+ * - Swap is idle
102
+ */
103
+ Idle = "Idle",
104
+ /**
105
+ * - Initiate tx detected on-chain but not confirmed
106
+ */
107
+ InitiateDetected = "InitiateDetected",
108
+ /**
109
+ * - Swap is initiated
110
+ */
111
+ Initiated = "Initiated",
112
+ /**
113
+ * - Redeem tx detected on-chain but not confirmed
114
+ */
115
+ RedeemDetected = "RedeemDetected",
116
+ /**
117
+ * - Redeemed
118
+ */
119
+ Redeemed = "Redeemed",
120
+ /**
121
+ * - Refund tx detected on-chain but not confirmed
122
+ */
123
+ RefundDetected = "RefundDetected",
124
+ /**
125
+ * - Refunded
126
+ */
127
+ Refunded = "Refunded",
128
+ /**
129
+ * - Swap is expired.
130
+ * - Should refund.
131
+ */
132
+ Expired = "Expired"
133
+ }
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
+ }
@@ -0,0 +1,27 @@
1
+ import { MatchedOrder, Swap } from '@gardenfi/orderbook';
2
+ import { OrderActions, OrderStatus, SwapStatus } from './orderExecutor.types';
3
+
4
+ /**
5
+ * Parse the order status based on the current block number and checks if its expired or initiated or redeemed or refunded
6
+ * @param order Order object
7
+ * @param sourceChainCurrentBlockNumber source chain current block number
8
+ * @param destChainCurrentBlockNumber destination chain current block number
9
+ * @note send the current block number of the L1 chain even if the order is on L2 chain.
10
+ * @returns {OrderStatus} The status of the order
11
+ */
12
+ export declare const ParseOrderStatus: (order: MatchedOrder, sourceChainCurrentBlockNumber: number, destChainCurrentBlockNumber: number) => OrderStatus;
13
+ /**
14
+ * Parse the swap status based on the current block number and checks if its expired or initiated or redeemed or refunded
15
+ * @param swap The swap object
16
+ * @param currentBlockNumber
17
+ */
18
+ export declare const ParseSwapStatus: (swap: Swap, currentBlockNumber: number) => SwapStatus;
19
+ /**
20
+ * Parse the action to be performed on the.
21
+ * @param order Order object
22
+ * @param sourceChainCurrentBlockNumber source chain current block number
23
+ * @param destChainCurrentBlockNumber destination chain current block number
24
+ * @note send the current block number of the L1 chain even if the order is on L2 chain.
25
+ * @returns {OrderActions} The action to be performed on the order
26
+ */
27
+ export declare const parseAction: (order: MatchedOrder, sourceChainCurrentBlockNumber: number, destChainCurrentBlockNumber: number) => OrderActions;
@@ -0,0 +1,15 @@
1
+ import { WalletClient } from 'viem';
2
+ import { ISecretManager } from './secretManager.types';
3
+
4
+ export declare class SecretManager implements ISecretManager {
5
+ private privKey;
6
+ private constructor();
7
+ 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
+ secret: `0x${string}`;
12
+ secretHash: `0x${string}`;
13
+ }, never>;
14
+ private signMessage;
15
+ }
@@ -0,0 +1,26 @@
1
+ import { Result } from '@catalogfi/utils';
2
+
3
+ export type Secret = {
4
+ secretHash: string;
5
+ secret: string;
6
+ };
7
+ /**
8
+ * Secret Manager Interface.
9
+ * This interface is used to generate and retrieve secrets and secretHashes.
10
+ * The secret is generated by hashing the signature and the secretHash is generated by hashing the secret.
11
+ */
12
+ export interface ISecretManager {
13
+ /**
14
+ * Generates a new master private key.
15
+ * If the master private key already exists, it will return the existing master private key.
16
+ * Else, it will generate a new master private key by hashing a signature generated by signing a message with the wallet client.
17
+ * @returns Master private key
18
+ */
19
+ getMasterPrivKey: () => string;
20
+ /**
21
+ * Generates secret and secretHash by signing the nonce. Secret is generated by hashing the signature and secretHash is generated by hashing the secret.
22
+ * @param nonce
23
+ * @returns {AsyncResult<Secret, string>} secret,secretHash
24
+ */
25
+ generateSecret: (nonce: number) => Result<Secret, string>;
26
+ }
@@ -3,3 +3,18 @@ import { Chain } from '@gardenfi/orderbook';
3
3
 
4
4
  export declare const computeSecret: (fromChain: Chain, toChain: Chain, wallets: Partial<Record<Chain, IBaseWallet>>, nonce: number) => Promise<string>;
5
5
  export declare const isFromChainBitcoin: (chain: Chain) => chain is "bitcoin" | "bitcoin_testnet" | "bitcoin_regtest";
6
+ /**
7
+ * Given a hex string or a buffer, return the x-only pubkey. (removes y coordinate the prefix)
8
+ */
9
+ export declare function xOnlyPubkey(pubkey: Buffer | string): Buffer;
10
+ export declare function assert(condition: boolean, message: string): void;
11
+ /**
12
+ * concats the leaf version, the length of the script, and the script itself
13
+ */
14
+ export declare function serializeScript(leafScript: Buffer): Buffer;
15
+ /**
16
+ * concats the length of the script and the script itself
17
+ */
18
+ export declare function prefixScriptLength(s: Buffer): Buffer;
19
+ export declare function sortLeaves(leaf1: Buffer, leaf2: Buffer): Buffer[];
20
+ export declare const toXOnly: (pubKey: string) => string;
package/package.json CHANGED
@@ -1,18 +1,13 @@
1
1
  {
2
2
  "name": "@gardenfi/core",
3
- "version": "0.1.14",
3
+ "version": "0.2.0-beta.0",
4
4
  "type": "module",
5
- "dependencies": {
6
- "@catalogfi/utils": "^0.1.6",
7
- "@catalogfi/wallets": "^0.2.45",
8
- "@gardenfi/orderbook": "^0.1.9",
9
- "ethers": "6.8.0"
10
- },
11
5
  "files": [
12
6
  "dist"
13
7
  ],
14
8
  "scripts": {
15
9
  "build": "vite build",
10
+ "test": "vitest run",
16
11
  "dev": "vite build --watch"
17
12
  },
18
13
  "main": "./dist/index.cjs",
@@ -30,15 +25,24 @@
30
25
  "access": "public",
31
26
  "registry": "https://registry.npmjs.org/"
32
27
  },
28
+ "dependencies": {
29
+ "@catalogfi/utils": "^0.1.6",
30
+ "@catalogfi/wallets": "^0.2.49",
31
+ "@gardenfi/orderbook": "workspace:^",
32
+ "bitcoinjs-lib": "^6.1.6",
33
+ "tiny-secp256k1": "^2.2.3",
34
+ "varuint-bitcoin": "^1.1.2",
35
+ "viem": "^2.21.15",
36
+ "vite-plugin-node-polyfills": "^0.22.0"
37
+ },
33
38
  "devDependencies": {
34
- "@types/jest": "^29.5.12",
35
39
  "dotenv": "^16.3.1",
36
- "jest": "^29.7.0",
37
40
  "typescript": "^5.2.2",
38
41
  "vite": "^5.1.6",
39
42
  "vite-plugin-dts": "^3.7.3",
40
43
  "vite-plugin-top-level-await": "^1.4.1",
41
- "vite-plugin-wasm": "^3.3.0"
44
+ "vite-plugin-wasm": "^3.3.0",
45
+ "vitest": "^1.6.0"
42
46
  },
43
47
  "sideEffects": false
44
- }
48
+ }
@@ -1,3 +0,0 @@
1
- export declare const catalogWalletActions: {
2
- createOrderAndSwap: string;
3
- };
@@ -1,46 +0,0 @@
1
- import { Orders, Order, Asset, IOrderbook } from '@gardenfi/orderbook';
2
- import { ISwapper } from './swapper';
3
- import { IGardenJS, Wallets } from './garden.types';
4
-
5
- /**
6
- * GardenJS is the core component of the Garden SDK. It allows you to create orders,
7
- * subscribe to order updates, and perform initiates, redeems, and refunds on swaps.
8
- *
9
- * Visit the [GardenJS documentation](https://docs.garden.finance/developers/sdk/) for more information.
10
- */
11
- export declare class GardenJS implements IGardenJS {
12
- private readonly orderbook;
13
- private readonly wallets;
14
- /**
15
- * @constructor
16
- *
17
- * @param {IOrderbook} orderbook - The orderbook you want to connect to
18
- * @param {Partial<Wallets>} wallets - Each field in the wallet corresponds to the chain name and it's corresponding value is the wallet
19
- *
20
- */
21
- constructor(orderbook: IOrderbook, wallets: Partial<Wallets>);
22
- subscribeOrders(address: string, callback: (orders: Orders) => void): void;
23
- unsubscribeOrders(): void;
24
- /**
25
- * Creates a swap order in the orderbook backend. Checkout `Assets` from @gardenfi/orderbook for the available assets
26
- * @param from from asset (e.g. Assets.ethereum.WBTC)
27
- * @param to to asset (e.g. Assets.bitcoin.BTC)
28
- * @param amt send amount
29
- * @param receiveAmount receive amount
30
- * @param opts if the swap destination is bitcoin, you can provide a btcInputAddress to redeem funds to.
31
- * @returns order id
32
- *
33
- * Note: localnet assets require `merry` running, else it will throw an error saying unsupported asset.
34
- */
35
- swap(from: Asset, to: Asset, amt: number, receiveAmount: number, opts?: {
36
- btcUserAddress?: string;
37
- }): Promise<number>;
38
- /**
39
- * Given an order, returns a Swapper instance to progress the swap (init, redeem, refund)
40
- */
41
- getSwap(order: Order): ISwapper;
42
- /**
43
- * Calculates the receive amount for the given send amount
44
- */
45
- calculateReceiveAmt(from: Asset, to: Asset, sendAmt: number): Promise<number>;
46
- }
@@ -1,53 +0,0 @@
1
- import { Asset, Chain, EvmChain, Order, Orders } from '@gardenfi/orderbook';
2
- import { IBitcoinWallet, IEVMWallet } from '@catalogfi/wallets';
3
- import { ISwapper } from './swapper';
4
-
5
- export interface IGardenJS {
6
- /**
7
- *
8
- * Creates an order
9
- *
10
- * @method
11
- * @param {Asset} from - The asset you want to swap from
12
- * @param {Asset} to - The asset you want to swap to
13
- * @param {number} amt - The amount you want to swap in it's lowest denomination
14
- * @param {number} receiveAmount - The amount you want to receive in it's lowest denomination
15
- * @param {Object} [opts] - Additional options for creating an order
16
- * @param {string} [opts.btcUserAddress] - If specified, BTC will be sent to this address
17
- * @returns {Promise<number>} The order ID
18
- */
19
- swap(from: Asset, to: Asset, amt: number, receiveAmount: number, opts?: {
20
- btcUserAddress?: string;
21
- }): Promise<number>;
22
- /**
23
- * Calculates the amount you receive. Currently deducts 0.3% of the amount you send.
24
- */
25
- calculateReceiveAmt(from: Asset, to: Asset, sendAmt: number): Promise<number>;
26
- /**
27
- * Subscribes to order updates
28
- *
29
- * @method
30
- * @param {string} address - The address to subscribe to, currently each CatalogJS instance can only connect to a single address
31
- * @param {(orders: Orders) => void} callback - The callback to call when the orders are updated. The first response are all the orders created by a given address
32
- *
33
- * @returns {void}
34
- */
35
- subscribeOrders(address: string, callback: (orders: Orders) => void): void;
36
- /**
37
- * Unsubscribes from order updates
38
- *
39
- * @method
40
- * @returns {void}
41
- */
42
- unsubscribeOrders(): void;
43
- /**
44
- * Gets the swapper for an order, which allows you to progess (init, redeem, refund) the swap
45
- *
46
- * @param {Order} order - The order to get the swapper for
47
- * @returns {ISwapper} The swapper
48
- */
49
- getSwap(order: Order): ISwapper;
50
- }
51
- export type Wallets<T extends Chain = Chain> = {
52
- [K in T]: K extends EvmChain ? IEVMWallet : IBitcoinWallet;
53
- };
@@ -1,35 +0,0 @@
1
- import { Actions, Order, Chain } from '@gardenfi/orderbook';
2
- import { IBaseWallet } from '@catalogfi/wallets';
3
-
4
- export interface ISwapper {
5
- id(): string;
6
- next(): Promise<SwapOutput>;
7
- }
8
- export declare class Swapper implements ISwapper {
9
- private order;
10
- private wallets;
11
- constructor(order: Order, wallet: Partial<Record<Chain, IBaseWallet>>);
12
- get action(): Actions;
13
- get status(): number;
14
- next(): Promise<SwapOutput>;
15
- private init;
16
- private redeem;
17
- private refund;
18
- id(): string;
19
- private getWallet;
20
- }
21
- export declare enum SwapperRole {
22
- INITIATOR = "initiator",
23
- REDEEMER = "redeemer"
24
- }
25
- export declare enum SwapperActions {
26
- Init = "Initiate",
27
- Redeem = "Redeem",
28
- Refund = "Refund",
29
- None = "None"
30
- }
31
- export type SwapOutput = {
32
- user: SwapperRole;
33
- action: SwapperActions;
34
- output: string;
35
- };