1
0
mirror of https://github.com/oceanprotocol/market.git synced 2024-06-25 18:56:50 +02:00
market/src/components/molecules/PoolTransactions.tsx

134 lines
3.6 KiB
TypeScript
Raw Normal View History

import { PoolTransaction } from '@oceanprotocol/lib/dist/node/balancer/OceanPool'
import { useOcean } from '@oceanprotocol/react'
import React, { ReactElement, useEffect, useState } from 'react'
import EtherscanLink from '../atoms/EtherscanLink'
import Time from '../atoms/Time'
import Table from '../atoms/Table'
import AssetTitle from './AssetListTitle'
import styles from './PoolTransactions.module.css'
import { formatCurrency } from '@coingecko/cryptoformat'
import { useUserPreferences } from '../../providers/UserPreferences'
function formatNumber(number: number, locale: string) {
return formatCurrency(number, '', locale, true, {
significantFigures: 6
})
}
function Title({ row }: { row: PoolTransaction }) {
const { ocean, networkId } = useOcean()
const [dtSymbol, setDtSymbol] = useState<string>()
const { locale } = useUserPreferences()
Swap tokens (#204) * swap Signed-off-by: mihaisc <mihai.scarlat@smartcontrol.ro> * validation and calculation Signed-off-by: mihaisc <mihai.scarlat@smartcontrol.ro> * refactor Signed-off-by: mihaisc <mihai.scarlat@smartcontrol.ro> * remove unused effect Signed-off-by: mihaisc <mihai.scarlat@smartcontrol.ro> * fix interval Signed-off-by: mihaisc <mihai.scarlat@smartcontrol.ro> * increase refresh timer, remove optional params Signed-off-by: mihaisc <mihai.scarlat@smartcontrol.ro> * make inputs show up without wallet * style fixes * restyling * styling * more styling * fix refresh price Signed-off-by: mihaisc <mihai.scarlat@smartcontrol.ro> * remove test effect * fixes, get data as early as possible from DDO and initial state * refactor * refactor * refactor * label tweaks * copy * typo * prototype output * remove price header * ouput swap fee * fix * spacing * copy * refactor pool transaction titles * copy * update math Signed-off-by: mihaisc <mihai.scarlat@smartcontrol.ro> * use messaging tweaks * tab tweaks, output refactor * fix dark mode selection style * prototype output * method tweaks * slippage to 1%, added warnig banner Signed-off-by: mihaisc <mihai.scarlat@smartcontrol.ro> * form tweaks * error fix * empty inputs by default * longer intervals * maxOcean validation fix * slippage tolerance UI * modified slippage UI * refactor, refresh ocean user balance * move typings/models around * typing fix * fixed output values Signed-off-by: mihaisc <mihai.scarlat@smartcontrol.ro> * bump oceanlib Signed-off-by: mihaisc <mihai.scarlat@smartcontrol.ro> * remove console.log * remove placeholder * tweak * non-web3 browser tweak Co-authored-by: Matthias Kretschmann <m@kretschmann.io>
2020-11-16 16:21:15 +01:00
const symbol = dtSymbol || 'OCEAN'
const title =
row.type === 'join'
? `Add ${formatNumber(Number(row.tokenAmountIn), locale)} ${symbol}`
: row.type === 'exit'
? `Remove ${formatNumber(Number(row.tokenAmountOut), locale)} ${symbol}`
: `Swap ${formatNumber(
Number(row.tokenAmountIn),
locale
)} ${symbol} for ${formatNumber(
Number(row.tokenAmountOut),
locale
)} ${symbol}`
useEffect(() => {
if (!ocean) return
async function getSymbol() {
const symbol = await ocean.datatokens.getSymbol(
row.tokenIn || row.tokenOut
)
setDtSymbol(symbol)
}
getSymbol()
}, [ocean, row])
return (
<EtherscanLink networkId={networkId} path={`/tx/${row.transactionHash}`}>
{title}
</EtherscanLink>
)
}
function getColumns(minimal?: boolean) {
return [
{
name: 'Title',
selector: function getTitleRow(row: PoolTransaction) {
return <Title row={row} />
},
minWidth: '14rem',
grow: 1
},
{
name: 'Data Set',
selector: function getAssetRow(row: PoolTransaction) {
const did = row.dtAddress.replace('0x', 'did:op:')
return <AssetTitle did={did} />
},
omit: minimal
},
{
name: 'Time',
selector: function getTimeRow(row: PoolTransaction) {
return (
<Time
className={styles.time}
date={row.timestamp.toString()}
relative
isUnix
/>
)
}
}
]
}
export default function PoolTransactions({
poolAddress,
minimal
}: {
poolAddress?: string
minimal?: boolean
}): ReactElement {
const { ocean, accountId } = useOcean()
const [logs, setLogs] = useState<PoolTransaction[]>()
const [isLoading, setIsLoading] = useState(false)
useEffect(() => {
async function getLogs() {
if (!ocean || !accountId) return
setIsLoading(true)
const logs = poolAddress
? await ocean.pool.getPoolLogs(poolAddress, 0, accountId)
: await ocean.pool.getAllPoolLogs(accountId)
// sort logs by date, newest first
const logsSorted = logs.sort((a, b) => {
if (a.timestamp > b.timestamp) return -1
if (a.timestamp < b.timestamp) return 1
return 0
})
setLogs(logsSorted)
setIsLoading(false)
}
getLogs()
}, [ocean, accountId, poolAddress])
return (
<Table
columns={getColumns(minimal)}
data={logs}
isLoading={isLoading}
noTableHead={minimal}
dense={minimal}
pagination={minimal ? logs?.length >= 4 : logs?.length >= 9}
paginationPerPage={minimal ? 5 : 10}
emptyMessage="Your pool transactions will show up here"
/>
)
}