-
Notifications
You must be signed in to change notification settings - Fork 11.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
update activities to include events coinBalance (#6953)
<img width="395" alt="Screenshot 2022-12-20 at 5 15 04 PM" src="https://user-images.githubusercontent.com/8676844/208778061-b4366d61-353c-4d8c-8723-15ec2d6316e3.png">
- Loading branch information
Showing
7 changed files
with
256 additions
and
165 deletions.
There are no files selected for viewing
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,104 @@ | ||
// Copyright (c) Mysten Labs, Inc. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
import { | ||
getPaySuiTransaction, | ||
getPayTransaction, | ||
getTransferSuiTransaction, | ||
getTransferObjectTransaction, | ||
getTransactionKindName, | ||
} from '@mysten/sui.js'; | ||
|
||
import type { | ||
SuiTransactionKind, | ||
TransactionEffects, | ||
SuiEvent, | ||
} from '@mysten/sui.js'; | ||
|
||
const getCoinType = ( | ||
txEffects: TransactionEffects, | ||
address: string | ||
): string | null => { | ||
const events = txEffects?.events || []; | ||
const coinType = events | ||
?.map((event: SuiEvent) => { | ||
const data = Object.values(event).find( | ||
(itm) => itm?.owner?.AddressOwner === address | ||
); | ||
return data?.coinType; | ||
}) | ||
.filter(Boolean); | ||
return coinType?.[0] ? coinType[0] : null; | ||
}; | ||
|
||
type FormattedBalance = { | ||
amount?: number | null; | ||
coinType?: string | null; | ||
recipientAddress: string; | ||
}[]; | ||
|
||
export function getAmount( | ||
txnData: SuiTransactionKind, | ||
txnEffect: TransactionEffects | ||
): FormattedBalance | null { | ||
const txKindName = getTransactionKindName(txnData); | ||
if (txKindName === 'TransferObject') { | ||
const txn = getTransferObjectTransaction(txnData); | ||
return txn?.recipient | ||
? [ | ||
{ | ||
recipientAddress: txn?.recipient, | ||
}, | ||
] | ||
: null; | ||
} | ||
|
||
if (txKindName === 'TransferSui') { | ||
const txn = getTransferSuiTransaction(txnData); | ||
return txn?.recipient | ||
? [ | ||
{ | ||
recipientAddress: txn.recipient, | ||
amount: txn?.amount, | ||
coinType: | ||
txnEffect && getCoinType(txnEffect, txn.recipient), | ||
}, | ||
] | ||
: null; | ||
} | ||
|
||
const paySuiData = | ||
getPaySuiTransaction(txnData) ?? getPayTransaction(txnData); | ||
|
||
const amountByRecipient = paySuiData?.recipients.reduce( | ||
(acc, value, index) => { | ||
return { | ||
...acc, | ||
[value]: { | ||
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], | ||
}, | ||
}; | ||
}, | ||
{} as { | ||
[key: string]: { | ||
amount: number; | ||
coinType: string | null; | ||
recipientAddress: string; | ||
}; | ||
} | ||
); | ||
|
||
return amountByRecipient ? Object.values(amountByRecipient) : null; | ||
} |
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,63 @@ | ||
// Copyright (c) Mysten Labs, Inc. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
import type { TransactionEffects } from '@mysten/sui.js'; | ||
|
||
export type CoinsMetaProps = { | ||
amount: number; | ||
coinType: string; | ||
receiverAddress: string; | ||
}; | ||
|
||
export type TxnMetaResponse = { | ||
objectIDs: string[]; | ||
coins: CoinsMetaProps[]; | ||
}; | ||
|
||
export function getEventsSummary( | ||
txEffects: TransactionEffects, | ||
address: string | ||
): TxnMetaResponse { | ||
const events = txEffects?.events || []; | ||
const coinsMeta = {} as { [coinType: string]: CoinsMetaProps }; | ||
const objectIDs: string[] = []; | ||
|
||
events.forEach((event) => { | ||
// Aggregate coinBalanceChange by coinType and address | ||
// A net positive amount means the user received coins | ||
// A net negative amount means the user sent coins | ||
if ( | ||
'coinBalanceChange' in event && | ||
event?.coinBalanceChange?.changeType && | ||
['Receive', 'Pay'].includes(event?.coinBalanceChange?.changeType) | ||
) { | ||
const { coinBalanceChange } = event; | ||
const { coinType, amount, owner } = coinBalanceChange; | ||
const { AddressOwner } = owner as { AddressOwner: string }; | ||
|
||
coinsMeta[`${AddressOwner}${coinType}`] = { | ||
amount: | ||
(coinsMeta[`${AddressOwner}${coinType}`]?.amount || 0) + | ||
amount, | ||
coinType: coinType, | ||
receiverAddress: AddressOwner, | ||
}; | ||
} | ||
|
||
// return objectIDs of the transfer objects | ||
if ('transferObject' in event) { | ||
const { transferObject } = event; | ||
const { AddressOwner } = transferObject.recipient as { | ||
AddressOwner: string; | ||
}; | ||
if (AddressOwner === address) { | ||
objectIDs.push(transferObject?.objectId); | ||
} | ||
} | ||
}); | ||
|
||
return { | ||
objectIDs, | ||
coins: Object.values(coinsMeta), | ||
}; | ||
} |
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
Oops, something went wrong.
293d455
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
explorer-storybook – ./
explorer-storybook-git-main-mysten-labs.vercel.app
explorer-storybook-mysten-labs.vercel.app
explorer-storybook.vercel.app
293d455
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
explorer – ./apps/explorer
explorer.sui.io
explorer-topaz.vercel.app
explorer-git-main-mysten-labs.vercel.app
explorer-mysten-labs.vercel.app