Skip to content

Commit

Permalink
chore(api): added retry for faucet fund transfer (#377)
Browse files Browse the repository at this point in the history
* chore(api): added retry for faucet fund transfer

* lint

* minor fix
  • Loading branch information
fullstackninja864 authored Nov 25, 2023
1 parent eaff382 commit 9f126d1
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 19 deletions.
16 changes: 8 additions & 8 deletions apps/server/src/MetascanServerApp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@ export class MetascanServerApp<App extends NestFastifyApplication = NestFastifyA
methods: ['GET', 'PUT', 'POST', 'DELETE', 'OPTIONS'],
maxAge: 60 * 24 * 7,
origin: '*',
// process.env.NODE_ENV === 'production'
// ? [
// 'https://meta.defiscan.live',
// /https:\/\/([^.]*.\.)*defimetascan\.app/, // allow all subdomains of defimetascan
// /https:\/\/([^.]*.)--defimetascan\.netlify\.app/, // allow all netlify preview deployments
// /https?:\/\/localhost(:\d+)?/, // allow localhost connection
// ]
// : '*',
// process.env.NODE_ENV === 'production'
// ? [
// 'https://meta.defiscan.live',
// /https:\/\/([^.]*.\.)*defimetascan\.app/, // allow all subdomains of defimetascan
// /https:\/\/([^.]*.)--defimetascan\.netlify\.app/, // allow all netlify preview deployments
// /https?:\/\/localhost(:\d+)?/, // allow localhost connection
// ]
// : '*',
});
}

Expand Down
35 changes: 25 additions & 10 deletions apps/server/src/faucet/FaucetService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,38 @@ export class FaucetService {

private readonly privateKey: string;

private readonly retry = 10;

constructor(private configService: ConfigService) {
this.logger = new Logger(FaucetService.name);
this.privateKey = this.configService.getOrThrow('privateKey');
}

async transferFund(address, amount, network, retryCount = 1): Promise<TransactionResponse> {
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<TransactionResponse> {
// 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
Expand Down
2 changes: 1 addition & 1 deletion apps/web/src/pages/faucet/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ export default function Faucet() {
</div>
<LinkText
label={data.hash}
href={`/txn/${data.hash}`}
href={`/tx/${data.hash}`}
customStyle="text-base font-semibold tracking-[0.032em]"
/>
</div>
Expand Down

0 comments on commit 9f126d1

Please sign in to comment.