Skip to content

Commit 3f51d64

Browse files
authored
remove web3 references (#1344)
1 parent 1a48b80 commit 3f51d64

File tree

1 file changed

+66
-40
lines changed

1 file changed

+66
-40
lines changed

publish/src/commands/remove-synths.js

Lines changed: 66 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
'use strict';
22

33
const fs = require('fs');
4-
const { gray, yellow, red, cyan } = require('chalk');
5-
const Web3 = require('web3');
6-
const w3utils = require('web3-utils');
4+
const { gray, yellow, red, cyan, green } = require('chalk');
5+
const ethers = require('ethers');
76

87
const {
98
toBytes32,
9+
getUsers,
1010
constants: { CONFIG_FILENAME, DEPLOYMENT_FILENAME },
1111
} = require('../../..');
1212

@@ -18,9 +18,10 @@ const {
1818
loadConnections,
1919
confirmAction,
2020
stringify,
21-
performTransactionalStepWeb3,
2221
} = require('../util');
2322

23+
const { performTransactionalStep } = require('../command-utils/transact');
24+
2425
const DEFAULTS = {
2526
network: 'kovan',
2627
gasLimit: 3e5,
@@ -34,6 +35,8 @@ const removeSynths = async ({
3435
gasLimit = DEFAULTS.gasLimit,
3536
synthsToRemove = [],
3637
yes,
38+
useFork,
39+
dryRun = false,
3740
privateKey,
3841
}) => {
3942
ensureNetwork(network);
@@ -74,19 +77,29 @@ const removeSynths = async ({
7477

7578
const { providerUrl, privateKey: envPrivateKey, etherscanLinkPrefix } = loadConnections({
7679
network,
80+
useFork,
7781
});
7882

7983
// allow local deployments to use the private key passed as a CLI option
8084
if (network !== 'local' || !privateKey) {
8185
privateKey = envPrivateKey;
8286
}
8387

84-
const web3 = new Web3(new Web3.providers.HttpProvider(providerUrl));
85-
web3.eth.accounts.wallet.add(privateKey);
86-
const account = web3.eth.accounts.wallet[0].address;
87-
console.log(gray(`Using account with public key ${account}`));
88+
const provider = new ethers.providers.JsonRpcProvider(providerUrl);
89+
let wallet;
90+
if (useFork) {
91+
const account = getUsers({ network, user: 'owner' }).address; // protocolDAO
92+
wallet = provider.getSigner(account);
93+
} else {
94+
wallet = new ethers.Wallet(privateKey, provider);
95+
}
96+
if (!wallet.address) wallet.address = wallet._address;
97+
98+
console.log(gray(`Using account with public key ${wallet.address}`));
8899
console.log(gray(`Using gas of ${gasPrice} GWEI with a max of ${gasLimit}`));
89100

101+
console.log(gray('Dry-run:'), dryRun ? green('yes') : yellow('no'));
102+
90103
if (!yes) {
91104
try {
92105
await confirmAction(
@@ -104,14 +117,16 @@ const removeSynths = async ({
104117
}
105118
}
106119

107-
const Synthetix = new web3.eth.Contract(
120+
const Synthetix = new ethers.Contract(
121+
deployment.targets['Synthetix'].address,
108122
deployment.sources['Synthetix'].abi,
109-
deployment.targets['Synthetix'].address
123+
wallet
110124
);
111125

112-
const Issuer = new web3.eth.Contract(
126+
const Issuer = new ethers.Contract(
127+
deployment.targets['Issuer'].address,
113128
deployment.sources['Issuer'].abi,
114-
deployment.targets['Issuer'].address
129+
wallet
115130
);
116131

117132
// deep clone these configurations so we can mutate and persist them
@@ -124,9 +139,9 @@ const removeSynths = async ({
124139
`Synth${currencyKey}`
125140
];
126141
const { abi: synthABI } = deployment.sources[synthSource];
127-
const Synth = new web3.eth.Contract(synthABI, synthAddress);
142+
const Synth = new ethers.Contract(synthAddress, synthABI, wallet);
128143

129-
const currentSynthInSNX = await Synthetix.methods.synths(toBytes32(currencyKey)).call();
144+
const currentSynthInSNX = await Synthetix.synths(toBytes32(currencyKey));
130145

131146
if (synthAddress !== currentSynthInSNX) {
132147
console.error(
@@ -141,7 +156,7 @@ const removeSynths = async ({
141156
}
142157

143158
// now check total supply (is required in Synthetix.removeSynth)
144-
const totalSupply = w3utils.fromWei(await Synth.methods.totalSupply().call());
159+
const totalSupply = ethers.utils.formatEther(await Synth.totalSupply());
145160
if (Number(totalSupply) > 0) {
146161
console.error(
147162
red(
@@ -155,32 +170,36 @@ const removeSynths = async ({
155170
}
156171

157172
// perform transaction if owner of Synthetix or append to owner actions list
158-
await performTransactionalStepWeb3({
159-
account,
160-
contract: 'Issuer',
161-
target: Issuer,
162-
write: 'removeSynth',
163-
writeArg: toBytes32(currencyKey),
164-
gasLimit,
165-
gasPrice,
166-
etherscanLinkPrefix,
167-
ownerActions,
168-
ownerActionsFile,
169-
encodeABI: network === 'mainnet',
170-
});
171-
172-
// now update the config and deployment JSON files
173-
const contracts = ['Proxy', 'TokenState', 'Synth'].map(name => `${name}${currencyKey}`);
174-
for (const contract of contracts) {
175-
delete updatedConfig[contract];
176-
delete updatedDeployment.targets[contract];
173+
if (dryRun) {
174+
console.log(green('Would attempt to remove the synth:', currencyKey));
175+
} else {
176+
await performTransactionalStep({
177+
account: wallet,
178+
contract: 'Issuer',
179+
target: Issuer,
180+
write: 'removeSynth',
181+
writeArg: toBytes32(currencyKey),
182+
gasLimit,
183+
gasPrice,
184+
etherscanLinkPrefix,
185+
ownerActions,
186+
ownerActionsFile,
187+
encodeABI: network === 'mainnet',
188+
});
189+
190+
// now update the config and deployment JSON files
191+
const contracts = ['Proxy', 'TokenState', 'Synth'].map(name => `${name}${currencyKey}`);
192+
for (const contract of contracts) {
193+
delete updatedConfig[contract];
194+
delete updatedDeployment.targets[contract];
195+
}
196+
fs.writeFileSync(configFile, stringify(updatedConfig));
197+
fs.writeFileSync(deploymentFile, stringify(updatedDeployment));
198+
199+
// and update the synths.json file
200+
updatedSynths = updatedSynths.filter(({ name }) => name !== currencyKey);
201+
fs.writeFileSync(synthsFile, stringify(updatedSynths));
177202
}
178-
fs.writeFileSync(configFile, stringify(updatedConfig));
179-
fs.writeFileSync(deploymentFile, stringify(updatedDeployment));
180-
181-
// and update the synths.json file
182-
updatedSynths = updatedSynths.filter(({ name }) => name !== currencyKey);
183-
fs.writeFileSync(synthsFile, stringify(updatedSynths));
184203
}
185204
};
186205

@@ -197,6 +216,13 @@ module.exports = {
197216
.option('-g, --gas-price <value>', 'Gas price in GWEI', 1)
198217
.option('-l, --gas-limit <value>', 'Gas limit', 1e6)
199218
.option('-n, --network <value>', 'The network to run off.', x => x.toLowerCase(), 'kovan')
219+
.option('-r, --dry-run', 'Dry run - no changes transacted')
220+
.option(
221+
'-k, --use-fork',
222+
'Perform the deployment on a forked chain running on localhost (see fork command).',
223+
false
224+
)
225+
.option('-y, --yes', 'Dont prompt, just reply yes.')
200226
.option(
201227
'-s, --synths-to-remove <value>',
202228
'The list of synths to remove',

0 commit comments

Comments
 (0)