Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set default coinType for PaySui txn sui #8283

Merged
merged 8 commits into from
Feb 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 16 additions & 17 deletions apps/explorer/src/utils/getAmount.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,11 @@ import type {
} from '@mysten/sui.js';

const getCoinType = (
txEffects: TransactionEffects,
txEffects: TransactionEffects | null,
address: string
): string | null => {
if (!txEffects) return null;

const events = txEffects?.events || [];
const coinType = events
?.map((event: SuiEvent) => {
Expand Down Expand Up @@ -72,25 +74,22 @@ export function getTransfersAmount(
: null;
}

const paySuiData =
getPaySuiTransaction(txnData) ?? getPayTransaction(txnData);
const payData = getPaySuiTransaction(txnData) ?? getPayTransaction(txnData);

const amountByRecipient = paySuiData?.recipients.reduce(
(acc, value, index) => ({
const amountByRecipient = payData?.recipients.reduce(
(acc, recipient, index) => ({
...acc,
[value]: {
[recipient]: {
amount:
paySuiData.amounts[index] +
(value in acc ? acc[value].amount : 0),
coinType: txnEffect
? getCoinType(
txnEffect,
paySuiData.recipients[index] ||
paySuiData.recipients[0]
)
: null,
address:
paySuiData.recipients[index] || paySuiData.recipients[0],
payData.amounts[index] +
(recipient in acc ? acc[recipient].amount : 0),

// for PaySuiTransaction the coinType is SUI
coinType:
txKindName === 'PaySui'
? SUI_TYPE_ARG
: getCoinType(txnEffect || null, recipient),
address: recipient,
},
}),
{} as {
Expand Down
54 changes: 28 additions & 26 deletions apps/wallet/src/ui/app/components/transactions-card/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,12 @@ export function TransactionCard({
const transferAmount = useMemo(() => {
// Find SUI transfer amount
const amountTransfersSui = transfer.find(
({ receiverAddress, coinType }) =>
receiverAddress === address && coinType === SUI_TYPE_ARG
({ coinType }) => coinType === SUI_TYPE_ARG
);

// Find non-SUI transfer amount
const amountTransfersNonSui = transfer.find(
({ receiverAddress, coinType }) =>
receiverAddress === address && coinType !== SUI_TYPE_ARG
({ coinType }) => coinType !== SUI_TYPE_ARG
);

return {
Expand All @@ -93,7 +91,7 @@ export function TransactionCard({
amountTransfersNonSui?.coinType ||
null,
};
}, [address, transfer]);
}, [transfer]);

const recipientAddress = useGetTxnRecipientAddress({ txn, address });

Expand Down Expand Up @@ -144,6 +142,14 @@ export function TransactionCard({
moveCallLabel === 'Staked' ||
moveCallLabel === 'Unstaked';

const transferAmountComponent = transferAmount.coinType &&
transferAmount.amount && (
<CoinBalance
amount={Math.abs(transferAmount.amount)}
coinType={transferAmount.coinType}
/>
);

return (
<Link
to={`/receipt?${new URLSearchParams({
Expand All @@ -160,20 +166,23 @@ export function TransactionCard({
</div>
<div className="flex flex-col w-full gap-1.5">
{error ? (
<div className="flex flex-col w-full gap-1.5">
<Text color="gray-90" weight="medium">
Transaction Failed
</Text>

<div className="flex break-all">
<Text
variant="subtitle"
weight="medium"
color="issue-dark"
>
{error}
<div className="flex w-full justify-between">
<div className="flex flex-col w-full gap-1.5">
<Text color="gray-90" weight="medium">
Transaction Failed
</Text>

<div className="flex break-all">
<Text
variant="subtitle"
weight="medium"
color="issue-dark"
>
{error}
</Text>
</div>
</div>
{transferAmountComponent}
</div>
) : (
<div className="flex w-full justify-between flex-col ">
Expand All @@ -192,15 +201,8 @@ export function TransactionCard({
</Text>
)}
</div>
{transferAmount.coinType &&
transferAmount.amount && (
<CoinBalance
amount={Math.abs(
transferAmount.amount
)}
coinType={transferAmount.coinType}
/>
)}

{transferAmountComponent}
</div>
<div className="flex flex-col w-full gap-1.5">
<TxnTypeLabel
Expand Down
23 changes: 10 additions & 13 deletions apps/wallet/src/ui/app/helpers/getAmount.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
getTransferSuiTransaction,
getTransferObjectTransaction,
getTransactionKindName,
SUI_TYPE_ARG,
} from '@mysten/sui.js';

import type {
Expand Down Expand Up @@ -71,23 +72,19 @@ export function getAmount(
getPaySuiTransaction(txnData) ?? getPayTransaction(txnData);

const amountByRecipient = paySuiData?.recipients.reduce(
(acc, value, index) => {
(acc, recipient, index) => {
const coinType =
txKindName === 'PaySui'
? SUI_TYPE_ARG
: getCoinType(txnEffect, recipient);
return {
...acc,
[value]: {
[recipient]: {
amount:
paySuiData.amounts[index] +
(value in acc ? acc[value].amount : 0),
coinType: txnEffect
? getCoinType(
txnEffect,
paySuiData.recipients[index] ||
paySuiData.recipients[0]
)
: null,
recipientAddress:
paySuiData.recipients[index] ||
paySuiData.recipients[0],
(recipient in acc ? acc[recipient].amount : 0),
coinType,
recipientAddress: recipient,
},
};
},
Expand Down