فهرست منبع

Merge remote-tracking branch 'origin/main'

hel 1 سال پیش
والد
کامیت
ea14e53ce4
1فایلهای تغییر یافته به همراه68 افزوده شده و 48 حذف شده
  1. 68 48
      src/faucet/faucet.ts

+ 68 - 48
src/faucet/faucet.ts

@@ -1,15 +1,16 @@
 import { ethers } from 'ethers'
-import { forEachAsync, getAxiosClient } from '../utils'
+import { getAxiosClient } from '../utils'
 import { getAltchaPayload } from './captcha'
 import { DBClient } from '../singletons'
 import { InitiaClient } from '../InitiaClient'
 import { Faucet } from '@prisma/client'
 import polly from 'polly-js'
 import cron from 'node-cron'
+import { forEachAsync } from 'useless-helpers'
 
 export async function faucetAccount(address: string, passportPK: string) {
   const passportWallet = new ethers.Wallet(passportPK)
-  const client = getAxiosClient(true)
+  let client = getAxiosClient(true)
   const statusResp = await client.get(`
 https://faucet-api.initiation-1.initia.xyz/status/${passportWallet.address}`)
   if (statusResp.data.passport_score < 20) {
@@ -35,60 +36,78 @@ https://faucet-api.initiation-1.initia.xyz/status/${passportWallet.address}`)
     throw new Error(`still in cooldown: ${statusResp.data.ms_before_next}`)
   }
 
-  const passportChallengeResp = await client.post(
-    'https://faucet-api.initiation-1.initia.xyz/create_passport_message',
-    { passport_address: passportWallet.address },
-  )
-  const passportSignature = await passportWallet.signMessage(
-    passportChallengeResp.data.message,
-  )
+  await polly()
+    .waitAndRetry(3)
+    .executeForPromise(async info => {
+      if (info.count > 0) client = getAxiosClient(true)
+      const passportChallengeResp = await client.post(
+        'https://faucet-api.initiation-1.initia.xyz/create_passport_message',
+        { passport_address: passportWallet.address },
+      )
+      const passportSignature = await passportWallet.signMessage(
+        passportChallengeResp.data.message,
+      )
 
-  const altcha = await getAltchaPayload(client)
-  try {
-    const res = await client.post(
-      `https://faucet-api.initiation-1.initia.xyz/claim`,
-      {
-        address: address,
-        altcha_payload: altcha,
-        denom: 'uinit',
-        discord_code: '',
-        passport_address: passportWallet.address,
-        passport_signature: passportSignature,
-      },
-    )
-    console.log(JSON.stringify(res.data))
-  } catch (e) {
-    console.log(e)
-    throw e
-  }
+      const altcha = await getAltchaPayload(client)
+      try {
+        const res = await client.post(
+          `https://faucet-api.initiation-1.initia.xyz/claim`,
+          {
+            address: address,
+            altcha_payload: altcha,
+            denom: 'uinit',
+            discord_code: '',
+            passport_address: passportWallet.address,
+            passport_signature: passportSignature,
+          },
+        )
+        console.log(JSON.stringify(res.data))
+      } catch (e) {
+        console.log(e)
+        throw e
+      }
+    })
 }
 
+// [0,1,2,3,4,5,6,7,8]
+// 5
+// i * 4 + 0
+// i * 4 + 1
+// i * 4 + 2
+// i * 4 + 3
+
 async function checkFaucetBalance() {
   const accounts = await DBClient.instance.faucet.findMany({
-    where: { balance: 0 },
+    where: { balance: { lt: 1000000 } },
+    // take: 50,
   })
-  await forEachAsync(accounts, 10, async (account, index) => {
-    try {
-      console.log(`checking ${index}/${accounts.length}`)
-      await polly()
-        .waitAndRetry(5)
-        .executeForPromise(async () => {
-          const client = new InitiaClient(account.mnemonic, true)
-          const gasAmount = await client.getGasAmount()
-          console.log(index, gasAmount)
-          await DBClient.instance.faucet.update({
-            where: { id: account.id },
-            data: { balance: gasAmount },
+  await forEachAsync(
+    accounts,
+    20,
+    async (account, index) => {
+      try {
+        console.log(`checking ${index}/${accounts.length}`)
+        await polly()
+          .waitAndRetry(5)
+          .executeForPromise(async () => {
+            const client = new InitiaClient(account.mnemonic, true)
+            const gasAmount = await client.getGasAmount()
+            console.log(index, gasAmount)
+            await DBClient.instance.faucet.update({
+              where: { id: account.id },
+              data: { balance: gasAmount },
+            })
           })
+      } catch (e) {
+        console.log(`${index}: error`, e)
+        await DBClient.instance.faucet.update({
+          where: { id: account.id },
+          data: { balance: 0 },
         })
-    } catch (e) {
-      console.log(`${index}: error`, e)
-      await DBClient.instance.faucet.update({
-        where: { id: account.id },
-        data: { balance: 0 },
-      })
-    }
-  })
+      }
+    },
+    { timeout: 10000 },
+  )
 }
 
 function shuffleArray(array) {
@@ -146,6 +165,7 @@ async function startFaucet(concurrency: number) {
               availableFrom: new Date(
                 new Date().getTime() + 24 * 60 * 60 * 1000,
               ),
+              message: 'success',
             },
           })
         })