Skip to content

Commit 491a744

Browse files
authored
Merge pull request #17 from alephium/show-multisig-token
Show tokens in multisig transaction
2 parents 83e542d + a9a1523 commit 491a744

File tree

1 file changed

+42
-17
lines changed

1 file changed

+42
-17
lines changed

src/components/Multisig/SignMultisigTx.tsx

Lines changed: 42 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,21 @@ import {
1616
isHexString,
1717
node,
1818
prettifyAttoAlphAmount,
19+
prettifyTokenAmount,
1920
} from '@alephium/web3'
2021
import CopyTextarea from '../Misc/CopyTextarea'
21-
import { useAlephium } from '../../utils/utils'
22+
import { useAlephium, useTokenList } from '../../utils/utils'
2223
import MyTable from '../Misc/MyTable'
24+
import { TokenInfo } from '@alephium/token-list'
2325

2426
type P2MPKUnlockScript = { pubkey: string; index: number }[]
27+
type TxInfo = { recipient: string; tokens: {symbol: string, amount: string}[]; fee: string; txId: string }
2528

2629
function SignMultisigTx() {
2730
const [signature, setSignature] = useState<
2831
{ signer: string; signature: string } | undefined
2932
>()
33+
const tokens = useTokenList()
3034
const [unsignedTx, setUnsignedTx] = useState<string | undefined>()
3135
const [loadingTxInfo, setLoadingTxInfo] = useState<boolean>(false)
3236
const [multisigConfig, setMultisigConfig] = useState<
@@ -35,9 +39,7 @@ function SignMultisigTx() {
3539
const [unlockScript, setUnlockScript] = useState<
3640
P2MPKUnlockScript | undefined
3741
>()
38-
const [txInfo, setTxInfo] = useState<
39-
{ recipient: string; amount: string; fee: string; txId: string } | undefined
40-
>()
42+
const [txInfo, setTxInfo] = useState<TxInfo>()
4143
const { account, signer, connectionStatus } = useWallet()
4244

4345
const [loadingTxError, setLoadingTxError] = useState<string>()
@@ -56,21 +58,10 @@ function SignMultisigTx() {
5658
const unlockScript = decodeUnlockScript(
5759
decodedTx.unsignedTx.inputs[0].unlockScript
5860
)
59-
const recipientOutput = decodedTx.unsignedTx.fixedOutputs[0]
6061
const multisigConfig = getMultisigByUnlockScript(unlockScript)
6162
setUnlockScript(unlockScript)
6263
setMultisigConfig(multisigConfig)
63-
setTxInfo({
64-
recipient: recipientOutput.address,
65-
amount: prettifyAttoAlphAmount(
66-
BigInt(recipientOutput.attoAlphAmount)
67-
)!,
68-
fee: prettifyAttoAlphAmount(
69-
BigInt(decodedTx.unsignedTx.gasPrice) *
70-
BigInt(decodedTx.unsignedTx.gasAmount)
71-
)!,
72-
txId: decodedTx.unsignedTx.txId,
73-
})
64+
setTxInfo(getDecodedTxInfo(decodedTx, tokens))
7465
setLoadingTxInfo(false)
7566
setLoadingTxError(undefined)
7667
} catch (error) {
@@ -184,7 +175,11 @@ function SignMultisigTx() {
184175
<Mark color="red">unknown</Mark>
185176
),
186177
Recipient: <CopyTextarea value={txInfo?.recipient ?? ''} />,
187-
'ALPH Amount': txInfo?.amount + ' ALPH',
178+
'Token Amount': txInfo?.tokens.map(({ symbol, amount }) => (
179+
<Box key={symbol}>
180+
{amount} {symbol}
181+
</Box>
182+
)),
188183
'Transaction Fee': txInfo?.fee + ' ALPH',
189184
'Transaction Hash': <CopyTextarea value={txInfo?.txId ?? ''} />,
190185
}}
@@ -285,4 +280,34 @@ async function getDecodedUnsignedTx(
285280
return decodedTx
286281
}
287282

283+
function getDecodedTxInfo(
284+
decodedTx: node.DecodeUnsignedTxResult,
285+
tokens: TokenInfo[]
286+
): TxInfo {
287+
const recipientOutput = decodedTx.unsignedTx.fixedOutputs[0]
288+
return {
289+
recipient: recipientOutput.address,
290+
tokens: [
291+
{
292+
symbol: 'ALPH',
293+
amount: prettifyAttoAlphAmount(BigInt(recipientOutput.attoAlphAmount))!,
294+
},
295+
...recipientOutput.tokens.map((token) => {
296+
const tokenInfo = tokens.find((t) => t.id === token.id)
297+
return {
298+
symbol: tokenInfo?.symbol ?? token.id,
299+
amount:
300+
prettifyTokenAmount(token.amount, tokenInfo?.decimals ?? 0) ??
301+
'???',
302+
}
303+
}),
304+
],
305+
fee: prettifyAttoAlphAmount(
306+
BigInt(decodedTx.unsignedTx.gasPrice) *
307+
BigInt(decodedTx.unsignedTx.gasAmount)
308+
)!,
309+
txId: decodedTx.unsignedTx.txId,
310+
}
311+
}
312+
288313
export default SignMultisigTx

0 commit comments

Comments
 (0)