-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add getBalance endpoint using etherscan api (#58)
* feat: add balance api endpoint * feat: etherscan api key
- Loading branch information
Showing
5 changed files
with
53 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
NEXT_PUBLIC_STACKUP_BUNDLER_API_KEY="" | ||
NEXT_PUBLIC_RPC_ENDPOINT="https://eth-sepolia.g.alchemy.com/v2/N5ZVikrQ0kBZLVjyY8GwbpYSLPLRuINB" | ||
NEXT_PUBLIC_RPC_ENDPOINT="" | ||
ETHERSCAN_API_KEY="" | ||
NEXT_PUBLIC_FACTORY_CONTRACT_ADDRESS="0xDD0f9cB4Cf53d28b976C13e7ee4a169F841924c0" | ||
RELAYER_PRIVATE_KEY="" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { Hex, stringify } from "viem"; | ||
|
||
export async function GET(_req: Request, { params }: { params: { address: Hex } }) { | ||
const { address } = params; | ||
if (!address) { | ||
return Response.json(JSON.parse(stringify({ error: "address is required" }))); | ||
} | ||
const result = await fetch( | ||
`https://api-sepolia.etherscan.io/api?module=account&action=balance&address=${address}&tag=latest&apikey=${process.env.ETHERSCAN_API_KEY}`, | ||
); | ||
const resultJSON = await result.json(); | ||
const balance = BigInt(resultJSON?.result || 0); | ||
|
||
return Response.json(JSON.parse(stringify({ balance }))); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { Address, Hex } from "viem"; | ||
|
||
export type User = { id: Hex; pubKey: { x: Hex; y: Hex }; account: Address; balance: bigint }; | ||
|
||
export async function getBalance(address: Hex): Promise<{ balance: bigint }> { | ||
const response = await fetch(`/api/balance/${address}`, { | ||
method: "GET", | ||
}); | ||
|
||
const user = await response.json(); | ||
return { | ||
balance: user.balance, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters