@shapeshiftoss/swapper 17.0.1 → 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
|
-
*
|
|
3
|
-
* https://dev.thorchain.org/thorchain-dev/memos#
|
|
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
|
|
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
|
-
}
|
|
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
|
|
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
|
|
18
|
-
|
|
19
|
-
|
|
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
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
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
|
|
71
|
+
return memo;
|
|
46
72
|
};
|
|
47
73
|
exports.makeSwapMemo = makeSwapMemo;
|
package/package.json
CHANGED