-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
142 lines (109 loc) · 3.8 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
const validate = require('./validate')
const chainMapping = require('./chains')
const diff = require('deep-diff').diff
const tokenNode = process.env.NODE_ENV === 'production'
? 'wss://mainnet.infura.io/ws/v3/786ade30f36244469480aa5c2bf0743b'
: 'wss://rinkeby.infura.io/ws/v3/786ade30f36244469480aa5c2bf0743b'
const ethProvider = require('eth-provider')
const eth = ethProvider(['frame', tokenNode])
const nebula = require('nebula').default(
`https://${process.env.NEBULA_AUTH_TOKEN}@ipfs.nebula.land`, eth
)
const tokenDomain = process.env.TOKEN_DOMAIN || 'tokens.frame.eth'
const tokenBlacklist = require('./token-blacklist')
function version ({ major, minor, patch }) {
return {
major, minor, patch,
bump: function (patchInc = 1, minorInc = 0, majorInc = 0) {
return version({
major: major + majorInc,
minor: minor + minorInc,
patch: patch + patchInc
})
},
toString: () => `v${major}.${minor}.${patch}`
}
}
function tokenList (version, tokens) {
const { major, minor, patch } = version
return {
name: 'Frame Token List',
timestamp: new Date().toISOString(),
version: {
major, minor, patch
},
tokens
}
}
// returns true if the lists are not the same
function listChanged (oldList, newList) {
function compareTokens (t1, t2) {
if (t1.symbol < t2.symbol) {
return -1
}
if (t2.symbol < t1.symbol) {
return 1
}
return t1.chainId - t2.chainId
}
const differences = diff(oldList.sort(compareTokens), newList.sort(compareTokens))
return !!differences
}
async function getExistingList () {
try {
const existingListCid = (await nebula.resolve(tokenDomain)).record.content
const existingList = await nebula.ipfs.getJson(existingListCid)
return existingList
} catch (e) {
console.warn('could not load existing list', e)
return tokenList({ major: 0, minor: 0, patch: 0 }, [])
}
}
async function loadTokens (chain) {
let blacklistedTokens = [...tokenBlacklist]
const tokens = (await chain.loadTokens()).map(token => {
const blacklistIndex = blacklistedTokens
.findIndex(t => t.address.toLowerCase() === token.address.toLowerCase())
if (blacklistIndex >= 0) {
blacklistedTokens.splice(blacklistIndex, 1)
const badToken = { ...token }
return {
...badToken,
extensions: {
...badToken.extensions,
omit: true
}
}
}
return token
})
// add any explicitly black-listed tokens that aren't already in the list
return [...tokens, ...blacklistedTokens]
}
async function updateTokens () {
const chainId = parseInt(await eth.request({ method: 'eth_chainId' }))
const existingList = await getExistingList()
const existingVersion = version(existingList.version || { major: 0, minor: 0, patch: 0 })
console.log(`found existing list, ${existingVersion} with ${existingList.tokens.length} tokens`)
const chainTokens = await loadTokens(chainMapping[chainId])
const tokens = existingList.tokens.filter(token => token.chainId !== chainId).concat(chainTokens)
if (listChanged(existingList.tokens, tokens)) {
const newVersion = existingVersion.bump()
const completeList = tokenList(newVersion, tokens)
validate(completeList)
try {
const resp = await nebula.update(tokenDomain, {
version: newVersion.toString().substring(1),
content: completeList
})
console.log('got response from Nebula', resp)
} catch (e) {
// this times out a lot due to a gateway max on the server of 30s
console.warn(e)
}
console.log(`updated token list at ${tokenDomain} to ${newVersion} with ${completeList.tokens.length} tokens`)
} else {
console.log(`no changes, latest token list is ${existingVersion}`)
}
}
updateTokens().catch(console.error).finally(process.exit)