Browse Source

add retry

Shawn Lu 1 year ago
parent
commit
2d925c349d
1 changed files with 31 additions and 26 deletions
  1. 31 26
      src/faucet/faucet.ts

+ 31 - 26
src/faucet/faucet.ts

@@ -10,7 +10,7 @@ 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) {
@@ -36,32 +36,37 @@ 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]