-
Notifications
You must be signed in to change notification settings - Fork 142
Expand file tree
/
Copy pathindex.tsx
More file actions
149 lines (132 loc) · 4.42 KB
/
Copy pathindex.tsx
File metadata and controls
149 lines (132 loc) · 4.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import { FC, PropsWithChildren, useContext, useEffect } from 'react'
import { useQuery } from 'react-query'
import { Helmet } from 'react-helmet-async'
import { useLanguage } from '../../../shared/hooks'
import '../../styles.scss'
import { formatAmount } from '../../../../rippled/lib/txSummary/formatAmount'
import {
getAccountInfo,
getAMMInfoByAssets,
getLedgerEntry,
} from '../../../../rippled/lib/rippled'
import { Tabs } from '../../../shared/components/Tabs'
import { useAnalytics } from '../../../shared/analytics'
import { buildPath, useRouteParams } from '../../../shared/routing'
import { formatAsset } from '../../../shared/utils'
import SocketContext from '../../../shared/SocketContext'
import { ERROR_MESSAGES } from '../../Errors'
import NoMatch from '../../../NoMatch'
import {
AMMAccountHeader,
AmmDataType,
} from './AMMAccountHeader/AMMAccountHeader'
import { AccountTransactionTable } from '../../AccountTransactionTable'
import { hexToString } from '../../../shared/components/Currency'
import { ACCOUNT_ROUTE } from '../../../App/routes'
const getErrorMessage = (error: string) =>
ERROR_MESSAGES[error] || ERROR_MESSAGES.default
function renderError(error: any) {
const message = getErrorMessage(error.code)
return (
<div className="accounts-page">
<NoMatch title={message.title} hints={message.hints} />
</div>
)
}
const Page: FC<PropsWithChildren<{ accountId: string }>> = ({
accountId,
children,
}) => (
<div className="accounts-page">
<Helmet title={`AMM ${accountId.substring(0, 12)}...`} />
{children}
</div>
)
export const AMMAccounts = () => {
const { id: accountId = '', tab = 'transactions' } =
useRouteParams(ACCOUNT_ROUTE)
const mainPath = buildPath(ACCOUNT_ROUTE, { id: accountId })
const rippledSocket = useContext(SocketContext)
const language = useLanguage()
const { trackException, trackScreenLoaded } = useAnalytics()
const { data, error } = useQuery([accountId, language], () => {
let asset1: { currency: string; issuer?: string }
let asset2: { currency: string; issuer?: string }
/*
Get the first account transaction which in this case should be AMMCreate. From this we get
the two assets in the asset pool.
*/
return getAccountInfo(rippledSocket, accountId)
.then((accountInfo) =>
getLedgerEntry(rippledSocket, accountInfo.AMMID)
.then((ammLedgerEntry) => {
asset1 = formatAsset(ammLedgerEntry.node.Asset)
asset2 = formatAsset(ammLedgerEntry.node.Asset2)
// if one of the assets is XRP, make sure it's the second one
if (asset1.currency === 'XRP') {
const temp = asset2
asset2 = asset1
asset1 = temp
}
return getAMMInfoByAssets(rippledSocket, asset1, asset2)
})
/*
Use the assets to get the AMM Info.
*/
.then((ammDataWrapper) => {
const ammData = ammDataWrapper.amm
const balance = formatAmount(ammData.amount)
const balance2 = formatAmount(ammData.amount2)
const ammInfo: AmmDataType = {
balance,
balance2,
tradingFee: ammData.trading_fee,
lpBalance: ammData.lp_token.value,
accountId,
language,
}
return ammInfo
}),
)
.catch((e) => {
trackException(`Error setting up amm account --- ${JSON.stringify(e)}`)
throw e
})
})
useEffect(
() => () => {
window.scrollTo(0, 0)
},
[],
)
useEffect(() => {
if (!data) {
return
}
trackScreenLoaded({
account_id: data.accountId,
asset1: `${hexToString(data.balance.currency)}.${data.balance.issuer}`,
asset2: `${hexToString(data.balance2.currency)}.${data.balance2.issuer}`,
})
}, [data, trackScreenLoaded])
const tabs = ['transactions']
if (error) {
return <Page accountId={accountId}>{renderError(error)}</Page>
}
return (
<div className="accounts-page section">
<Helmet>
<title>AMM {accountId.substring(0, 12)}...</title>
</Helmet>
{data && (
<>
<AMMAccountHeader data={data} />
<Tabs tabs={tabs} selected={tab} path={mainPath} />
{tab === 'transactions' && (
<AccountTransactionTable accountId={accountId} hasTokensColumn />
)}
</>
)}
</div>
)
}