Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PP-510: script for removing tokens #81

Merged
merged 3 commits into from
Oct 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
"lint:sol": "solhint -f unix \"contracts/**/*.sol\" --max-warnings 0",
"allowTokens": "./scripts/allowTokens",
"allowedTokens": "./scripts/allowedTokens",
"removeTokens": "./scripts/removeTokens",
"prettier:fix": "npx prettier --write --ignore-unknown .",
"prettier": "npx prettier --check .",
"test:truffle": "truffle test --network regtest",
Expand Down
110 changes: 110 additions & 0 deletions scripts/removeTokens
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
#!/bin/bash

TOKEN_ADDRESSES=$1
NETWORK_NAME=$2

if [ "${TOKEN_ADDRESSES}" == "" ]; then
echo "You need to specify at least one token address."
exit 1
fi

if [ "${NETWORK_NAME}" == "" ]; then
NETWORK_NAME="regtest"
fi

cat > removeTokens.js << EOF

async function getRevertReason(txHash) {
const tx = await web3.eth.getTransaction(txHash);
const txBlockNumber = tx.blockNumber;
try {
delete tx['hash'];
delete tx['blockHash'];
delete tx['blockNumber'];
delete tx['transactionIndex'];
delete tx['v'];
delete tx['r'];
delete tx['s'];
let result = await web3.eth.call(tx, txBlockNumber);
result = result.startsWith('0x') ? result : '0x' + result;
if (result && result.substr(138)) {
return web3.utils.toAscii(result.substr(138));
} else {
return 'Cannot get reason - No return value';
}
} catch (reason) {
return reason;
}
}

module.exports = async function( ) {
const truffleConfig = require('./truffle');
const contractAddresses = require('./contract-addresses.json');

const networkConfiguration = truffleConfig.networks["${NETWORK_NAME}"];
const networkId = networkConfiguration.network_id;

const smartWalletDeployVerifierAbi = require("./build/contracts/DeployVerifier.json").abi;
const customSmartWalletDeployVerifierAbi = require("./build/contracts/CustomSmartWalletDeployVerifier.json").abi;
const relayVerifierAbi = require("./build/contracts/RelayVerifier.json").abi;

const smartWalletDeployVerifier = await new web3.eth.Contract(smartWalletDeployVerifierAbi, contractAddresses[networkId].smartWalletDeployVerifier);
const smartWalletRelayVerifier = await new web3.eth.Contract(relayVerifierAbi, contractAddresses[networkId].smartWalletRelayVerifier);
const customSmartWalletDeployVerifier = await new web3.eth.Contract(customSmartWalletDeployVerifierAbi, contractAddresses[networkId].customSmartWalletDeployVerifier);
const customSmartWalletRelayVerifier = await new web3.eth.Contract(relayVerifierAbi, contractAddresses[networkId].customSmartWalletRelayVerifier);

const tokenAddresses = '${TOKEN_ADDRESSES}'.split(',');

const accounts = await web3.eth.getAccounts();

try {
for (let tokenAddress of tokenAddresses) {
try {
const acceptedTokens = await smartWalletDeployVerifier.methods.getAcceptedTokens().call({from: accounts[0]});
const index = acceptedTokens.indexOf(tokenAddress);
await smartWalletDeployVerifier.methods.removeToken(tokenAddress, index).send({from: accounts[0]});
} catch (error) {
const reason = error.hasOwnProperty('receipt') ? await getRevertReason(error.receipt.transactionHash) : error;
console.error("Error removing token with address " + tokenAddress + " from allowed tokens on smart wallet deploy verifier", reason);
throw error;
}
try {
const acceptedTokens = await smartWalletRelayVerifier.methods.getAcceptedTokens().call({from: accounts[0]});
const index = acceptedTokens.indexOf(tokenAddress);
await smartWalletRelayVerifier.methods.removeToken(tokenAddress, index).send({from: accounts[0]});
} catch (error) {
const reason = error.hasOwnProperty('receipt') ? await getRevertReason(error.receipt.transactionHash) : error;
console.error("Error removing token with address " + tokenAddress + " from allowed tokens on smart wallet relay verifier", reason);
throw error;
}
try {
const acceptedTokens = await customSmartWalletDeployVerifier.methods.getAcceptedTokens().call({from: accounts[0]});
const index = acceptedTokens.indexOf(tokenAddress);
await customSmartWalletDeployVerifier.methods.removeToken(tokenAddress, index).send({from: accounts[0]});
} catch (error) {
const reason = error.hasOwnProperty('receipt') ? await getRevertReason(error.receipt.transactionHash) : error;
console.error("Error removing token with address " + tokenAddress + " from allowed tokens on custom smart deploy verifier", reason);
throw error;
}
try {
const acceptedTokens = await customSmartWalletRelayVerifier.methods.getAcceptedTokens().call({from: accounts[0]});
const index = acceptedTokens.indexOf(tokenAddress);
await customSmartWalletRelayVerifier.methods.removeToken(tokenAddress, index).send({from: accounts[0]});
} catch (error) {
const reason = error.hasOwnProperty('receipt') ? await getRevertReason(error.receipt.transactionHash) : error;
console.error("Error removing token with address " + tokenAddress + " from allowed tokens on custom smart wallet relay verifier", reason);
throw error;
}
}
} catch (error) {
console.error(error);
console.error("Failed to remove tokens");
return;
}
console.log("Tokens removed successfully!");
}
EOF

truffle exec --network ${NETWORK_NAME} removeTokens.js

rm removeTokens.js