@xchainjs/xchain-thorchain-amm 0.1.0-alpha

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2022 THORChain
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,106 @@
1
+ # `@xchainjs/xchain-thorchain-amm`
2
+
3
+ ## Modules
4
+
5
+ module for to interact with Thorchain AMM
6
+
7
+
8
+ ## Installation
9
+
10
+ ```
11
+ yarn add @xchainjs/xchain-thorchain-amm
12
+ ```
13
+
14
+ Following peer dependencies have to be installed into your project. These are not included in `@xchainjs/xchain-thorchain-amm`.
15
+
16
+ ```
17
+ yarn add @xchainjs/xchain-client @xchainjs/xchain-crypto @xchainjs/xchain-util @xchainjs/xchain-cosmos axios @cosmos-client/core bech32-buffer
18
+ ```
19
+
20
+ ## Usage
21
+
22
+ ### Estimating a swap from BTC -> BUSD
23
+
24
+ ```typescript
25
+ import { Network } from '@xchainjs/xchain-client'
26
+ import { AssetBTC, assetAmount, assetFromString, assetToBase } from '@xchainjs/xchain-util'
27
+ import BigNumber from 'bignumber.js'
28
+
29
+ import { CryptoAmount, EstimateSwapParams, Midgard, SwapEstimate, ThorchainAMM } from '@xchainjs/xchain-thorchain-amm'
30
+
31
+ const BUSD = assetFromString('BNB.BUSD-BD1')
32
+ if (!BUSD) throw Error('bad asset')
33
+
34
+ function print(estimate: SwapEstimate, input: CryptoAmount) {
35
+ const expanded = {
36
+ input: input.formatedAssetString(),
37
+ totalFees: {
38
+ inboundFee: estimate.totalFees.inboundFee.formatedAssetString(),
39
+ swapFee: estimate.totalFees.swapFee.formatedAssetString(),
40
+ outboundFee: estimate.totalFees.outboundFee.formatedAssetString(),
41
+ affiliateFee: estimate.totalFees.affiliateFee.formatedAssetString(),
42
+ },
43
+ slipPercentage: estimate.slipPercentage.toFixed(),
44
+ netOutput: estimate.netOutput.formatedAssetString(),
45
+ waitTimeSeconds: estimate.waitTimeSeconds.toFixed(),
46
+ canSwap: estimate.canSwap,
47
+ errors: estimate.errors,
48
+ }
49
+ console.log(expanded)
50
+ }
51
+ try {
52
+ const midgard = new Midgard(Network.Mainnet) //defaults to mainnet
53
+ const thorchainAmm = new ThorchainAMM(midgard)
54
+ const swapParams: EstimateSwapParams = {
55
+ input: new CryptoAmount(assetToBase(assetAmount('1')), AssetBTC),
56
+ destinationAsset: BUSD,
57
+ // affiliateFeePercent: 0.003, //optional
58
+ slipLimit: new BigNumber('0.03'), //optional
59
+ }
60
+ const estimate = await thorchainAmm.estimateSwap(swapParams)
61
+ print(estimate, swapParams.input)
62
+
63
+ // convert fees (by defualt returned in RUNE) to a different asset (BUSD)
64
+ const estimateInBusd = await thorchainAmm.getFeesIn(estimate.totalFees, BUSD)
65
+ estimate.totalFees = estimateInBusd
66
+ print(estimate, swapParams.input)
67
+ } catch (e) {
68
+ console.error(e)
69
+ }
70
+
71
+ ```
72
+
73
+ ## output
74
+
75
+ ```
76
+ ===Estimate with fees in Rune===
77
+ {
78
+ input: '₿ 1',
79
+ totalFees: {
80
+ inboundFee: 'ᚱ 0.02165694',
81
+ swapFee: 'ᚱ 69.57707207',
82
+ outboundFee: 'ᚱ 0.03235451',
83
+ affiliateFee: 'ᚱ 0'
84
+ },
85
+ slipPercentage: '0.00299630164689824336',
86
+ netOutput: '$ 23,151.33630401',
87
+ waitTimeSeconds: '1116',
88
+ canSwap: true,
89
+ errors: undefined
90
+ }
91
+ ===Estimate with fees in BUSD===
92
+ {
93
+ input: '₿ 1',
94
+ totalFees: {
95
+ inboundFee: '$ 0.05822673',
96
+ swapFee: '$ 187.06454402',
97
+ outboundFee: '$ 0.08698816',
98
+ affiliateFee: '$ 0'
99
+ },
100
+ slipPercentage: '0.00299630164689824336',
101
+ netOutput: '$ 23,151.33630401',
102
+ waitTimeSeconds: '1116',
103
+ canSwap: true,
104
+ errors: undefined
105
+ }
106
+ ```