@shapeshiftoss/swapper 3.0.0 → 3.0.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 @@
1
+ export {};
@@ -0,0 +1,111 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ const asset_service_1 = require("@shapeshiftoss/asset-service");
7
+ const chain_adapters_1 = require("@shapeshiftoss/chain-adapters");
8
+ const hdwallet_native_1 = require("@shapeshiftoss/hdwallet-native");
9
+ const types_1 = require("@shapeshiftoss/types");
10
+ const bignumber_js_1 = __importDefault(require("bignumber.js"));
11
+ const dotenv_1 = __importDefault(require("dotenv"));
12
+ const readline_sync_1 = __importDefault(require("readline-sync"));
13
+ const web3_1 = __importDefault(require("web3"));
14
+ const SwapperManager_1 = require("./manager/SwapperManager");
15
+ const ZrxSwapper_1 = require("./swappers/zrx/ZrxSwapper");
16
+ dotenv_1.default.config();
17
+ const { UNCHAINED_HTTP_API = 'http://localhost:31300', UNCHAINED_WS_API = 'wss://localhost:31300', ETH_NODE_URL = 'http://localhost:3000', DEVICE_ID = 'device123', MNEMONIC = 'salon adapt foil saddle orient make page zero cheese marble test catalog' } = process.env;
18
+ const toBaseUnit = (amount, precision) => {
19
+ return new bignumber_js_1.default(amount)
20
+ .multipliedBy(new bignumber_js_1.default(10).exponentiatedBy(new bignumber_js_1.default(precision)))
21
+ .toString();
22
+ };
23
+ const fromBaseUnit = (amount, precision) => {
24
+ return new bignumber_js_1.default(amount).times(new bignumber_js_1.default(10).exponentiatedBy(precision * -1)).toString();
25
+ };
26
+ const getWallet = async () => {
27
+ if (!MNEMONIC) {
28
+ throw new Error('Cannot init native wallet without mnemonic');
29
+ }
30
+ const nativeAdapterArgs = {
31
+ mnemonic: MNEMONIC,
32
+ deviceId: DEVICE_ID
33
+ };
34
+ const wallet = new hdwallet_native_1.NativeHDWallet(nativeAdapterArgs);
35
+ await wallet.initialize();
36
+ return wallet;
37
+ };
38
+ const main = async () => {
39
+ const [, , ...args] = process.argv;
40
+ const [sellSymbol, buySymbol, sellAmount] = args;
41
+ console.info(`sellSymbol: sell ${sellAmount} of ${sellSymbol} to ${buySymbol}`);
42
+ if (!sellAmount || !sellSymbol || !buySymbol) {
43
+ console.error(`
44
+ Usage:
45
+ swapcli [sellSymbol] [buySymbol] [sellAmount](denominated in sell asset, not wei)
46
+ `);
47
+ return;
48
+ }
49
+ const assetService = new asset_service_1.AssetService('');
50
+ await assetService.initialize();
51
+ const assets = assetService.byNetwork(types_1.NetworkTypes.MAINNET);
52
+ if (!assets) {
53
+ console.error('No assets found in asset service');
54
+ return;
55
+ }
56
+ const assetMap = assets.reduce((acc, val) => {
57
+ if (val) {
58
+ acc[val.symbol] = val;
59
+ }
60
+ return acc;
61
+ }, {});
62
+ const sellAsset = assetMap[sellSymbol];
63
+ const buyAsset = assetMap[buySymbol];
64
+ if (!sellAsset) {
65
+ console.error(`No asset ${sellSymbol} found in asset service`);
66
+ return;
67
+ }
68
+ if (!buyAsset) {
69
+ console.error(`No asset ${buySymbol} found in asset service`);
70
+ return;
71
+ }
72
+ // Swapper Deps
73
+ const wallet = await getWallet();
74
+ const unchainedUrls = {
75
+ [types_1.ChainTypes.Ethereum]: {
76
+ httpUrl: UNCHAINED_HTTP_API,
77
+ wsUrl: UNCHAINED_WS_API
78
+ }
79
+ };
80
+ const adapterManager = new chain_adapters_1.ChainAdapterManager(unchainedUrls);
81
+ const web3Provider = new web3_1.default.providers.HttpProvider(ETH_NODE_URL);
82
+ const web3 = new web3_1.default(web3Provider);
83
+ const zrxSwapperDeps = { wallet, adapterManager, web3 };
84
+ const manager = new SwapperManager_1.SwapperManager();
85
+ const zrxSwapper = new ZrxSwapper_1.ZrxSwapper(zrxSwapperDeps);
86
+ manager.addSwapper(types_1.SwapperType.Zrx, zrxSwapper);
87
+ const swapper = manager.getSwapper(types_1.SwapperType.Zrx);
88
+ const sellAmountBase = toBaseUnit(sellAmount, sellAsset.precision);
89
+ const quote = await swapper.buildQuoteTx({
90
+ input: {
91
+ sellAsset,
92
+ buyAsset,
93
+ sellAmount: sellAmountBase,
94
+ sellAssetAccountId: '0',
95
+ buyAssetAccountId: '0'
96
+ },
97
+ wallet
98
+ });
99
+ console.info('quote = ', JSON.stringify(quote));
100
+ if (!quote.success) {
101
+ console.error('Obtaining the quote failed: ', quote.statusReason);
102
+ return;
103
+ }
104
+ const buyAmount = fromBaseUnit(quote.buyAmount || '0', buyAsset.precision);
105
+ const answer = readline_sync_1.default.question(`Swap ${sellAmount} ${sellAsset.symbol} for ${buyAmount} ${buyAsset.symbol} on ${swapper.getType()}? (y/n): `);
106
+ if (answer === 'y') {
107
+ const txid = await swapper.executeQuote({ quote, wallet });
108
+ console.info('broadcast tx with id: ', txid);
109
+ }
110
+ };
111
+ main().then(() => console.info('Done'));
@@ -1,4 +1,3 @@
1
- /// <reference types="jest" />
2
1
  import { ChainAdapterManager } from '@shapeshiftoss/chain-adapters';
3
2
  import Web3 from 'web3';
4
3
  export declare const chainAdapterMockFuncs: {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@shapeshiftoss/swapper",
3
- "version": "3.0.0",
3
+ "version": "3.0.1",
4
4
  "description": "TODO: description",
5
5
  "repository": "https://github.com/shapeshift/lib",
6
6
  "license": "MIT",
@@ -13,11 +13,13 @@
13
13
  "access": "public"
14
14
  },
15
15
  "scripts": {
16
- "build": "yarn clean && tsc --project tsconfig.build.json",
16
+ "build": "yarn clean && yarn compile",
17
17
  "dev": "tsc --watch",
18
- "clean": "rm -rf dist",
18
+ "clean": "rm -rf dist && rm -rf tsconfig.build.tsbuildinfo",
19
+ "compile": "tsc -p tsconfig.build.json",
20
+ "prepare": "yarn build",
19
21
  "test": "jest test",
20
- "type-check": "tsc --project ./tsconfig.json --noEmit",
22
+ "type-check": "tsc --project ./tsconfig.build.json --noEmit",
21
23
  "swapcli": "yarn build && node ./dist/swappercli.js"
22
24
  },
23
25
  "dependencies": {
@@ -39,7 +41,7 @@
39
41
  "@shapeshiftoss/caip": "^2.0.0",
40
42
  "@shapeshiftoss/chain-adapters": "^2.0.0",
41
43
  "@shapeshiftoss/hdwallet-core": "^1.20.0",
42
- "@shapeshiftoss/types": "^2.1.0",
44
+ "@shapeshiftoss/types": "^3.0.0",
43
45
  "@types/readline-sync": "^1.4.4",
44
46
  "readline-sync": "^1.4.10",
45
47
  "web3-utils": "^1.5.2"