Skip to content

Commit

Permalink
Merge pull request #164 from gabrieltemtsen/add-faucet-balance-endpoint
Browse files Browse the repository at this point in the history
feat: balance endpoint #153
  • Loading branch information
karacurt authored Nov 26, 2024
2 parents 691e617 + c76ca64 commit 473d485
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 4 deletions.
12 changes: 12 additions & 0 deletions api/src/controllers/faucet.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,16 @@ export class FaucetController {
return res.status(500).send({ status: 'error', result: "Internal server error" });
}
}
async getBalance(req: Request, res: Response) {
try {
console.log(`[FaucetController::getBalance] Retrieving faucet balance`);
const { address, balance } = await this.faucetService.getBalance();
console.log(`[FaucetController::getBalance] Signer Address: ${address}, Balance: ${balance} ETH`);
return res.status(200).send({ status: 'success', result: { address, balance } });
} catch (error: any) {
const decodedError = await tokenSenderErrorDecoder.decode(error);
console.log(`[FaucetController::getBalance] Error retrieving balance: ${decodedError.name} - ${decodedError.args}`);
return res.status(500).send({ status: 'error', result: "Internal server error" });
}
}
}
16 changes: 16 additions & 0 deletions api/src/routes/faucet.router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,22 @@ const faucetRoutes = (): Router => {
(req: Request, res: Response) => faucetController.getRemainingTime(req, res)
);

/**
* @swagger
* /faucet/balance:
* get:
* summary: Get the current balance of the faucet.
* description: Retrieve the current balance of the faucet contract in Ether.
* responses:
* '200':
* description: A successful response with the faucet balance.
* '500':
* description: Internal server error
*/
FaucetRouter.get('/balance', (req: Request, res: Response) =>
faucetController.getBalance(req, res)
);

return FaucetRouter;
};

Expand Down
19 changes: 15 additions & 4 deletions api/src/services/faucet.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@ import { toUnixTimestamp } from '../utils/date';

export class FaucetService {
tokenSender: ethers.Contract;
provider: ethers.JsonRpcProvider;
signer: AwsKmsSigner;

constructor() {
const provider = new ethers.JsonRpcProvider(GAME7_TESTNET_RPC_URL);
const signer = new AwsKmsSigner(
this.provider = new ethers.JsonRpcProvider(GAME7_TESTNET_RPC_URL);
this.signer = new AwsKmsSigner(
{
region: KMS_CREDENTIALS.region,
keyId: KMS_CREDENTIALS.keyId,
Expand All @@ -23,12 +25,12 @@ export class FaucetService {
secretAccessKey: KMS_CREDENTIALS.secretAccessKey,
},
},
provider
this.provider
);
this.tokenSender = new ethers.Contract(
TOKEN_SENDER_ADDRESS,
TokenSenderABI,
signer
this.signer
);
}

Expand Down Expand Up @@ -58,4 +60,13 @@ export class FaucetService {
if (remainingTime <= 0) return 0;
else return remainingTime;
}
async getBalance() {
const signerAddress = await this.signer.getAddress();
const balance = await this.provider.getBalance(signerAddress);

return {
address: signerAddress,
balance: ethers.formatEther(balance),
};
}
}

0 comments on commit 473d485

Please sign in to comment.