@shapeshiftoss/caip 1.11.2 → 1.11.3

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.
@@ -5,7 +5,9 @@ export declare enum AssetNamespace {
5
5
  CW721 = "cw721",
6
6
  ERC20 = "erc20",
7
7
  ERC721 = "erc721",
8
- Slip44 = "slip44"
8
+ Slip44 = "slip44",
9
+ NATIVE = "native",
10
+ IBC = "ibc"
9
11
  }
10
12
  export declare enum AssetReference {
11
13
  Bitcoin = 0,
@@ -17,6 +19,8 @@ declare type ToCAIP19Args = {
17
19
  network: NetworkTypes;
18
20
  contractType?: ContractTypes;
19
21
  tokenId?: string;
22
+ assetNamespace?: AssetNamespace;
23
+ assetReference?: string;
20
24
  };
21
25
  declare type ToCAIP19 = (args: ToCAIP19Args) => string;
22
26
  export declare const toCAIP19: ToCAIP19;
@@ -25,6 +29,8 @@ declare type FromCAIP19Return = {
25
29
  network: NetworkTypes;
26
30
  contractType?: ContractTypes;
27
31
  tokenId?: string;
32
+ assetNamespace?: AssetNamespace;
33
+ assetReference?: string;
28
34
  };
29
35
  declare type FromCAIP19 = (caip19: string) => FromCAIP19Return;
30
36
  export declare const fromCAIP19: FromCAIP19;
@@ -11,6 +11,8 @@ var AssetNamespace;
11
11
  AssetNamespace["ERC20"] = "erc20";
12
12
  AssetNamespace["ERC721"] = "erc721";
13
13
  AssetNamespace["Slip44"] = "slip44";
14
+ AssetNamespace["NATIVE"] = "native";
15
+ AssetNamespace["IBC"] = "ibc";
14
16
  })(AssetNamespace = exports.AssetNamespace || (exports.AssetNamespace = {}));
15
17
  var AssetReference;
16
18
  (function (AssetReference) {
@@ -18,13 +20,25 @@ var AssetReference;
18
20
  AssetReference[AssetReference["Ethereum"] = 60] = "Ethereum";
19
21
  AssetReference[AssetReference["Cosmos"] = 118] = "Cosmos";
20
22
  })(AssetReference = exports.AssetReference || (exports.AssetReference = {}));
21
- const toCAIP19 = ({ chain, network, contractType, tokenId }) => {
23
+ const toCAIP19 = ({ chain, network, contractType, tokenId, assetNamespace, assetReference }) => {
22
24
  const caip2 = (0, caip2_1.toCAIP2)({ chain, network });
23
25
  switch (chain) {
24
26
  // TODO: There is no chain-level fungible token standard in Cosmos SDK, but CosmWasm chains have CW20/CW721
25
27
  // This should be implemented when we want to support tokens for Cosmos SDK chains
26
28
  case types_1.ChainTypes.Cosmos: {
27
- return `${caip2}/${AssetNamespace.Slip44}:${AssetReference.Cosmos}`;
29
+ if (!assetNamespace || !assetReference) {
30
+ return `${caip2}/${AssetNamespace.Slip44}:${AssetReference.Cosmos}`;
31
+ }
32
+ switch (assetNamespace) {
33
+ case AssetNamespace.CW20:
34
+ case AssetNamespace.CW721:
35
+ case AssetNamespace.IBC:
36
+ case AssetNamespace.NATIVE:
37
+ return `${caip2}/${assetNamespace}:${assetReference}`;
38
+ default: {
39
+ throw new Error(`Could not construct CAIP19 chain: ${chain}, network: ${network}, assetNamespace: ${assetNamespace}, assetReference: ${assetReference}`);
40
+ }
41
+ }
28
42
  }
29
43
  case types_1.ChainTypes.Ethereum: {
30
44
  tokenId = tokenId === null || tokenId === void 0 ? void 0 : tokenId.toLowerCase();
@@ -65,6 +79,9 @@ const toCAIP19 = ({ chain, network, contractType, tokenId }) => {
65
79
  case types_1.ChainTypes.Bitcoin: {
66
80
  return `${caip2}/${AssetNamespace.Slip44}:${AssetReference.Bitcoin}`;
67
81
  }
82
+ default: {
83
+ throw new Error(`Chain type not supported: ${chain}`);
84
+ }
68
85
  }
69
86
  };
70
87
  exports.toCAIP19 = toCAIP19;
@@ -87,18 +104,13 @@ const fromCAIP19 = (caip19) => {
87
104
  case AssetReference.Bitcoin: {
88
105
  return { chain, network };
89
106
  }
90
- case AssetReference.Ethereum:
91
107
  default: {
92
108
  throw new Error(`fromCAIP19: invalid asset reference ${reference} on chain ${chain}`);
93
109
  }
94
110
  }
95
111
  }
96
- case AssetNamespace.ERC20:
97
- case AssetNamespace.ERC721:
98
- case AssetNamespace.CW20:
99
- case AssetNamespace.CW721:
100
112
  default: {
101
- throw new Error(`fromCAIP19: invalid asset reference ${reference} on chain ${chain}`);
113
+ throw new Error(`fromCAIP19: invalid asset namespace ${namespace} on chain ${chain}`);
102
114
  }
103
115
  }
104
116
  }
@@ -109,7 +121,6 @@ const fromCAIP19 = (caip19) => {
109
121
  case AssetReference.Ethereum: {
110
122
  return { chain, network };
111
123
  }
112
- case AssetReference.Bitcoin:
113
124
  default: {
114
125
  throw new Error(`fromCAIP19: invalid asset reference ${reference} on chain ${chain}`);
115
126
  }
@@ -125,10 +136,55 @@ const fromCAIP19 = (caip19) => {
125
136
  const tokenId = referenceString.toLowerCase();
126
137
  return { chain, network, contractType, tokenId };
127
138
  }
139
+ default: {
140
+ throw new Error(`fromCAIP19: invalid asset namespace ${namespace} on chain ${chain}`);
141
+ }
142
+ }
143
+ }
144
+ case types_1.ChainTypes.Cosmos: {
145
+ switch (namespace) {
146
+ case AssetNamespace.Slip44: {
147
+ switch (reference) {
148
+ case AssetReference.Cosmos: {
149
+ return { chain, network };
150
+ }
151
+ case AssetReference.Ethereum:
152
+ case AssetReference.Bitcoin:
153
+ default: {
154
+ throw new Error(`fromCAIP19: invalid asset namespace ${namespace} on chain ${chain}`);
155
+ }
156
+ }
157
+ }
128
158
  case AssetNamespace.CW20:
159
+ return {
160
+ chain,
161
+ network,
162
+ assetNamespace: AssetNamespace.CW20,
163
+ assetReference: referenceString
164
+ };
129
165
  case AssetNamespace.CW721:
166
+ return {
167
+ chain,
168
+ network,
169
+ assetNamespace: AssetNamespace.CW721,
170
+ assetReference: referenceString
171
+ };
172
+ case AssetNamespace.IBC:
173
+ return {
174
+ chain,
175
+ network,
176
+ assetNamespace: AssetNamespace.IBC,
177
+ assetReference: referenceString
178
+ };
179
+ case AssetNamespace.NATIVE:
180
+ return {
181
+ chain,
182
+ network,
183
+ assetNamespace: AssetNamespace.NATIVE,
184
+ assetReference: referenceString
185
+ };
130
186
  default: {
131
- throw new Error(`fromCAIP19: invalid asset reference ${reference} on chain ${chain}`);
187
+ throw new Error(`fromCAIP19: invalid asset namespace ${namespace} on chain ${chain}`);
132
188
  }
133
189
  }
134
190
  }
@@ -105,7 +105,7 @@ const fromCAIP2 = (caip2) => {
105
105
  const chain = types_1.ChainTypes.Cosmos;
106
106
  switch (n) {
107
107
  case ChainReference.CosmosHubMainnet: {
108
- const network = types_1.NetworkTypes.MAINNET;
108
+ const network = types_1.NetworkTypes.COSMOSHUB_MAINNET;
109
109
  return { chain, network };
110
110
  }
111
111
  case ChainReference.CosmosHubVega: {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@shapeshiftoss/caip",
3
- "version": "1.11.2",
3
+ "version": "1.11.3",
4
4
  "description": "CAIP Implementation",
5
5
  "homepage": "",
6
6
  "license": "MIT",