-
Notifications
You must be signed in to change notification settings - Fork 99
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0f350cd
commit 88401a6
Showing
9 changed files
with
216 additions
and
45 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 +1 @@ | ||
v18 | ||
v18 |
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 |
---|---|---|
@@ -1,5 +1,121 @@ | ||
"use client"; | ||
|
||
import { useEffect, useState } from "react"; | ||
import { Alert, Card, Input, Text, Button } from "@stellar/design-system"; | ||
|
||
import { shortenStellarAddress } from "@/helpers/shortenStellarAddress"; | ||
import { validatePublicKey } from "@/helpers/validatePublicKey"; | ||
import { useFriendBot } from "@/query/useFriendBot"; | ||
import { useStore } from "@/store/useStore"; | ||
|
||
import "../styles.scss"; | ||
|
||
export default function FundAccount() { | ||
return <div>Fund Account</div>; | ||
const { account, network } = useStore(); | ||
|
||
const [showAlert, setShowAlert] = useState<boolean>(false); | ||
const [generatedPublicKey, setGeneratedPublicKey] = useState<string>(""); | ||
const [inlineErrorMessage, setInlineErrorMessage] = useState<string>(""); | ||
|
||
const { error, isError, isLoading, isSuccess, refetch, isFetchedAfterMount } = | ||
useFriendBot({ | ||
network: network.id, | ||
publicKey: generatedPublicKey, | ||
}); | ||
|
||
useEffect(() => { | ||
if (isError || isSuccess) { | ||
setShowAlert(true); | ||
} | ||
}, [isError, isSuccess]); | ||
|
||
return ( | ||
<div className="Account"> | ||
<Card> | ||
<div className="Account__card"> | ||
<div className="CardText"> | ||
<Text size="lg" as="h1" weight="medium"> | ||
Friendbot: fund a {network.id} network account | ||
</Text> | ||
|
||
<Text size="sm" as="p"> | ||
The friendbot is a horizon API endpoint that will fund an account | ||
with 10,000 lumens on the {network.id} network. | ||
</Text> | ||
</div> | ||
|
||
<Input | ||
id="generate-keypair-publickey" | ||
fieldSize="md" | ||
label="Public Key" | ||
value={generatedPublicKey} | ||
onChange={(e) => { | ||
setGeneratedPublicKey(e.target.value); | ||
|
||
const error = validatePublicKey(e.target.value); | ||
setInlineErrorMessage(error); | ||
}} | ||
placeholder="Ex: GCEXAMPLE5HWNK4AYSTEQ4UWDKHTCKADVS2AHF3UI2ZMO3DPUSM6Q4UG" | ||
error={inlineErrorMessage} | ||
/> | ||
|
||
<div className="Account__CTA"> | ||
<Button | ||
disabled={!generatedPublicKey || Boolean(inlineErrorMessage)} | ||
size="md" | ||
variant={isFetchedAfterMount && isError ? "error" : "secondary"} | ||
isLoading={isLoading} | ||
onClick={() => { | ||
if (!inlineErrorMessage) { | ||
refetch(); | ||
} | ||
}} | ||
> | ||
Get lumens | ||
</Button> | ||
|
||
<Button | ||
disabled={!account.publicKey || isLoading} | ||
size="md" | ||
variant="tertiary" | ||
onClick={() => { | ||
setInlineErrorMessage(""); | ||
setGeneratedPublicKey(account.publicKey); | ||
}} | ||
> | ||
Fill in with generated key | ||
</Button> | ||
</div> | ||
</div> | ||
</Card> | ||
|
||
{showAlert && isFetchedAfterMount && isSuccess && ( | ||
<Alert | ||
placement="inline" | ||
variant="primary" | ||
actionLabel="View on stellar.expert" | ||
actionLink={`https://stellar.expert/explorer/${network.id}/account/${account.publicKey}`} | ||
onClose={() => { | ||
setShowAlert(false); | ||
}} | ||
title={`Successfully funded ${shortenStellarAddress(account.publicKey)} on | ||
${network.id}`} | ||
> | ||
{""} | ||
</Alert> | ||
)} | ||
{showAlert && isFetchedAfterMount && isError && ( | ||
<Alert | ||
placement="inline" | ||
variant="error" | ||
onClose={() => { | ||
setShowAlert(false); | ||
}} | ||
title={error?.message} | ||
> | ||
{""} | ||
</Alert> | ||
)} | ||
</div> | ||
); | ||
} |
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
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,7 @@ | ||
export const shortenStellarAddress = (address: string, size: number = 8) => { | ||
const slice = Math.ceil(size / 2); | ||
|
||
return `${address.substring(0, slice)}…${address.substring( | ||
address.length - slice, | ||
)}`; | ||
}; |
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,17 @@ | ||
import { StrKey } from "stellar-sdk"; | ||
|
||
export const validatePublicKey = (issuer: string) => { | ||
if (!issuer) { | ||
return "Asset issuer is required."; | ||
} | ||
|
||
if (issuer.startsWith("M")) { | ||
if (!StrKey.isValidMed25519PublicKey(issuer)) { | ||
return "Muxed account address is invalid."; | ||
} | ||
} else if (!StrKey.isValidEd25519PublicKey(issuer)) { | ||
return "Public key is invalid."; | ||
} | ||
|
||
return ""; | ||
}; |
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,43 @@ | ||
import { useQuery } from "@tanstack/react-query"; | ||
|
||
import { shortenStellarAddress } from "@/helpers/shortenStellarAddress"; | ||
|
||
export const useFriendBot = ({ | ||
network, | ||
publicKey, | ||
}: { | ||
network: string; | ||
publicKey: string; | ||
}) => { | ||
const friendbotURL = | ||
network === "futurenet" | ||
? "https://friendbot-futurenet.stellar.org" | ||
: "https://friendbot.stellar.org"; | ||
|
||
const query = useQuery({ | ||
queryKey: ["friendBot"], | ||
queryFn: async () => { | ||
try { | ||
const response = await fetch(`${friendbotURL}/?addr=${publicKey}`); | ||
|
||
if (!response.ok) { | ||
const errorBody = await response.json(); | ||
|
||
throw new Error(errorBody.status); | ||
} | ||
return response; | ||
} catch (e: any) { | ||
if (e.status === 0) { | ||
throw new Error(`Unable to reach Friendbot server at ${network}`); | ||
} else { | ||
throw new Error( | ||
`Unable to fund ${shortenStellarAddress(publicKey)} on the test network`, | ||
); | ||
} | ||
} | ||
}, | ||
enabled: false, | ||
}); | ||
|
||
return query; | ||
}; |