@tetherto/wdk-wallet 1.0.0-beta.1

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,272 @@
1
+ // Copyright 2024 Tether Operations Limited
2
+ //
3
+ // Licensed under the Apache License, Version 2.0 (the "License");
4
+ // you may not use this file except in compliance with the License.
5
+ // You may obtain a copy of the License at
6
+ //
7
+ // http://www.apache.org/licenses/LICENSE-2.0
8
+ //
9
+ // Unless required by applicable law or agreed to in writing, software
10
+ // distributed under the License is distributed on an "AS IS" BASIS,
11
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ // See the License for the specific language governing permissions and
13
+ // limitations under the License.
14
+ 'use strict'
15
+
16
+ import { NotImplementedError } from '../errors.js'
17
+
18
+ /** @typedef {import('../wallet-account-read-only.js').IWalletAccountReadOnly} IWalletAccountReadOnly */
19
+
20
+ /** @typedef {import('../wallet-account.js').IWalletAccount} IWalletAccount */
21
+
22
+ /**
23
+ * @typedef {Object} SupplyOptions
24
+ * @property {string} token - The address of the token to supply.
25
+ * @property {number | bigint} amount - The amount of tokens to supply (in base unit).
26
+ * @property {string} [onBehalfOf] - The address on behalf of which the supply operation should be performed. If not set, the supply operation will be performed on behalf of the account itself.
27
+ */
28
+
29
+ /**
30
+ * @typedef {Object} SupplyResult
31
+ * @property {string} hash - The hash of the supply operation.
32
+ * @property {bigint} fee - The gas cost.
33
+ */
34
+
35
+ /**
36
+ * @typedef {Object} WithdrawOptions
37
+ * @property {string} token - The address of the token to withdraw.
38
+ * @property {number | bigint} amount - The amount of tokens to withdraw (in base unit).
39
+ * @property {string} [to] - The address that should receive the tokens. If not set, the account itself will receive the funds.
40
+ */
41
+
42
+ /**
43
+ * @typedef {Object} WithdrawResult
44
+ * @property {string} hash - The hash of the withdraw operation.
45
+ * @property {bigint} fee - The gas cost.
46
+ */
47
+
48
+ /**
49
+ * @typedef {Object} BorrowOptions
50
+ * @property {string} token - The address of the token to borrow.
51
+ * @property {number | bigint} amount - The amount of tokens to borrow (in base unit).
52
+ * @property {string} [onBehalfOf] - The address on behalf of which the borrow operation should be performed. If not set, the borrow operation will be performed on behalf of the account itself.
53
+ */
54
+
55
+ /**
56
+ * @typedef {Object} BorrowResult
57
+ * @property {string} hash - The hash of the borrow operation.
58
+ * @property {bigint} fee - The gas cost.
59
+ */
60
+
61
+ /**
62
+ * @typedef {Object} RepayOptions
63
+ * @property {string} token - The address of the token to repay.
64
+ * @property {number | bigint} amount - The amount of tokens to repay (in base unit).
65
+ * @property {string} [onBehalfOf] - The address on behalf of which the repay operation should be performed. If not set, the repay operation will be performed on behalf of the account itself.
66
+ */
67
+
68
+ /**
69
+ * @typedef {Object} RepayResult
70
+ * @property {string} hash - The hash of the repay operation.
71
+ * @property {bigint} fee - The gas cost.
72
+ */
73
+
74
+ /** @interface */
75
+ export class ILendingProtocol {
76
+ /**
77
+ * Supplies a specific token amount to the lending pool.
78
+ *
79
+ * @param {SupplyOptions} options - The supply's options.
80
+ * @returns {Promise<SupplyResult>} The supply's result.
81
+ */
82
+ async supply (options) {
83
+ throw new NotImplementedError('supply(options)')
84
+ }
85
+
86
+ /**
87
+ * Quotes the costs of a supply operation.
88
+ *
89
+ * @param {SupplyOptions} options - The supply's options.
90
+ * @returns {Promise<Omit<SupplyResult, 'hash'>>} The supply's costs.
91
+ */
92
+ async quoteSupply (options) {
93
+ throw new NotImplementedError('quoteSupply(options)')
94
+ }
95
+
96
+ /**
97
+ * Withdraws a specific token amount from the pool.
98
+ *
99
+ * @param {WithdrawOptions} options - The withdraw's options.
100
+ * @returns {Promise<WithdrawResult>} The withdraw's result.
101
+ */
102
+ async withdraw (options) {
103
+ throw new NotImplementedError('withdraw(options)')
104
+ }
105
+
106
+ /**
107
+ * Quotes the costs of a withdraw operation.
108
+ *
109
+ * @param {WithdrawOptions} options - The withdraw's options.
110
+ * @returns {Promise<Omit<WithdrawResult, 'hash'>>} The withdraw's costs.
111
+ */
112
+ async quoteWithdraw (options) {
113
+ throw new NotImplementedError('quoteWithdraw(options)')
114
+ }
115
+
116
+ /**
117
+ * Borrows a specific token amount.
118
+ *
119
+ * @param {BorrowOptions} options - The borrow's options.
120
+ * @returns {Promise<BorrowResult>} The borrow's result.
121
+ */
122
+ async borrow (options) {
123
+ throw new NotImplementedError('borrow(options)')
124
+ }
125
+
126
+ /**
127
+ * Quotes the costs of a borrow operation.
128
+ *
129
+ * @param {BorrowOptions} options - The borrow's options.
130
+ * @returns {Promise<Omit<BorrowResult, 'hash'>>} The borrow's costs.
131
+ */
132
+ async quoteBorrow (options) {
133
+ throw new NotImplementedError('quoteBorrow(options)')
134
+ }
135
+
136
+ /**
137
+ * Repays a specific token amount.
138
+ *
139
+ * @param {RepayOptions} options - The borrow's options.
140
+ * @returns {Promise<RepayResult>} The repay's result.
141
+ */
142
+ async repay (options) {
143
+ throw new NotImplementedError('repay(options)')
144
+ }
145
+
146
+ /**
147
+ * Quotes the costs of a repay operation.
148
+ *
149
+ * @param {RepayOptions} options - The repay's options.
150
+ * @returns {Promise<Omit<RepayResult, 'hash'>>} The repay's costs.
151
+ */
152
+ async quoteRepay (options) {
153
+ throw new NotImplementedError('quoteRepay(options)')
154
+ }
155
+ }
156
+
157
+ /**
158
+ * @abstract
159
+ * @implements {ILendingProtocol}
160
+ */
161
+ export default class LendingProtocol {
162
+ /**
163
+ * Creates a new read-only lending protocol.
164
+ *
165
+ * @overload
166
+ * @param {IWalletAccountReadOnly} account - The wallet account to use to interact with the protocol.
167
+ */
168
+
169
+ /**
170
+ * Creates a new lending protocol.
171
+ *
172
+ * @overload
173
+ * @param {IWalletAccount} account - The wallet account to use to interact with the protocol.
174
+ */
175
+ constructor (account) {
176
+ /**
177
+ * The wallet account to use to interact with the protocol.
178
+ *
179
+ * @protected
180
+ * @type {IWalletAccountReadOnly | IWalletAccount}
181
+ */
182
+ this._account = account
183
+ }
184
+
185
+ /**
186
+ * Supplies a specific token amount to the lending pool.
187
+ *
188
+ * @abstract
189
+ * @param {SupplyOptions} options - The supply's options.
190
+ * @returns {Promise<SupplyResult>} The supply's result.
191
+ */
192
+ async supply (options) {
193
+ throw new NotImplementedError('supply(options)')
194
+ }
195
+
196
+ /**
197
+ * Quotes the costs of a supply operation.
198
+ *
199
+ * @abstract
200
+ * @param {SupplyOptions} options - The supply's options.
201
+ * @returns {Promise<Omit<SupplyResult, 'hash'>>} The supply's costs.
202
+ */
203
+ async quoteSupply (options) {
204
+ throw new NotImplementedError('quoteSupply(options)')
205
+ }
206
+
207
+ /**
208
+ * Withdraws a specific token amount from the pool.
209
+ *
210
+ * @abstract
211
+ * @param {WithdrawOptions} options - The withdraw's options.
212
+ * @returns {Promise<WithdrawResult>} The withdraw's result.
213
+ */
214
+ async withdraw (options) {
215
+ throw new NotImplementedError('withdraw(options)')
216
+ }
217
+
218
+ /**
219
+ * Quotes the costs of a withdraw operation.
220
+ *
221
+ * @abstract
222
+ * @param {WithdrawOptions} options - The withdraw's options.
223
+ * @returns {Promise<Omit<WithdrawResult, 'hash'>>} The withdraw's costs.
224
+ */
225
+ async quoteWithdraw (options) {
226
+ throw new NotImplementedError('quoteWithdraw(options)')
227
+ }
228
+
229
+ /**
230
+ * Borrows a specific token amount.
231
+ *
232
+ * @abstract
233
+ * @param {BorrowOptions} options - The borrow's options.
234
+ * @returns {Promise<BorrowResult>} The borrow's result.
235
+ */
236
+ async borrow (options) {
237
+ throw new NotImplementedError('borrow(options)')
238
+ }
239
+
240
+ /**
241
+ * Quotes the costs of a borrow operation.
242
+ *
243
+ * @abstract
244
+ * @param {BorrowOptions} options - The borrow's options.
245
+ * @returns {Promise<Omit<BorrowResult, 'hash'>>} The borrow's costs.
246
+ */
247
+ async quoteBorrow (options) {
248
+ throw new NotImplementedError('quoteBorrow(options)')
249
+ }
250
+
251
+ /**
252
+ * Repays a specific token amount.
253
+ *
254
+ * @abstract
255
+ * @param {RepayOptions} options - The borrow's options.
256
+ * @returns {Promise<RepayResult>} The repay's result.
257
+ */
258
+ async repay (options) {
259
+ throw new NotImplementedError('repay(options)')
260
+ }
261
+
262
+ /**
263
+ * Quotes the costs of a repay operation.
264
+ *
265
+ * @abstract
266
+ * @param {RepayOptions} options - The repay's options.
267
+ * @returns {Promise<Omit<RepayResult, 'hash'>>} The repay's costs.
268
+ */
269
+ async quoteRepay (options) {
270
+ throw new NotImplementedError('quoteRepay(options)')
271
+ }
272
+ }
@@ -0,0 +1,140 @@
1
+ // Copyright 2024 Tether Operations Limited
2
+ //
3
+ // Licensed under the Apache License, Version 2.0 (the "License");
4
+ // you may not use this file except in compliance with the License.
5
+ // You may obtain a copy of the License at
6
+ //
7
+ // http://www.apache.org/licenses/LICENSE-2.0
8
+ //
9
+ // Unless required by applicable law or agreed to in writing, software
10
+ // distributed under the License is distributed on an "AS IS" BASIS,
11
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ // See the License for the specific language governing permissions and
13
+ // limitations under the License.
14
+ 'use strict'
15
+
16
+ import { NotImplementedError } from '../errors.js'
17
+
18
+ /** @typedef {import('../wallet-account-read-only.js').IWalletAccountReadOnly} IWalletAccountReadOnly */
19
+
20
+ /** @typedef {import('../wallet-account.js').IWalletAccount} IWalletAccount */
21
+
22
+ /**
23
+ * @typedef {Object} SwapProtocolConfig
24
+ * @property {number | bigint} [swapMaxFee] - The maximum fee amount for swap operations.
25
+ */
26
+
27
+ /**
28
+ * @typedef {SwapCommonOptions & (SwapBuyOptions | SwapSellOptions)} SwapOptions
29
+ */
30
+
31
+ /**
32
+ * @typedef {Object} SwapCommonOptions
33
+ * @property {string} tokenIn - The address of the token to sell.
34
+ * @property {string} tokenOut - The address of the token to buy.
35
+ * @property {string} [to] - The address that will receive the output tokens. If not set, the account itself will receive the funds.
36
+ */
37
+
38
+ /**
39
+ * @typedef {Object} SwapBuyOptions
40
+ * @property {never} [tokenInAmount] - The amount of input tokens to sell (in base unit).
41
+ * @property {number | bigint} tokenOutAmount - The amount of output tokens to buy (in base unit).
42
+ */
43
+
44
+ /**
45
+ * @typedef {Object} SwapSellOptions
46
+ * @property {number | bigint} tokenInAmount - The amount of input tokens to sell (in base unit).
47
+ * @property {never} [tokenOutAmount] - The amount of output tokens to buy (in base unit).
48
+ */
49
+
50
+ /**
51
+ * @typedef {Object} SwapResult
52
+ * @property {string} hash - The hash of the swap operation.
53
+ * @property {bigint} fee - The gas cost.
54
+ * @property {bigint} tokenInAmount - The amount of input tokens sold.
55
+ * @property {bigint} tokenOutAmount - The amount of output tokens bought.
56
+ */
57
+
58
+ /** @interface */
59
+ export class ISwapProtocol {
60
+ /**
61
+ * Swaps a pair of tokens.
62
+ *
63
+ * @param {SwapOptions} options - The swap's options.
64
+ * @returns {Promise<SwapResult>} The swap's result.
65
+ */
66
+ async swap (options) {
67
+ throw new NotImplementedError('swap(options)')
68
+ }
69
+
70
+ /**
71
+ * Quotes the costs of a swap operation.
72
+ *
73
+ * @param {SwapOptions} options - The swap's options.
74
+ * @returns {Promise<Omit<SwapResult, 'hash'>>} The swap's quotes.
75
+ */
76
+ async quoteSwap (options) {
77
+ throw new NotImplementedError('quoteSwap(options)')
78
+ }
79
+ }
80
+
81
+ /**
82
+ * @abstract
83
+ * @implements {ISwapProtocol}
84
+ */
85
+ export default class SwapProtocol {
86
+ /**
87
+ * Creates a new read-only swap protocol.
88
+ *
89
+ * @overload
90
+ * @param {IWalletAccountReadOnly} account - The wallet account to use to interact with the protocol.
91
+ * @param {SwapProtocolConfig} [config] - The swap protocol configuration.
92
+ */
93
+
94
+ /**
95
+ * Creates a new swap protocol.
96
+ *
97
+ * @overload
98
+ * @param {IWalletAccount} account - The wallet account to use to interact with the protocol.
99
+ * @param {SwapProtocolConfig} [config] - The swap protocol configuration.
100
+ */
101
+ constructor (account, config = {}) {
102
+ /**
103
+ * The wallet account to use to interact with the protocol.
104
+ *
105
+ * @protected
106
+ * @type {IWalletAccountReadOnly | IWalletAccount}
107
+ */
108
+ this._account = account
109
+
110
+ /**
111
+ * The swap protocol configuration.
112
+ *
113
+ * @protected
114
+ * @type {SwapProtocolConfig}
115
+ */
116
+ this._config = config
117
+ }
118
+
119
+ /**
120
+ * Swaps a pair of tokens.
121
+ *
122
+ * @abstract
123
+ * @param {SwapOptions} options - The swap's options.
124
+ * @returns {Promise<SwapResult>} The swap's result.
125
+ */
126
+ async swap (options) {
127
+ throw new NotImplementedError('swap(options)')
128
+ }
129
+
130
+ /**
131
+ * Quotes the costs of a swap operation.
132
+ *
133
+ * @abstract
134
+ * @param {SwapOptions} options - The swap's options.
135
+ * @returns {Promise<Omit<SwapResult, 'hash'>>} The swap's quotes.
136
+ */
137
+ async quoteSwap (options) {
138
+ throw new NotImplementedError('quoteSwap(options)')
139
+ }
140
+ }
@@ -0,0 +1,195 @@
1
+ // Copyright 2024 Tether Operations Limited
2
+ //
3
+ // Licensed under the Apache License, Version 2.0 (the "License");
4
+ // you may not use this file except in compliance with the License.
5
+ // You may obtain a copy of the License at
6
+ //
7
+ // http://www.apache.org/licenses/LICENSE-2.0
8
+ //
9
+ // Unless required by applicable law or agreed to in writing, software
10
+ // distributed under the License is distributed on an "AS IS" BASIS,
11
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ // See the License for the specific language governing permissions and
13
+ // limitations under the License.
14
+ 'use strict'
15
+
16
+ import { NotImplementedError } from './errors.js'
17
+
18
+ /**
19
+ * @typedef {Object} Transaction
20
+ * @property {string} to - The transaction's recipient.
21
+ * @property {number | bigint} value - The amount of native tokens to send to the recipient (in base unit).
22
+ */
23
+
24
+ /**
25
+ * @typedef {Object} TransactionResult
26
+ * @property {string} hash - The transaction's hash.
27
+ * @property {bigint} fee - The gas cost.
28
+ */
29
+
30
+ /**
31
+ * @typedef {Object} TransferOptions
32
+ * @property {string} token - The address of the token to transfer.
33
+ * @property {string} recipient - The address of the recipient.
34
+ * @property {number | bigint} amount - The amount of tokens to transfer to the recipient (in base units).
35
+ */
36
+
37
+ /**
38
+ * @typedef {Object} TransferResult
39
+ * @property {string} hash - The hash of the transfer operation.
40
+ * @property {bigint} fee - The gas cost.
41
+ */
42
+
43
+ /** @interface */
44
+ export class IWalletAccountReadOnly {
45
+ /**
46
+ * Returns the account's address.
47
+ *
48
+ * @returns {Promise<string>} The account's address.
49
+ */
50
+ async getAddress () {
51
+ throw new NotImplementedError('getAddress()')
52
+ }
53
+
54
+ /**
55
+ * Returns the account's native token balance.
56
+ *
57
+ * @returns {Promise<bigint>} The native token balance.
58
+ */
59
+ async getBalance () {
60
+ throw new NotImplementedError('getBalance()')
61
+ }
62
+
63
+ /**
64
+ * Returns the account balance for a specific token.
65
+ *
66
+ * @param {string} tokenAddress - The smart contract address of the token.
67
+ * @returns {Promise<bigint>} The token balance.
68
+ */
69
+ async getTokenBalance (tokenAddress) {
70
+ throw new NotImplementedError('getTokenBalance(tokenAddress)')
71
+ }
72
+
73
+ /**
74
+ * Quotes the costs of a send transaction operation.
75
+ *
76
+ * @param {Transaction} tx - The transaction.
77
+ * @returns {Promise<Omit<TransactionResult, 'hash'>>} The transaction's quotes.
78
+ */
79
+ async quoteSendTransaction (tx) {
80
+ throw new NotImplementedError('quoteSendTransaction(tx)')
81
+ }
82
+
83
+ /**
84
+ * Quotes the costs of a transfer operation.
85
+ *
86
+ * @param {TransferOptions} options - The transfer's options.
87
+ * @returns {Promise<Omit<TransferResult, 'hash'>>} The transfer's quotes.
88
+ */
89
+ async quoteTransfer (options) {
90
+ throw new NotImplementedError('quoteTransfer(options)')
91
+ }
92
+
93
+ /**
94
+ * Returns a transaction's receipt.
95
+ *
96
+ * @param {string} hash - The transaction's hash.
97
+ * @returns {Promise<unknown | null>} The receipt, or null if the transaction has not been included in a block yet.
98
+ */
99
+ async getTransactionReceipt (hash) {
100
+ throw new NotImplementedError('getTransactionReceipt(hash)')
101
+ }
102
+ }
103
+
104
+ /**
105
+ * @abstract
106
+ * @implements {IWalletAccountReadOnly}
107
+ */
108
+ export default class WalletAccountReadOnly {
109
+ /**
110
+ * Creates a new read-only wallet account.
111
+ *
112
+ * @param {string} [address] - The account's address.
113
+ */
114
+ constructor (address) {
115
+ /** @private */
116
+ this.__address = address
117
+ }
118
+
119
+ /**
120
+ * The account's address.
121
+ *
122
+ * @protected
123
+ * @type {string | undefined}
124
+ */
125
+ get _address () {
126
+ return this.__address
127
+ }
128
+
129
+ /**
130
+ * Returns the account's address.
131
+ *
132
+ * @returns {Promise<string>} The account's address.
133
+ */
134
+ async getAddress () {
135
+ if (!this._address) {
136
+ throw new Error("The account's address must be set to perform this operation.")
137
+ }
138
+
139
+ return this._address
140
+ }
141
+
142
+ /**
143
+ * Returns the account's native token balance.
144
+ *
145
+ * @abstract
146
+ * @returns {Promise<bigint>} The native token balance.
147
+ */
148
+ async getBalance () {
149
+ throw new NotImplementedError('getBalance()')
150
+ }
151
+
152
+ /**
153
+ * Returns the account balance for a specific token.
154
+ *
155
+ * @abstract
156
+ * @param {string} tokenAddress - The smart contract address of the token.
157
+ * @returns {Promise<bigint>} The token balance.
158
+ */
159
+ async getTokenBalance (tokenAddress) {
160
+ throw new NotImplementedError('getTokenBalance(tokenAddress)')
161
+ }
162
+
163
+ /**
164
+ * Quotes the costs of a send transaction operation.
165
+ *
166
+ * @abstract
167
+ * @param {Transaction} tx - The transaction.
168
+ * @returns {Promise<Omit<TransactionResult, 'hash'>>} The transaction's quotes.
169
+ */
170
+ async quoteSendTransaction (tx) {
171
+ throw new NotImplementedError('quoteSendTransaction(tx)')
172
+ }
173
+
174
+ /**
175
+ * Quotes the costs of a transfer operation.
176
+ *
177
+ * @abstract
178
+ * @param {TransferOptions} options - The transfer's options.
179
+ * @returns {Promise<Omit<TransferResult, 'hash'>>} The transfer's quotes.
180
+ */
181
+ async quoteTransfer (options) {
182
+ throw new NotImplementedError('quoteTransfer(options)')
183
+ }
184
+
185
+ /**
186
+ * Returns a transaction's receipt.
187
+ *
188
+ * @abstract
189
+ * @param {string} hash - The transaction's hash.
190
+ * @returns {Promise<unknown | null>} The receipt, or null if the transaction has not been included in a block yet.
191
+ */
192
+ async getTransactionReceipt (hash) {
193
+ throw new NotImplementedError('getTransactionReceipt(hash)')
194
+ }
195
+ }