@shapeshiftoss/caip 5.0.1 → 5.2.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.
@@ -4,3 +4,4 @@ export * from './yearn';
4
4
  export * from './osmosis';
5
5
  export * from './gem';
6
6
  export * from './banxa';
7
+ export * from './thortrading';
@@ -20,3 +20,4 @@ __exportStar(require("./yearn"), exports);
20
20
  __exportStar(require("./osmosis"), exports);
21
21
  __exportStar(require("./gem"), exports);
22
22
  __exportStar(require("./banxa"), exports);
23
+ __exportStar(require("./thortrading"), exports);
@@ -0,0 +1,6 @@
1
+ import { AssetId } from './../../assetId/assetId';
2
+ export declare const poolAssetIdToAssetId: (id: string) => AssetId | undefined;
3
+ export declare const assetIdToPoolAssetId: ({ assetId, symbol }: {
4
+ assetId: AssetId;
5
+ symbol?: string | undefined;
6
+ }) => string | undefined;
@@ -0,0 +1,80 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.assetIdToPoolAssetId = exports.poolAssetIdToAssetId = void 0;
4
+ const assetId_1 = require("../../assetId/assetId");
5
+ const constants_1 = require("../../constants");
6
+ const constants_2 = require("./../../constants");
7
+ // naming convention for regex variables are taken from thorchian docs
8
+ // https://dev.thorchain.org/thorchain-dev/memos#asset-notation
9
+ // https://regex101.com/r/MAj4WA/1
10
+ const thorIdRegex = /(?<chain>[A-Za-z]+)\.(?<ticker>[A-Za-z]+)(-(?<id>[A-Za-z0-9]+))?/;
11
+ /*
12
+ * Note: this only works with thorchain assets we support
13
+ * see https://dev.thorchain.org/thorchain-dev/memos#asset-notation for reference
14
+ *
15
+ * We are also not supporting asset abbreviations using fuzzy logic at this time
16
+ * see https://dev.thorchain.org/thorchain-dev/memos#asset-abbreviations
17
+ */
18
+ const poolAssetIdToAssetId = (id) => {
19
+ var _a;
20
+ const matches = (_a = thorIdRegex.exec(id)) === null || _a === void 0 ? void 0 : _a.groups;
21
+ // To avoid any case sensitive issues, uppercase both chain and ticker.
22
+ const chain = matches === null || matches === void 0 ? void 0 : matches.chain.toUpperCase();
23
+ const ticker = matches === null || matches === void 0 ? void 0 : matches.ticker.toUpperCase();
24
+ const contractAddress = matches === null || matches === void 0 ? void 0 : matches.id;
25
+ switch (chain) {
26
+ case 'ETH': {
27
+ if (ticker === 'ETH')
28
+ return constants_1.ethAssetId;
29
+ // We make Eth contract addresses lower case to simplify comparisons
30
+ const assetReference = contractAddress === null || contractAddress === void 0 ? void 0 : contractAddress.toLowerCase();
31
+ if (!assetReference)
32
+ return undefined;
33
+ const chainId = constants_1.ethChainId;
34
+ const assetNamespace = 'erc20';
35
+ try {
36
+ return (0, assetId_1.toAssetId)({ chainId, assetNamespace, assetReference });
37
+ }
38
+ catch (e) {
39
+ console.error(e);
40
+ return undefined;
41
+ }
42
+ }
43
+ case 'BTC': {
44
+ return constants_1.btcAssetId;
45
+ }
46
+ default: {
47
+ return undefined;
48
+ }
49
+ }
50
+ };
51
+ exports.poolAssetIdToAssetId = poolAssetIdToAssetId;
52
+ const assetIdToPoolAssetId = ({ assetId, symbol }) => {
53
+ try {
54
+ const { chainId, assetReference } = (0, assetId_1.fromAssetId)(assetId);
55
+ // https://dev.thorchain.org/thorchain-dev/memos#asset-notation
56
+ switch (chainId) {
57
+ case constants_1.ethChainId: {
58
+ if (assetId === constants_1.ethAssetId)
59
+ return 'ETH.ETH';
60
+ if (!symbol)
61
+ return undefined;
62
+ // this is predicated on the assumption that the symbol from the asset service
63
+ // and the contract address are static, correct, and won't ever change. Midgard
64
+ // also returns the asset notation with the assetReference in uppercase format,
65
+ // so we will keep that consistent here and uppercase it.
66
+ return `ETH.${symbol}-${assetReference.toUpperCase()}`;
67
+ }
68
+ case constants_2.btcChainId: {
69
+ return 'BTC.BTC';
70
+ }
71
+ default: {
72
+ return undefined;
73
+ }
74
+ }
75
+ }
76
+ catch (e) {
77
+ return undefined;
78
+ }
79
+ };
80
+ exports.assetIdToPoolAssetId = assetIdToPoolAssetId;
@@ -72,16 +72,20 @@ const fromAssetId = (assetId) => {
72
72
  var _a;
73
73
  if (!(0, typeGuards_1.isAssetId)(assetId))
74
74
  throw new Error(`fromAssetId: invalid AssetId: ${assetId}`);
75
- const matches = (_a = utils_1.parseAssetIdRegExp.exec(assetId)) !== null && _a !== void 0 ? _a : [];
75
+ const matches = (_a = utils_1.parseAssetIdRegExp.exec(assetId)) === null || _a === void 0 ? void 0 : _a.groups;
76
+ if (!matches)
77
+ throw new Error(`fromAssetId: could not parse AssetId: ${assetId}`);
76
78
  // These should never throw because isAssetId() would have already caught it, but they help with type inference
77
- (0, typeGuards_1.assertIsChainNamespace)(matches[1]);
78
- (0, typeGuards_1.assertIsChainReference)(matches[2]);
79
- (0, typeGuards_1.assertIsAssetNamespace)(matches[3]);
80
- const chainNamespace = matches[1];
81
- const chainReference = matches[2];
82
- const assetNamespace = matches[3];
79
+ (0, typeGuards_1.assertIsChainNamespace)(matches.chainNamespace);
80
+ (0, typeGuards_1.assertIsChainReference)(matches.chainReference);
81
+ (0, typeGuards_1.assertIsAssetNamespace)(matches.assetNamespace);
82
+ const chainNamespace = matches.chainNamespace;
83
+ const chainReference = matches.chainReference;
84
+ const assetNamespace = matches.assetNamespace;
83
85
  const shouldLowercaseAssetReference = assetNamespace && ['erc20', 'erc721'].includes(assetNamespace);
84
- const assetReference = shouldLowercaseAssetReference ? (0, toLower_1.default)(matches[4]) : matches[4];
86
+ const assetReference = shouldLowercaseAssetReference
87
+ ? (0, toLower_1.default)(matches.assetReference)
88
+ : matches.assetReference;
85
89
  (0, typeGuards_1.assertIsChainReference)(chainReference);
86
90
  (0, typeGuards_1.assertIsChainNamespace)(chainNamespace);
87
91
  const chainId = (0, chainId_1.toChainId)({ chainNamespace, chainReference });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@shapeshiftoss/caip",
3
- "version": "5.0.1",
3
+ "version": "5.2.1",
4
4
  "description": "CAIP Implementation",
5
5
  "homepage": "",
6
6
  "license": "MIT",
@@ -31,10 +31,10 @@
31
31
  "type-check": "tsc --project ./tsconfig.build.json --noEmit"
32
32
  },
33
33
  "peerDependencies": {
34
- "@shapeshiftoss/types": "3.1.3"
34
+ "@shapeshiftoss/types": "^4.3.1"
35
35
  },
36
36
  "devDependencies": {
37
- "@shapeshiftoss/types": "3.1.3",
37
+ "@shapeshiftoss/types": "^4.3.1",
38
38
  "@yfi/sdk": "^1.0.30",
39
39
  "axios": "^0.26.1",
40
40
  "ts-node": "^10.7.0"