Fetch sequence on account creation.

This commit is contained in:
muzam1l
2023-03-10 16:18:35 +05:30
parent 55c68c580a
commit 0fce9af77c

View File

@@ -1,5 +1,6 @@
import toast from 'react-hot-toast' import toast from 'react-hot-toast'
import state, { FaucetAccountRes } from '../index' import state, { FaucetAccountRes } from '../index'
import fetchAccountInfo from '../../utils/accountInfo';
export const names = [ export const names = [
'Alice', 'Alice',
@@ -35,40 +36,37 @@ export const addFaucetAccount = async (name?: string, showToast: boolean = false
}) })
const json: FaucetAccountRes | { error: string } = await res.json() const json: FaucetAccountRes | { error: string } = await res.json()
if ('error' in json) { if ('error' in json) {
if (showToast) { if (!showToast) return;
return toast.error(json.error, { id: toastId }) return toast.error(json.error, { id: toastId })
} else { }
return const currNames = state.accounts.map(acc => acc.name)
} const info = await fetchAccountInfo(json.address, { silent: true })
} else { state.accounts.push({
if (showToast) { name: name || names.filter(name => !currNames.includes(name))[0],
toast.success('New account created', { id: toastId }) xrp: (json.xrp || 0 * 1000000).toString(),
} address: json.address,
const currNames = state.accounts.map(acc => acc.name) secret: json.secret,
state.accounts.push({ sequence: info?.Sequence || 1,
name: name || names.filter(name => !currNames.includes(name))[0], hooks: [],
xrp: (json.xrp || 0 * 1000000).toString(), isLoading: false,
address: json.address, version: '2'
secret: json.secret, })
sequence: 1, if (showToast) {
hooks: [], toast.success('New account created', { id: toastId })
isLoading: false,
version: '2'
})
} }
} }
// fetch initial faucets // fetch initial faucets
;(async function fetchFaucets() { ; (async function fetchFaucets() {
if (typeof window !== 'undefined') { if (typeof window !== 'undefined') {
if (state.accounts.length === 0) { if (state.accounts.length === 0) {
await addFaucetAccount() await addFaucetAccount()
// setTimeout(() => { // setTimeout(() => {
// addFaucetAccount(); // addFaucetAccount();
// }, 10000); // }, 10000);
}
} }
} })()
})()
export const addFunds = async (address: string) => { export const addFunds = async (address: string) => {
const toastId = toast.loading('Requesting funds') const toastId = toast.loading('Requesting funds')