@shapeshiftoss/swapper 17.0.0 → 17.0.2

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.
@@ -1,9 +1,20 @@
1
1
  /**
2
- * @returns thorchain memo shortened to a max of 80 characters as described:
3
- * https://dev.thorchain.org/thorchain-dev/memos#mechanism-for-transaction-intent
2
+ * definition of THORChain asset notation
3
+ * https://dev.thorchain.org/thorchain-dev/concepts/memos#asset-notation
4
+ *
5
+ * e.g. BTC/BTC (bitcoin synth) - unaffected by this function
6
+ * e.g. ETH.USDT-0xdac17f958d2ee523a2206206994597c13d831ec7 (usdt on ethereum) - abbreviated by this function
4
7
  */
5
- export declare const makeSwapMemo: ({ buyAssetId, destinationAddress, limit, }: {
8
+ export declare const abbreviateThorAssetId: (fullThorAssetId: string) => string;
9
+ declare type MakeSwapMemoArgs = {
6
10
  buyAssetId: string;
7
11
  destinationAddress: string;
8
12
  limit: string;
9
- }) => string;
13
+ };
14
+ declare type MakeSwapMemo = (args: MakeSwapMemoArgs) => string;
15
+ /**
16
+ * @returns thorchain memo shortened to a max of 80 characters as described:
17
+ * https://dev.thorchain.org/thorchain-dev/memos#mechanism-for-transaction-intent
18
+ */
19
+ export declare const makeSwapMemo: MakeSwapMemo;
20
+ export {};
@@ -1,22 +1,56 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.makeSwapMemo = void 0;
3
+ 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 memos
8
- const MAX_LENGTH = 80;
7
+ // BTC (and likely other utxo coins) can only support up to 80 character (byte) memos
8
+ const MAX_MEMO_LENGTH = 80;
9
+ /**
10
+ * definition of THORChain asset notation
11
+ * https://dev.thorchain.org/thorchain-dev/concepts/memos#asset-notation
12
+ *
13
+ * e.g. BTC/BTC (bitcoin synth) - unaffected by this function
14
+ * e.g. ETH.USDT-0xdac17f958d2ee523a2206206994597c13d831ec7 (usdt on ethereum) - abbreviated by this function
15
+ */
16
+ const abbreviateThorAssetId = (fullThorAssetId) => {
17
+ const CONTRACT_ADDRESS_DELIMITER = '-';
18
+ const [existingPrefix, contractAddress] = fullThorAssetId.split(CONTRACT_ADDRESS_DELIMITER);
19
+ // we can't shorten any asset that doesn't have a contract address
20
+ if (!contractAddress)
21
+ return fullThorAssetId;
22
+ /**
23
+ * https://dev.thorchain.org/thorchain-dev/concepts/memos#asset-abbreviations
24
+ *
25
+ * ETH.USDT
26
+ * ETH.USDT-ec7 // 3 characters - "short"
27
+ * ETH.USDT-6994597c13d831ec7 // 17 characters - "medium"
28
+ * ETH.USDT-0xdac17f958d2ee523a2206206994597c13d831ec7 // 26 characters - "long"
29
+ */
30
+ /**
31
+ * for the purposes of future proofing, let's use the shortest permissible format for contract addresses
32
+ * this has (26 + 10)^3 = 46,656 of entropy, and per the docs, the deepest pool will be used upon a collision
33
+ */
34
+ const SHORT_ABBREVIATION_LENGTH = 3;
35
+ // take the last n characters of the contract address (negative slice takes last n characters)
36
+ const shortenedContractAddress = contractAddress.slice(-SHORT_ABBREVIATION_LENGTH);
37
+ // and put it back together with the existing prefix
38
+ return [existingPrefix, shortenedContractAddress].join(CONTRACT_ADDRESS_DELIMITER);
39
+ };
40
+ exports.abbreviateThorAssetId = abbreviateThorAssetId;
9
41
  // use symbol of thor Asset?
10
42
  const runeThorId = 'RUNE';
11
43
  /**
12
44
  * @returns thorchain memo shortened to a max of 80 characters as described:
13
45
  * https://dev.thorchain.org/thorchain-dev/memos#mechanism-for-transaction-intent
14
46
  */
15
- const makeSwapMemo = ({ buyAssetId, destinationAddress, limit, }) => {
47
+ const makeSwapMemo = ({ buyAssetId, destinationAddress, limit }) => {
16
48
  const isRune = buyAssetId === caip_1.thorchainAssetId;
17
- const thorId = isRune ? runeThorId : caip_1.adapters.assetIdToPoolAssetId({ assetId: buyAssetId });
18
- if (!thorId)
19
- throw new api_1.SwapError('[makeSwapMemo] - undefined thorId for given buyAssetId', {
49
+ const fullThorAssetId = isRune
50
+ ? runeThorId
51
+ : caip_1.adapters.assetIdToPoolAssetId({ assetId: buyAssetId });
52
+ if (!fullThorAssetId)
53
+ throw new api_1.SwapError('[makeSwapMemo] - undefined thorAssetId for given buyAssetId', {
20
54
  code: api_1.SwapErrorType.MAKE_MEMO_FAILED,
21
55
  details: { buyAssetId },
22
56
  });
@@ -27,21 +61,13 @@ const makeSwapMemo = ({ buyAssetId, destinationAddress, limit, }) => {
27
61
  const parsedDestinationAddress = destinationAddress.includes('bitcoincash:')
28
62
  ? destinationAddress.replace('bitcoincash:', '')
29
63
  : destinationAddress;
30
- const memo = `s:${thorId}:${parsedDestinationAddress}:${limit}:${constants_1.THORCHAIN_AFFILIATE_NAME}:${constants_1.THORCHAIN_AFFILIATE_BIPS}`;
31
- if (memo.length <= MAX_LENGTH)
32
- return memo;
33
- const abbreviationAmount = memo.length - MAX_LENGTH;
34
- if (abbreviationAmount > 39)
35
- throw new api_1.SwapError('[makeSwapMemo] - too much abbreviation for accurate matching', {
36
- code: api_1.SwapErrorType.MAKE_MEMO_FAILED,
37
- });
38
- // delimeter between ticker and id allowing us to abbreviate the id: https://dev.thorchain.org/thorchain-dev/memos#asset-notation
39
- const delimeterIndex = memo.indexOf('-') + 1;
40
- if (!delimeterIndex) {
41
- throw new api_1.SwapError('[makeSwapMemo] - unable to abbreviate asset, no delimeter found', {
64
+ const abbreviatedThorAssetId = (0, exports.abbreviateThorAssetId)(fullThorAssetId);
65
+ 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`, {
42
68
  code: api_1.SwapErrorType.MAKE_MEMO_FAILED,
43
69
  });
44
70
  }
45
- return memo.replace(memo.slice(delimeterIndex, delimeterIndex + abbreviationAmount), '');
71
+ return memo;
46
72
  };
47
73
  exports.makeSwapMemo = makeSwapMemo;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@shapeshiftoss/swapper",
3
- "version": "17.0.0",
3
+ "version": "17.0.2",
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",
@@ -33,7 +33,7 @@
33
33
  "peerDependencies": {
34
34
  "@shapeshiftoss/asset-service": "^8.0.1",
35
35
  "@shapeshiftoss/caip": "^8.0.0",
36
- "@shapeshiftoss/chain-adapters": "^10.5.0",
36
+ "@shapeshiftoss/chain-adapters": "^11.0.0",
37
37
  "@shapeshiftoss/errors": "^1.1.2",
38
38
  "@shapeshiftoss/hdwallet-core": "^1.42.0",
39
39
  "@shapeshiftoss/types": "^8.1.0"
@@ -41,7 +41,7 @@
41
41
  "devDependencies": {
42
42
  "@shapeshiftoss/asset-service": "^8.0.1",
43
43
  "@shapeshiftoss/caip": "^8.0.0",
44
- "@shapeshiftoss/chain-adapters": "^10.5.0",
44
+ "@shapeshiftoss/chain-adapters": "^11.0.0",
45
45
  "@shapeshiftoss/errors": "^1.1.2",
46
46
  "@shapeshiftoss/hdwallet-core": "^1.42.0",
47
47
  "@shapeshiftoss/types": "^8.1.0",