-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathindex.ts
167 lines (146 loc) · 5.2 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
import { Asset, AssetList, Chain } from '@chain-registry/types';
import { Bech32Address } from '@keplr-wallet/cosmos';
import { ChainInfo, Currency, FeeCurrency } from '@keplr-wallet/types';
import semver from 'semver';
const getRpc = (chain: Chain): string => chain.apis?.rpc?.[0]?.address ?? '';
const getRest = (chain: Chain): string => chain.apis?.rest?.[0]?.address ?? '';
const getExplr = (chain: Chain): string => chain.explorers?.[0]?.url ?? '';
const cleanVer = (ver: string) => {
if (!semver.valid(ver)) {
// try to split by @ for repo@version
ver = ver.split('@').pop();
if (semver.valid(ver)) return ver;
const spaces = ver.split('.').length;
switch (spaces) {
case 1:
return ver + '.0.0';
case 2:
return ver + '.0';
case 3:
default:
throw new Error(
'contact @chain-registry/keplr maintainers: bad version'
);
}
}
};
export const chainRegistryChainToKeplr = (
chain: Chain,
assets: AssetList[],
options: {
getRpcEndpoint?: (chain: Chain) => string;
getRestEndpoint?: (chain: Chain) => string;
getExplorer?: (chain: Chain) => string;
} = {
getRpcEndpoint: getRpc,
getRestEndpoint: getRest,
getExplorer: getExplr
}
): ChainInfo => {
if (!options.getRestEndpoint) options.getRestEndpoint = getRest;
if (!options.getRpcEndpoint) options.getRpcEndpoint = getRpc;
if (!options.getExplorer) options.getExplorer = getExplr;
const features = [];
// if NOT specified, we assume stargate, sorry not sorry
const sdkVer = chain.codebase?.cosmos_sdk_version
? cleanVer(chain.codebase?.cosmos_sdk_version)
: '0.40';
// stargate
if (semver.satisfies(sdkVer, '>=0.40')) features.push('stargate');
// no-legacy-stdTx
if (semver.satisfies(sdkVer, '>=0.43')) features.push('no-legacy-stdTx');
// until further notice, assume 'ibc-transfer'
features.push('ibc-transfer');
// ibc-go
if (semver.satisfies(sdkVer, '>=0.45')) features.push('ibc-go');
if (chain.codebase?.cosmwasm_enabled) {
features.push('cosmwasm');
const wasmVer = cleanVer(chain.codebase.cosmwasm_version ?? '0.24');
if (semver.satisfies(wasmVer, '>=0.24')) features.push('wasmd_0.24+');
}
const chainAssets =
assets.find((asset) => asset.chain_name === chain.chain_name)?.assets || [];
const feeDenoms =
chain.fees?.fee_tokens.map<string>((feeToken) => feeToken.denom) || [];
/**
* FROM KEPLR chain-info.d.ts:
* This is used to set the fee of the transaction.
* If this field is empty, it just use the default gas price step (low: 0.01, average: 0.025, high: 0.04).
* And, set field's type as primitive number because it is hard to restore the prototype after deserialzing if field's type is `Dec`.
*/
interface GasPriceStep {
low: number;
average: number;
high: number;
}
const gasPriceSteps: Record<string, GasPriceStep> = chain.fees?.fee_tokens?.reduce((m, feeToken) => {
m[feeToken.denom] = {
low: feeToken.low_gas_price ?? 0.01,
average: feeToken.average_gas_price ?? 0.025,
high: feeToken.high_gas_price ?? 0.04
};
return m;
}, {} as Record<string, GasPriceStep>);
const stakingDenoms =
chain.staking?.staking_tokens.map<string>(
(stakingToken) => stakingToken.denom
) || [];
const currencies: Currency[] = chainAssets.map((currency: Asset) => {
return {
coinDenom: currency.symbol,
coinMinimalDenom: currency.base,
coinDecimals: currency.denom_units.filter(
(denomUnit: { denom: string }) => denomUnit.denom === currency.display
)[0]?.exponent,
coinGeckoId: currency.coingecko_id || undefined,
coinImageUrl: currency.logo_URIs?.svg ?? currency.logo_URIs?.png
};
});
const stakeCurrency: Currency =
currencies.find((currency) => stakingDenoms.includes(currency.coinDenom)) ??
currencies[0];
const feeCurrencies: FeeCurrency[] = currencies
// USE THE FEE DENOMS
.filter((currency) => feeDenoms.includes(currency.coinMinimalDenom))
.map((feeCurrency) => {
if (!(feeCurrency.coinMinimalDenom in gasPriceSteps)) {
return feeCurrency;
}
// has gas
const gasPriceStep = gasPriceSteps[feeCurrency.coinMinimalDenom];
return {
...feeCurrency,
gasPriceStep
};
});
const feeCurrenciesDefault: FeeCurrency[] = currencies
// USE THE STAKE CURRENCY
.filter((currency) => stakeCurrency.coinDenom === currency.coinDenom)
.map((feeCurrency) => {
if (!(feeCurrency.coinMinimalDenom in gasPriceSteps)) {
return feeCurrency;
}
// has gas
const gasPriceStep = gasPriceSteps[feeCurrency.coinMinimalDenom];
return {
...feeCurrency,
gasPriceStep
};
});
const chainInfo: ChainInfo = {
rpc: options.getRpcEndpoint(chain),
rest: options.getRestEndpoint(chain),
chainId: chain.chain_id,
chainName: chain.pretty_name,
bip44: {
coinType: chain.slip44
},
bech32Config: Bech32Address.defaultBech32Config(chain.bech32_prefix),
currencies: currencies,
stakeCurrency: stakeCurrency || currencies[0],
feeCurrencies:
feeCurrencies.length !== 0 ? feeCurrencies : feeCurrenciesDefault,
features
};
return chainInfo;
};