-
Notifications
You must be signed in to change notification settings - Fork 608
/
Copy pathpurge-synths.js
292 lines (260 loc) · 8.14 KB
/
purge-synths.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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
'use strict';
const { gray, green, yellow, red, cyan } = require('chalk');
const ethers = require('ethers');
const axios = require('axios');
const {
toBytes32,
getUsers,
constants: { CONFIG_FILENAME, DEPLOYMENT_FILENAME },
} = require('../../..');
const {
ensureNetwork,
ensureDeploymentPath,
getDeploymentPathForNetwork,
loadAndCheckRequiredSources,
loadConnections,
confirmAction,
} = require('../util');
const { performTransactionalStep } = require('../command-utils/transact');
const DEFAULTS = {
network: 'kovan',
gasLimit: 3e6,
gasPrice: '1',
batchSize: 15,
};
const purgeSynths = async ({
network = DEFAULTS.network,
deploymentPath,
gasPrice = DEFAULTS.gasPrice,
gasLimit = DEFAULTS.gasLimit,
synthsToPurge = [],
dryRun = false,
yes,
privateKey,
addresses = [],
batchSize = DEFAULTS.batchSize,
proxyAddress,
useFork,
}) => {
ensureNetwork(network);
deploymentPath = deploymentPath || getDeploymentPathForNetwork({ network });
ensureDeploymentPath(deploymentPath);
const { synths, deployment } = loadAndCheckRequiredSources({
deploymentPath,
network,
});
if (synthsToPurge.length < 1) {
console.log(gray('No synths provided. Please use --synths-to-purge option'));
return;
}
// sanity-check the synth list
for (const synth of synthsToPurge) {
if (synths.filter(({ name }) => name === synth).length < 1) {
console.error(red(`Synth ${synth} not found!`));
process.exitCode = 1;
return;
} else if (['sUSD'].indexOf(synth) >= 0) {
console.error(red(`Synth ${synth} cannot be purged`));
process.exitCode = 1;
return;
}
}
if (synthsToPurge.length > 1 && proxyAddress) {
console.error(red(`Cannot provide a proxy address with multiple synths`));
process.exitCode = 1;
return;
}
const { providerUrl, privateKey: envPrivateKey, etherscanLinkPrefix } = loadConnections({
network,
useFork,
});
// allow local deployments to use the private key passed as a CLI option
if (network !== 'local' || !privateKey) {
privateKey = envPrivateKey;
}
console.log(gray(`Provider url: ${providerUrl}`));
const provider = new ethers.providers.JsonRpcProvider(providerUrl);
let wallet;
if (useFork) {
const account = getUsers({ network, user: 'owner' }).address; // protocolDAO
wallet = provider.getSigner(account);
} else {
wallet = new ethers.Wallet(privateKey, provider);
}
console.log(wallet);
if (!wallet.address) wallet.address = wallet._address;
console.log(gray(`Using account with public key ${wallet.address}`));
console.log(gray(`Using gas of ${gasPrice} GWEI with a max of ${gasLimit}`));
console.log(gray('Dry-run:'), dryRun ? green('yes') : yellow('no'));
if (!yes) {
try {
await confirmAction(
cyan(
`${yellow(
'⚠ WARNING'
)}: This action will purge the following synths from the Synthetix contract on ${network}:\n- ${synthsToPurge.join(
'\n- '
)}`
) + '\nDo you want to continue? (y/n) '
);
} catch (err) {
console.log(gray('Operation cancelled'));
return;
}
}
const { address: synthetixAddress, source } = deployment.targets['Synthetix'];
const { abi: synthetixABI } = deployment.sources[source];
const Synthetix = new ethers.Contract(synthetixAddress, synthetixABI, wallet);
let totalBatches = 0;
for (const currencyKey of synthsToPurge) {
const { address: synthAddress, source: synthSource } = deployment.targets[
`Synth${currencyKey}`
];
const { abi: synthABI } = deployment.sources[synthSource];
const Synth = new ethers.Contract(synthAddress, synthABI, wallet);
proxyAddress = proxyAddress || deployment.targets[`Proxy${currencyKey}`].address;
console.log(
gray(
'For',
currencyKey,
'using source of',
synthSource,
'at address',
synthAddress,
'proxy',
proxyAddress
)
);
const currentSynthInSNX = await Synthetix.synths(toBytes32(currencyKey));
if (synthAddress !== currentSynthInSNX) {
console.error(
red(
`Synth address in Synthetix for ${currencyKey} is different from what's deployed in Synthetix to the local ${DEPLOYMENT_FILENAME} of ${network} \ndeployed: ${yellow(
currentSynthInSNX
)}\nlocal: ${yellow(synthAddress)}`
)
);
process.exitCode = 1;
return;
}
// step 1. fetch all holders via ethplorer api
if (network === 'mainnet') {
const topTokenHoldersUrl = `http://api.ethplorer.io/getTopTokenHolders/${proxyAddress}`;
const response = await axios.get(topTokenHoldersUrl, {
params: {
apiKey: process.env.ETHPLORER_API_KEY || 'freekey',
limit: 1000,
},
});
const topTokenHolders = response.data.holders.map(({ address }) => address);
console.log(gray(`Found ${topTokenHolders.length} possible holders of ${currencyKey}`));
// Filter out any 0 holder
const supplyPerEntry = await Promise.all(
topTokenHolders.map(entry => Synth.balanceOf(entry))
);
addresses = topTokenHolders.filter((e, i) => supplyPerEntry[i] !== '0');
console.log(gray(`Filtered to ${addresses.length} with supply`));
}
const totalSupplyBefore = ethers.utils.formatEther(await Synth.totalSupply());
if (Number(totalSupplyBefore) === 0) {
console.log(gray('Total supply is 0, exiting.'));
continue;
} else {
console.log(gray('Total supply before purge is:', totalSupplyBefore));
}
// Split the addresses into batch size
// step 2. start the purge
for (let batch = 0; batch * batchSize < addresses.length; batch++) {
const start = batch * batchSize;
const end = Math.min((batch + 1) * batchSize, addresses.length);
const entries = addresses.slice(start, end);
totalBatches++;
console.log(`batch: ${batch} of addresses with ${entries.length} entries`);
if (dryRun) {
console.log(green('Would attempt to purge:', entries));
} else {
await performTransactionalStep({
account: wallet,
contract: `Synth${currencyKey}`,
target: Synth,
write: 'purge',
writeArg: [entries], // explicitly pass array of args so array not splat as params
gasLimit,
gasPrice,
etherscanLinkPrefix,
encodeABI: network === 'mainnet',
});
}
}
// step 3. confirmation
const totalSupply = ethers.utils.formatEther(await Synth.totalSupply());
if (Number(totalSupply) > 0) {
console.log(
yellow(
`⚠⚠⚠ WARNING: totalSupply is not 0 after purge of ${currencyKey}. It is ${totalSupply}. ` +
`Were there 100 or 1000 holders noted above? If so then we have likely hit the tokenHolder ` +
`API limit; another purge is required for this synth.`
)
);
}
}
console.log(`Total number of batches: ${totalBatches}`);
};
module.exports = {
purgeSynths,
cmd: program =>
program
.command('purge-synths')
.description('Purge a number of synths from the system')
.option(
'-a, --addresses <value>',
'The list of holder addresses (use in testnets when Ethplorer API does not return holders)',
(val, memo) => {
memo.push(val);
return memo;
},
[]
)
.option(
'-d, --deployment-path <value>',
`Path to a folder that has your input configuration file ${CONFIG_FILENAME} and where your ${DEPLOYMENT_FILENAME} files will go`
)
.option('-g, --gas-price <value>', 'Gas price in GWEI', DEFAULTS.gasPrice)
.option('-l, --gas-limit <value>', 'Gas limit', DEFAULTS.gasLimit)
.option(
'-n, --network [value]',
'The network to run off.',
x => x.toLowerCase(),
DEFAULTS.network
)
.option('-r, --dry-run', 'Dry run - no changes transacted')
.option(
'-v, --private-key [value]',
'The private key to transact with (only works in local mode, otherwise set in .env).'
)
.option(
'-bs, --batch-size [value]',
'Batch size for the addresses to be split into',
DEFAULTS.batchSize
)
.option(
'-p, --proxy-address <value>',
'Override the proxy address for the token (only works with a single synth given)'
)
.option(
'-k, --use-fork',
'Perform the deployment on a forked chain running on localhost (see fork command).',
false
)
.option('-y, --yes', 'Dont prompt, just reply yes.')
.option(
'-s, --synths-to-purge <value>',
'The list of synths to purge',
(val, memo) => {
memo.push(val);
return memo;
},
[]
)
.action(purgeSynths),
};