This repository has been archived by the owner on Aug 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
226 lines (199 loc) · 6.77 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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
const fcl = require('@onflow/fcl')
const t = require('@onflow/types')
const EC = require('elliptic').ec
const { SHA3 } = require('sha3')
const addToken = require('./scripts/addToken')
const updateToken = require('./scripts/updateToken')
const removeToken = require('./scripts/removeToken')
const addPair = require('./scripts/addPair')
const updatePair = require('./scripts/updatePair')
const removePair = require('./scripts/removePair')
require('dotenv').config()
const NETWORK = process.env.NETWORK || 'testnet'
const ACCESS_NODE = process.env.ACCESS_NODE
const ADDRESS = process.env.ADDRESS
const PRIVATE_KEY = process.env.PRIVATE_KEY
const TOKENS_LIST = require(`./data/${NETWORK}/tokens.json`)
const PAIRS_LIST = require(`./data/${NETWORK}/pairs.json`)
const ec = new EC('secp256k1')
const signWithKey = (privateKey, msgHex) => {
const key = ec.keyFromPrivate(Buffer.from(privateKey, 'hex'))
const sig = key.sign(hashMsgHex(msgHex))
const n = 32 // half of signature length?
const r = sig.r.toArrayLike(Buffer, 'be', n)
const s = sig.s.toArrayLike(Buffer, 'be', n)
return Buffer.concat([r, s]).toString('hex')
}
const hashMsgHex = msgHex => {
const sha = new SHA3(256)
sha.update(Buffer.from(msgHex, 'hex'))
return sha.digest()
}
// Will be handled by fcl.user(addr).info()
const getAccount = async addr => {
const { account } = await fcl.send([fcl.getAccount(addr)])
return account
}
const authorization = async (account = {}) => {
const user = await getAccount(ADDRESS)
const key = user.keys[0]
let sequenceNum
if (account.role && account.role.proposer) sequenceNum = key.sequenceNumber
const signingFunction = async data => {
return {
addr: user.address,
keyId: key.index,
signature: signWithKey(PRIVATE_KEY, data.message),
}
}
return {
...account,
addr: user.address,
keyId: key.index,
sequenceNum,
signature: account.signature || null,
signingFunction,
resolve: null,
roles: account.roles,
}
}
const executeScript = async (script, args = []) =>
fcl
.send([fcl.getBlock(true)])
.then(fcl.decode)
.then(block => fcl.send([
fcl.transaction(script),
fcl.args(args),
fcl.authorizations([authorization]),
fcl.proposer(authorization),
fcl.payer(authorization),
fcl.ref(block.id),
fcl.limit(100),
]))
.then(({ transactionId }) => fcl.tx(transactionId).onceSealed())
.catch(e => {
console.error(e)
})
async function diffTokens() {
const getTokensScript = `\
import ListedTokens from ${ADDRESS}
pub fun main(): [ListedTokens.TokenInfo] {
return ListedTokens.getTokens()
}`
const onchainList = await fcl.send([fcl.script(getTokensScript)]).then(fcl.decode)
const newTokens = TOKENS_LIST.filter(token => !onchainList.find(({ name }) => name === token.name))
const updatedTokens = TOKENS_LIST
.filter(token => onchainList
.find(t => {
if (t.name === token.name && t.address === token.address && JSON.stringify(token) !== JSON.stringify(t)) {
return true
}
})
)
const removedTokens = onchainList.filter(token => !TOKENS_LIST.find(({ name }) => name === token.name))
return { newTokens, updatedTokens, removedTokens }
}
async function diffPairs() {
const getPairsScript = `\
import ListedPairs from ${ADDRESS}
pub fun main(): [ListedPairs.PairInfo] {
return ListedPairs.getPairs()
}`
const onchainList = await fcl.send([fcl.script(getPairsScript)]).then(fcl.decode)
const newPairs = PAIRS_LIST.filter(pair => !onchainList.find(({ name }) => name === pair.name))
const updatedPairs = PAIRS_LIST
.filter(pair => onchainList
.find(p => p.name === pair.name && p.address === pair.address && JSON.stringify(pair) !== JSON.stringify(p))
)
const removedPairs = onchainList.filter(pair => !PAIRS_LIST.find(({ name }) => name === pair.name))
return { newPairs, updatedPairs, removedPairs }
}
async function addTokens(tokens) {
for (const token of tokens) {
const args = [
fcl.arg(token.name, t.String),
fcl.arg(token.displayName, t.String),
fcl.arg(token.symbol, t.String),
fcl.arg(token.address, t.Address),
fcl.arg(token.vaultPath, t.String),
fcl.arg(token.receiverPath, t.String),
fcl.arg(token.balancePath, t.String),
]
console.log("Adding Token:", token.displayName)
await executeScript(addToken, args)
}
}
async function updateTokens(tokens) {
for (const token of tokens) {
const args = [
fcl.arg(token.name, t.String),
fcl.arg(token.displayName, t.String),
fcl.arg(token.symbol, t.String),
fcl.arg(token.address, t.Address),
fcl.arg(token.vaultPath, t.String),
fcl.arg(token.receiverPath, t.String),
fcl.arg(token.balancePath, t.String),
]
console.log("Updating Token:", token.displayName)
await executeScript(updateToken, args)
}
}
async function removeTokens(tokens) {
for (const token of tokens) {
const key = `${token.name}.0x${token.address.slice(2).replace(/^0+/g, '')}`
const args = [fcl.arg(key, t.String)]
console.log("Removing Token:", token.displayName)
await executeScript(removeToken, args)
}
}
async function addPairs(pairs) {
for (const pair of pairs) {
const args = [
fcl.arg(pair.name, t.String),
fcl.arg(pair.token0, t.String),
fcl.arg(pair.token1, t.String),
fcl.arg(pair.address, t.Address),
fcl.arg(pair.liquidityToken, t.Optional(t.String)),
]
console.log("Adding Pair:", pair.name)
await executeScript(addPair, args)
}
}
async function updatePairs(pairs) {
for (const pair of pairs) {
const args = [
fcl.arg(pair.name, t.String),
fcl.arg(pair.address, t.Address),
fcl.arg(pair.liquidityToken, t.Optional(t.String)),
]
console.log("Updating Pair:", pair.name)
await executeScript(updatePair, args)
}
}
async function removePairs(pairs) {
for (const pair of pairs) {
const key = `${pair.name}.0x${pair.address.slice(2).replace(/^0+/g, '')}`
const args = [fcl.arg(key, t.String)]
console.log("Removing Pair:", pair.name)
await executeScript(removePair, args)
}
}
async function main() {
fcl.config({ "accessNode.api": ACCESS_NODE, "0xProfile": ADDRESS })
const { newTokens, updatedTokens, removedTokens } = await diffTokens()
const { newPairs, updatedPairs, removedPairs } = await diffPairs()
console.log("[Info] Adding Tokens")
await addTokens(newTokens)
console.log("[Info] Updating Tokens")
await updateTokens(updatedTokens)
console.log("[Info] Removing Tokens")
await removeTokens(removedTokens)
console.log("[Info] Adding Pairs")
await addPairs(newPairs)
console.log("[Info] Updating Pairs")
await updatePairs(updatedPairs)
console.log("[Info] Removing Pairs")
await removePairs(removedPairs)
console.log("[Info] Done")
}
main()