@shapeshiftoss/swapper 17.0.2 → 17.2.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.
@@ -0,0 +1,3 @@
1
+ import { ChainId } from '@shapeshiftoss/caip';
2
+ export declare const isValidMemoAddress: (chainId: ChainId, thorId: string, address: string) => boolean;
3
+ export declare const assertIsValidMemo: (memo: string) => void;
@@ -0,0 +1,66 @@
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
+ exports.assertIsValidMemo = exports.isValidMemoAddress = void 0;
7
+ const caip_1 = require("@shapeshiftoss/caip");
8
+ const chain_adapters_1 = require("@shapeshiftoss/chain-adapters");
9
+ const multicoin_address_validator_1 = __importDefault(require("multicoin-address-validator"));
10
+ const api_1 = require("../../../../api");
11
+ const thorChainAssetToChainId = new Map([
12
+ ['ETH', caip_1.ethChainId],
13
+ ['AVAX', caip_1.avalancheChainId],
14
+ ['BTC', caip_1.btcChainId],
15
+ ['BCH', caip_1.bchChainId],
16
+ ['LTC', caip_1.ltcChainId],
17
+ ['BNB', caip_1.binanceChainId],
18
+ ['DOGE', caip_1.dogeChainId],
19
+ ['GAIA', caip_1.cosmosChainId],
20
+ ['RUNE', caip_1.thorchainChainId],
21
+ ]);
22
+ const isValidMemoAddress = (chainId, thorId, address) => {
23
+ switch (true) {
24
+ case thorId.startsWith('ETH.'):
25
+ case thorId.startsWith('AVAX.'):
26
+ case thorId.startsWith('BTC.'):
27
+ case thorId.startsWith('BCH.'):
28
+ case thorId.startsWith('LTC.'):
29
+ case thorId.startsWith('BNB.'):
30
+ case thorId.startsWith('DOGE.'): {
31
+ const chainLabel = (0, chain_adapters_1.chainIdToChainLabel)(chainId);
32
+ return multicoin_address_validator_1.default.validate(address, chainLabel);
33
+ }
34
+ case thorId.startsWith('GAIA.'):
35
+ return address.startsWith('cosmos');
36
+ case thorId.startsWith('RUNE.'):
37
+ return address.startsWith('thor');
38
+ default:
39
+ return false;
40
+ }
41
+ };
42
+ exports.isValidMemoAddress = isValidMemoAddress;
43
+ const assertIsValidMemo = (memo) => {
44
+ // BTC (and likely other utxo coins) can only support up to 80 character (byte) memos
45
+ const MAX_MEMO_LENGTH = 80;
46
+ const MEMO_PART_DELIMITER = ':';
47
+ const POOL_PART_DELIMITER = '.';
48
+ const memoParts = memo.split(MEMO_PART_DELIMITER);
49
+ const pool = memoParts[1];
50
+ const address = memoParts[2];
51
+ const buyAssetChainId = thorChainAssetToChainId.get(pool.split(POOL_PART_DELIMITER)[0]);
52
+ const isAddressValid = buyAssetChainId
53
+ ? (0, exports.isValidMemoAddress)(buyAssetChainId, pool, address)
54
+ : undefined;
55
+ if (!isAddressValid) {
56
+ throw new api_1.SwapError(`[makeSwapMemo] - memo ${memo} invalid`, {
57
+ code: api_1.SwapErrorType.MAKE_MEMO_FAILED,
58
+ });
59
+ }
60
+ if (memo.length > MAX_MEMO_LENGTH) {
61
+ throw new api_1.SwapError(`[makeSwapMemo] - memo length exceeds ${MAX_MEMO_LENGTH} characters`, {
62
+ code: api_1.SwapErrorType.MAKE_MEMO_FAILED,
63
+ });
64
+ }
65
+ };
66
+ exports.assertIsValidMemo = assertIsValidMemo;
@@ -4,8 +4,7 @@ exports.makeSwapMemo = exports.abbreviateThorAssetId = void 0;
4
4
  const caip_1 = require("@shapeshiftoss/caip");
5
5
  const api_1 = require("../../../../api");
6
6
  const constants_1 = require("../constants");
7
- // BTC (and likely other utxo coins) can only support up to 80 character (byte) memos
8
- const MAX_MEMO_LENGTH = 80;
7
+ const assertIsValidMemo_1 = require("./assertIsValidMemo");
9
8
  /**
10
9
  * definition of THORChain asset notation
11
10
  * https://dev.thorchain.org/thorchain-dev/concepts/memos#asset-notation
@@ -63,11 +62,7 @@ const makeSwapMemo = ({ buyAssetId, destinationAddress, limit }) => {
63
62
  : destinationAddress;
64
63
  const abbreviatedThorAssetId = (0, exports.abbreviateThorAssetId)(fullThorAssetId);
65
64
  const memo = `s:${abbreviatedThorAssetId}:${parsedDestinationAddress}:${limit}:${constants_1.THORCHAIN_AFFILIATE_NAME}:${constants_1.THORCHAIN_AFFILIATE_BIPS}`;
66
- if (memo.length > MAX_MEMO_LENGTH) {
67
- throw new api_1.SwapError(`[makeSwapMemo] - memo length exceeds ${MAX_MEMO_LENGTH} characters`, {
68
- code: api_1.SwapErrorType.MAKE_MEMO_FAILED,
69
- });
70
- }
65
+ (0, assertIsValidMemo_1.assertIsValidMemo)(memo);
71
66
  return memo;
72
67
  };
73
68
  exports.makeSwapMemo = makeSwapMemo;
@@ -1,3 +1,3 @@
1
1
  export declare const APPROVAL_GAS_LIMIT = "100000";
2
- export declare const DEFAULT_SLIPPAGE = "0.03";
2
+ export declare const DEFAULT_SLIPPAGE = "0.002";
3
3
  export declare const ALLOWABLE_MARKET_MOVEMENT = "0.01";
@@ -2,5 +2,5 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.ALLOWABLE_MARKET_MOVEMENT = exports.DEFAULT_SLIPPAGE = exports.APPROVAL_GAS_LIMIT = void 0;
4
4
  exports.APPROVAL_GAS_LIMIT = '100000'; // Most approvals are around 40k, we've seen 72k in the wild, so 100000 for safety.
5
- exports.DEFAULT_SLIPPAGE = '0.03'; // 3%
5
+ exports.DEFAULT_SLIPPAGE = '0.002'; // .2%
6
6
  exports.ALLOWABLE_MARKET_MOVEMENT = '0.01'; // 1%
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@shapeshiftoss/swapper",
3
- "version": "17.0.2",
3
+ "version": "17.2.0",
4
4
  "description": "An interface and implementations to trade coins via various protocols",
5
5
  "repository": "https://github.com/shapeshift/lib",
6
6
  "license": "MIT",