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

feat: generate activities from persisted transactions #2015

Merged
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export enum BlockscoutTransactionStatus {
Ok = 'ok',
Error = 'error',
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './blockscout-transaction-status.enum'
2 changes: 2 additions & 0 deletions packages/shared/src/lib/auxiliary/blockscout/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './enums'
export * from './interfaces'
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { BlockscoutTransactionStatus } from '../enums'
import { IBlockscoutAssetMetadata } from './blockscout-asset-metadata.interface'

interface IFee {
Expand Down Expand Up @@ -51,11 +52,6 @@ enum BlockscoutTransactionType {
CoinTransfer = 'coin_transfer',
}

enum BlockscoutTransactionStatus {
Ok = 'ok',
Error = 'error',
}

export interface IBlockscoutTransaction {
timestamp: string
fee: IFee
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,19 @@ import { network } from '@core/network'
import { getPersistedTransactionsForChain } from '@core/transactions/stores'
import { get } from 'svelte/store'
import { Activity } from '../types'
import { generateActivityFromEvmTransaction } from './generateActivityFromEvmTransaction'
import { generateActivityFromPersistedTransaction } from './generateActivityFromPeristedTransaction'

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

const chains = get(network)?.getChains() ?? []
for (const chain of chains) {
const persistedTransactions = getPersistedTransactionsForChain(profileId, account.index, chain)
for (const transaction of persistedTransactions) {
for (const persistedTransaction of persistedTransactions) {
try {
if (transaction.local) {
// TODO: build activities from persisted transactions
const activity = await generateActivityFromEvmTransaction(transaction.local, chain, account)
if (activity) {
activities.push(activity)
}
const activity = await generateActivityFromPersistedTransaction(persistedTransaction, chain, account)
if (activity) {
activities.push(activity)
}
} catch (error) {
console.error(error)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { IAccountState } from '@core/account/interfaces'
import { getTransferInfoFromTransactionData } from '@core/layer-2/utils/getTransferInfoFromTransactionData'
import { IChain } from '@core/network'
import { PersistedTransaction, buildPersistedEvmTransactionFromBlockscoutTransaction } from '@core/transactions'
import { ActivityType } from '../enums'
import { Activity, PersistedEvmTransaction } from '../types'
import { generateNftActivity } from './evm/generateNftActivity'
import { generateSmartContractActivity } from './evm/generateSmartContractActivity'
import { generateTokenActivity } from './evm/generateTokenActivity'

export async function generateActivityFromPersistedTransaction(
persistedTransaction: PersistedTransaction,
chain: IChain,
account: IAccountState
): Promise<Activity | undefined> {
const transaction = persistedTransaction.blockscout
? buildPersistedEvmTransactionFromBlockscoutTransaction(persistedTransaction.blockscout)
nicole-obrien marked this conversation as resolved.
Show resolved Hide resolved
: (persistedTransaction.local as PersistedEvmTransaction)
const transferInfo = getTransferInfoFromTransactionData(transaction, chain)
if (transferInfo?.type === ActivityType.SmartContract) {
return generateSmartContractActivity(transaction, chain, account)
} else if (transferInfo?.type === ActivityType.Basic) {
const { tokenId, rawAmount, additionalBaseTokenAmount, recipientAddress } = transferInfo
return generateTokenActivity(
transaction,
chain,
tokenId,
rawAmount,
additionalBaseTokenAmount,
recipientAddress,
account
)
} else if (transferInfo?.type === ActivityType.Nft) {
const { nftId, additionalBaseTokenAmount, recipientAddress } = transferInfo
return generateNftActivity(transaction, chain, nftId, additionalBaseTokenAmount, recipientAddress, account)
}
}
2 changes: 2 additions & 0 deletions packages/shared/src/lib/core/transactions/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './types'
export * from './utils'
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,7 @@ import { IChain } from '@core/network/interfaces'
import { activeProfileId } from '@core/profile/stores'
import { persistent } from '@core/utils/store'
import { get } from 'svelte/store'

type PersistedTransaction =
| {
blockscout: IBlockscoutTransaction
local: PersistedEvmTransaction
}
| {
blockscout?: IBlockscoutTransaction
local: PersistedEvmTransaction
}
| {
blockscout: IBlockscoutTransaction
local?: PersistedEvmTransaction
}
import { PersistedTransaction } from '../types/'

type PersistedTransactions = {
[profileId: string]: {
Expand All @@ -30,7 +17,7 @@ type PersistedTransactions = {
}
}

export const persistedTransactions = persistent<PersistedTransactions>('transactions', {})
export const persistedTransactions = persistent<PersistedTransactions>('', {})

export function getPersistedTransactionsForChain(
profileId: string,
Expand Down
1 change: 1 addition & 0 deletions packages/shared/src/lib/core/transactions/types/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './persisted-transaction.type'
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { IBlockscoutTransaction } from '@auxiliary/blockscout/interfaces'
import { PersistedEvmTransaction } from '@core/activity'

export type PersistedTransaction =
| {
blockscout: IBlockscoutTransaction
local: PersistedEvmTransaction
}
| {
blockscout?: IBlockscoutTransaction
local: PersistedEvmTransaction
}
| {
blockscout: IBlockscoutTransaction
local?: PersistedEvmTransaction
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { IBlockscoutTransaction, BlockscoutTransactionStatus } from '@auxiliary/blockscout'
import { PersistedEvmTransaction } from '@core/activity'
import { LegacyTxData, TransactionType } from '@ethereumjs/tx'
import { TransactionReceipt } from 'web3-core'

export function buildPersistedEvmTransactionFromBlockscoutTransaction(
blockscoutTransaction: IBlockscoutTransaction
): PersistedEvmTransaction {
const transactionReceipt: TransactionReceipt = {
status: blockscoutTransaction.status === BlockscoutTransactionStatus.Ok ? true : false,
transactionHash: blockscoutTransaction.hash,
transactionIndex: blockscoutTransaction.position,
blockHash: '', // Unknown
blockNumber: blockscoutTransaction.block,
from: blockscoutTransaction.from.hash.toLowerCase(),
to: blockscoutTransaction.to.hash.toLowerCase(),
// contractAddress?: string,
cumulativeGasUsed: Number(blockscoutTransaction.gas_used), // Not sure if this is the right field
gasUsed: Number(blockscoutTransaction.gas_used),
effectiveGasPrice: Number(blockscoutTransaction.gas_price),
logs: [],
logsBloom: '',
// events?: {
// [eventName: string]: EventLog,
// },
}

const transactionData: LegacyTxData & { estimatedGas?: number; timestamp?: number } = {
type: TransactionType.Legacy,
...(blockscoutTransaction.nonce && { nonce: blockscoutTransaction.nonce }),
...(blockscoutTransaction.gas_price && { gasPrice: blockscoutTransaction.gas_price }),
...(blockscoutTransaction.gas_limit && { gasLimit: blockscoutTransaction.gas_limit }),
// to?: AddressLike, // already set in transactionReceipt
...(blockscoutTransaction.value && { value: blockscoutTransaction.value }),
...(blockscoutTransaction.raw_input &&
blockscoutTransaction.raw_input !== '0x' && { data: blockscoutTransaction.raw_input }), // Not sure if this is the right field

// estimatedGas?: number, // No way to know this for fetched transactions
...(blockscoutTransaction.timestamp && { timestamp: new Date(blockscoutTransaction.timestamp).getTime() }),
}
return {
...transactionReceipt,
...transactionData,
} as PersistedEvmTransaction
}
1 change: 1 addition & 0 deletions packages/shared/src/lib/core/transactions/utils/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './buildPersistedEvmTransactionFromBlockscoutTransaction'
Loading