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,31 +36,28 @@ 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
}
} else {
if (showToast) {
toast.success('New account created', { id: toastId })
} }
const currNames = state.accounts.map(acc => acc.name) const currNames = state.accounts.map(acc => acc.name)
const info = await fetchAccountInfo(json.address, { silent: true })
state.accounts.push({ state.accounts.push({
name: name || names.filter(name => !currNames.includes(name))[0], name: name || names.filter(name => !currNames.includes(name))[0],
xrp: (json.xrp || 0 * 1000000).toString(), xrp: (json.xrp || 0 * 1000000).toString(),
address: json.address, address: json.address,
secret: json.secret, secret: json.secret,
sequence: 1, sequence: info?.Sequence || 1,
hooks: [], hooks: [],
isLoading: false, isLoading: false,
version: '2' version: '2'
}) })
if (showToast) {
toast.success('New account created', { id: toastId })
} }
} }
// 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()
@@ -68,7 +66,7 @@ export const addFaucetAccount = async (name?: string, showToast: boolean = false
// }, 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')