Shawn Lu 1 سال پیش
والد
کامیت
ff2f193aa5
7فایلهای تغییر یافته به همراه98 افزوده شده و 1 حذف شده
  1. 2 0
      package.json
  2. 15 0
      src/InitiaClient.ts
  3. 21 0
      src/faucet/faucet.ts
  4. 35 0
      src/faucet/recaptcha.ts
  5. 20 0
      src/test.ts
  6. 0 1
      src/utils/index.ts
  7. 5 0
      yarn.lock

+ 2 - 0
package.json

@@ -23,6 +23,7 @@
   "scripts": {
     "prestart": "npm run build",
     "start": "node build/src/main.js",
+    "testShawn": "npm run build && node build/src/test.js",
     "clean": "rimraf coverage build tmp",
     "build": "tsc -p tsconfig.json && tsc-alias",
     "build:watch": "tsc -w -p tsconfig.json && tsc-alias",
@@ -36,6 +37,7 @@
     "@initia/initia.js": "^0.1.51",
     "@prisma/client": "^5.14.0",
     "axios": "^1.6.8",
+    "dotenv": "^16.4.5",
     "https-proxy-agent": "^7.0.4",
     "node-cron": "^3.0.3",
     "polly-js": "^1.8.3",

+ 15 - 0
src/InitiaClient.ts

@@ -1,4 +1,5 @@
 import { InitiaTask } from './InitiaTask'
+import { MsgSend } from '@initia/initia.js'
 
 export class InitiaClient extends InitiaTask {
   constructor(mnemonic: string) {
@@ -12,4 +13,18 @@ export class InitiaClient extends InitiaTask {
     console.log(balances)
     //todo filter balance
   }
+
+  async transfer(to: string, amount: number) {
+    const msg = new MsgSend(
+      this.wallet.key.accAddress,
+      to,
+      `${amount * Math.pow(10, 6)}uinit`,
+    )
+    const signed = await this.wallet.createAndSignTx({
+      // sequence: 3,
+      msgs: [msg],
+    })
+    const broadcast = await this.lcd.tx.broadcast(signed)
+    console.log(broadcast)
+  }
 }

+ 21 - 0
src/faucet/faucet.ts

@@ -0,0 +1,21 @@
+import { getAxiosClient } from '../utils'
+import { getCaptcha } from './recaptcha'
+
+export async function faucetAccount(address: string) {
+  const client = getAxiosClient(true)
+  const captcha = await getCaptcha(address)
+  try {
+    const res = await client.post(
+      `https://faucet-api.initiation-1.initia.xyz/claim`,
+      {
+        address: address,
+        denom: 'uinit',
+        response: captcha,
+      },
+    )
+    console.log(JSON.stringify(res.data))
+  } catch (e) {
+    console.log(e)
+    throw e
+  }
+}

+ 35 - 0
src/faucet/recaptcha.ts

@@ -0,0 +1,35 @@
+import axios from 'axios'
+import dotenv from 'dotenv'
+import polly from 'polly-js'
+dotenv.config()
+
+const googlekey = '6LdLhtYpAAAAAOe1xmceNR-i6MTtzq7N6AYztoVI'
+const captchaApiKey = process.env.CAPTCHA_API_KEY
+
+export async function getCaptcha(address: string) {
+  return await polly()
+    .waitAndRetry(2)
+    .executeForPromise(async () => {
+      const res = await axios.post('https://2captcha.com/in.php', {
+        method: 'userrecaptcha',
+        key: captchaApiKey,
+        googlekey,
+        pageUrl: `https://faucet.testnet.initia.xyz/?address=${address}`,
+        json: 1,
+        action: 'verify',
+      })
+      const request = res.data.request
+      return await polly()
+        .waitAndRetry([10000, 10000, 10000, 10000, 10000, 10000, 10000, 10000])
+        .executeForPromise(async () => {
+          const captchaRes = await axios.get(
+            `https://2captcha.com/res.php?key=${captchaApiKey}&action=get&id=${request}&json=1`,
+          )
+          const token = captchaRes.data.request
+          console.log(token)
+          if (token === 'CAPCHA_NOT_READY') throw new Error('CAPCHA_NOT_READY')
+          if (!token) throw new Error('no result')
+          return token
+        })
+    })
+}

+ 20 - 0
src/test.ts

@@ -0,0 +1,20 @@
+import { getCaptcha } from './faucet/recaptcha'
+import { faucetAccount } from './faucet/faucet'
+
+import { MnemonicKey } from '@initia/initia.js'
+import { InitiaClient } from './InitiaClient'
+import polly from 'polly-js'
+
+await polly()
+  .waitAndRetry(4)
+  .executeForPromise(async () => {
+    // const key = new MnemonicKey()
+    // console.log(key.accAddress)
+    // console.log(key.mnemonic)
+    await faucetAccount('init1yvmuhk70rp7n84qws7ws6emqtve0u22cthfwck')
+  })
+// const client = new InitiaClient(
+//   'evil pact small brass now dirt gun horror route screen camp rookie crouch valid feel useful ladder robot slide adjust pact salad horse gesture',
+// )
+// console.log(client.key.accAddress)
+// await client.transfer('init1yvmuhk70rp7n84qws7ws6emqtve0u22cthfwck', 4)

+ 0 - 1
src/utils/index.ts

@@ -17,7 +17,6 @@ export function generateRandomString(length: number) {
 
 export function getAxiosClient(useProxy: boolean) {
   const pass = `test1234_session-${generateRandomString(8)}_lifetime-20m`
-
   const proxyAgent = useProxy
     ? new HttpsProxyAgent(`http://tuxla:${pass}@geo.iproyal.com:12321`)
     : undefined

+ 5 - 0
yarn.lock

@@ -898,6 +898,11 @@ doctrine@^3.0.0:
   dependencies:
     esutils "^2.0.2"
 
+dotenv@^16.4.5:
+  version "16.4.5"
+  resolved "https://registry.yarnpkg.com/dotenv/-/dotenv-16.4.5.tgz#cdd3b3b604cb327e286b4762e13502f717cb099f"
+  integrity sha512-ZmdL2rui+eB2YwhsWzjInR8LldtZHGDoQ1ugH85ppHKwpUHL7j7rN0Ti9NCnGiQbhaZ11FpR+7ao1dNsmduNUg==
+
 eastasianwidth@^0.2.0:
   version "0.2.0"
   resolved "https://registry.npmjs.org/eastasianwidth/-/eastasianwidth-0.2.0.tgz"