From 9f126d1493a59bb8c7cbcc28dccac779d72f24a4 Mon Sep 17 00:00:00 2001 From: Harsh R <53080940+fullstackninja864@users.noreply.github.com> Date: Sat, 25 Nov 2023 10:35:17 +0530 Subject: [PATCH] chore(api): added retry for faucet fund transfer (#377) * chore(api): added retry for faucet fund transfer * lint * minor fix --- apps/server/src/MetascanServerApp.ts | 16 +++++------ apps/server/src/faucet/FaucetService.ts | 35 ++++++++++++++++++------- apps/web/src/pages/faucet/index.tsx | 2 +- 3 files changed, 34 insertions(+), 19 deletions(-) diff --git a/apps/server/src/MetascanServerApp.ts b/apps/server/src/MetascanServerApp.ts index d2e4006b..66b3e704 100644 --- a/apps/server/src/MetascanServerApp.ts +++ b/apps/server/src/MetascanServerApp.ts @@ -24,14 +24,14 @@ export class MetascanServerApp { + try { + const evmProviderService = new EVMProviderService(network); + const wallet = new ethers.Wallet(this.privateKey, evmProviderService.provider); + const nonce = await evmProviderService.provider.getTransactionCount(wallet.address); + const tx = { + to: address, + value: parseEther(amount), + nonce, + }; + return await wallet.sendTransaction(tx); + } catch (e) { + if (e.code === 'SERVER_ERROR' && retryCount < this.retry) { + this.logger.error( + `Getting SERVER_ERROR retrying: ${retryCount} for ${amount} DFI ${network} to address ${address}`, + ); + return await this.transferFund(address, amount, network, retryCount + 1); + } + throw new Error(e); + } + } + async sendFundsToUser(address: string, amount: string, network: EnvironmentNetwork): Promise { - // Send funds to user if recaptcha validation is successful - const evmProviderService = new EVMProviderService(network); - const wallet = new ethers.Wallet(this.privateKey, evmProviderService.provider); - const nonce = await evmProviderService.provider.getTransactionCount(wallet.address); - const tx = { - to: address, - value: parseEther(amount), - nonce, - }; this.logger.log(`Initiating transfer of ${amount} DFI ${network} to address ${address}`); - const response = await wallet.sendTransaction(tx); + const response = await this.transferFund(address, amount, network); this.logger.log( `Transfer done to address ${address} of amount ${amount} DFI ${network} with txn hash ${ response.hash diff --git a/apps/web/src/pages/faucet/index.tsx b/apps/web/src/pages/faucet/index.tsx index eaaef2f9..8d8a2d3c 100644 --- a/apps/web/src/pages/faucet/index.tsx +++ b/apps/web/src/pages/faucet/index.tsx @@ -141,7 +141,7 @@ export default function Faucet() {