-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #177 from mintlayer/dev
Dev-16-07-2024
- Loading branch information
Showing
19 changed files
with
273 additions
and
20 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
26 changes: 26 additions & 0 deletions
26
src/components/composed/LockedBalanceList/LockedBalanceList.css
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,26 @@ | ||
.locked-table { | ||
width: 100%; | ||
} | ||
|
||
.locked-table-wrapper { | ||
display: flex; | ||
justify-content: center; | ||
align-items: flex-start; | ||
height: 465px; | ||
overflow: auto; | ||
} | ||
|
||
.locked-title { | ||
text-align: left; | ||
padding: 10px; | ||
background: rgb(var(--color-green)); | ||
width: 50%; | ||
color: rgb(var(--color-white)); | ||
} | ||
|
||
.locked-loading-wrapper { | ||
display: flex; | ||
justify-content: center; | ||
align-items: center; | ||
height: 465px; | ||
} |
122 changes: 122 additions & 0 deletions
122
src/components/composed/LockedBalanceList/LockedBalanceList.js
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,122 @@ | ||
import React, { useContext, useState, useEffect, useCallback } from 'react' | ||
import { NetworkContext } from '@Contexts' | ||
|
||
import { Loading } from '@ComposedComponents' | ||
import { Mintlayer } from '@APIs' | ||
import { addDays } from 'date-fns' | ||
import LockedBalanceListItem from './LockedBalanceListItem' | ||
import './LockedBalanceList.css' | ||
|
||
const LockedBalanceList = () => { | ||
const { lockedUtxos, transactions, fetchingUtxos } = | ||
useContext(NetworkContext) | ||
const [loading, setLoading] = useState(false) | ||
const [updatedUtxosList, setUpdatedUtxosList] = useState([]) | ||
|
||
const getBlockData = async (blockId) => { | ||
try { | ||
const blockData = await Mintlayer.getBlockDataByHash(blockId) | ||
const parsedData = JSON.parse(blockData) | ||
if (parsedData && parsedData.height) { | ||
return parsedData.height | ||
} | ||
} catch (error) { | ||
console.error('Failed to fetch block data:', error) | ||
} | ||
} | ||
|
||
const getUpdatedUtxosWithDate = useCallback( | ||
async (utxos) => { | ||
setLoading(true) | ||
const updatedUtxos = await Promise.all( | ||
utxos.map(async (utxo) => { | ||
if (utxo.utxo.lock.type === 'ForBlockCount') { | ||
const initialTransaction = transactions.find( | ||
(tx) => tx.txid === utxo.outpoint.source_id, | ||
) | ||
if (initialTransaction && !utxo.utxo.lock.content.unlockBlock) { | ||
// Calculating the unlock date | ||
const calculatedUnlockTimestamp = | ||
addDays( | ||
new Date(initialTransaction.date * 1000), | ||
10, | ||
).getTime() / 1000 | ||
|
||
const blockData = await getBlockData(initialTransaction.blockId) | ||
|
||
utxo.utxo.lock.content = { | ||
lockedFor: utxo.utxo.lock.content, | ||
timestamp: calculatedUnlockTimestamp, | ||
unlockBlock: blockData + utxo.utxo.lock.content, | ||
} | ||
} | ||
} | ||
return utxo | ||
}), | ||
) | ||
setLoading(false) | ||
return updatedUtxos | ||
}, | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
[lockedUtxos, transactions], | ||
) | ||
|
||
useEffect(() => { | ||
const fetchUpdatedUtxos = async () => { | ||
const updatedUtxos = await getUpdatedUtxosWithDate(lockedUtxos) | ||
const sortedUtxos = updatedUtxos.sort( | ||
(a, b) => a.utxo.lock.content.timestamp - b.utxo.lock.content.timestamp, | ||
) | ||
setUpdatedUtxosList(sortedUtxos) | ||
} | ||
|
||
fetchUpdatedUtxos() | ||
}, [lockedUtxos, transactions, getUpdatedUtxosWithDate]) | ||
|
||
return ( | ||
<> | ||
<div className="locked-table-wrapper"> | ||
{(fetchingUtxos || loading) ? ( | ||
<div className="locked-loading-wrapper"> | ||
<Loading /> | ||
</div> | ||
) : ( | ||
<table | ||
className="locked-table" | ||
data-testid="locked-table" | ||
> | ||
<thead> | ||
<tr> | ||
<th | ||
style={{ padding: '10px', textAlign: 'left' }} | ||
className="locked-title" | ||
> | ||
Date | ||
</th> | ||
<th | ||
style={{ padding: '10px', textAlign: 'left' }} | ||
className="locked-title" | ||
> | ||
Amount | ||
</th> | ||
</tr> | ||
</thead> | ||
<tbody> | ||
{lockedUtxos && | ||
updatedUtxosList.map((utxo, index) => ( | ||
<LockedBalanceListItem | ||
key={index} | ||
index={index} | ||
utxo={utxo} | ||
transactions={transactions} | ||
/> | ||
))} | ||
</tbody> | ||
</table> | ||
)} | ||
</div> | ||
</> | ||
) | ||
} | ||
|
||
export default LockedBalanceList |
Empty file.
34 changes: 34 additions & 0 deletions
34
src/components/composed/LockedBalanceList/LockedBalanceListItem.js
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,34 @@ | ||
import { useMemo } from 'react' | ||
import { format } from 'date-fns' | ||
|
||
import './LockedBalanceListItem.css' | ||
|
||
const LockedBalanceListItem = ({ index, utxo }) => { | ||
|
||
const displayDate = useMemo(() => { | ||
if (utxo.utxo.lock.type === 'ForBlockCount') { | ||
return `~ ${format( | ||
new Date(utxo.utxo.lock.content.timestamp * 1000), | ||
'dd/MM/yyyy HH:mm', | ||
)} (Block height: ${utxo.utxo.lock.content.unlockBlock})` | ||
} else if (utxo.utxo.lock.type !== 'ForBlockCount') { | ||
return format( | ||
new Date(utxo.utxo.lock.content.timestamp * 1000), | ||
'dd/MM/yyyy HH:mm', | ||
) | ||
} | ||
}, [utxo]) | ||
|
||
return ( | ||
<tr | ||
style={{ | ||
backgroundColor: index % 2 === 0 ? 'white' : 'rgb(247, 250, 250)', | ||
}} | ||
> | ||
<td style={{ padding: '10px' }}>{displayDate}</td> | ||
<td style={{ padding: '10px' }}>{utxo.utxo.value.amount.decimal} ML</td> | ||
</tr> | ||
) | ||
} | ||
|
||
export default LockedBalanceListItem |
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
Oops, something went wrong.