Browse Source

faucet freeze

Shawn Lu 8 tháng trước cách đây
mục cha
commit
c9765ee2af

+ 4 - 2
package.json

@@ -32,6 +32,7 @@
     "start": "node build/src/main.js",
     "testShawn": "npm run build && node build/src/test.js",
     "faucet": "npm run build && node build/src/faucet/faucet.js",
+    "faucet-freeeze": "npm run build && node build/src/faucet/faucetFreeze.js",
     "faucetTuc": "npm run build && node build/src/faucet/faucetTuc.js",
     "faucetMilkyWay": "npm run build && node build/src/faucet/faucetMilkyWay.js",
     "clean": "rimraf coverage build tmp",
@@ -78,11 +79,12 @@
     "shawnlu96-altcha-lib": "0.3.6",
     "stridejs": "^0.10.0-alpha",
     "tslib": "~2.6.2",
-    "useless-helpers": "^0.0.7",
+    "useless-helpers": "^0.0.20",
     "uuid": "^9.0.1",
     "xrpl": "^3.0.0"
   },
   "volta": {
     "node": "18.12.1"
-  }
+  },
+  "packageManager": "yarn@1.22.22+sha512.a6b2f7906b721bba3d67d4aff083df04dad64c399707841b7acf00f6b133b7ac24255f2652fa22ae3534329dc6180534e98d17432037ff6fd140556e2bb3137e"
 }

+ 7 - 0
prisma/schema.prisma

@@ -65,3 +65,10 @@ model RandomTask2 {
   task6     Int     @default(0)
   finish    Int     @default(0)
 }
+
+model FaucetFreeze {
+  id      Int      @id @default(autoincrement())
+  address String   @db.VarChar(100)
+  nextRun DateTime @db.DateTime(0)
+  message String?  @db.Text
+}

+ 6 - 6
src/faucet/captcha.ts

@@ -1,16 +1,16 @@
 import axios, { AxiosInstance } from 'axios'
 import dotenv from 'dotenv'
 import polly from 'polly-js'
-import { solveChallenge, solveChallengeWorkers } from 'shawnlu96-altcha-lib'
+import { solveChallengeWorkers } from 'shawnlu96-altcha-lib'
 import * as os from 'node:os'
 dotenv.config()
 const cpus = os.cpus()
 console.log('current cores', cpus.length)
-const googlekey = '6LdLhtYpAAAAAOe1xmceNR-i6MTtzq7N6AYztoVI'
+const googlekey = '6Lf-lU0qAAAAAN6q-4UEs3ifAFHAgZXRiL6JD-3T'
 const hCaptchaKey = '04d28d90-d5b9-4a90-94e5-a12c595bd4e2'
-const captchaApiKey = process.env.CAPTCHA_API_KEY
+const captchaApiKey = '3fa9ccf791a4178915c4e52f7be1cb41'
 
-export async function getRecaptcha(address: string) {
+export async function getRecaptcha() {
   return await polly()
     .waitAndRetry(2)
     .executeForPromise(async () => {
@@ -18,7 +18,7 @@ export async function getRecaptcha(address: string) {
         method: 'userrecaptcha',
         key: captchaApiKey,
         googlekey,
-        pageUrl: `https://faucet.testnet.initia.xyz/?address=${address}`,
+        pageUrl: `https://faucet.testnet.initia.xyz/`,
         json: 1,
         action: 'verify',
       })
@@ -30,7 +30,7 @@ export async function getRecaptcha(address: string) {
             `https://2captcha.com/res.php?key=${captchaApiKey}&action=get&id=${request}&json=1`,
           )
           const token = captchaRes.data.request
-          console.log(token)
+          // console.log(token)
           if (token === 'CAPCHA_NOT_READY') throw new Error('CAPCHA_NOT_READY')
           if (!token) throw new Error('no result')
           return token

+ 1 - 1
src/faucet/faucet.ts

@@ -106,7 +106,7 @@ async function checkFaucetBalance() {
         })
       }
     },
-    { timeout: 10000 },
+    // { timeout: 10000 },
   )
 }
 

+ 99 - 0
src/faucet/faucetFreeze.ts

@@ -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)

+ 4 - 4
src/faucet/faucetMilkyWay.ts

@@ -24,20 +24,20 @@ async function faucetMilkyWay(mnemonic: string) {
 
 async function startFaucet(concurrency: number) {
   const accounts = await DBClient.instance.randomTask2.findMany({
-    where: { task5: 0 },
+    where: { finish: 2 },
   })
   await forEachAsync(accounts, concurrency, async account => {
     try {
       await faucetMilkyWay(account.mnemonic)
       await DBClient.instance.randomTask2.update({
         where: { id: account.id },
-        data: { task5: 1 },
+        data: { finish: 0 },
       })
     } catch (e) {
       console.log(e)
       await DBClient.instance.randomTask2.update({
         where: { id: account.id },
-        data: { task5: -1 },
+        data: { finish: -1 },
       })
     }
   })
@@ -45,5 +45,5 @@ async function startFaucet(concurrency: number) {
 
 //"bKL8sOZgmwSp9EBq+QempUqdNVPOwlPlKI5o1JiSn8EYMPPNQdNv5OJh4Z+xOC+GWNscJ8IihgUtb1mLTy03ag=="
 // decoded: 0x6ca2fcb0e6609b04a9f4406af907a6a54a9d3553cec253e5288e68d498929fc1
-// await startFaucet(20)
+await startFaucet(20)