forked from stationmoney/station-assets
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
210 lines (182 loc) · 6.24 KB
/
index.js
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
const glob = require('glob-promise')
const path = require('path')
const axios = require('axios')
const { Buffer } = require('buffer')
const { Hash } = require('@keplr-wallet/crypto')
const { AccAddress } = require('@terra-money/feather.js')
const fs = require('fs').promises
;(async () => {
// create build directory
fs.mkdir('./build', { recursive: true })
// convert chains to json
const chains = {}
const chainsOutPath = './build/chains.json'
const chainFiles = await glob('./chains/*/*.js')
const tokens = []
await Promise.all(
chainFiles.map(async (file) => {
const [_, folder, network, fileName] = file.split('/')
if (typeof chains[network] === 'undefined') {
chains[network] = {}
}
const fullPath = `./${file}`
const chainData = require(fullPath)
if (network !== 'localterra' && !isValidUrl(chainData.lcd)) {
console.log(`${chainData.chainID}: Invalid LCD URL: ${chainData.lcd}`)
return
}
tokens.push(
...(chainData.tokens ?? []).map((t) => ({
...t,
chainID: chainData.chainID,
network,
}))
)
delete chainData['tokens']
const gasPrices = Object.fromEntries(
await Promise.all(
Object.entries(chainData.gasPrices).map(async ([key, value]) => {
if (typeof value === 'number') {
return [key, value]
} else {
if (value.type === 'OSMOSIS') {
try {
const { data } = await axios.get(value.url, {
baseURL: chainData.lcd,
})
return [key, Number(data.base_fee) * value.adjustment]
} catch (e) {
console.error(e)
console.error(
`Failed to get the gas price from ${value.url} on ${chainData.lcd}`
)
return [key, value.defaultValue]
}
}
}
})
)
)
chains[network][chainData.chainID] = { ...chainData, gasPrices }
})
)
// convert coins to json
const coinsOutPath = './build/denoms.json'
const coinsOut = {}
const ibcDenomMapOutPath = './build/ibc.json'
const ibcDenomMapOutPathV2 = './build/ibc_tokens.json'
const ibcDenomMapOut = {}
tokens.forEach((token) => {
const { network, chainID, ...coinData } = token
if (typeof coinsOut[network] === 'undefined') {
coinsOut[network] = {}
}
if (typeof ibcDenomMapOut[network] === 'undefined') {
ibcDenomMapOut[network] = {}
}
const tokenId = [chainID, coinData.token].join(':')
coinsOut[network][tokenId] = { ...coinData, chainID, chains: [chainID] }
// chain is disabled
if (!chains[network][chainID]) {
console.log(`${chainID} used by ${coinData.token} is disabled.`)
return
}
const isICS = AccAddress.validate(coinData.token)
if (!isICS && chains[network][chainID]?.channels) {
const channels = chains[network][chainID].channels
Object.keys(channels).forEach((otherChainID) => {
const channel = chains[network][otherChainID]?.channels?.[chainID]
if (!channel) {
// throw error only if the other chain exists
!!chains[network][otherChainID] &&
console.error(
`${otherChainID} doesn't have an IBC channel configured with ${chainID}.`
)
return
}
const denom =
chains[network][chainID].prefix === 'kujira'
? coinData.token?.replaceAll('/', ':')
: coinData.token
const ibcDenom = calculateIBCDenom(channel, denom)
ibcDenomMapOut[network][`${otherChainID}:${ibcDenom}`] = {
token: tokenId,
chainID: otherChainID,
}
})
} else if (isICS && chains[network][chainID]?.ics20Channels) {
const channels = chains[network][chainID].ics20Channels
const denom = `cw20:${coinData.token}`
Object.entries(channels).forEach(([otherChainID, icsChannels]) => {
const icsChannel =
// specific ICS channel for that token
icsChannels.find(
({ tokens }) => !!tokens && tokens.includes(coinData.token)
) ||
// generic ICS channel for that chain
icsChannels.find(({ tokens }) => !tokens)
// no valid ICS channel found for this token
if (!icsChannel) return
const { channel, otherChannel } = icsChannel
const ibcDenom = calculateIBCDenom(otherChannel, denom)
ibcDenomMapOut[network][`${otherChainID}:${ibcDenom}`] = {
token: tokenId,
chainID: otherChainID,
// to send it back on the original chain
icsChannel: channel,
}
})
}
})
// Format the JSON with indentions before writing.
const jsonList = JSON.stringify(chains)
await fs.writeFile(chainsOutPath, jsonList)
const coinsList = JSON.stringify(coinsOut)
await fs.writeFile(coinsOutPath, coinsList)
const ibcList = JSON.stringify(
Object.fromEntries(
Object.keys(ibcDenomMapOut).map((networkName) => [
networkName,
Object.fromEntries(
Object.entries(ibcDenomMapOut[networkName]).map(([k, v]) => [
k.split(':')[1],
v,
])
),
])
)
)
await fs.writeFile(ibcDenomMapOutPath, ibcList)
const ibcListV2 = JSON.stringify(ibcDenomMapOut)
await fs.writeFile(ibcDenomMapOutPathV2, ibcListV2)
const currenciesList = require('./currencies.js')
await fs.writeFile('./build/currencies.json', JSON.stringify(currenciesList))
// copy images inside ./build
const images = [
...(await glob('./img/*/*.{png,svg,webp}')),
...(await glob('./img/*.{png,svg,webp}')),
]
await Promise.all(
images.map(async (file) => {
await fs.mkdir(`./build/${path.dirname(file).replace('./', '')}`, {
recursive: true,
})
await fs.copyFile(file, `./build/${file.replace('./', '')}`)
})
)
})()
function calculateIBCDenom(channel, denom) {
return (
'ibc/' +
Buffer.from(Hash.sha256(Buffer.from(`transfer/${channel}/${denom}`)))
.toString('hex')
.toUpperCase()
)
}
function isValidUrl(url) {
try {
return new URL(url).protocol === 'https:'
} catch (e) {
return false
}
}