Skip to content

Commit 2a552b7

Browse files
committed
Create a script to withdraw all ERC20 tokens from XRequestProcessor contract
1 parent 04b89eb commit 2a552b7

File tree

1 file changed

+97
-0
lines changed

1 file changed

+97
-0
lines changed

scripts/withdrawTokens.js

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
const hre = require('hardhat')
2+
const ethers = require('ethers')
3+
4+
const readline = require('node:readline/promises')
5+
const process = require('node:process')
6+
7+
const { humanReadableAmount } = require('./modules/utils')
8+
9+
const X_REQUEST_PROCESSOR_ABI =
10+
require('../artifacts/contracts/XRequestProcessor.sol/XRequestProcessor.json').abi
11+
12+
const ERC20_ABI = require('../artifacts/contracts/StandardERC20.sol/StandardERC20.json').abi
13+
14+
const cliReader = readline.createInterface({ input: process.stdin, output: process.stdout })
15+
16+
async function main() {
17+
const chainId = hre.network.config.chainId
18+
const rpcUrl = hre.network.config.url
19+
const provider = new ethers.JsonRpcProvider(rpcUrl)
20+
21+
const privateKey = process.env.PRIVATE_KEY || 'YOUR_PRIVATE_KEY'
22+
const wallet = new hre.ethers.Wallet(privateKey)
23+
const walletSigner = wallet.connect(provider)
24+
25+
const requestProcessorAddress =
26+
process.env.XREQUEST_PROCESSOR_ADDRESS ||
27+
(await cliReader.question(`Enter XRequestProcessor address": `)).trim()
28+
if (!ethers.isAddress(requestProcessorAddress)) {
29+
console.log('Invalid XRequestProcessor address. Aborting.')
30+
return
31+
}
32+
33+
const networkName = hre.network.name
34+
.replace(/(?<=[a-z])(?=[A-Z])/g, ' ')
35+
.split(' ')
36+
.map(s => `${s[0].toUpperCase()}${s.slice(1)}`)
37+
.join(' ')
38+
39+
console.log(
40+
`Loading ERC20 balance in XRequestProcessor deployed at: ${requestProcessorAddress} on ${networkName}`
41+
)
42+
console.log()
43+
44+
const requestProcessorContract = new ethers.Contract(
45+
requestProcessorAddress,
46+
X_REQUEST_PROCESSOR_ABI,
47+
walletSigner
48+
)
49+
50+
const erc20TokenAddress = await requestProcessorContract.getPaymentTokenAddress()
51+
const erc20Contract = new ethers.Contract(erc20TokenAddress, ERC20_ABI, walletSigner)
52+
const erc20Name = await erc20Contract.name()
53+
const erc20Symbol = await erc20Contract.symbol()
54+
const erc20Supply = await erc20Contract.totalSupply()
55+
console.log(`ERC20 Address: ${erc20TokenAddress}`)
56+
console.log(`ERC20 Name: ${erc20Name}`)
57+
console.log(`ERC20 Symbol: ${erc20Symbol}`)
58+
console.log(`ERC20 Supply: ${humanReadableAmount(erc20Supply)}`)
59+
console.log()
60+
61+
const requestProcessorBalance = await erc20Contract.balanceOf(requestProcessorAddress)
62+
console.log(
63+
`XRequestProcessor $${erc20Symbol} balance: ${humanReadableAmount(requestProcessorBalance)}`
64+
)
65+
66+
if (requestProcessorBalance === 0n) {
67+
console.log('Empty balance, nothing to withdraw')
68+
return
69+
}
70+
71+
console.log(`Preparing withdrawal of all XRequestProcessor contract $${erc20Symbol} tokens...`)
72+
const withdrawAddress = (await cliReader.question('Enter withdraw address: ')).trim()
73+
if (!ethers.isAddress(withdrawAddress)) {
74+
console.log('Invalid ERC20 address. Aborting.')
75+
return
76+
}
77+
78+
const proceedAction = await cliReader.question('Do you wish to proceed? [y]: ')
79+
if (proceedAction !== '' && proceedWithDeployment.slice(0, 1) !== 'y') {
80+
console.log('Aborting withdrawal.')
81+
return
82+
}
83+
84+
const txData = await requestProcessorContract.withdrawFunds(withdrawAddress)
85+
console.log(`TX: ${txData.hash}`)
86+
}
87+
88+
// We recommend this pattern to be able to use async/await everywhere
89+
// and properly handle errors.
90+
main()
91+
.catch(error => {
92+
console.error(error)
93+
process.exitCode = 1
94+
})
95+
.finally(() => {
96+
cliReader.close()
97+
})

0 commit comments

Comments
 (0)