Skip to content

Instantly share code, notes, and snippets.

@YogaSakti
Created November 23, 2022 06:42
Show Gist options
  • Save YogaSakti/926e1ad332c62a9c70fa977c0a15c3b2 to your computer and use it in GitHub Desktop.
Save YogaSakti/926e1ad332c62a9c70fa977c0a15c3b2 to your computer and use it in GitHub Desktop.
activate polygon wallet
/* eslint-disable init-declarations */
/* eslint-disable radix */
/* eslint-disable no-continue */
const AlchemyWeb3 = require('@alch/alchemy-web3');
const delay = require('delay');
const provider = 'https://rpc-mainnet.matic.quiknode.pro'
const web3 = AlchemyWeb3.createAlchemyWeb3(provider);
const Ora = require('ora');
const spinner = new Ora();
let data = require('./10K.json');
// format [ { address: '0x', privateKey: '0x' } ]
(async () => {
try {
const wallets = data // .map((x) => ({ address: x.wallet, privateKey: x.privateKey }))
spinner.stopAndPersist({ symbol: 'πŸŽ‰', text: `Creating Self Tx for ${wallets.length} address!` })
for (let i = 0; i < wallets.length; i++) {
spinner.start('Collecting data...')
const { address, privateKey } = wallets[i]
const nonce = await web3.eth.getTransactionCount(address)
if (nonce >= 1) {
spinner.stopAndPersist({ symbol: 'πŸŽ‰', text: `[${i + 1}] Skip, Sudah bikin ${nonce} tx! ${address}` })
continue
}
let mainBalance = 750000000000000 // default 0.75 ether
mainBalance = await web3.eth.getBalance(address)
if (mainBalance == 0) {
spinner.stopAndPersist({ symbol: 'πŸŽ‰', text: `[${i + 1}] Skip, Balance 0! ${address}` })
continue
}
const estimatedGas = 21000 // await web3.eth.estimateGas({ to: address, data: '0x' })
const gasPriceDefault = 31 * 1000000000 // 31 gwei
const gasPriceLimit = parseInt(mainBalance / estimatedGas).toString() // return wei
spinner.start('Checking gas fee...')
let baseFeePerGas, maxFeePerGas, maxPriorityFeePerGas;
let gasGede = false,
gasKecil = false;
do {
maxPriorityFeePerGas = await web3.eth.getMaxPriorityFeePerGas()
maxPriorityFeePerGas = web3.utils.hexToNumber(maxPriorityFeePerGas)
maxPriorityFeePerGas = parseInt(maxPriorityFeePerGas + maxPriorityFeePerGas / 100 * 5)
baseFeePerGas = await web3.eth.getBlock('pending').then((block) => Number(block.baseFeePerGas))
maxFeePerGas = Number(maxPriorityFeePerGas) + baseFeePerGas - 1
maxFeePerGas += 1000000000 // add 1 gwei
if (maxFeePerGas < gasPriceLimit) gasGede = true
if (maxFeePerGas > gasPriceDefault) gasKecil = true
if (!gasGede) {
spinner.text = `Skip, Gas kegedean! (${web3.utils.fromWei(maxFeePerGas.toString(), 'Gwei')} Gwei)`
await delay(2000)
}
if (!gasKecil) {
spinner.text = `Skip, Gas kekecilan! (${web3.utils.fromWei(maxFeePerGas.toString(), 'Gwei')} Gwei)`
await delay(2000)
}
} while (!gasGede || !gasKecil);
spinner.start(`[${i + 1}] Sending Transaction...`)
const signedTx = await web3.eth.accounts.signTransaction({
from: address,
to: address,
gas: estimatedGas,
value: web3.utils.toWei('0', 'ether'),
maxPriorityFeePerGas: 31500000000, // maxPriorityFeePerGas
maxFeePerGas: 32500000000 // maxFeePerGas
}, privateKey);
web3.eth.sendSignedTransaction(signedTx.rawTransaction, (err, tx3Hash) => {
if (err) return spinner.stopAndPersist({ symbol: '❌', text: `Err: ${err.message}` })
spinner.stopAndPersist({ symbol: 'πŸŽ‰', text: `[${i + 1}] Tx3Hash: ${tx3Hash}` })
})
}
} catch (error) {
spinner.stopAndPersist({ symbol: '❌', text: `Err: ${error.message}` })
}
})()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment