-
Notifications
You must be signed in to change notification settings - Fork 99
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Creat and Fund Account Page #788
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
943977e
Creat Account Page
194e2e1
revert .nvmrc
dc6405b
remove comments
ef2b0ec
replace futurenet text with dynamic text
8680b25
const instead of function
c5390bf
Update src/app/(sidebar)/account/fund/page.tsx
jeesunikim b159fd0
use useQuery and isFetchAfter to disable cache
e2228a3
applying suggestions
9c6ece1
remove unused code from <Alert/> and styles
b3b8a15
Update src/app/(sidebar)/account/fund/page.tsx
jeesunikim File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"> | ||
jeesunikim marked this conversation as resolved.
Show resolved
Hide resolved
|
||
<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); | ||
jeesunikim marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
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; | ||
}; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
isFetchAfterMount
is needed to disable the cache