@axis-markets/client 0.3.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.
package/src/index.js ADDED
@@ -0,0 +1,225 @@
1
+ import {Networks} from '@stellar/stellar-sdk'
2
+ import ContractClient from './contract-client.js'
3
+ import {ContractErrors, processSimulationErrors} from './errors.js'
4
+ import {ApiClient, AxisApiError} from './api-client.js'
5
+
6
+ export {ContractErrors, ApiClient, AxisApiError}
7
+
8
+ /** Trading order type - instructions to contract how to execute the trade */
9
+ export const OrderKind = {
10
+ /** Execute trade, create a limit order if not executed in full */
11
+ Limit: 1,
12
+ /** Execute trade without creating a limit order */
13
+ Fill: 2,
14
+ /** Execute trade, cancel if was not executed in full */
15
+ FillOrKill: 3
16
+ }
17
+
18
+ /** Trade direction instructions */
19
+ export const TradeDirection = {
20
+ Sell: 1,
21
+ Buy: 2
22
+ }
23
+
24
+ /**
25
+ * Smart contract client for trading with AXIS DEX
26
+ */
27
+ export class AxisContractClient {
28
+ /**
29
+ * @param {ClientInitializationParams} params
30
+ */
31
+ constructor(params) {
32
+ const options = {
33
+ publicKey: params.publicKey,
34
+ signTransaction: params.signTransaction,
35
+ rpcUrl: params.rpcUrl,
36
+ allowHttp: true,
37
+ networkPassphrase: params.networkPassphrase || Networks.PUBLIC,
38
+ contractId: params.contractId
39
+ }
40
+ this.client = new ContractClient(options)
41
+ this.fee = params.fee || '100000'
42
+ }
43
+
44
+ /**
45
+ * Retrieve last order id
46
+ * @return {Promise<bigint>} - Last created order id
47
+ */
48
+ async last() {
49
+ const tx = await this.client.last()
50
+ processSimulationErrors(tx)
51
+ return tx.result
52
+ }
53
+
54
+ /**
55
+ * @param {bigint} id - ID of the order to fetch
56
+ * @return {Promise<Order>} - Order fetched from the storage
57
+ */
58
+ async order(id) {
59
+ const tx = await this.client.order({id})
60
+ processSimulationErrors(tx)
61
+ return tx.result
62
+ }
63
+
64
+ /**
65
+ * Cancel existing order
66
+ * @param {bigint[]} ids - ID of the order to cancel
67
+ * @param {string} trader - Trader address
68
+ * @return {Promise<void>}
69
+ * @throws {Error} - If trader is not the owner of the order
70
+ */
71
+ async cancel(ids, trader) {
72
+ const tx = await this.client.cancel({ids, trader}, {fee: this.fee})
73
+ processSimulationErrors(tx)
74
+ await tx.signAndSend()
75
+ }
76
+
77
+ /**
78
+ * Fill existing orders using another matching order from the orderbook
79
+ * @param {string} trader - Trader address
80
+ * @param {bigint} takerOrderId - ID of the order that serves as a taker
81
+ * @param {bigint[]} orders - List of order IDs to match before creating the order on chain
82
+ * @return {Promise<[bigint, bigint]>} - Amount of sold and bought tokens
83
+ * @throws {Error} - If the taker order was not found
84
+ * @throws {Error} - If any of the orders provided do not match selling/buying asset
85
+ * @throws {Error} - If the trade causes an overflow
86
+ */
87
+ async fill_order(trader, takerOrderId, orders) {
88
+ const tx = await this.client.fill_order({trader, taker_order_id: takerOrderId, orders}, {fee: this.fee})
89
+ processSimulationErrors(tx)
90
+ const res = await tx.signAndSend()
91
+ return [res.result.sold, res.result.bought]
92
+ }
93
+
94
+ /**
95
+ * Trade with DEX and create buy limit order if quote not executed in full
96
+ * @param {BuyTradeArguments} params
97
+ * @return {Promise<[bigint, bigint, bigint]>} - Amount of sold and bought tokens, ID of the newly created order if any
98
+ * @throws {Error} - If the trader has insufficient balance
99
+ * @throws {Error} - If any of the orders provided do not match selling/buying asset
100
+ * @throws {Error} - If the trade causes an overflow
101
+ */
102
+ async buy(params) {
103
+ return await execTrade(params, TradeDirection.Buy, this)
104
+ }
105
+
106
+ /**
107
+ * Trade with DEX and create sell limit order if quote not executed in full
108
+ * @param {SellTradeArguments} params
109
+ * @return {Promise<[bigint, bigint, bigint]>} - Amount of sold and bought tokens, ID of the newly created order if any
110
+ * @throws {Error} - If the trader has insufficient balance
111
+ * @throws {Error} - If any of the orders provided do not match selling/buying asset
112
+ * @throws {Error} - If the trade causes an overflow
113
+ */
114
+ async sell(params) {
115
+ return await execTrade(params, TradeDirection.Sell, this)
116
+ }
117
+
118
+ async swap(params) {
119
+ const tx = await this.client.swap({
120
+ direction: params.direction,
121
+ trader: params.trader,
122
+ selling: params.selling,
123
+ selling_amount: params.sellingAmount,
124
+ buying_amount: params.buyingAmount,
125
+ path: params.path.map(step => ({asset: step.asset, orders: step.orders}))
126
+ }, {fee: this.fee})
127
+ processSimulationErrors(tx)
128
+ const res = await tx.signAndSend()
129
+ return [res.result.sold, res.result.bought]
130
+ }
131
+ }
132
+
133
+ async function execTrade(params, direction, context) {
134
+ const payload = {
135
+ direction: direction,
136
+ kind: params.kind,
137
+ trader: params.trader,
138
+ amount: params.amount,
139
+ selling: params.selling,
140
+ buying: params.buying,
141
+ price: params.price,
142
+ orders: params.orders
143
+ }
144
+ const tx = await context.client.trade(payload, {fee: context.fee})
145
+ processSimulationErrors(tx)
146
+ const {result} = await tx.signAndSend()
147
+ return [result[0], result[1], result[2]]
148
+ }
149
+
150
+ /**
151
+ * @typedef {{}} ClientInitializationParams
152
+ * @property {string} publicKey - Public key of the account that will interact with the contract
153
+ * @property {SignTransactionCallback} signTransaction - Callback for signing transactions generated by the client
154
+ * @property {string} rpcUrl - URL of the RPC server
155
+ * @property {string} contractId - DEX contract ID
156
+ * @property {string} [networkPassphrase] - Network passphrase (Pubnet passphrase by default)
157
+ * @property {string} [fee] - Transaction fee (0.1 XLM by default)
158
+ */
159
+
160
+ /**
161
+ * @callback SignTransactionCallback - Callback for signing transactions generated by the client
162
+ * @param {string} tx - Transaction XDR to sign
163
+ * @param {{network: string, networkPassphrase: string, accountToSign: string}} context - Signing context
164
+ * @return {Promise<{signedTxXdr: string, [signerAddress]: string}>} - Signed transaction response
165
+ */
166
+
167
+ /**
168
+ * @typedef {{}} TradeArguments
169
+ * @property {OrderKind} kind - Trading order behavior
170
+ * @property {string} trader - Trader address
171
+ * @property {bigint} amount - Tokens amount
172
+ * @property {string} selling - Selling token address
173
+ * @property {string} buying - Buying token address
174
+ * @property {bigint} price - Price a trader willing to accept
175
+ * @property {Array<bigint>} orders - List of order IDs to match before creating the order on-chain
176
+ */
177
+
178
+ /**
179
+ * @typedef {{}} SellTradeArguments
180
+ * @extends TradeArguments
181
+ * @property {bigint} amount - Amount of `selling` tokens to sell
182
+ * @property {bigint} price - Price a trader willing to accept, minimum `buying` tokens per 1 `selling`
183
+ */
184
+
185
+ /**
186
+ * @typedef {{}} BuyTradeArguments
187
+ * @extends TradeArguments
188
+ * @property {bigint} amount - Amount of `buying` tokens to acquire
189
+ * @property {bigint} price - Price a trader willing to accept, maximum `selling` tokens per 1 `buying`
190
+ */
191
+
192
+ /**
193
+ * @typedef {{}} TradeStep - A trade step in a multi-market swap path
194
+ * @property {string} asset - Asset to buy at this step
195
+ * @property {Array<bigint>} orders - Maker order IDs to match
196
+ */
197
+
198
+ /**
199
+ * @typedef {{}} SwapArguments
200
+ * @property {TradeDirection} direction - Trade direction: `Sell` or `Buy`
201
+ * @property {string} trader - Trader address
202
+ * @property {string} selling - Token address sent by the trader
203
+ * @property {bigint} selling_amount - Maximum amount of selling tokens to send
204
+ * @property {bigint} buying_amount - Minimum amount of buying tokens to receive
205
+ * @property {Array<TradeStep>} path - Ordered list of the trade route steps
206
+ */
207
+
208
+ /**
209
+ * @typedef {{}} MethodOptions
210
+ * @property {string} [fee] - Transaction fee (0.1 XLM by default)
211
+ */
212
+
213
+ /**
214
+ * @typedef {{}} Order - Order properties, stored on-chain
215
+ * @property {bigint} id - Order ID
216
+ * @property {OrderKind} kind - Trading behavior
217
+ * @property {string} owner - Owner of the order
218
+ * @property {string} selling - Selling token address
219
+ * @property {string} buying - Buying token address
220
+ * @property {bigint} amount - Amount of tokens to sell
221
+ * @property {bigint} price - Price of the order
222
+ * @property {bigint} quote - Total amount of tokens to buy
223
+ * @property {bigint} expires - Expiration time of the order
224
+ */
225
+