Skip to content

Commit

Permalink
Remove events from effects (MystenLabs#7822)
Browse files Browse the repository at this point in the history
This PR removes events from TransactionEffects, replacing it with a
`EventsSummary`, which contain the event count and an optional event
digest.

I am including the `TransactionEvents` in `TransactionResponse`,
`DryRunTransactionResponse` and `DevInspectResponse` to minimise
disruption to the apps.

In order to return `TransactionEvents` in execute_transaction,
`process_certificates` now returns the events as well as the effects.

I have also fixed errors in typescript SDK, explorer, wallet and
frenemies due to the scheme changes.
  • Loading branch information
patrickkuo authored Mar 1, 2023
1 parent f06df3c commit a6ffb80
Show file tree
Hide file tree
Showing 78 changed files with 960 additions and 602 deletions.
6 changes: 6 additions & 0 deletions .changeset/thirty-wolves-taste.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@mysten/wallet-adapter-unsafe-burner": minor
"@mysten/sui.js": minor
---

Removed events from transaction effects, TransactionEvents will now be provided in the TransactionResponse, along side TransactionEffects.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ function TransactionView({
);

const txKindData = formatByTransactionKind(txKindName, txnDetails, sender);
const txEventData = transaction.effects.events?.map(eventToDisplay);
const txEventData = transaction.events?.map(eventToDisplay);

let eventTitles: [string, string][] = [];
const txEventDisplay = txEventData?.map((ed, index) => {
Expand Down
21 changes: 10 additions & 11 deletions apps/explorer/src/utils/getAmount.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@ import type {
TransactionEffects,
SuiTransactionResponse,
SuiEvent,
TransactionEvents,
} from '@mysten/sui.js';

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

const events = txEffects?.events || [];
const coinType = events
?.map((event: SuiEvent) => {
const data = Object.values(event).find(
Expand All @@ -46,7 +46,8 @@ type FormattedBalance = {
// For TransferObject, TransferSui, Pay, PaySui, transactions get the amount from the transfer data
export function getTransfersAmount(
txnData: SuiTransactionKind,
txnEffect?: TransactionEffects
txnEffect?: TransactionEffects,
events?: TransactionEvents
): FormattedBalance[] | null {
const txKindName = getTransactionKindName(txnData);
if (txKindName === 'TransferObject') {
Expand All @@ -67,8 +68,7 @@ export function getTransfersAmount(
{
address: txn.recipient,
amount: txn?.amount,
coinType:
txnEffect && getCoinType(txnEffect, txn.recipient),
coinType: events && getCoinType(events, txn.recipient),
},
]
: null;
Expand All @@ -88,7 +88,7 @@ export function getTransfersAmount(
coinType:
txKindName === 'PaySui'
? SUI_TYPE_ARG
: getCoinType(txnEffect || null, recipient),
: getCoinType(events || null, recipient),
address: recipient,
},
}),
Expand All @@ -106,10 +106,9 @@ export function getTransfersAmount(
// Get transaction amount from coinBalanceChange event for Call Txn
// Aggregate coinBalanceChange by coinType and address
function getTxnAmountFromCoinBalanceEvent(
txEffects: TransactionEffects,
events: TransactionEvents,
address: string
): FormattedBalance[] {
const events = txEffects?.events || [];
const coinsMeta = {} as { [coinType: string]: FormattedBalance };

events.forEach((event) => {
Expand Down Expand Up @@ -145,11 +144,11 @@ export function getAmount({
txnData: SuiTransactionResponse;
suiCoinOnly?: boolean;
}) {
const { effects } = txnData;
const { effects, events } = txnData;
const txnDetails = getTransactions(txnData)[0];
const sender = getTransactionSender(txnData);
const suiTransfer = getTransfersAmount(txnDetails, effects);
const coinBalanceChange = getTxnAmountFromCoinBalanceEvent(effects, sender);
const coinBalanceChange = getTxnAmountFromCoinBalanceEvent(events, sender);
const transfers = suiTransfer || coinBalanceChange;
if (suiCoinOnly) {
return transfers?.filter(({ coinType }) => coinType === SUI_TYPE_ARG);
Expand Down
15 changes: 10 additions & 5 deletions apps/wallet/src/ui/app/components/receipt-card/StakeTxnCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,29 +11,34 @@ import { Text } from '_src/ui/app/shared/text';
import { IconTooltip } from '_src/ui/app/shared/tooltip';
import { useSystemState } from '_src/ui/app/staking/useSystemState';

import type { TransactionEffects, MoveEvent } from '@mysten/sui.js';
import type {
TransactionEffects,
MoveEvent,
TransactionEvents,
} from '@mysten/sui.js';

type StakeTxnCardProps = {
txnEffects: TransactionEffects;
events: TransactionEvents;
};

const REQUEST_DELEGATION_EVENT = '0x2::validator_set::DelegationRequestEvent';

// TODO: moveEvents is will be changing
// For Staked Transaction use moveEvent Field to get the validator address, delegation amount, epoch
export function StakeTxnCard({ txnEffects }: StakeTxnCardProps) {
export function StakeTxnCard({ txnEffects, events }: StakeTxnCardProps) {
const stakingData = useMemo(() => {
if (!txnEffects?.events) return null;
if (!events) return null;

const event = txnEffects.events.find(
const event = events.find(
(event) =>
'moveEvent' in event &&
event.moveEvent.type === REQUEST_DELEGATION_EVENT
);
if (!event) return null;
const { moveEvent } = event as { moveEvent: MoveEvent };
return moveEvent;
}, [txnEffects.events]);
}, [events]);

const { data: system } = useSystemState();

Expand Down
8 changes: 4 additions & 4 deletions apps/wallet/src/ui/app/components/receipt-card/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ type ReceiptCardProps = {
};

function ReceiptCard({ txn, activeAddress }: ReceiptCardProps) {
const { effects } = txn;
const { effects, events } = txn;
const timestamp = txn.timestamp_ms || txn.timestampMs;
const executionStatus = getExecutionStatusType(txn);
const error = useMemo(() => getExecutionStatusError(txn), [txn]);
Expand All @@ -58,8 +58,8 @@ function ReceiptCard({ txn, activeAddress }: ReceiptCardProps) {

return transferId
? transferId
: getTxnEffectsEventID(effects, activeAddress)[0];
}, [activeAddress, transaction, effects]);
: getTxnEffectsEventID(effects, events, activeAddress)[0];
}, [transaction, effects, events, activeAddress]);

const gasTotal = getTotalGasUsed(txn);

Expand Down Expand Up @@ -112,7 +112,7 @@ function ReceiptCard({ txn, activeAddress }: ReceiptCardProps) {

{isStakeTxn ? (
moveCallLabel === 'Staked' ? (
<StakeTxnCard txnEffects={effects} />
<StakeTxnCard txnEffects={effects} events={events} />
) : (
<UnStakeTxnCard
txn={txn}
Expand Down
28 changes: 14 additions & 14 deletions apps/wallet/src/ui/app/components/transactions-card/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@
// SPDX-License-Identifier: Apache-2.0

import {
getExecutionStatusError,
getExecutionStatusType,
getTransactionKindName,
getMoveCallTransaction,
getExecutionStatusError,
getTransferObjectTransaction,
SUI_TYPE_ARG,
getTransactionDigest,
getTransactionKindName,
getTransactions,
getTransactionSender,
getTransactionDigest,
getTransferObjectTransaction,
SUI_TYPE_ARG,
} from '@mysten/sui.js';
import { useMemo } from 'react';
import { Link } from 'react-router-dom';
Expand All @@ -21,30 +21,30 @@ import { TxnImage } from './TxnImage';
import { CoinBalance } from '_app/shared/coin-balance';
import { DateCard } from '_app/shared/date-card';
import { Text } from '_app/shared/text';
import { notEmpty, checkStakingTxn } from '_helpers';
import { useGetTxnRecipientAddress, useGetTransferAmount } from '_hooks';
import { checkStakingTxn, notEmpty } from '_helpers';
import { useGetTransferAmount, useGetTxnRecipientAddress } from '_hooks';

import type {
SuiTransactionResponse,
SuiAddress,
TransactionEffects,
SuiEvent,
SuiTransactionResponse,
TransactionEffects,
TransactionEvents,
} from '@mysten/sui.js';

export const getTxnEffectsEventID = (
txEffects: TransactionEffects,
events: TransactionEvents,
address: string
): string[] => {
const events = txEffects?.events || [];
const objectIDs = events
return events
?.map((event: SuiEvent) => {
const data = Object.values(event).find(
(itm) => itm?.recipient?.AddressOwner === address
);
return data?.objectId;
})
.filter(notEmpty);
return objectIDs;
};

export function TransactionCard({
Expand All @@ -63,8 +63,8 @@ export function TransactionCard({
getTransferObjectTransaction(transaction)?.objectRef?.objectId;
return transferId
? transferId
: getTxnEffectsEventID(txn.effects, address)[0];
}, [address, transaction, txn.effects]);
: getTxnEffectsEventID(txn.effects, txn.events, address)[0];
}, [address, transaction, txn.effects, txn.events]);

const transfer = useGetTransferAmount({
txn,
Expand Down
11 changes: 7 additions & 4 deletions apps/wallet/src/ui/app/helpers/getAmount.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,15 @@ import {
import type {
SuiTransactionKind,
TransactionEffects,
TransactionEvents,
SuiEvent,
} from '@mysten/sui.js';

const getCoinType = (
txEffects: TransactionEffects,
events: TransactionEvents,
address: string
): string | null => {
const events = txEffects?.events || [];
const coinType = events
?.map((event: SuiEvent) => {
const data = Object.values(event).find(
Expand All @@ -40,7 +41,8 @@ type FormattedBalance = {

export function getAmount(
txnData: SuiTransactionKind,
txnEffect: TransactionEffects
txnEffect: TransactionEffects,
events: TransactionEvents
): FormattedBalance | null {
const txKindName = getTransactionKindName(txnData);
if (txKindName === 'TransferObject') {
Expand All @@ -62,7 +64,8 @@ export function getAmount(
recipientAddress: txn.recipient,
amount: txn?.amount,
coinType:
txnEffect && getCoinType(txnEffect, txn.recipient),
txnEffect &&
getCoinType(txnEffect, events, txn.recipient),
},
]
: null;
Expand All @@ -76,7 +79,7 @@ export function getAmount(
const coinType =
txKindName === 'PaySui'
? SUI_TYPE_ARG
: getCoinType(txnEffect, recipient);
: getCoinType(txnEffect, events, recipient);
return {
...acc,
[recipient]: {
Expand Down
5 changes: 2 additions & 3 deletions apps/wallet/src/ui/app/helpers/getEventsSummary.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) Mysten Labs, Inc.
// SPDX-License-Identifier: Apache-2.0

import type { TransactionEffects } from '@mysten/sui.js';
import type { TransactionEvents } from '@mysten/sui.js';

export type CoinsMetaProps = {
amount: number;
Expand All @@ -15,10 +15,9 @@ export type TxnMetaResponse = {
};

export function getEventsSummary(
txEffects: TransactionEffects,
events: TransactionEvents,
address: string
): TxnMetaResponse {
const events = txEffects?.events || [];
const coinsMeta = {} as { [coinType: string]: CoinsMetaProps };
const objectIDs: string[] = [];

Expand Down
8 changes: 4 additions & 4 deletions apps/wallet/src/ui/app/hooks/useGetTransferAmount.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ export function useGetTransferAmount({
txn: SuiTransactionResponse;
activeAddress: SuiAddress;
}) {
const { effects } = txn;
const { coins } = getEventsSummary(effects, activeAddress);
const { effects, events } = txn;
const { coins } = getEventsSummary(events, activeAddress);

const suiTransfer = useMemo(() => {
const txdetails = getTransactions(txn)[0];
return getAmount(txdetails, effects)?.map(
return getAmount(txdetails, effects, events)?.map(
({ amount, coinType, recipientAddress }) => {
return {
amount: amount || 0,
Expand All @@ -29,7 +29,7 @@ export function useGetTransferAmount({
};
}
);
}, [txn, effects]);
}, [txn, effects, events]);

const transferAmount = useMemo(() => {
return suiTransfer?.length
Expand Down
8 changes: 4 additions & 4 deletions apps/wallet/src/ui/app/hooks/useGetTxnRecipientAddress.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@ type Props = {
};

export function useGetTxnRecipientAddress({ txn, address }: Props) {
const { effects } = txn;
const { events } = txn;

const eventsSummary = useMemo(() => {
const { coins } = getEventsSummary(effects, address);
const { coins } = getEventsSummary(events, address);
return coins;
}, [effects, address]);
}, [events, address]);

const [transaction] = getTransactions(txn);
const amountByRecipient = getAmount(transaction, txn.effects);
const amountByRecipient = getAmount(transaction, txn.effects, events);

const recipientAddress = useMemo(() => {
const transferObjectRecipientAddress =
Expand Down
5 changes: 3 additions & 2 deletions apps/wallet/src/ui/app/hooks/useTransactionSummary.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,11 @@ export function useTransactionSummary({
const { data } = useTransactionDryRun(txData, addressForTransaction);

const eventsSummary = useMemo(
() => (data ? getEventsSummary(data, addressForTransaction) : null),
() =>
data ? getEventsSummary(data.events, addressForTransaction) : null,
[data, addressForTransaction]
);
const txGasEstimation = data && getTotalGasUsed(data);
const txGasEstimation = data && getTotalGasUsed(data.effects);

return [eventsSummary, txGasEstimation || null];
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export function CoinActivitiesCard({ coinType }: { coinType: string }) {
const txnByCoinType = useMemo(() => {
if (!txns || !activeAddress) return null;
return txns?.filter((txn) => {
const { coins } = getEventsSummary(txn.effects, activeAddress);
const { coins } = getEventsSummary(txn.events, activeAddress);
// find txn with coinType from eventsSummary
return !!coins.find(
({ coinType: summaryCoinType }) => summaryCoinType === coinType
Expand Down
6 changes: 4 additions & 2 deletions apps/wallet/src/ui/app/redux/slices/sui-objects/Coin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import {
Coin as CoinAPI,
getEvents,
getTransactionEffects,
SUI_SYSTEM_STATE_OBJECT_ID,
} from '@mysten/sui.js';
Expand Down Expand Up @@ -187,12 +188,13 @@ export class Coin {
});

const effects = getTransactionEffects(result);
const events = getEvents(result);

if (!effects || !effects.events) {
if (!effects || !events) {
throw new Error('Missing effects or events');
}

const changeEvent = effects.events.find((event) => {
const changeEvent = events.find((event) => {
if ('coinBalanceChange' in event) {
return event.coinBalanceChange.amount === Number(amount);
}
Expand Down
Loading

0 comments on commit a6ffb80

Please sign in to comment.