-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
PP-565: migrate the removeTokens script to use Hardhat and Ethers.js (#…
…92) * refactor: migrate the removeTokens script to use Hardhat and Ethers.js * fix: fix the task in hardhat.config, update Readme and apply format Co-authored-by: Antonio Morrone <antonio@iovlabs.org>
- Loading branch information
1 parent
9c2696d
commit 36e0648
Showing
5 changed files
with
196 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
import { ContractTransaction } from 'ethers'; | ||
import { HardhatRuntimeEnvironment } from 'hardhat/types'; | ||
import { getExistingConfig } from './deploy'; | ||
|
||
export const removeTokens = async ( | ||
taskArgs: { tokenlist: string }, | ||
hre: HardhatRuntimeEnvironment | ||
) => { | ||
const tokenAddresses = taskArgs.tokenlist.split(','); | ||
|
||
const { ethers, network } = hre; | ||
|
||
if (!network) { | ||
throw new Error('Unknown Network'); | ||
} | ||
|
||
const { chainId } = network.config; | ||
|
||
if (!chainId) { | ||
throw new Error('Unknown Chain Id'); | ||
} | ||
|
||
const contractAddresses = getExistingConfig(); | ||
|
||
if (!contractAddresses) { | ||
throw new Error('No contracts deployed'); | ||
} | ||
|
||
const networkChainKey = `${network.name}.${chainId}`; | ||
const contractAddressesDeployed = contractAddresses[networkChainKey]; | ||
|
||
if (!contractAddressesDeployed) { | ||
throw new Error(`Contracts not deployed for chain ID ${chainId}`); | ||
} | ||
|
||
const deployVerifierAddress = contractAddressesDeployed.DeployVerifier; | ||
const relayVerifierAddress = contractAddressesDeployed.RelayVerifier; | ||
const customDeployVerifierAddress = | ||
contractAddressesDeployed.CustomSmartWalletDeployVerifier; | ||
const customRelayVerifierAddress = | ||
contractAddressesDeployed.CustomSmartWalletRelayVerifier; | ||
|
||
if (!deployVerifierAddress) { | ||
throw new Error('Could not obtain deploy verifier address'); | ||
} | ||
|
||
if (!relayVerifierAddress) { | ||
throw new Error('Could not obtain relay verifier address'); | ||
} | ||
|
||
if (!customDeployVerifierAddress) { | ||
throw new Error('Could not obtain custom deploy verifier address'); | ||
} | ||
|
||
if (!customRelayVerifierAddress) { | ||
throw new Error('Could not obtain custom deploy verifier address'); | ||
} | ||
|
||
const deployVerifier = await ethers.getContractAt( | ||
'DeployVerifier', | ||
deployVerifierAddress | ||
); | ||
const relayVerifier = await ethers.getContractAt( | ||
'RelayVerifier', | ||
relayVerifierAddress | ||
); | ||
const customDeployVerifier = await ethers.getContractAt( | ||
'CustomSmartWalletDeployVerifier', | ||
customDeployVerifierAddress | ||
); | ||
const customRelayVerifier = await ethers.getContractAt( | ||
'RelayVerifier', | ||
customRelayVerifierAddress | ||
); | ||
|
||
const verifierMap: Map< | ||
string, | ||
{ | ||
removeToken: ( | ||
tokenAddress: string, | ||
index: number | ||
) => Promise<ContractTransaction>; | ||
getAcceptedTokens: () => Promise<string[]>; | ||
} | ||
> = new Map(); | ||
|
||
verifierMap.set('deployVerifier', deployVerifier); | ||
verifierMap.set('relayVerifier', relayVerifier); | ||
verifierMap.set('customDeployVerifier', customDeployVerifier); | ||
verifierMap.set('customRelayVerifier', customRelayVerifier); | ||
|
||
for (const tokenAddress of tokenAddresses) { | ||
for (const [key, verifier] of verifierMap) { | ||
try { | ||
const index = (await verifier.getAcceptedTokens()).indexOf( | ||
tokenAddress | ||
); | ||
await verifier.removeToken(tokenAddress, index); | ||
} catch (error) { | ||
console.error( | ||
`Error removing token with address ${tokenAddress} from allowed tokens on ${key}` | ||
); | ||
throw error; | ||
} | ||
} | ||
} | ||
console.log('Tokens removed successfully!'); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import { expect, use } from 'chai'; | ||
import chaiAsPromised from 'chai-as-promised'; | ||
import { Contract } from 'ethers'; | ||
import fs from 'fs'; | ||
import * as hre from 'hardhat'; | ||
import { ethers } from 'hardhat'; | ||
import sinon from 'sinon'; | ||
import { | ||
removeTokens | ||
} from '../../scripts/removeTokens'; | ||
|
||
use(chaiAsPromised); | ||
|
||
describe('Remove Tokens Script', function () { | ||
describe('removeTokens', function () { | ||
const taskArgs = { tokenlist: '0x145845fd06c85B7EA1AA2d030E1a747B3d8d15D7' }; | ||
|
||
const contractAddresses = { | ||
Penalizer: '0x145845fd06c85B7EA1AA2d030E1a747B3d8d15D7', | ||
RelayHub: '0x145845fd06c85B7EA1AA2d030E1a747B3d8d15D7', | ||
SmartWallet: '0x145845fd06c85B7EA1AA2d030E1a747B3d8d15D7', | ||
SmartWalletFactory: '0x145845fd06c85B7EA1AA2d030E1a747B3d8d15D7', | ||
DeployVerifier: '0x145845fd06c85B7EA1AA2d030E1a747B3d8d15D7', | ||
RelayVerifier: '0x145845fd06c85B7EA1AA2d030E1a747B3d8d15D7', | ||
CustomSmartWallet: '0x145845fd06c85B7EA1AA2d030E1a747B3d8d15D7', | ||
CustomSmartWalletFactory: '0x145845fd06c85B7EA1AA2d030E1a747B3d8d15D7', | ||
CustomSmartWalletDeployVerifier: | ||
'0x145845fd06c85B7EA1AA2d030E1a747B3d8d15D7', | ||
CustomSmartWalletRelayVerifier: | ||
'0x145845fd06c85B7EA1AA2d030E1a747B3d8d15D7', | ||
VersionRegistry: '0x145845fd06c85B7EA1AA2d030E1a747B3d8d15D7', | ||
UtilToken: '0x145845fd06c85B7EA1AA2d030E1a747B3d8d15D7', | ||
}; | ||
|
||
const chainContractAddresses = { | ||
'hardhat.33': contractAddresses, | ||
}; | ||
|
||
beforeEach(function () { | ||
sinon.stub(fs, 'existsSync').returns(true); | ||
sinon | ||
.stub(fs, 'readFileSync') | ||
.returns(JSON.stringify(chainContractAddresses)); | ||
hre.network.config.chainId = 33; | ||
}); | ||
|
||
afterEach(function () { | ||
sinon.restore(); | ||
}); | ||
|
||
it('should remove a list of tokens', async function () { | ||
const stubContract = sinon.createStubInstance(Contract); | ||
stubContract['removeToken'] = () => undefined; | ||
stubContract['getAcceptedTokens'] = () => { | ||
return [ | ||
'0x145845fd06c85B7EA1AA2d030E1a747B3d8d15D8', | ||
'0x145845fd06c85B7EA1AA2d030E1a747B3d8d15D7' | ||
]; | ||
}; | ||
sinon.stub(ethers, 'getContractAt').resolves(stubContract); | ||
await expect(removeTokens(taskArgs, hre)).to.not.be.rejected; | ||
}); | ||
|
||
it('should throw error and print it if token cannot be removed', async function () { | ||
const stubContract = sinon.createStubInstance(Contract); | ||
stubContract['removeToken'] = () => { | ||
throw new Error(); | ||
}; | ||
sinon.stub(ethers, 'getContractAt').resolves(stubContract); | ||
await expect(removeTokens(taskArgs, hre)).to.be.rejected; | ||
}); | ||
}); | ||
}); |