@ref-finance/ref-sdk 1.1.3

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.
Files changed (39) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +1723 -0
  3. package/dist/constant.d.ts +44 -0
  4. package/dist/dcl-swap/dcl-pool.d.ts +24 -0
  5. package/dist/dcl-swap/limit-order.d.ts +32 -0
  6. package/dist/dcl-swap/swap.d.ts +46 -0
  7. package/dist/error.d.ts +20 -0
  8. package/dist/index.d.ts +15 -0
  9. package/dist/index.js +8 -0
  10. package/dist/indexer.d.ts +3 -0
  11. package/dist/metaIcons.d.ts +4 -0
  12. package/dist/near.d.ts +16 -0
  13. package/dist/ref-sdk.cjs.development.css +296 -0
  14. package/dist/ref-sdk.cjs.development.js +8893 -0
  15. package/dist/ref-sdk.cjs.development.js.map +1 -0
  16. package/dist/ref-sdk.cjs.production.min.js +2 -0
  17. package/dist/ref-sdk.cjs.production.min.js.map +1 -0
  18. package/dist/ref-sdk.esm.js +8771 -0
  19. package/dist/ref-sdk.esm.js.map +1 -0
  20. package/dist/ref-sdk.umd.development.js +8908 -0
  21. package/dist/ref-sdk.umd.development.js.map +1 -0
  22. package/dist/ref-sdk.umd.production.min.js +2 -0
  23. package/dist/ref-sdk.umd.production.min.js.map +1 -0
  24. package/dist/ref.d.ts +19 -0
  25. package/dist/stable-swap.d.ts +5 -0
  26. package/dist/swap-widget/components.d.ts +90 -0
  27. package/dist/swap-widget/constant.d.ts +25 -0
  28. package/dist/swap-widget/defaultTokenList.d.ts +120 -0
  29. package/dist/swap-widget/index.d.ts +4 -0
  30. package/dist/swap-widget/state.d.ts +49 -0
  31. package/dist/swap-widget/types.d.ts +40 -0
  32. package/dist/types.d.ts +129 -0
  33. package/dist/utils.d.ts +92 -0
  34. package/dist/v1-swap/instantSwap.d.ts +9 -0
  35. package/dist/v1-swap/parallelSwapLogic.d.ts +84 -0
  36. package/dist/v1-swap/pool.d.ts +16 -0
  37. package/dist/v1-swap/smartRoutingLogic.d.ts +4 -0
  38. package/dist/v1-swap/swap.d.ts +239 -0
  39. package/package.json +81 -0
package/README.md ADDED
@@ -0,0 +1,1723 @@
1
+ # Ref SDK
2
+
3
+ This SDK is designed to assist developers when interacting with the main functions of the protocol. Main functions that can be defined as:
4
+
5
+ - Trade: Swap tokens with our Automated Market Maker (AMM)
6
+ - Pool: Add/Remove liquidity and earn revenue from swap fee (Coming soon)
7
+ - Farm: Stale LP tokens into farms and earn liquidity incentives (Coming soon)
8
+ - Boost Farm: Stake LOVE tokens to get boosted liquidity incentives (Coming soon)
9
+ - Stake: Stake REF tokens to earn fees generated by the protocol (Coming soon)
10
+ - Vote: Lock REF<>NEAR LP tokens to get veTokens and participate in the governance of the protocol and the allocation of liquidity incentives (Coming soon)
11
+
12
+ ## Install
13
+
14
+ yarn: `yarn add @ref-finance/ref-sdk`
15
+
16
+ npm: `npm install @ref-finance/ref-sdk`
17
+
18
+ ## Initialization
19
+
20
+ Ref SDK identifies env variable NEAR_ENV or REACT_APP_REF_SDK_ENV to get global configuration.
21
+
22
+ ```plain
23
+ export function getConfig(
24
+ env: string | undefined = process.env.NEAR_ENV ||
25
+ process.env.REACT_APP_REF_SDK_ENV
26
+ ) {
27
+ switch (env) {
28
+ case 'mainnet':
29
+ return {
30
+ networkId: 'mainnet',
31
+ nodeUrl: 'https://rpc.mainnet.near.org',
32
+ walletUrl: 'https://wallet.near.org',
33
+ WRAP_NEAR_CONTRACT_ID: 'wrap.near',
34
+ REF_FI_CONTRACT_ID: 'v2.ref-finance.near',
35
+ REF_TOKEN_ID: 'token.v2.ref-finance.near',
36
+ indexerUrl: 'https://indexer.ref.finance',
37
+ explorerUrl: 'https://testnet.nearblocks.io',
38
+ REF_DCL_SWAP_CONTRACT_ID: '',
39
+ };
40
+ case 'testnet':
41
+ return {
42
+ networkId: 'testnet',
43
+ nodeUrl: 'https://rpc.testnet.near.org',
44
+ walletUrl: 'https://wallet.testnet.near.org',
45
+ indexerUrl: 'https://testnet-indexer.ref-finance.com',
46
+ WRAP_NEAR_CONTRACT_ID: 'wrap.testnet',
47
+ REF_FI_CONTRACT_ID: 'ref-finance-101.testnet',
48
+ REF_TOKEN_ID: 'ref.fakes.testnet',
49
+ explorerUrl: 'https://testnet.nearblocks.io',
50
+ REF_DCL_SWAP_CONTRACT_ID: 'dcl.ref-dev.testnet',
51
+ };
52
+ default:
53
+ return {
54
+ networkId: 'mainnet',
55
+ nodeUrl: 'https://rpc.mainnet.near.org',
56
+ walletUrl: 'https://wallet.near.org',
57
+ REF_FI_CONTRACT_ID: 'v2.ref-finance.near',
58
+ WRAP_NEAR_CONTRACT_ID: 'wrap.near',
59
+ REF_TOKEN_ID: 'token.v2.ref-finance.near',
60
+ indexerUrl: 'https://indexer.ref.finance',
61
+ explorerUrl: 'https://nearblocks.io',
62
+ REF_DCL_SWAP_CONTRACT_ID: '',
63
+ };
64
+ }
65
+ }
66
+
67
+ ```
68
+
69
+ ## Ref V1 Swap
70
+
71
+ ### Tokens
72
+
73
+ #### ftGetTokenMetadata
74
+
75
+ Get token metadata.
76
+
77
+ Parameters
78
+
79
+ ```plain
80
+ id: string;
81
+ ```
82
+
83
+ Example
84
+
85
+ ```plain
86
+ const WrapNear = await ftGetTokenMetadata('wrap.testnet');
87
+ ```
88
+
89
+ Response
90
+
91
+ ```plain
92
+ {
93
+ decimals: 24;
94
+ icon: null;
95
+ id: 'wrap.testnet';
96
+ name: 'Wrapped NEAR fungible token';
97
+ reference: null;
98
+ reference_hash: null;
99
+ spec: 'ft-1.0.0';
100
+ symbol: 'wNEAR';
101
+ }
102
+ ```
103
+
104
+ ---
105
+
106
+ #### ftGetTokensMetadata
107
+
108
+ Get tokens metadata and set token id as index.
109
+
110
+ Parameters
111
+
112
+ ```plain
113
+ tokenIds: string[]
114
+ ```
115
+
116
+ Example
117
+
118
+ ```plain
119
+ const tokensMetadata = await ftGetTokensMetadata([
120
+ 'ref.fakes.testnet',
121
+ 'wrap.testnet',
122
+ ]);
123
+ ```
124
+
125
+ Response
126
+
127
+ ```plain
128
+ {
129
+ "ref.fakes.testnet":{
130
+ decimals: 18
131
+ icon: "data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='16 24 248 248' style='background: %23000'%3E%3Cpath d='M164,164v52h52Zm-45-45,20.4,20.4,20.6-20.6V81H119Zm0,18.39V216h41V137.19l-20.6,20.6ZM166.5,81H164v33.81l26.16-26.17A40.29,40.29,0,0,0,166.5,81ZM72,153.19V216h43V133.4l-11.6-11.61Zm0-18.38,31.4-31.4L115,115V81H72ZM207,121.5h0a40.29,40.29,0,0,0-7.64-23.66L164,133.19V162h2.5A40.5,40.5,0,0,0,207,121.5Z' fill='%23fff'/%3E%3Cpath d='M189 72l27 27V72h-27z' fill='%2300c08b'/%3E%3C/svg%3E%0A"
132
+ id: "ref.fakes.testnet"
133
+ name: "Ref Finance Token"
134
+ reference: null
135
+ reference_hash: null
136
+ spec: "ft-1.0.0"
137
+ symbol: "REF"
138
+ },
139
+ "wrap.testnet":{
140
+ decimals: 24
141
+ icon: null
142
+ id: "wrap.testnet"
143
+ name: "Wrapped NEAR fungible token"
144
+ reference: null
145
+ reference_hash: null
146
+ spec: "ft-1.0.0"
147
+ symbol: "wNEAR"
148
+ }
149
+ }
150
+ ```
151
+
152
+ ### Pools
153
+
154
+ #### fetchAllPools
155
+
156
+ Fetch all existing pools, including vanilla/simple pools, stable pools and rated pools (designed for yield-bearing tokens).
157
+
158
+ Parameters
159
+
160
+ None
161
+
162
+ Example
163
+
164
+ ```plain
165
+ const { ratedPools, unRatedPools, simplePools } = await fetchAllPools();
166
+ ```
167
+
168
+ Response
169
+
170
+ ```plain
171
+ {
172
+ ratedPools:[{
173
+ fee: 5,
174
+ id: 568,
175
+ pool_kind: "RATED_SWAP",
176
+ shareSupply: "80676034815429711745720012070",
177
+ supplies:{
178
+ "meta-v2.pool.testnet": "1298314415249170366960739764"
179
+ "wrap.testnet": "80182803630538035347294614770"
180
+ },
181
+ token0_ref_price: undefined,
182
+ tokenIds: ["meta-v2.pool.testnet", "wrap.testnet"],
183
+ tvl: undefined
184
+ },...]
185
+ unRatedPools:[...],
186
+ simplePools:[...],
187
+ }
188
+ ```
189
+
190
+ ---
191
+
192
+ #### getStablePools
193
+
194
+ We define `unRatedPools` and `ratedPools` as `stablePool`. You can use this function to get details of stable pools.
195
+
196
+ Parameters
197
+
198
+ ```plain
199
+ stablePools: Pool[]
200
+ ```
201
+
202
+ Example
203
+
204
+ ```plain
205
+ const stablePools: Pool[] = unRatedPools.concat(ratedPools);
206
+
207
+ const stablePoolsDetail: StablePool[] = await getStablePools(stablePools);
208
+ ```
209
+
210
+ Response
211
+
212
+ ```plain
213
+ [
214
+ {
215
+ amounts: ['1298314415249170366960739764', '80182803630538035347294614770'],
216
+ amp: 240,
217
+ c_amounts:["1298314415249170366960739764","80182803630538035347294614770"],
218
+ decimals:[24,24],
219
+ id: 568,
220
+ pool_kind: "RATED_SWAP",
221
+ rates:["1972101024157559347385372","1000000000000000000000000"],
222
+ shares_total_supply: "80676034815429711745720012070",
223
+ token_account_ids:["meta-v2.pool.testnet","wrap.testnet"],
224
+ total_fee:5
225
+ },
226
+ ...
227
+ ]
228
+ ```
229
+
230
+ ---
231
+
232
+ ### Swap
233
+
234
+ #### estimateSwap
235
+
236
+ Get token output amount and corresponding route.
237
+
238
+ As there is a memory limitation on Ledger, note that we set `enableSmartRouting` option for developers.
239
+
240
+ This function integrates a smart routing algorithm, designed to deliver the best output token amount.
241
+
242
+ Parameters
243
+
244
+ ```plain
245
+ interface SwapParams {
246
+ tokenIn: TokenMetadata;
247
+ tokenOut: TokenMetadata;
248
+ amountIn: string;
249
+ simplePools: Pool[];
250
+ options?: SwapOptions;
251
+ }
252
+
253
+ interface SwapOptions {
254
+ enableSmartRouting?: boolean;
255
+ stablePools?: Pool[];
256
+ stablePoolsDetail?: StablePool[];
257
+ }
258
+ ```
259
+
260
+ Example (enableSmartRouting == false)
261
+
262
+ ```plain
263
+ // enableSmartRouting as FALSE, swap from Ref to wNear, with amount 1
264
+ const tokenIn = await ftGetTokenMetadata('ref.fakes.testnet');
265
+ const tokenOut = await ftGetTokenMetadata('wrap.testnet');
266
+
267
+ const swapTodos: EstimateSwapView[] = await estimateSwap({
268
+ tokenIn,
269
+ tokenOut,
270
+ amountIn: '1',
271
+ simplePools,
272
+ });
273
+ ```
274
+
275
+ Response (enableSmartRouting == false)
276
+
277
+ ```plain
278
+ // enableSmartRouting as FALSE, swap from Ref to wNear, with amount 1
279
+
280
+ [
281
+ {
282
+ estimate: '0.7338604246699393',
283
+ inputToken: 'ref.fakes.testnet',
284
+ outputToken: 'wrap.testnet',
285
+ pool: {
286
+ fee: 30,
287
+ id: 38,
288
+ partialAmountIn: '1000000000000000000',
289
+ pool_kind: 'SIMPLE_POOL',
290
+ shareSupply: '1000587315520795219676332',
291
+ supplies: {
292
+ 'ref.fakes.testnet': '7789776060978885018',
293
+ 'wrap.testnet': '6467670222256390319335181',
294
+ },
295
+ token0_ref_price: undefined,
296
+ tokenIds: (2)[('ref.fakes.testnet', 'wrap.testnet')],
297
+ tvl: undefined,
298
+ },
299
+ },
300
+ ];
301
+ ```
302
+
303
+ Example (enableSmartRouting == true)
304
+
305
+ ```plain
306
+ // enableSmartRouting as TRUE, swap from Ref to wNear, with amount 1
307
+ const tokenIn = await ftGetTokenMetadata('ref.fakes.testnet');
308
+ const tokenOut = await ftGetTokenMetadata('wrap.testnet');
309
+
310
+ const options: SwapOptions = {
311
+ enableSmartRouting: true,
312
+ stablePools,
313
+ stablePoolsDetail,
314
+ };
315
+
316
+ const swapTodos: EstimateSwapView[] = await estimateSwap({
317
+ tokenIn,
318
+ tokenOut,
319
+ amountIn: '1',
320
+ simplePools,
321
+ options,
322
+ });
323
+ ```
324
+
325
+ Response (enableSmartRouting == true)
326
+
327
+ ```plain
328
+ // enableSmartRouting as true, swap from Ref to wNear, with amount 1
329
+ [
330
+ {
331
+ estimate: "0.000225321544275095902371355566972009167",
332
+ inputToken: "ref.fakes.testnet",
333
+ outputToken: "nusdt.ft-fin.testnet",
334
+ pool:{
335
+ id: 341,
336
+ partialAmountIn: "836859596261755688",
337
+ tokenIds: ["ref.fakes.testnet","nusdt.ft-fin.testnet"],
338
+ ...
339
+ },
340
+ ...
341
+ },{
342
+ estimate: "0.5203255327171591155634413370660973429264",
343
+ inputToken: "nusdt.ft-fin.testnet",
344
+ outputToken: "wrap.testnet",
345
+ pool:{
346
+ id: 1625,
347
+ tokenIds: ["nusdt.ft-fin.testnet","wrap.testnet"],
348
+ ...
349
+ },
350
+ ...
351
+ },{
352
+ estimate: "5.3206339674140066489000963525772735539612",
353
+ inputToken: "ref.fakes.testnet",
354
+ outputToken: "usdn.testnet",
355
+ pool:{
356
+ id: 376,
357
+ tokenIds: ["usdn.testnet","ref.fakes.testnet"],
358
+ partialAmountIn: "163140403738244312",
359
+ ...
360
+ },
361
+ ...
362
+ },{
363
+ estimate: "0.2036473132202839680683206178037243543302",
364
+ inputToken: "usdn.testnet",
365
+ outputToken: "wrap.testnet",
366
+ pool:{
367
+ id: 385,
368
+ tokenIds: ["usdn.testnet","wrap.testnet"],
369
+ ...
370
+ },
371
+ ...
372
+ }
373
+ ]
374
+ ```
375
+
376
+ ---
377
+
378
+ #### getExpectedOutputFromSwapTodos
379
+
380
+ Get token output amount from swapTodos.
381
+
382
+ Parameters
383
+
384
+ ```plain
385
+ (swapTodos: EstimateSwapView[], outputToken: string)
386
+ ```
387
+
388
+ Example
389
+
390
+ ```plain
391
+ const swapTodos: EstimateSwapView[] = await estimateSwap({
392
+ tokenIn,
393
+ tokenOut,
394
+ amountIn: '1',
395
+ simplePools,
396
+ options,
397
+ });
398
+
399
+ const amountOut:string = getExpectedOutputFromSwapTodos(swapTodos, tokenOut.id);
400
+ ```
401
+
402
+ Response
403
+
404
+ ```plain
405
+ "0.723972845937443"
406
+ ```
407
+
408
+ ---
409
+
410
+ #### getPoolEstimate
411
+
412
+ Get token output amount from one single pool. Method can be used for simple and stable pools.
413
+
414
+ Parameters
415
+
416
+ ```plain
417
+ {
418
+ tokenIn: TokenMetadata;
419
+ tokenOut: TokenMetadata;
420
+ amountIn: string;
421
+ pool: Pool;
422
+ // please input stablePoolDetail if you want estimate output on stable pool or the pool will be recognized as simple pool
423
+ stablePoolDetail?: StablePool;
424
+ }
425
+ ```
426
+
427
+ Example (on simple pool)
428
+
429
+ ```plain
430
+ // estimate on simple Pool
431
+ const estimate = await getPoolEstimate({
432
+ tokenIn,
433
+ tokenOut,
434
+ amountIn: '1',
435
+ pool,
436
+ });
437
+ ```
438
+
439
+ Response (on simple pool)
440
+
441
+ ```plain
442
+ // estimate on simple pool, swap from Ref to wNear, with amount 1
443
+ {
444
+ estimate: "0.7338604246699393",
445
+ inputToken: "ref.fakes.testnet",
446
+ outputToken: "wrap.testnet",
447
+ pool:{
448
+ fee: 30,
449
+ id: 38,
450
+ partialAmountIn: "1000000000000000000",
451
+ pool_kind: "SIMPLE_POOL",
452
+ shareSupply: "1000587315520795219676332",
453
+ supplies: {"ref.fakes.testnet": '7789776060978885018', "wrap.testnet": '6467670222256390319335181'},
454
+ token0_ref_price: undefined,
455
+ tokenIds: (2) ['ref.fakes.testnet', 'wrap.testnet'],
456
+ tvl: undefined
457
+ }
458
+ }
459
+ ```
460
+
461
+ Example (on stable pool)
462
+
463
+ ```plain
464
+ // estimate on stable pool
465
+ const estimate = await getPoolEstimate({
466
+ tokenIn,
467
+ tokenOut,
468
+ amountIn: '1',
469
+ pool: stablePool,
470
+ stablePoolDetail: stablePoolDetail,
471
+ });
472
+ ```
473
+
474
+ Response (on stable pool)
475
+
476
+ ```plain
477
+ // estimate on stable pool, swap from stNear to wNear, with amount 1
478
+ {
479
+ estimate: "2.4898866773442284",
480
+ inputToken: "meta-v2.pool.testnet",
481
+ noFeeAmountOut: "2.491132243465961",
482
+ outputToken: "wrap.testnet",
483
+ pool:{
484
+ amounts:["1298314415249170366960739764","80182803630538035347294614770"],
485
+ amp: 240,
486
+ c_amounts:["1298314415249170366960739764","80182803630538035347294614770"],
487
+ decimals:[24,24],
488
+ id: 568,
489
+ pool_kind: "RATED_SWAP",
490
+ rates:["1972204647926836788049038","1000000000000000000000000"],
491
+ shares_total_supply: "80676034815429711745720012070",
492
+ token_account_ids:["meta-v2.pool.testnet", "wrap.testnet"],
493
+ total_fee: 5,
494
+ }
495
+ }
496
+ ```
497
+
498
+ ### Transactions
499
+
500
+ #### instantSwap
501
+
502
+ Set up transactions through swap routes. Please ensure that the AccountId has an active balance storage in the token-in contract, otherwise the transaction will fail and the user will lose the token input amount.
503
+
504
+ Parameters
505
+
506
+ ```plain
507
+ {
508
+ tokenIn: TokenMetadata;
509
+ tokenOut: TokenMetadata;
510
+ amountIn: string;
511
+ slippageTolerance: number;
512
+ swapTodos: EstimateSwapView[];
513
+ AccountId: string;
514
+ }
515
+ ```
516
+
517
+ Example
518
+
519
+ ```plain
520
+ const transactionsRef: Transaction[] = await instantSwap({
521
+ tokenIn,
522
+ tokenOut,
523
+ amountIn: '1',
524
+ swapTodos,
525
+ slippageTolerance = 0.01,
526
+ AccountId: 'your-account-id.testnet'
527
+ });
528
+ ```
529
+
530
+ Response
531
+
532
+ ```plain
533
+ [
534
+ {
535
+ functionCalls: [
536
+ {
537
+ amount: '0.000000000000000000000001',
538
+ args: {
539
+ amount: '1000000000000000000',
540
+ msg:
541
+ '{"force":0,"actions":[{"pool_id":38,"token_in":"ref.fakes.testnet","token_out":"wrap.testnet","amount_in":"1000000000000000000","min_amount_out":"730191122546589600000000"}]}',
542
+ receiver_id: 'ref-finance-101.testnet',
543
+ },
544
+ gas: '180000000000000',
545
+ methodName: 'ft_transfer_call',
546
+ },
547
+ ],
548
+ receiverId: 'ref.fakes.testnet',
549
+ },
550
+ ];
551
+ ```
552
+
553
+ ---
554
+
555
+ #### getSignedTransactionsByMemoryKey (Node)
556
+
557
+ In the local env, developers can add credentials by `near login` .
558
+
559
+ This function utilizes credentials stored in the local env to sign transactions.
560
+
561
+ Parameters
562
+
563
+ ```plain
564
+ {
565
+ transactionsRef: Transaction[];
566
+ AccountId: string;
567
+ keyPath: string;
568
+ }
569
+ ```
570
+
571
+ Example
572
+
573
+ ```plain
574
+ const signedTransactions:nearTransactions.SignedTransaction[] = getSignedTransactionsByMemoryKey({
575
+ transactionsRef;
576
+ AccountId: "your-account-id.testnet",
577
+ keyPath: "/.near-credentials/testnet/your-account-id.testnet.json"
578
+ })
579
+ ```
580
+
581
+ Response
582
+
583
+ ```plain
584
+ [
585
+ SignedTransaction {
586
+ transaction: Transaction {
587
+ signerId: 'your-account-id.testnet',
588
+ publicKey: [PublicKey],
589
+ nonce: 91940092000042,
590
+ receiverId: 'ref.fakes.testnet',
591
+ actions: [Array],
592
+ blockHash: <Buffer 45 e5 fd 36 87 3b 10 59 81 d9 a7 b5 20 c7 29 33 f7 27 48 59 06 90 ca 8a 17 03 5c 25 f2 76 ab 7c>
593
+ },
594
+ signature: Signature { keyType: 0, data: [Uint8Array] }
595
+ }
596
+ ]
597
+ ```
598
+
599
+ ---
600
+
601
+ #### sendTransactionsByMemoryKey (Node)
602
+
603
+ This function utilizes credentials stored in the local env to send transactions.
604
+
605
+ Parameters
606
+
607
+ ```plain
608
+ {
609
+ signedTransactions: nearTransactions.SignedTransaction[];
610
+ }
611
+ ```
612
+
613
+ Example
614
+
615
+ ```plain
616
+ sendTransactionsByMemoryKey({
617
+ signedTransactions,
618
+ });
619
+ ```
620
+
621
+ Response
622
+
623
+ ```plain
624
+ [
625
+ {
626
+ receipts_outcome: [
627
+ [Object], [Object],
628
+ [Object], [Object],
629
+ [Object], [Object],
630
+ [Object], [Object],
631
+ [Object], [Object]
632
+ ],
633
+ status: { SuccessValue: 'xxxxxx' },
634
+ transaction: {
635
+ actions: [Array],
636
+ hash: 'xxxxxxxx',
637
+ nonce: 91940092000042,
638
+ public_key: 'ed25519:xxxxxxxx',
639
+ receiver_id: 'ref.fakes.testnet',
640
+ signature: 'ed25519:xxxxxxxxxxxxx',
641
+ signer_id: 'your-account-id.testnet'
642
+ },
643
+ transaction_outcome: {
644
+ block_hash: '3QXEF941UvsHzXrMhwbchk7wmy3qmPNs3w9gkfvW84QK',
645
+ id: 'xxxxxxxx',
646
+ outcome: [Object],
647
+ proof: []
648
+ }
649
+ }
650
+ ]
651
+ ```
652
+
653
+ #
654
+
655
+ ## Ref Swap Widget
656
+
657
+ ### Description
658
+
659
+ The Ref Swap Widget is a useful tool, allowing any third party service to access Ref's liquidity. Users of ecosystem dapps have the ability to swap via the Widget, without the need to go to Ref app, thus improving the user experience.
660
+
661
+ Here are some use cases:
662
+
663
+ - Swapping stablecoins for your project's token
664
+ - Swapping tokens to lend, farm or stake
665
+ - Swapping one token for a specific token, which can be used to buy a NFT in the associated marketplace
666
+
667
+ Using the Ref Swap Widget, with a few customizations, developers can integrate the Swap funtion directly into their dapps. Both mobile and/or website version are available.
668
+ ![图片](https://user-images.githubusercontent.com/50706666/199178215-f2b184dc-f683-4740-af9f-fd67efd41503.png)
669
+
670
+ For the default theme, developers can chose between the light mode and dark mode.
671
+
672
+ More themes can be selected: [Click here to check them on figma](https://www.figma.com/file/v069nTXfE8pXDJQcDcC5wl/Swap-Widget?node-id=0%3A1).
673
+
674
+ To integrate the Ref Swap Widget, please follow this guide.
675
+
676
+ ### Getting started
677
+
678
+ A QuickStart of Ref Swap component.
679
+
680
+ #### Props
681
+
682
+ ```plain
683
+ export interface SwapWidgetProps {
684
+ theme?: Theme;
685
+ extraTokenList?: string[];
686
+ onSwap: (transactionsRef: Transaction[]) => void;
687
+ onDisConnect: () => void;
688
+ width: string;
689
+ height?: string;
690
+ enableSmartRouting?: boolean;
691
+ className?: string;
692
+ darkMode?: boolean;
693
+ connection: {
694
+ AccountId: string;
695
+ isSignedIn: boolean;
696
+ };
697
+ defaultTokenIn?: string;
698
+ defaultTokenOut?: string;
699
+ transactionState?: {
700
+ state: 'success' | 'fail' | null;
701
+ tx?: string;
702
+ detail?: string;
703
+ };
704
+ onConnect: () => void;
705
+ }
706
+ ```
707
+
708
+ - theme: widget theme for customization.
709
+ - extraTokenList: introduce extra tokens with ref whitelist into default token list in the widget.
710
+ - onSwap: Swap button triggers this function.
711
+ - width: width of widget component.
712
+ - height: height of widget component.
713
+ - enableSmartRouting: option to choose if enable smart routing in swap routes estimation.
714
+ - className: extra className added to widget component.
715
+ - darkMode: if true, will automatically set theme to default dark mode.
716
+ - connection: connection to wallets, input { AccountId:"", isSignedIn:false } if wallet not connected.
717
+ - defaultTokenIn: default token-in.
718
+ - defaultTokenOut: default token-out.
719
+ - transactionState: entry to input transaction states after you send transactions.
720
+ - state: denote if last transaction is failed or successfull.
721
+ - setState: used to change setState to interact with pop-up.
722
+ - tx: will add link to near explorer according to this tx.
723
+ - detail: you could input some tips to show on sucess pop-up.
724
+
725
+ ![111](https://user-images.githubusercontent.com/50706666/199178453-8d09be3f-5a00-4b62-a6f1-af42ce4beae6.png)
726
+
727
+ - onDisConnect: Disconnect button triggers this function.
728
+ - onConnect: Connect to Near Wallet button triggers this function.
729
+
730
+ ### Usage
731
+
732
+ #### Theme
733
+
734
+ ```plain
735
+ export interface Theme {
736
+ container: string; // container background
737
+ buttonBg: string; // button background
738
+ primary: string; // primary theme color
739
+ secondary: string; // secondary theme color
740
+ borderRadius: string; // border radius
741
+ fontFamily: string; // font family
742
+ hover: string; // hovering color
743
+ active: string; // active color
744
+ secondaryBg: string; // secondary background color
745
+ borderColor: string; // border color
746
+ iconDefault: string; // default icon color
747
+ iconHover: string; // icon hovering color
748
+ refIcon?: string; // ref icon color, default to be black
749
+ }
750
+
751
+ export const defaultTheme: Theme = {
752
+ container: '#FFFFFF',
753
+ buttonBg: '#00C6A2',
754
+ primary: '#000000',
755
+ secondary: '#7E8A93',
756
+ borderRadius: '4px',
757
+ fontFamily: 'sans-serif',
758
+ hover: 'rgba(126, 138, 147, 0.2)',
759
+ active: 'rgba(126, 138, 147, 0.2)',
760
+ secondaryBg: '#F7F7F7',
761
+ borderColor: 'rgba(126, 138, 147, 0.2)',
762
+ iconDefault: '#7E8A93',
763
+ iconHover: '#B7C9D6',
764
+ };
765
+
766
+ export const defaultDarkModeTheme: Theme = {
767
+ container: '#26343E',
768
+ buttonBg: '#00C6A2',
769
+ primary: '#FFFFFF',
770
+ secondary: '#7E8A93',
771
+ borderRadius: '4px',
772
+ fontFamily: 'sans-serif',
773
+ hover: 'rgba(126, 138, 147, 0.2)',
774
+ active: 'rgba(126, 138, 147, 0.2)',
775
+ secondaryBg: 'rgba(0, 0, 0, 0.2)',
776
+ borderColor: 'rgba(126, 138, 147, 0.2)',
777
+ iconDefault: '#7E8A93',
778
+ iconHover: '#B7C9D6',
779
+ refIcon: 'white',
780
+ };
781
+ ```
782
+
783
+ #### Component
784
+
785
+ ```plain
786
+ // an example of combining SwapWidget with wallet-selector
787
+ import * as React from 'react';
788
+ import { SwapWidget } from '@ref-finance/ref-sdk';
789
+
790
+ // please check on wallet-selector example about how to set WalletSelectorContext
791
+ import { useWalletSelector } from './WalletSelectorContext';
792
+
793
+ import { WalletSelectorTransactions, NotLoginError } from '@ref-finance/ref-sdk';
794
+
795
+ export const Widget = ()=>{
796
+   
797
+   const { modal, selector, accountId } = useWalletSelector();
798
+   
799
+   const [swapState, setSwapState] = React.useState<'success' | 'fail' | null>(
800
+     null
801
+   );
802
+   const [tx, setTx] = React.useState<string | undefined>(undefined);
803
+   React.useEffect(() => {
804
+     const errorCode = new URLSearchParams(window.location.search).get(
805
+       'errorCode'
806
+     );
807
+
808
+     const transactions = new URLSearchParams(window.location.search).get(
809
+       'transactionHashes'
810
+     );
811
+
812
+     const lastTX = transactions?.split(',').pop();
813
+
814
+     setTx(lastTX);
815
+
816
+     setSwapState(!!errorCode ? 'fail' : !!lastTX ? 'success' : null);
817
+
818
+     window.history.replaceState(
819
+       {},
820
+       '',
821
+       window.location.origin + window.location.pathname
822
+     );
823
+   }, []);
824
+   
825
+   const onSwap = async (transactionsRef: Transaction[]) => {
826
+     const wallet = await selector.wallet();
827
+     if (!accountId) throw NotLoginError;
828
+
829
+     wallet.signAndSendTransactions(
830
+       WalletSelectorTransactions(transactionsRef, accountId)
831
+     );
832
+   };
833
+   
834
+   const onConnect = () => {
835
+     modal.show();
836
+   };
837
+
838
+   const onDisConnect = async () => {
839
+     const wallet = await selector.wallet();
840
+     return await wallet.signOut();
841
+   };
842
+
843
+   return (
844
+     <SwapWidget
845
+       onSwap={onSwap}
846
+       onDisConnect={onDisConnect}
847
+       width={'400px'}
848
+       connection={{
849
+         AccountId: accountId || '',
850
+         isSignedIn: !!accountId,
851
+       }}
852
+       transactionState={{
853
+         state: swapState,
854
+         setState: setSwapState,
855
+         tx,
856
+         detail: '(success details show here)',
857
+       }}
858
+       enableSmartRouting={true}
859
+       onConnect={onConnect}
860
+       defaultTokenIn={'wrap.testnet'}
861
+       defaultTokenOut={'ref.fakes.testnet'}
862
+     />
863
+   );
864
+ }
865
+ ```
866
+
867
+ ### Integrating Ref Swap function using the SDK
868
+
869
+ The SDK provides more flexibility/options.
870
+
871
+ The default Swap version can serve as a reference. [Click here to check on figma.](https://www.figma.com/file/v069nTXfE8pXDJQcDcC5wl/Swap-Widget?node-id=0%3A1)
872
+
873
+ For more details about the SDK, please refer to [here].
874
+
875
+ SDK integration tips:
876
+
877
+ - You can use 'ftGetTokensMetadata' function to get all available tokens, and list them in the token selector to allow users to select their trading pair. If you do not need all available tokens, you can limit the list at the frontend level, thus only displaying specific tokens such as REF, NEAR, and USDT, for example.
878
+ - Please pay attention to the user's 'tokenIn' balance. If the balance is zero, you should **DISABLE** the swap button.
879
+ - You can let the user set the slippage, or you can pre set a default number.
880
+ - For a better user experience, before the execution of the swap, you can show more details about the swap (ex: fee, rate, route, etc.), allowing users to take better data-driven decisions.
881
+ - You can redirect the user to the NEAR Explorer, once the transaction is confirmed.
882
+
883
+ ## Ref V2(DCL) Swap
884
+
885
+ #### An overview of Ref V2
886
+
887
+ The launch of concentrated liquidity AMM is an achievement for Ref Finance. In collaboration with Izumi Finance and Arctic, Ref is glad to introduce discretized concentrated liquidity and limit order two new key features to the NEAR ecosystem. Using REF SDK, developers can dig more opportunities and implement various trading strategies.Before introducing SDK details, let's have an overview of V2 exciting features.
888
+
889
+ #### Discretized concentrated liquidity
890
+
891
+ Discretized concentrated liquidity can improve as much as 4000x higher capital efficiency for liquidity providers(LPs).
892
+
893
+ When adding liquidity, LPs are can allocate their capital to a certain price range thus providing greater amounts of liquidity at this range. The fees earned are decided by the volumes swapped in the price range. Logically, if you want to earn more, you should set the price range most desired by the market.
894
+
895
+ Here are the advantages of using Ref V2 concentrated liquidity for LPS.
896
+
897
+ - **Efficient swap fee earnings and less impermanent loss** by setting a price range
898
+ - **Improved fee structure:** only LPs (and the protocol itself) can benefit from swap fees, <u>which means an LP can also benefit from a transaction between a taker and market makers as long the transaction closes at the price within the range set by him/her. This is quite different from Uniswap V3 by giving more benefits to LPS.</u>
899
+ - **Versatile strategies for different pairs:** according to correlations of trading pairs, market demand and fee tiers, versatile strategies can be designed to make a profit and hedge against risk.
900
+
901
+ #### Enhanced limit order
902
+
903
+ Izumi's algorithm of constant sum formula on small price range fragment enables an enhanced limit order function that is different than Uniswap V3. The following remarkable improvements are noticeable.
904
+
905
+ - **No price restrictions when placing a limit order:** Users can place limit buy orders at a price higher than current price or sell orders lower than current price. Ref V2 would auto match with best price first, similar to CLOBs (central limit order books) seen on centralized exchanges.
906
+ - **No need to keep an eye on order progress:** As a kind of one-way liquidity, users can claim their order earning at any time without any concern that the earned token would be reverted.
907
+ - **CEX order-book style user experience.**
908
+
909
+ Enhanced limit order will give more convenience for strategy trading like grid trading, arbitrage, day trading, etc.
910
+
911
+ #### Decreased swap slippage
912
+
913
+ With much thicker liquidity around the current price point, swap slippage has a notable decrease.
914
+
915
+ As mentioned above, we've compared our development features of this model in comparison to Uniswaps model, from Ethereum to NEAR, but far less costly. **Ref V2 provides a more capital efficient, user-friendly experience with dynamic pricing, reduced fees, and cross-chain integrations.**
916
+
917
+ The combination of concentrated liquidity, enhanced limit order and decreased swap slippage offers a good tool kit for trading in NEAR ecosystem. Enjoy it.
918
+
919
+ ---
920
+
921
+ ### DCL pool
922
+
923
+ #### getDCLPoolId
924
+
925
+ Get DCL pool id by tokenA, tokenB and fee.
926
+
927
+ Note: the fee should be in one of [100, 400, 2000, 10000], which means we charge [0.01%, 0.04%, 0.2%, 1%] separately.
928
+
929
+ Parameters
930
+
931
+ ```plain
932
+ (tokenA:string, tokenB:string, fee:number)
933
+ ```
934
+
935
+ Example
936
+
937
+ ```plain
938
+ const tokenA = "usdt.fakes.testnet";
939
+
940
+ const tokenB = "wrap.testnet";
941
+
942
+ const fee = 2000
943
+
944
+ const pool_id = getDCLPoolId(tokenA, tokenB, fee)
945
+ ```
946
+
947
+ Response
948
+
949
+ ```
950
+ "usdt.fakes.testnet|wrap.testnet|2000"
951
+ ```
952
+
953
+ ---
954
+
955
+ #### listDCLPools
956
+
957
+ List all DCL pools
958
+
959
+ Parameters
960
+
961
+ ```plain
962
+ None
963
+ ```
964
+
965
+ Example
966
+
967
+ ```plain
968
+ const allDCLPools = await listDCLPools()
969
+ ```
970
+
971
+ Response
972
+
973
+ ```
974
+ [
975
+ {
976
+ pool_id: 'usdt.fakes.testnet|wrap.testnet|100',
977
+ token_x: 'usdt.fakes.testnet',
978
+ token_y: 'wrap.testnet',
979
+ fee: 100,
980
+ point_delta: 1,
981
+ current_point: 391459,
982
+ liquidity: '0',
983
+ liquidity_x: '0',
984
+ max_liquidity_per_point: '212676346402870037870835460372692',
985
+ volume_x_in: '0',
986
+ volume_y_in: '0',
987
+ volume_x_out: '0',
988
+ volume_y_out: '0',
989
+ total_liquidity: '0',
990
+ total_order_x: '1000000000000',
991
+ total_order_y: '0',
992
+ total_x: '1000000000000',
993
+ total_y: '0',
994
+ state: 'Running'
995
+ },
996
+ ...
997
+ ]
998
+ ```
999
+
1000
+ ---
1001
+
1002
+ #### getDCLPool
1003
+
1004
+ Get DCL pool by pool id
1005
+
1006
+ Parameters
1007
+
1008
+ ```plain
1009
+ (pool_id: string)
1010
+ ```
1011
+
1012
+ Example
1013
+
1014
+ ```plain
1015
+ const tokenA = "usdt.fakes.testnet";
1016
+
1017
+ const tokenB = "wrap.testnet";
1018
+
1019
+ const fee = 2000
1020
+
1021
+ const pool_id = getDCLPoolId(tokenA, tokenB, fee)
1022
+
1023
+ const pool = await getDCLPool(pool_id)
1024
+
1025
+ ```
1026
+
1027
+ Response
1028
+
1029
+ ```
1030
+ {
1031
+ pool_id: 'usdt.fakes.testnet|wrap.testnet|2000',
1032
+ token_x: 'usdt.fakes.testnet',
1033
+ token_y: 'wrap.testnet',
1034
+ fee: 2000,
1035
+ point_delta: 40,
1036
+ current_point: 380120,
1037
+ liquidity: '1110725876876975',
1038
+ liquidity_x: '1110725876876975',
1039
+ max_liquidity_per_point: '8506846501860915063707772491481918',
1040
+ volume_x_in: '8754783773',
1041
+ volume_y_in: '109748224827667685031216606',
1042
+ volume_x_out: '1220525924',
1043
+ volume_y_out: '1301582127053053120024924721',
1044
+ total_liquidity: '9961756656973511',
1045
+ total_order_x: '101475486843',
1046
+ total_order_y: '31907047396923536842194449',
1047
+ total_x: '125636676594',
1048
+ total_y: '131645349254303605341104269',
1049
+ state: 'Running'
1050
+ }
1051
+ ```
1052
+
1053
+ ---
1054
+
1055
+ ### DCL Swap
1056
+
1057
+ #### quote
1058
+
1059
+ quote output amount by pool_ids, input_amount, input_token, output_token
1060
+
1061
+ Parameters
1062
+
1063
+ ```plain
1064
+ {
1065
+ pool_ids: string[];
1066
+ input_token: TokenMetadata;
1067
+ output_token: TokenMetadata;
1068
+ input_amount: string;
1069
+ tag?: string;
1070
+ }
1071
+ ```
1072
+
1073
+ Example
1074
+
1075
+ ```plain
1076
+ const tokenA = "usdt.fakes.testnet";
1077
+
1078
+ const tokenB = "wrap.testnet";
1079
+
1080
+ const fee = 10000
1081
+
1082
+ const pool_ids = [getDCLPoolId(tokenA, tokenB, fee)];
1083
+
1084
+ const res = await quote({
1085
+ pool_ids,
1086
+ input_amount,
1087
+ input_token: tokenA,
1088
+ output_token: tokenB,
1089
+ });
1090
+ ```
1091
+
1092
+ Response
1093
+
1094
+ ```
1095
+ { amount: '203807761645099642566723', tag: null }
1096
+ ```
1097
+
1098
+ ---
1099
+
1100
+ #### quote_by_output
1101
+
1102
+ quote in put amount by output amount to price by pool_ids, input_amount, input_token, output_token
1103
+
1104
+ Parameters
1105
+
1106
+ ```plain
1107
+ {
1108
+ pool_ids: string[];
1109
+ input_token: TokenMetadata;
1110
+ output_token: TokenMetadata;
1111
+ input_amount: string;
1112
+ tag?: string;
1113
+ }
1114
+ ```
1115
+
1116
+ Example
1117
+
1118
+ ```plain
1119
+ const tokenA = "usdt.fakes.testnet";
1120
+
1121
+ const tokenB = "wrap.testnet";
1122
+
1123
+ const fee = 10000
1124
+
1125
+ const pool_ids = [getDCLPoolId(tokenA, tokenB, fee)];
1126
+
1127
+ const res = await quote_by_output({
1128
+ pool_ids,
1129
+ output_amount: "0.1",
1130
+ input_token: tokenA,
1131
+ output_token: tokenB,
1132
+ });
1133
+ ```
1134
+
1135
+ Response
1136
+
1137
+ ```
1138
+ { amount: '490370', tag: null }
1139
+ ```
1140
+
1141
+ ---
1142
+
1143
+ #### DCLSwap
1144
+
1145
+ This function integrates Swap, SwapByOutput and LimitOrderWithSwap apis.
1146
+
1147
+ - Swap: swap from tokenA to get tokenB.
1148
+ - SwapByOutput: Swap to get specific tokenB and comsume tokenA.
1149
+ - LimitOrderWithSwap: compare the calculated price based on parameters with price in the pool, if calculated price is lower than the pool price, go to Swap, the left will generate an limit order higher than the pool price, and if no amount is left, no order generated; if the calculated price is higher than the pool price, will directly generate an limit order.
1150
+
1151
+ Parameters
1152
+
1153
+ ```
1154
+ interface SwapInfo {
1155
+ tokenA: TokenMetadata;
1156
+ tokenB: TokenMetadata;
1157
+ amountA: string;
1158
+ }
1159
+
1160
+ interface DCLSwapProps {
1161
+ swapInfo: SwapInfo;
1162
+ Swap?: {
1163
+ pool_ids: string[];
1164
+ min_output_amount: string;
1165
+ };
1166
+ SwapByOutput?: {
1167
+ pool_ids: string[];
1168
+ output_amount: string;
1169
+ };
1170
+ LimitOrderWithSwap?: {
1171
+ pool_id: string;
1172
+ output_amount: string;
1173
+ };
1174
+ AccountId: string;
1175
+ }
1176
+ ```
1177
+
1178
+ Example (Swap)
1179
+
1180
+ ```plain
1181
+ const tokenA = "usdt.fakes.testnet";
1182
+
1183
+ const tokenB = "wrap.testnet";
1184
+
1185
+ const fee = 2000
1186
+
1187
+ const pool_ids = [getDCLPoolId(tokenA, tokenB, fee)];
1188
+
1189
+ const res = await DCLSwap({
1190
+ swapInfo: {
1191
+ amountA: input_amount,
1192
+ tokenA: tokenA,
1193
+ tokenB: tokenB,
1194
+ },
1195
+ Swap: {
1196
+ min_output_amount: "0",
1197
+ pool_ids,
1198
+ },
1199
+ AccountId,
1200
+ });
1201
+ ```
1202
+
1203
+ Response (Swap)
1204
+
1205
+ ```
1206
+ [
1207
+ {
1208
+ receiverId: "usdt.fakes.testnet",
1209
+ functionCalls: [
1210
+ {
1211
+ methodName: "ft_transfer_call",
1212
+ args: {
1213
+ receiver_id: "dcl.ref-dev.testnet",
1214
+ amount: "1000000",
1215
+ msg: '{"Swap":{"pool_ids":["usdt.fakes.testnet|wrap.testnet|2000"],"output_token":"wrap.testnet","min_output_amount":"0"}}',
1216
+ },
1217
+ gas: "180000000000000",
1218
+ amount: "0.000000000000000000000001",
1219
+ },
1220
+ ],
1221
+ },
1222
+ ];
1223
+
1224
+ ```
1225
+
1226
+ Example (SwapByOutput)
1227
+
1228
+ ```plain
1229
+ const tokenA = "usdt.fakes.testnet";
1230
+
1231
+ const tokenB = "wrap.testnet";
1232
+
1233
+ const fee = 2000
1234
+
1235
+ const pool_ids = [getDCLPoolId(tokenA, tokenB, fee)];
1236
+
1237
+ const res = await DCLSwap({
1238
+ swapInfo: {
1239
+ amountA: input_amount,
1240
+ tokenA: tokenA,
1241
+ tokenB: tokenB,
1242
+ },
1243
+ SwapByOutput: {
1244
+ pool_ids,
1245
+ output_amount: "4.89454792",
1246
+ },
1247
+ AccountId,
1248
+ });
1249
+ ```
1250
+
1251
+ Response (SwapByOutput)
1252
+
1253
+ ```
1254
+
1255
+ [
1256
+ {
1257
+ receiverId: "usdt.fakes.testnet",
1258
+ functionCalls: [
1259
+ {
1260
+ methodName: "ft_transfer_call",
1261
+ args: {
1262
+ receiver_id: "dcl.ref-dev.testnet",
1263
+ amount: "900000",
1264
+ msg: '{"SwapByOutput":{"pool_ids":["usdt.fakes.testnet|wrap.testnet|2000"],"output_token":"wrap.testnet","output_amount":"4894547920000000000000000"}}',
1265
+ },
1266
+ gas: "180000000000000",
1267
+ amount: "0.000000000000000000000001",
1268
+ },
1269
+ ],
1270
+ },
1271
+ ];
1272
+ ```
1273
+
1274
+ Example (LimitOrderWithSwap)
1275
+
1276
+ ```plain
1277
+ const tokenA = "usdt.fakes.testnet";
1278
+
1279
+ const tokenB = "wrap.testnet";
1280
+
1281
+ const fee = 2000
1282
+
1283
+ const pool_ids = [getDCLPoolId(tokenA, tokenB, fee)];
1284
+
1285
+ const res = await DCLSwap({
1286
+ swapInfo: {
1287
+ amountA: input_amount,
1288
+ tokenA: tokenA,
1289
+ tokenB: tokenB,
1290
+ },
1291
+ LimitOrderWithSwap: {
1292
+ pool_id,
1293
+ output_amount: "3217.929",
1294
+ },
1295
+ AccountId,
1296
+ });
1297
+ ```
1298
+
1299
+ Response (LimitOrderWithSwap)
1300
+
1301
+ ```
1302
+
1303
+ [
1304
+ {
1305
+ receiverId: "usdt.fakes.testnet",
1306
+ functionCalls: [
1307
+ {
1308
+ methodName: "ft_transfer_call",
1309
+ args: {
1310
+ receiver_id: "dcl.ref-dev.testnet",
1311
+ amount: "1000000",
1312
+ msg: '{"LimitOrderWithSwap":{"pool_id":"usdt.fakes.testnet|wrap.testnet|2000","buy_token":"wrap.testnet","point":495240}}',
1313
+ },
1314
+ gas: "180000000000000",
1315
+ amount: "0.000000000000000000000001",
1316
+ },
1317
+ ],
1318
+ },
1319
+ ];
1320
+
1321
+ ```
1322
+
1323
+ ---
1324
+
1325
+ #### DCLSwapByInputOnBestPool
1326
+
1327
+ This function helps to swap on best price pool of at most 4 candidate pools based on tokenA, tokenB, amountA (input amount) and slippageTolerance.
1328
+
1329
+ Parameters
1330
+
1331
+ ```plain
1332
+ {
1333
+ tokenA: TokenMetadata;
1334
+ tokenB: TokenMetadata;
1335
+ amountA: string;
1336
+ slippageTolerance: number;
1337
+ AccountId: string;
1338
+ }
1339
+ ```
1340
+
1341
+ Example
1342
+
1343
+ ```plain
1344
+ const tokenA = "usdt.fakes.testnet";
1345
+
1346
+ const tokenB = "wrap.testnet";
1347
+
1348
+ const res = await DCLSwapByInputOnBestPool({
1349
+ tokenA,
1350
+ tokenB,
1351
+ amountA: "1",
1352
+ slippageTolerance: 0.1,
1353
+ AccountId,
1354
+ });
1355
+ ```
1356
+
1357
+ Response
1358
+
1359
+ ```
1360
+ [
1361
+ {
1362
+ receiverId: "usdt.fakes.testnet",
1363
+ functionCalls: [
1364
+ {
1365
+ methodName: "ft_transfer_call",
1366
+ args: {
1367
+ receiver_id: "dcl.ref-dev.testnet",
1368
+ amount: "1000000",
1369
+ msg: '{"Swap":{"pool_ids":["usdt.fakes.testnet|wrap.testnet|10000"],"output_token":"wrap.testnet","min_output_amount":"203606350172806050000000"}}',
1370
+ },
1371
+ gas: "180000000000000",
1372
+ amount: "0.000000000000000000000001",
1373
+ },
1374
+ ],
1375
+ },
1376
+ ];
1377
+
1378
+ ```
1379
+
1380
+ ---
1381
+
1382
+ ### Order
1383
+
1384
+ #### UserOrderInfo (interface)
1385
+
1386
+ ```
1387
+ interface UserOrderInfo {
1388
+ order_id: string;
1389
+ owner_id: string;
1390
+ pool_id: string;
1391
+ point: number;
1392
+ sell_token: string;
1393
+ created_at: string; // timestamp when this order created
1394
+ original_amount: string; // original amount of sell_token
1395
+ remain_amount: string; // remain amount to be swapped, 0 means a history order
1396
+ cancel_amount: string; // amount after cancel an active order
1397
+ original_deposit_amount: string; // the input amount of LimitOrderWithSwap through ft_transfer_call, maybe partially into instant Swap
1398
+ swap_earn_amount: string; // earn token amount through swap before actual place order
1399
+ buy_token: string;
1400
+ unclaimed_amount: string;
1401
+ bought_amount: string; // accumalated amount you get
1402
+ }
1403
+ ```
1404
+
1405
+ #### list_active_orders
1406
+
1407
+ Get your active orders
1408
+
1409
+ Parameters
1410
+
1411
+ ```plain
1412
+ (AccountId: string)
1413
+ ```
1414
+
1415
+ Example
1416
+
1417
+ ```plain
1418
+ const res = await list_active_orders(AccountId)
1419
+ ```
1420
+
1421
+ Response
1422
+
1423
+ ```
1424
+ [
1425
+ {
1426
+ order_id: "usdt.fakes.testnet|wrap.testnet|10000#93",
1427
+ owner_id: "your-account-id.testnet",
1428
+ pool_id: "usdt.fakes.testnet|wrap.testnet|10000",
1429
+ point: 398600,
1430
+ sell_token: "wrap.testnet",
1431
+ buy_token: "usdt.fakes.testnet",
1432
+ original_deposit_amount: "1000000000000000000000000",
1433
+ swap_earn_amount: "0",
1434
+ original_amount: "1000000000000000000000000",
1435
+ cancel_amount: "0",
1436
+ created_at: "1667292669983952215",
1437
+ remain_amount: "1000000000000000000000000",
1438
+ bought_amount: "0",
1439
+ unclaimed_amount: "0",
1440
+ },
1441
+ ];
1442
+
1443
+ ```
1444
+
1445
+ ---
1446
+
1447
+ #### list_history_orders
1448
+
1449
+ Get your history orders
1450
+
1451
+ Parameters
1452
+
1453
+ ```plain
1454
+ (AccountId: string)
1455
+ ```
1456
+
1457
+ Example
1458
+
1459
+ ```plain
1460
+ const res = await list_history_orders(AccountId)
1461
+ ```
1462
+
1463
+ Response
1464
+
1465
+ ```
1466
+ [
1467
+ {
1468
+ order_id: "usdc.fakes.testnet|wrap.testnet|2000#47",
1469
+ owner_id: "juaner.testnet",
1470
+ pool_id: "usdc.fakes.testnet|wrap.testnet|2000",
1471
+ point: 399120,
1472
+ sell_token: "wrap.testnet",
1473
+ buy_token: "usdc.fakes.testnet",
1474
+ original_deposit_amount: "1000000000000000000000000",
1475
+ swap_earn_amount: "0",
1476
+ original_amount: "1000000000000000000000000",
1477
+ cancel_amount: "1000000000000000000000000",
1478
+ created_at: "1665928620632469264",
1479
+ remain_amount: "0",
1480
+ bought_amount: "0",
1481
+ unclaimed_amount: null,
1482
+ },
1483
+ ];
1484
+
1485
+ ```
1486
+
1487
+ ---
1488
+
1489
+ #### cancel_order
1490
+
1491
+ cancel an active order
1492
+
1493
+ Parameters
1494
+
1495
+ ```plain
1496
+ (order_id: string)
1497
+ ```
1498
+
1499
+ Example
1500
+
1501
+ ```plain
1502
+ const res = await cancel_order("usdt.fakes.testnet|wrap.testnet|10000#93")
1503
+ ```
1504
+
1505
+ Response
1506
+
1507
+ ```
1508
+ [
1509
+ {
1510
+ receiverId: "dcl.ref-dev.testnet",
1511
+ functionCalls: [
1512
+ {
1513
+ methodName: "cancel_order",
1514
+ args: {
1515
+ order_id: "usdt.fakes.testnet|wrap.testnet|10000#93",
1516
+ amount: "1000000000000000000000000",
1517
+ },
1518
+ gas: "180000000000000",
1519
+ },
1520
+ ],
1521
+ },
1522
+ ];
1523
+
1524
+ ```
1525
+
1526
+ ---
1527
+
1528
+ #### claim_order
1529
+
1530
+ Claim your unclaimed_amount in the order.
1531
+
1532
+ Parameters
1533
+
1534
+ ```plain
1535
+ (order_id: string)
1536
+ ```
1537
+
1538
+ Example
1539
+
1540
+ ```plain
1541
+ const res = await claim_order("usdt.fakes.testnet|wrap.testnet|10000#93")
1542
+ ```
1543
+
1544
+ Response
1545
+
1546
+ ```
1547
+ [
1548
+ {
1549
+ receiverId: "dcl.ref-dev.testnet",
1550
+ functionCalls: [
1551
+ {
1552
+ methodName: "cancel_order",
1553
+ args: {
1554
+ order_id: "usdt.fakes.testnet|wrap.testnet|10000#93",
1555
+ amount: "0",
1556
+ },
1557
+ gas: "180000000000000",
1558
+ },
1559
+ ],
1560
+ },
1561
+ ];
1562
+
1563
+ ```
1564
+
1565
+ ---
1566
+
1567
+ #### get_order
1568
+
1569
+ Get order information by order_id
1570
+
1571
+ Parameters
1572
+
1573
+ ```plain
1574
+ (order_id: string)
1575
+ ```
1576
+
1577
+ Example
1578
+
1579
+ ```plain
1580
+ const res = await get_order("usdt.fakes.testnet|wrap.testnet|10000#93")
1581
+ ```
1582
+
1583
+ Response
1584
+
1585
+ ```
1586
+ {
1587
+ order_id: 'usdt.fakes.testnet|wrap.testnet|10000#93',
1588
+ owner_id: 'juaner.testnet',
1589
+ pool_id: 'usdt.fakes.testnet|wrap.testnet|10000',
1590
+ point: 398600,
1591
+ sell_token: 'wrap.testnet',
1592
+ buy_token: 'usdt.fakes.testnet',
1593
+ original_deposit_amount: '1000000000000000000000000',
1594
+ swap_earn_amount: '0',
1595
+ original_amount: '1000000000000000000000000',
1596
+ cancel_amount: '0',
1597
+ created_at: '1667292669983952215',
1598
+ remain_amount: '1000000000000000000000000',
1599
+ bought_amount: '0',
1600
+ unclaimed_amount: '0'
1601
+ }
1602
+ ```
1603
+
1604
+ ---
1605
+
1606
+ ### Asset
1607
+
1608
+ #### list_user_assets
1609
+
1610
+ Get user assets.
1611
+
1612
+ Parameters
1613
+
1614
+ ```plain
1615
+ (AccountId: string)
1616
+ ```
1617
+
1618
+ Example
1619
+
1620
+ ```plain
1621
+ const res = await list_user_assets("your-account-id.testnet")
1622
+ ```
1623
+
1624
+ Response
1625
+
1626
+ ```
1627
+ {
1628
+ 'meta-v2.pool.testnet': '1000000000000000000000000',
1629
+ 'ref.fakes.testnet': '54795238623455655224444',
1630
+ 'usdc.fakes.testnet': '204436562555',
1631
+ 'eth.fakes.testnet': '112348196732799990900',
1632
+ 'usdt.fakes.testnet': '164044235810',
1633
+ 'wrap.testnet': '1004944616890200800000000'
1634
+ }
1635
+ ```
1636
+
1637
+ ---
1638
+
1639
+ ### Utils
1640
+
1641
+ #### priceToPoint
1642
+
1643
+ Get point based on input price
1644
+
1645
+ Parameters
1646
+
1647
+ ```plain
1648
+ {
1649
+ tokenA: TokenMetadata;
1650
+ tokenB: TokenMetadata;
1651
+ amountA: string;
1652
+ amountB: string;
1653
+ fee: number;
1654
+ }
1655
+ ```
1656
+
1657
+ Example
1658
+
1659
+ ```plain
1660
+ const tokenA = await ftGetTokenMetadata("usdt.fakes.testnet");
1661
+
1662
+ const tokenB = await ftGetTokenMetadata("wrap.testnet");
1663
+
1664
+ const amountA = "0.9";
1665
+
1666
+ const amountB = "4.89454792";
1667
+
1668
+ const fee = 400;
1669
+
1670
+ const res = priceToPoint({
1671
+ tokenA,
1672
+ tokenB,
1673
+ amountA,
1674
+ amountB,
1675
+ fee,
1676
+ });
1677
+ ```
1678
+
1679
+ Response
1680
+
1681
+ ```
1682
+ 431416
1683
+ ```
1684
+
1685
+ ---
1686
+
1687
+ #### pointToPrice
1688
+
1689
+ Get price from tokenA to tokenB based on point
1690
+
1691
+ Parameters
1692
+
1693
+ ```plain
1694
+ {
1695
+ tokenA: TokenMetadata;
1696
+ tokenB: TokenMetadata;
1697
+ point: number;
1698
+ }
1699
+ ```
1700
+
1701
+ Example
1702
+
1703
+ ```plain
1704
+ const tokenA = await ftGetTokenMetadata("usdt.fakes.testnet");
1705
+
1706
+ const tokenB = await ftGetTokenMetadata("wrap.testnet");
1707
+
1708
+ const res = pointToPrice({
1709
+ tokenA,
1710
+ tokenB,
1711
+ point: 431416,
1712
+ });
1713
+ ```
1714
+
1715
+ Response
1716
+
1717
+ ```
1718
+ 5.43528191708623
1719
+ ```
1720
+
1721
+ ---
1722
+
1723
+ ###