Skip to content

Commit

Permalink
Merge pull request #28 from getsumer/refactor-not-processed-tx
Browse files Browse the repository at this point in the history
refactor: minor fixes
  • Loading branch information
cito-lito authored Jun 15, 2023
2 parents 558476d + 3195db6 commit d8f0982
Show file tree
Hide file tree
Showing 9 changed files with 66 additions and 156 deletions.
34 changes: 0 additions & 34 deletions src/models/ContractError.ts

This file was deleted.

37 changes: 0 additions & 37 deletions src/models/ProviderError.ts

This file was deleted.

10 changes: 10 additions & 0 deletions src/observers/Transaction.ts → src/models/Transaction.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { Parser } from 'bowser'

export interface Transaction {
hash?: string

Expand Down Expand Up @@ -26,4 +28,12 @@ export interface Transaction {
effectiveGasPrice?: string
cumulativeGasUsed?: string
status?: number

// Error
error?: {
code: number
message: string
}

metadata?: Parser.ParsedResult | Record<string, string>
}
6 changes: 5 additions & 1 deletion src/models/eip/1193.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
// https://eips.ethereum.org/EIPS/eip-1193
import { EipError } from '../ProviderError'
export interface EipError {
statusCode: number
name: string
description: string
}

export const ProviderErrorsEip1193: EipError[] = [
{
Expand Down
3 changes: 2 additions & 1 deletion src/models/eip/1474.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// https://eips.ethereum.org/EIPS/eip-1474
import { EipError } from '../ProviderError'

import { EipError } from './1193'

export const RPCErrorsEip1474: EipError[] = [
{ statusCode: -32700, name: 'Parse error', description: 'Invalid JSON' },
Expand Down
2 changes: 1 addition & 1 deletion src/models/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export * from '../observers/Transaction'
export * from './Transaction'
101 changes: 42 additions & 59 deletions src/observers/TransactionObserver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,48 +2,41 @@ import { Target, ExecutionPayload, TargetExecution } from './Target'
import { SumerObserver } from './SumerObserver'
import { ErrorParams } from '../services'

enum Rpc_Methods {
eth_sendTransaction = 'eth_sendTransaction',
eth_estimateGas = 'eth_estimateGas',
}

export class TransactionObserver extends SumerObserver {
public async inspect({ execution }: Target): Promise<void> {
if (!this.isCall(execution.args) && this.isTransaction(execution.result)) {
const { result, target } = execution
const wallet = this.getWallet(target)

await this.notifyService.trackTransaction(
{
hash: result['hash'] || result['transactionHash'] || result,
fromAddress: result['from'],
toAddress: result['to'],

// Transaction Response
chainId: this.parseNumber(result['chainId']) || this.getChainId(execution),
nonce: this.parseNumber(result['nonce']),
gasLimit: this.parseBigNumber(result['gasLimit']),
maxFeePerGas: this.parseBigNumber(result['maxFeePerGas']),
maxPriorityFeePerGas: this.parseBigNumber(result['maxPriorityFeePerGas']),
data: result['data'],
value: this.parseBigNumber(result['value']),

// Transaction Receipt
blockHash: result['blockHash'],
blockNumber: this.parseNumber(result['blockNumber']),
confirmations: result['confirmations'],
transactionIndex: this.parseNumber(result['transactionIndex']),
contractAddress: result['contractAddress'],
gasUsed: this.parseBigNumber(result['gasUsed']),
effectiveGasPrice: this.parseBigNumber(result['effectiveGasPrice']),
cumulativeGasUsed: this.parseBigNumber(result['cumulativeGasUsed']),
status: this.parseNumber(result['status']),

args: execution.args,
functionName: execution.methodName,
},
{ ...this.meta(), wallet },
)
await this.notifyService.trackTransaction({
hash: result['hash'] || result['transactionHash'] || result,
fromAddress: result['from'],
toAddress: result['to'],

// Transaction Response
chainId: this.parseNumber(result['chainId']) || this.getChainId(execution),
nonce: this.parseNumber(result['nonce']),
gasLimit: this.parseBigNumber(result['gasLimit']),
maxFeePerGas: this.parseBigNumber(result['maxFeePerGas']),
maxPriorityFeePerGas: this.parseBigNumber(result['maxPriorityFeePerGas']),
data: result['data'],
value: this.parseBigNumber(result['value']),

// Transaction Receipt
blockHash: result['blockHash'],
blockNumber: this.parseNumber(result['blockNumber']),
confirmations: result['confirmations'],
transactionIndex: this.parseNumber(result['transactionIndex']),
contractAddress: result['contractAddress'],
gasUsed: this.parseBigNumber(result['gasUsed']),
effectiveGasPrice: this.parseBigNumber(result['effectiveGasPrice']),
cumulativeGasUsed: this.parseBigNumber(result['cumulativeGasUsed']),
status: this.parseNumber(result['status']),

args: execution.args,
functionName: execution.methodName,
metadata: { ...this.meta(), wallet },
})
}

if (this.isNotProcessed(execution.result)) {
Expand All @@ -53,20 +46,19 @@ export class TransactionObserver extends SumerObserver {
const error = this.getErrorResult(result)

if (params) {
await this.notifyService.trackTransaction(
{
fromAddress: params['from'] ?? this.getAddress(execution),
toAddress: params['to'] ?? undefined,
value: params['value'] ?? undefined,
data: params['data'] ?? undefined,
gas: params['gas'] ?? undefined,

chainId: this.getChainId(execution),
functionName: this.getMethodName(args),
},
{ ...this.meta(), wallet: this.getWallet(target) },
await this.notifyService.trackTransaction({
fromAddress: params['from'] ?? this.getAddress(execution),
toAddress: params['to'] ?? undefined,
value: params['value'] ?? undefined,
data: params['data'] ?? undefined,
gas: params['gas'] ?? undefined,

chainId: this.getChainId(execution),
functionName: this.getMethodName(args),

metadata: { ...this.meta(), wallet: this.getWallet(target) },
error,
)
})
}
}
}
Expand All @@ -79,19 +71,10 @@ export class TransactionObserver extends SumerObserver {
}

private getArgsParams(args: unknown[]): object | undefined {
// refactor needed:
// check: https://ethereum.github.io/execution-apis/api-documentation/

if (!(args && args.length > 0)) {
return undefined
}
const method = this.getMethodName(args)

if (method === (Rpc_Methods.eth_sendTransaction || Rpc_Methods.eth_estimateGas)) {
return args[0]['params'][0] as object
}
// to be handled
return args[0]['params'][0]
return args[0]['params'][0] as object
}

private getErrorResult(result: ExecutionPayload): ErrorParams | undefined {
Expand Down
18 changes: 3 additions & 15 deletions src/services/NotifyServiceApi.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Parser } from 'bowser'
import { ErrorParams, NotifyService } from './NotifyService'
import { NotifyService } from './NotifyService'
import { Transaction } from '../models'

export class NotifyServiceApi implements NotifyService {
Expand All @@ -15,15 +14,9 @@ export class NotifyServiceApi implements NotifyService {
this.checkConnection()
}

public async trackTransaction(
transaction: Transaction,
metadata: Parser.ParsedResult | Record<string, string>,
error: ErrorParams,
): Promise<void> {
public async trackTransaction(transaction: Transaction): Promise<void> {
this.fetchPost('transactions', {
...transaction,
metadata,
error,
})
}

Expand All @@ -38,12 +31,7 @@ export class NotifyServiceApi implements NotifyService {
}
}

private fetchPost(
uriPath: string,
body?: Transaction & { metadata: Parser.ParsedResult | Record<string, string> } & {
error: ErrorParams
},
): void {
private fetchPost(uriPath: string, body?: Transaction): void {
try {
fetch(`${this.url}/${uriPath}`, {
method: 'POST',
Expand Down
11 changes: 3 additions & 8 deletions src/services/NotifyServiceLog.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { ErrorParams, NotifyService } from './NotifyService'
import { Transaction } from '../models'
import { Parser } from 'bowser'
import { NotifyService } from './NotifyService'

export class NotifyServiceLog implements NotifyService {
private _chainId: number
Expand All @@ -9,12 +8,8 @@ export class NotifyServiceLog implements NotifyService {
return this._chainId
}

public async trackTransaction(
transaction: Transaction,
metadata?: Parser.ParsedResult | Record<string, string>,
error?: ErrorParams,
): Promise<void> {
console.info('trackTransaction Log:', transaction, metadata, error)
public async trackTransaction(transaction: Transaction): Promise<void> {
console.info('trackTransaction Log:', transaction)
}

public async checkConnection(): Promise<void> {
Expand Down

0 comments on commit d8f0982

Please sign in to comment.