Skip to content

Commit

Permalink
熊链领水增加并发处理
Browse files Browse the repository at this point in the history
  • Loading branch information
0xsongsu committed May 22, 2024
1 parent 990af38 commit 5b7a3ad
Showing 1 changed file with 49 additions and 36 deletions.
85 changes: 49 additions & 36 deletions src/bearChain/bearToken.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const ethers = require('ethers');

const MAX_RETRIES = 5; // 最大重试次数
const MAX_PROXY_CHECK_ATTEMPTS = 3;
const CONCURRENT_REQUESTS = 10; // 并发请求数量

const agent = new HttpsProxyAgent(config.proxy);
const websiteKey = '0x4AAAAAAARdAuciFArKhVwt';
Expand All @@ -24,7 +25,7 @@ let headers = {
'pragma': 'no-cache',
'referer': 'https://artio.faucet.berachain.com/',
'user-agent': userAgent,
}
};

async function recaptcha() {
const { taskId } = await createTask(websiteUrl, websiteKey, 'TurnstileTaskProxylessM1');
Expand Down Expand Up @@ -52,6 +53,40 @@ async function processAddresses(filePath) {
}
}

async function claimToken(address) {
logger().info(`开始为 ${address} 地址领取测试币`);
let attempts = 0;
while (attempts < MAX_RETRIES) {
try {
const recaptchaToken = await recaptcha();
headers['authorization'] = `Bearer ${recaptchaToken}`;
const url = `https://artio-80085-faucet-api-cf.berachain.com/api/claim?address=${address}`;
const data = { address };
const urlConfig = {
headers: headers,
httpsAgent: agent,
httpAgent: agent,
method: 'post',
data: data,
};

const response = await sendRequest(url, urlConfig);
const txHash = response.msg;
logger().info(`领取成功✅,交易哈希: ${txHash}`);
return;
} catch (error) {
attempts++;
if (error.response && error.response.data && error.response.data.message === 'Faucet is overloading, please try again') {
logger().warn(`地址${address}正在重试第 ${attempts} 次...`);
await sleep(5);
} else {
logger().error(`领取失败❌,地址:${address}:`, error);
break;
}
}
}
}

async function main() {
try {
const addresses = await processAddresses('./address.txt');
Expand Down Expand Up @@ -82,47 +117,25 @@ async function main() {
return;
}

for (const address of addresses) {
let checksumAddress;
try {
checksumAddress = ethers.utils.getAddress(address);
} catch (error) {
logger().error(`地址格式错误: ${address}`, error);
continue;
}
logger().info(`领取地址: ${checksumAddress}`);
let currentIndex = 0;

let attempts = 0;
while (attempts < MAX_RETRIES) {
async function worker() {
while (currentIndex < addresses.length) {
const address = addresses[currentIndex++];
let checksumAddress;
try {
const recaptchaToken = await recaptcha();
headers['authorization'] = `Bearer ${recaptchaToken}`;
const url = `https://artio-80085-faucet-api-cf.berachain.com/api/claim?address=${checksumAddress}`;
const data = { address: checksumAddress };
const urlConfig = {
headers: headers,
httpsAgent: agent,
httpAgent: agent,
method: 'post',
data: data,
};

const response = await sendRequest(url, urlConfig);
const txHash = response.msg;
logger().info(`领取成功✅,交易哈希: ${txHash}`);
break;
checksumAddress = ethers.utils.getAddress(address);
} catch (error) {
attempts++;
if (error.response && error.response.data && error.response.data.message === 'Faucet is overloading, please try again') {
logger().warn(`地址${checksumAddress}正在重试第 ${attempts} 次...`);
await sleep(5);
} else {
logger().error(`领取失败❌,地址:${checksumAddress}:`, error);
break;
}
logger().error(`地址格式错误: ${address}`, error);
continue;
}
await claimToken(checksumAddress);
}
}

const workers = Array(CONCURRENT_REQUESTS).fill(null).map(() => worker());
await Promise.all(workers);

} catch (error) {
logger().error('处理地址列表时出错:', error);
}
Expand Down

0 comments on commit 5b7a3ad

Please sign in to comment.