Skip to content

Commit

Permalink
create activity from evm transaction
Browse files Browse the repository at this point in the history
  • Loading branch information
MarkNerdi committed Aug 17, 2023
1 parent a60771c commit 2d5987b
Show file tree
Hide file tree
Showing 10 changed files with 71 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
$: claimedTime = getFormattedTimeStamp(activity?.asyncData?.claimedDate)
$: hasStorageDeposit =
activity?.storageDeposit || (activity?.storageDeposit === 0 && activity?.giftedStorageDeposit === 0)
$: gasBudget = activity?.parsedLayer2Metadata?.gasBudget
$: gasBudget = activity?.gas ?? activity?.parsedLayer2Metadata?.gasBudget
$: formattedTransactionTime = getFormattedTimeStamp(activity?.time)
$: formattedTimelockDate = getFormattedTimeStamp(activity?.asyncData?.timelockDate)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ import { preprocessTransactionsForAccount } from './preprocessTransactionsForAcc
import { preprocessOutputsForAccount } from './preprocessOutputsForAccount'
import { linkTransactionsWithClaimingTransactions } from './linkTransactionsWithClaimingTransactions'
import { hideActivitiesForFoundries } from './hideActivitiesForFoundries'
import { generateActivitiesFromProcessedTransactions } from './generateActivitiesFromProcessedTransactions'
import { generateActivitiesFromProcessedTransactions } from '../utils/generateActivitiesFromProcessedTransactions'
import { loadAssetsForAllActivities } from './loadAssetsForAllAccounts'
import { generateActivitiesFromBalanceChanges, setOutgoingAsyncActivitiesToClaimed } from '@core/activity/actions'
import { setOutgoingAsyncActivitiesToClaimed } from '@core/activity/actions'
import { generateActivitiesFromBalanceChanges, generateActivitiesFromChains } from '../utils'

export async function generateAndStoreActivitiesForAccount(account: IAccountState): Promise<void> {
// Step 1: process account transactions and outputs into processed transactions
Expand All @@ -23,9 +24,11 @@ export async function generateAndStoreActivitiesForAccount(account: IAccountStat
// Step 3: generate activities from processed transactions
const activities = generateActivitiesFromProcessedTransactions(linkedProcessedTransactions, account)
const balanceChangeActivities = generateActivitiesFromBalanceChanges(account)

activities.push(...balanceChangeActivities)

const chainActivities = await generateActivitiesFromChains(account)
activities.push(...chainActivities)

// Step 4: set account activities with generated activities
setAccountActivitiesInAllAccountActivities(account.index, activities)

Expand Down
1 change: 0 additions & 1 deletion packages/shared/src/lib/core/activity/actions/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
export * from './calculateAndAddPersistedBalanceChange'
export * from './generateActivitiesFromBalanceChanges'
export * from './generateAndStoreActivitiesForAccount'
export * from './generateAndStoreActivitiesForAllAccounts'
export * from './hideActivitiesForFoundries'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ interface IPersistedEvmTransactions {
export const persistedEvmTransactions = persistent<IPersistedEvmTransactions>('evmTransactions', {})

export function getPersistedEvmTransactions(accountIndex: number, chainId: string | number): PersistedEvmTransaction[] {
return get(persistedEvmTransactions)?.[get(activeProfileId)]?.[accountIndex]?.[chainId]
return get(persistedEvmTransactions)?.[get(activeProfileId)]?.[accountIndex]?.[chainId] ?? []
}

export function addPersistedTransaction(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export type BaseActivity = {
action: ActivityAction
isInternal: boolean
storageDeposit: number
gas?: number
rawBaseCoinAmount?: number
subject: Subject | undefined
metadata?: string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Activity } from '../types'
import { getBalanceChanges } from '../stores'
import { get } from 'svelte/store'
import { network } from '@core/network'
import { generateBalanceChangeActivity } from '../utils'
import { generateBalanceChangeActivity } from './generateBalanceChangeActivity'

export function generateActivitiesFromBalanceChanges(account: IAccountState): Activity[] {
const activities: Activity[] = []
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { IAccountState } from '@core/account'
import { Activity } from '../types'
import { getPersistedEvmTransactions } from '../stores'
import { generateActivityFromEvmTransaction } from './generateActivityFromEvmTransaction'
import { get } from 'svelte/store'
import { network } from '@core/network'

export async function generateActivitiesFromChains(account: IAccountState): Promise<Activity[]> {
const activities: Activity[] = []

const chains = get(network)?.getChains() ?? []
for (const chain of chains) {
const chainId = chain.getConfiguration().chainId

const transactions = getPersistedEvmTransactions(account.index, chainId)
for (const transaction of transactions) {
const activity = await generateActivityFromEvmTransaction(transaction, chainId, chain.getProvider())
activities.push(activity)
}
}

return activities
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { IAccountState } from '@core/account'
import { Activity, IProcessedTransaction } from '../types'
import { generateActivities } from '../utils'
import { generateActivities } from '.'

export function generateActivitiesFromProcessedTransactions(
processedTransactions: IProcessedTransaction[],
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { PersistedEvmTransaction, TransactionActivity } from '../types'
import { ActivityAction, ActivityDirection, ActivityType, InclusionState } from '../enums'
import { getCoinType } from '@core/profile/actions'
import { SubjectType } from '@core/wallet'
import { WEI_PER_GLOW } from '@core/layer-2'
import Web3 from 'web3'

export async function generateActivityFromEvmTransaction(
transaction: PersistedEvmTransaction,
chainId: number,
provider: Web3
): Promise<TransactionActivity> {
const direction = ActivityDirection.Outgoing // Currently only sent transactions are supported

const timestamp = (await provider.eth.getBlock(transaction.blockNumber)).timestamp
return {
type: ActivityType.Basic,
id: transaction.transactionHash,
time: new Date(Number(timestamp) * 1000),
inclusionState: InclusionState.Confirmed,
containsValue: true,
isAssetHidden: false,
direction,
action: ActivityAction.Send,
isInternal: false,
storageDeposit: 0,
gas: transaction.gasUsed,
subject: { type: SubjectType.Address, address: transaction.to },
rawBaseCoinAmount: Number(transaction.value) / Number(WEI_PER_GLOW),
rawAmount: Number(transaction.value) / Number(WEI_PER_GLOW),
tokenId: getCoinType(),
chainId,
}
}
3 changes: 3 additions & 0 deletions packages/shared/src/lib/core/activity/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
export * from './activityOutputContainsValue'
export * from './generateActivities'
export * from './generateActivitiesFromAliasOutputs'
export * from './generateActivitiesFromBalanceChanges'
export * from './generateActivitiesFromBasicOutputs'
export * from './generateActivitiesFromChains'
export * from './generateActivitiesFromFoundryOutputs'
export * from './generateActivitiesFromNftOutputs'
export * from './generateActivitiesFromProcessedTransactions'
export * from './generateBalanceChangeActivity'
export * from './generateSingleAliasActivity'
export * from './generateSingleBasicActivity'
Expand Down

0 comments on commit 2d5987b

Please sign in to comment.