@chain-registry/utils 0.5.0 → 0.6.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.
- package/CHANGELOG.md +24 -0
- package/README.md +79 -1
- package/main/index.js +18 -399
- package/main/registry.js +312 -0
- package/main/utils.js +415 -0
- package/module/index.js +2 -338
- package/module/registry.js +216 -0
- package/module/utils.js +341 -0
- package/package.json +6 -3
package/module/utils.js
ADDED
|
@@ -0,0 +1,341 @@
|
|
|
1
|
+
import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
|
|
2
|
+
|
|
3
|
+
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
|
|
4
|
+
|
|
5
|
+
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
|
|
6
|
+
|
|
7
|
+
import { sha256 } from 'sha.js';
|
|
8
|
+
export const ibcDenom = (paths, coinMinimalDenom) => {
|
|
9
|
+
const prefixes = [];
|
|
10
|
+
|
|
11
|
+
for (const path of paths) {
|
|
12
|
+
prefixes.push(`${path.portId}/${path.channelId}`);
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
const prefix = prefixes.join('/');
|
|
16
|
+
const denom = `${prefix}/${coinMinimalDenom}`;
|
|
17
|
+
return 'ibc/' + Buffer.from(new Uint8Array(new sha256().update(Buffer.from(denom)).digest())).toString('hex').toUpperCase();
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
const findInfo = (ibc, to, from) => ibc.find(i => i['chain-1']['chain-name'] === from && i['chain-2']['chain-name'] === to);
|
|
21
|
+
|
|
22
|
+
export const getIbcInfo = (ibc, chain, counterparty) => {
|
|
23
|
+
return findInfo(ibc, chain, counterparty) || findInfo(ibc, counterparty, chain);
|
|
24
|
+
};
|
|
25
|
+
export const getTransferChannel = info => {
|
|
26
|
+
return info.channels.find(channel => channel['chain-1']['port-id'] === 'transfer' && channel['chain-2']['port-id'] === 'transfer');
|
|
27
|
+
};
|
|
28
|
+
export const getNonTransferChannel = info => {
|
|
29
|
+
return info.channels.find(channel => channel['chain-1']['port-id'] !== 'transfer' && channel['chain-2']['port-id'] === 'transfer' || channel['chain-1']['port-id'] === 'transfer' && channel['chain-2']['port-id'] !== 'transfer');
|
|
30
|
+
};
|
|
31
|
+
export const getWasmChannel = info => {
|
|
32
|
+
return info.channels.find(channel => channel['chain-1']['port-id'].startsWith('wasm.') && channel['chain-2']['port-id'] === 'transfer' || channel['chain-1']['port-id'] === 'transfer' && channel['chain-2']['port-id'].startsWith('wasm'));
|
|
33
|
+
};
|
|
34
|
+
export const getIbcDenomByBase = (ibc, chain, counterparty, assets, base) => {
|
|
35
|
+
const ibcInfo = getIbcInfo(ibc, chain, counterparty);
|
|
36
|
+
|
|
37
|
+
if (ibcInfo) {
|
|
38
|
+
const channel = getTransferChannel(ibcInfo);
|
|
39
|
+
|
|
40
|
+
if (!channel) {
|
|
41
|
+
return;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
let channelInfo;
|
|
45
|
+
|
|
46
|
+
if (ibcInfo['chain-1']['chain-name'] === chain) {
|
|
47
|
+
channelInfo = channel['chain-1'];
|
|
48
|
+
} else {
|
|
49
|
+
channelInfo = channel['chain-2'];
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
const assetList = assets.find(({
|
|
53
|
+
chain_name
|
|
54
|
+
}) => chain_name === counterparty);
|
|
55
|
+
|
|
56
|
+
if (!assetList) {
|
|
57
|
+
return;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
const assetInfo = assetList.assets.find(asset => {
|
|
61
|
+
return asset.base === base;
|
|
62
|
+
}); // if (!assetInfo) {
|
|
63
|
+
// console.warn('missing referrenced asset');
|
|
64
|
+
// }
|
|
65
|
+
// TODO transition later!
|
|
66
|
+
|
|
67
|
+
const traces = [];
|
|
68
|
+
|
|
69
|
+
if (assetInfo.ibc) {
|
|
70
|
+
traces.push({
|
|
71
|
+
type: 'ibc',
|
|
72
|
+
counterparty: {
|
|
73
|
+
denom: assetInfo.ibc.source_denom,
|
|
74
|
+
channel: assetInfo.ibc.source_channel
|
|
75
|
+
},
|
|
76
|
+
chain: {
|
|
77
|
+
channel: assetInfo.ibc.dst_channel
|
|
78
|
+
}
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
if (assetInfo.traces && assetInfo.traces.length) {
|
|
83
|
+
[].push.apply(traces, assetInfo.traces);
|
|
84
|
+
} // console.log(transition);
|
|
85
|
+
// if (transition.length) {
|
|
86
|
+
// return ibcDenom(
|
|
87
|
+
// [
|
|
88
|
+
// {
|
|
89
|
+
// portId: 'transfer',
|
|
90
|
+
// channelId: assetInfo.ibc.source_channel
|
|
91
|
+
// },
|
|
92
|
+
// {
|
|
93
|
+
// portId: channelInfo['port-id'],
|
|
94
|
+
// channelId: channelInfo['channel-id']
|
|
95
|
+
// }
|
|
96
|
+
// ],
|
|
97
|
+
// base
|
|
98
|
+
// );
|
|
99
|
+
// }
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+
return ibcDenom([{
|
|
103
|
+
portId: channelInfo['port-id'],
|
|
104
|
+
channelId: channelInfo['channel-id']
|
|
105
|
+
}], base);
|
|
106
|
+
}
|
|
107
|
+
};
|
|
108
|
+
export const getIbcDenomByBaseForCw20 = (ibc, chain, counterparty, assets, base) => {
|
|
109
|
+
const ibcInfo = getIbcInfo(ibc, chain, counterparty);
|
|
110
|
+
|
|
111
|
+
if (ibcInfo) {
|
|
112
|
+
const channel = getWasmChannel(ibcInfo);
|
|
113
|
+
|
|
114
|
+
if (!channel) {
|
|
115
|
+
return;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
let channelInfo;
|
|
119
|
+
|
|
120
|
+
if (ibcInfo['chain-1']['chain-name'] === chain) {
|
|
121
|
+
channelInfo = channel['chain-1'];
|
|
122
|
+
} else {
|
|
123
|
+
channelInfo = channel['chain-2'];
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
const assetInfo = assets.find(({
|
|
127
|
+
chain_name
|
|
128
|
+
}) => chain_name === counterparty);
|
|
129
|
+
|
|
130
|
+
if (!assetInfo) {
|
|
131
|
+
return;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
return ibcDenom([{
|
|
135
|
+
portId: channelInfo['port-id'],
|
|
136
|
+
channelId: channelInfo['channel-id']
|
|
137
|
+
}], base);
|
|
138
|
+
}
|
|
139
|
+
};
|
|
140
|
+
export const getIbcAssets = (chainName, ibc, assets) => {
|
|
141
|
+
const chainIbcInfo = ibc.filter(i => {
|
|
142
|
+
return i['chain-1']['chain-name'] === chainName || i['chain-2']['chain-name'] === chainName;
|
|
143
|
+
});
|
|
144
|
+
const ibcAssetLists = chainIbcInfo.map(ibcInfo => {
|
|
145
|
+
const counterpartyIs = ibcInfo['chain-1']['chain-name'] === chainName ? 'chain-2' : 'chain-1';
|
|
146
|
+
const chainIs = ibcInfo['chain-1']['chain-name'] === chainName ? 'chain-1' : 'chain-2';
|
|
147
|
+
const counterparty = ibcInfo[counterpartyIs]['chain-name'];
|
|
148
|
+
const counterpartyIbc = ibcInfo[counterpartyIs];
|
|
149
|
+
const chainIbc = ibcInfo[chainIs];
|
|
150
|
+
const counterpartyAssets = assets.find(a => {
|
|
151
|
+
return a.chain_name === counterparty;
|
|
152
|
+
});
|
|
153
|
+
|
|
154
|
+
if (!counterpartyAssets) {
|
|
155
|
+
// console.warn('asset not found: ' + counterparty);
|
|
156
|
+
return;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
const ibcAssets = counterpartyAssets.assets.filter(a => !a.base.startsWith('cw20:')).map(asset => {
|
|
160
|
+
const denom = getIbcDenomByBase(ibc, chainName, counterparty, //
|
|
161
|
+
assets, asset.base);
|
|
162
|
+
|
|
163
|
+
const newAsset = _objectSpread({}, asset);
|
|
164
|
+
|
|
165
|
+
newAsset.base = denom;
|
|
166
|
+
newAsset.denom_units = newAsset.denom_units.map(unit => {
|
|
167
|
+
if (unit.denom === asset.base) {
|
|
168
|
+
const newUnit = _objectSpread({}, unit);
|
|
169
|
+
|
|
170
|
+
newUnit.denom = denom;
|
|
171
|
+
newUnit.aliases = [unit.denom];
|
|
172
|
+
return newUnit;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
return unit;
|
|
176
|
+
});
|
|
177
|
+
return newAsset;
|
|
178
|
+
});
|
|
179
|
+
const channel = getTransferChannel(ibcInfo);
|
|
180
|
+
return {
|
|
181
|
+
chain: _objectSpread(_objectSpread({}, chainIbc), channel[chainIs]),
|
|
182
|
+
counterparty: _objectSpread(_objectSpread({}, counterpartyIbc), channel[counterpartyIs]),
|
|
183
|
+
assets: ibcAssets
|
|
184
|
+
};
|
|
185
|
+
}).filter(Boolean);
|
|
186
|
+
const hash = ibcAssetLists.reduce((m, v) => {
|
|
187
|
+
m[v.chain['chain-name']] = m[v.chain['chain-name']] || [];
|
|
188
|
+
const assets = v.assets.map(asset => {
|
|
189
|
+
return _objectSpread(_objectSpread({}, asset), {}, {
|
|
190
|
+
traces: [{
|
|
191
|
+
type: 'ibc',
|
|
192
|
+
counterparty: {
|
|
193
|
+
// source_channel
|
|
194
|
+
channel: v.counterparty['channel-id'],
|
|
195
|
+
// source_denom
|
|
196
|
+
denom: asset.denom_units[0].aliases[0],
|
|
197
|
+
chain_name: v.counterparty['chain-name'] // port: v.counterparty['port-id']
|
|
198
|
+
|
|
199
|
+
},
|
|
200
|
+
chain: {
|
|
201
|
+
// dst_denom
|
|
202
|
+
channel: v.chain['channel-id'] // chain_name: v.chain['chain-name'],
|
|
203
|
+
// port: v.chain['port-id']
|
|
204
|
+
|
|
205
|
+
}
|
|
206
|
+
}]
|
|
207
|
+
});
|
|
208
|
+
});
|
|
209
|
+
|
|
210
|
+
const obj = _objectSpread(_objectSpread({}, v), {}, {
|
|
211
|
+
assets
|
|
212
|
+
});
|
|
213
|
+
|
|
214
|
+
m[v.chain['chain-name']].push(obj);
|
|
215
|
+
return m;
|
|
216
|
+
}, {});
|
|
217
|
+
return Object.keys(hash).map(chain => {
|
|
218
|
+
return {
|
|
219
|
+
chain_name: chain,
|
|
220
|
+
assets: hash[chain].reduce((m, v) => {
|
|
221
|
+
return [...m, ...v.assets];
|
|
222
|
+
}, [])
|
|
223
|
+
};
|
|
224
|
+
});
|
|
225
|
+
};
|
|
226
|
+
export const getCw20Assets = (chainName, ibc, assets) => {
|
|
227
|
+
const chainIbcInfo = ibc.filter(i => {
|
|
228
|
+
return i['chain-1']['chain-name'] === chainName || i['chain-2']['chain-name'] === chainName;
|
|
229
|
+
});
|
|
230
|
+
const cw20AssetLists = chainIbcInfo.map(ibcInfo => {
|
|
231
|
+
const counterpartyIs = ibcInfo['chain-1']['chain-name'] === chainName ? 'chain-2' : 'chain-1';
|
|
232
|
+
const chainIs = ibcInfo['chain-1']['chain-name'] === chainName ? 'chain-1' : 'chain-2';
|
|
233
|
+
const counterparty = ibcInfo[counterpartyIs]['chain-name'];
|
|
234
|
+
const counterpartyIbc = ibcInfo[counterpartyIs];
|
|
235
|
+
const chainIbc = ibcInfo[chainIs];
|
|
236
|
+
const counterpartyAssets = assets.find(a => {
|
|
237
|
+
return a.chain_name === counterparty;
|
|
238
|
+
});
|
|
239
|
+
|
|
240
|
+
if (!counterpartyAssets) {
|
|
241
|
+
// console.warn('asset not found: ' + counterparty);
|
|
242
|
+
return;
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
const cw20Assets = counterpartyAssets.assets.filter(a => a.base.startsWith('cw20:')).map(asset => {
|
|
246
|
+
const denom = getIbcDenomByBaseForCw20(ibc, chainName, counterparty, //
|
|
247
|
+
assets, asset.base);
|
|
248
|
+
|
|
249
|
+
const newAsset = _objectSpread({}, asset);
|
|
250
|
+
|
|
251
|
+
newAsset.base = denom;
|
|
252
|
+
newAsset.denom_units = newAsset.denom_units.map(unit => {
|
|
253
|
+
if (unit.denom === asset.base) {
|
|
254
|
+
const newUnit = _objectSpread({}, unit);
|
|
255
|
+
|
|
256
|
+
newUnit.denom = denom;
|
|
257
|
+
newUnit.aliases = [unit.denom];
|
|
258
|
+
return newUnit;
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
return unit;
|
|
262
|
+
});
|
|
263
|
+
return newAsset;
|
|
264
|
+
});
|
|
265
|
+
if (!cw20Assets.length) return;
|
|
266
|
+
const channel = getWasmChannel(ibcInfo);
|
|
267
|
+
|
|
268
|
+
if (!channel) {
|
|
269
|
+
// console.warn(
|
|
270
|
+
// chainIbc['chain-name'],
|
|
271
|
+
// '<>',
|
|
272
|
+
// counterpartyIbc['chain-name'],
|
|
273
|
+
// 'MISSING cw20 IBC info'
|
|
274
|
+
// );
|
|
275
|
+
return;
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
return {
|
|
279
|
+
chain: _objectSpread(_objectSpread({}, chainIbc), channel[chainIs]),
|
|
280
|
+
counterparty: _objectSpread(_objectSpread({}, counterpartyIbc), channel[counterpartyIs]),
|
|
281
|
+
assets: cw20Assets
|
|
282
|
+
};
|
|
283
|
+
}).filter(Boolean);
|
|
284
|
+
const hash = cw20AssetLists.reduce((m, v) => {
|
|
285
|
+
m[v.chain['chain-name']] = m[v.chain['chain-name']] || [];
|
|
286
|
+
const assets = v.assets.map(asset => {
|
|
287
|
+
return _objectSpread(_objectSpread({}, asset), {}, {
|
|
288
|
+
traces: [{
|
|
289
|
+
type: 'ibc',
|
|
290
|
+
counterparty: {
|
|
291
|
+
port: v.counterparty['port-id'],
|
|
292
|
+
// source_channel
|
|
293
|
+
channel: v.counterparty['channel-id'],
|
|
294
|
+
// source_denom
|
|
295
|
+
denom: asset.denom_units[0].aliases[0],
|
|
296
|
+
chain_name: v.counterparty['chain-name']
|
|
297
|
+
},
|
|
298
|
+
chain: {
|
|
299
|
+
// dst_denom
|
|
300
|
+
port: v.chain['port-id'],
|
|
301
|
+
channel: v.chain['channel-id'] // chain_name: v.chain['chain-name'],
|
|
302
|
+
|
|
303
|
+
}
|
|
304
|
+
}]
|
|
305
|
+
});
|
|
306
|
+
});
|
|
307
|
+
|
|
308
|
+
const obj = _objectSpread(_objectSpread({}, v), {}, {
|
|
309
|
+
assets
|
|
310
|
+
});
|
|
311
|
+
|
|
312
|
+
m[v.chain['chain-name']].push(obj);
|
|
313
|
+
return m;
|
|
314
|
+
}, {});
|
|
315
|
+
return Object.keys(hash).map(chain => {
|
|
316
|
+
return {
|
|
317
|
+
chain_name: chain,
|
|
318
|
+
assets: hash[chain].reduce((m, v) => {
|
|
319
|
+
return [...m, ...v.assets];
|
|
320
|
+
}, [])
|
|
321
|
+
};
|
|
322
|
+
});
|
|
323
|
+
};
|
|
324
|
+
export const getAssetLists = (chainName, ibc, assets) => {
|
|
325
|
+
const ibcAssetLists = getIbcAssets(chainName, ibc, assets);
|
|
326
|
+
const cw20Assets = getCw20Assets(chainName, ibc, assets);
|
|
327
|
+
return ibcAssetLists.reduce((m, v) => {
|
|
328
|
+
const chain = v.chain_name;
|
|
329
|
+
const assets = [...v.assets];
|
|
330
|
+
const cw20 = cw20Assets.find(a => a.chain_name === chain);
|
|
331
|
+
|
|
332
|
+
if (cw20) {
|
|
333
|
+
[].push.apply(assets, cw20.assets);
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
return [{
|
|
337
|
+
chain_name: chain,
|
|
338
|
+
assets
|
|
339
|
+
}, ...m];
|
|
340
|
+
}, []);
|
|
341
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@chain-registry/utils",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.6.1",
|
|
4
4
|
"description": "Chain Registry Utils",
|
|
5
5
|
"author": "Dan Lynch <pyramation@gmail.com>",
|
|
6
6
|
"homepage": "https://github.com/cosmology-tech/chain-registry",
|
|
@@ -56,12 +56,14 @@
|
|
|
56
56
|
"@babel/preset-env": "7.18.2",
|
|
57
57
|
"@babel/preset-typescript": "^7.17.12",
|
|
58
58
|
"@types/jest": "^28.1.1",
|
|
59
|
+
"@types/sha.js": "^2.4.0",
|
|
59
60
|
"@typescript-eslint/eslint-plugin": "5.31.0",
|
|
60
61
|
"@typescript-eslint/parser": "5.31.0",
|
|
61
62
|
"babel-core": "7.0.0-bridge.0",
|
|
62
63
|
"babel-jest": "28.1.1",
|
|
63
64
|
"babel-watch": "^7.0.0",
|
|
64
65
|
"cross-env": "^7.0.2",
|
|
66
|
+
"cross-fetch": "^3.1.5",
|
|
65
67
|
"eslint": "8.20.0",
|
|
66
68
|
"eslint-config-prettier": "^8.5.0",
|
|
67
69
|
"eslint-plugin-prettier": "^4.0.0",
|
|
@@ -77,7 +79,8 @@
|
|
|
77
79
|
"dependencies": {
|
|
78
80
|
"@babel/runtime": "^7.18.3",
|
|
79
81
|
"@chain-registry/types": "^0.6.0",
|
|
80
|
-
"
|
|
82
|
+
"bfs-path": "^1.0.2",
|
|
83
|
+
"sha.js": "^2.4.11"
|
|
81
84
|
},
|
|
82
|
-
"gitHead": "
|
|
85
|
+
"gitHead": "a09e68e6397e979882e732f4fc833d28c4151293"
|
|
83
86
|
}
|