|
@@ -0,0 +1,99 @@
|
|
|
+import { getRecaptcha } from './captcha'
|
|
|
+import { getAxiosClient } from '../utils'
|
|
|
+import { DBClient } from '../singletons'
|
|
|
+import { forEachAsync, sleep } from 'useless-helpers'
|
|
|
+
|
|
|
+async function faucetSingle(address: string, useProxy = true) {
|
|
|
+ const recaptchaToken = await getRecaptcha()
|
|
|
+ console.log('got recaptcha token')
|
|
|
+ const axiosClient = getAxiosClient(useProxy)
|
|
|
+ axiosClient.defaults.validateStatus = status =>
|
|
|
+ status === 200 || status === 400
|
|
|
+ let finished = false
|
|
|
+ let message = ''
|
|
|
+ const send = () => {
|
|
|
+ return new Promise(async resolve => {
|
|
|
+ const handler = setInterval(() => {
|
|
|
+ if (finished) {
|
|
|
+ clearInterval(handler)
|
|
|
+ resolve(true)
|
|
|
+ }
|
|
|
+ }, 200)
|
|
|
+ try {
|
|
|
+ const resp = await axiosClient.post(
|
|
|
+ 'https://faucet-api.testnet.initia.xyz/claim',
|
|
|
+ {
|
|
|
+ address: address,
|
|
|
+ denom: 'uinit',
|
|
|
+ response: recaptchaToken,
|
|
|
+ },
|
|
|
+ )
|
|
|
+ console.log(resp.data)
|
|
|
+ if (
|
|
|
+ resp.status === 200 ||
|
|
|
+ (resp.status === 400 && resp.data.includes('recently received funds'))
|
|
|
+ ) {
|
|
|
+ console.log('faucet success')
|
|
|
+ finished = true
|
|
|
+ resolve(true)
|
|
|
+ message = resp.data
|
|
|
+ } else {
|
|
|
+ throw new Error('faucet failed: ' + resp.data)
|
|
|
+ }
|
|
|
+ } catch (e) {
|
|
|
+ console.log(e.message)
|
|
|
+ resolve(false)
|
|
|
+ } finally {
|
|
|
+ clearInterval(handler)
|
|
|
+ }
|
|
|
+ })
|
|
|
+ }
|
|
|
+ await Promise.all(
|
|
|
+ Array(10)
|
|
|
+ .fill(1)
|
|
|
+ .map(() => send()),
|
|
|
+ )
|
|
|
+ return message
|
|
|
+}
|
|
|
+
|
|
|
+async function faucetAll(concurrency = 20, useProxy = true) {
|
|
|
+ while (true) {
|
|
|
+ console.log(
|
|
|
+ '==================start faucet all accounts...===================',
|
|
|
+ )
|
|
|
+ try {
|
|
|
+ const accountsToFaucet = await DBClient.instance.faucetFreeze.findMany({
|
|
|
+ where: {
|
|
|
+ nextRun: {
|
|
|
+ lte: new Date(),
|
|
|
+ },
|
|
|
+ },
|
|
|
+ })
|
|
|
+ await forEachAsync(
|
|
|
+ accountsToFaucet,
|
|
|
+ concurrency,
|
|
|
+ async account => {
|
|
|
+ account.message = await faucetSingle(account.address, useProxy)
|
|
|
+ account.nextRun = new Date(Date.now() + 1000 * 60 * 60 * 24)
|
|
|
+ await DBClient.instance.faucetFreeze.update({
|
|
|
+ where: {
|
|
|
+ id: account.id,
|
|
|
+ },
|
|
|
+ data: {
|
|
|
+ ...account,
|
|
|
+ },
|
|
|
+ })
|
|
|
+ },
|
|
|
+ { skipError: true, showProgress: true },
|
|
|
+ )
|
|
|
+ } catch (e) {
|
|
|
+ console.error(e.message)
|
|
|
+ }
|
|
|
+ console.log('==================faucet all accounts done===================')
|
|
|
+ await sleep(1000 * 60 * 10)
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+await faucetAll()
|
|
|
+
|
|
|
+// await faucetSingle('init18eq2f4nj7u5d5tc7xrpdgfq33h57z0dh3vfh6r', false)
|