Skip to content

Commit

Permalink
BaseTransaction:
Browse files Browse the repository at this point in the history
  * add `_type` and aliases
  * move EIP-2930 aliases for senderS, senderR, yParity so they can be available for all transactions
  • Loading branch information
ryanio committed Apr 8, 2021
1 parent f21640b commit 2ea2ebc
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 33 deletions.
55 changes: 51 additions & 4 deletions packages/tx/src/baseTransaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,13 @@ import {
ecsign,
publicToAddress,
} from 'ethereumjs-util'
import { TxData, TxOptions, JsonTx, AccessListEIP2930ValuesArray } from './types'
import {
TxData,
TxOptions,
JsonTx,
AccessListEIP2930ValuesArray,
AccessListEIP2930TxData,
} from './types'

/**
* This base class will likely be subject to further
Expand All @@ -18,6 +24,8 @@ import { TxData, TxOptions, JsonTx, AccessListEIP2930ValuesArray } from './types
* It is therefore not recommended to use directly.
*/
export abstract class BaseTransaction<TransactionObject> {
private readonly _type: number

public readonly nonce: BN
public readonly gasLimit: BN
public readonly gasPrice: BN
Expand All @@ -30,8 +38,16 @@ export abstract class BaseTransaction<TransactionObject> {
public readonly r?: BN
public readonly s?: BN

constructor(txData: TxData, txOptions: TxOptions = {}) {
constructor(txData: TxData | AccessListEIP2930TxData, txOptions: TxOptions = {}) {
const { nonce, gasLimit, gasPrice, to, value, data, v, r, s } = txData

const type = (txData as AccessListEIP2930TxData).type
if (type !== undefined) {
this._type = new BN(toBuffer(type)).toNumber()
} else {
this._type = 0
}

This comment has been minimized.

Copy link
@holgerd77

holgerd77 Apr 9, 2021

Member

I think it would have been a bit more consistent to apply this super() call format of super({ ...txData, type: 1 }, opts) also for the legacy tx with type: 0 instead of setting this here but at the end some minor internal which eventually can be refactored on accasion.

Otherwise this looks good, thanks! 😄


const toB = toBuffer(to === '' ? '0x' : to)
const vB = toBuffer(v === '' ? '0x' : v)
const rB = toBuffer(r === '' ? '0x' : r)
Expand Down Expand Up @@ -59,9 +75,40 @@ export abstract class BaseTransaction<TransactionObject> {
}

/**
* Checks if the transaction has the minimum amount of gas required
* (DataFee + TxFee + Creation Fee).
* Returns the transaction type
*/
get transactionType(): number {
return this._type
}

/**
* Alias for `transactionType`
*/
get type() {
return this.transactionType
}

/**
* EIP-2930 alias for `r`
*/
get senderR() {
return this.r
}

/**
* EIP-2930 alias for `s`
*/
get senderS() {
return this.s
}

/**
* EIP-2930 alias for `v`
*/
get yParity() {
return this.v
}

/**
* Checks if the transaction has the minimum amount of gas required
* (DataFee + TxFee + Creation Fee).
Expand Down
26 changes: 1 addition & 25 deletions packages/tx/src/eip2930Transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,30 +35,6 @@ export default class AccessListEIP2930Transaction extends BaseTransaction<Access
public readonly accessList: AccessListBuffer
public readonly AccessListJSON: AccessList

get transactionType(): number {
return 1
}

// Alias for transactionType
get type() {
return this.transactionType
}

// EIP-2930 alias for `s`
get senderS() {
return this.s
}

// EIP-2930 alias for `r`
get senderR() {
return this.r
}

// EIP-2930 alias for `v`
get yParity() {
return this.v
}

/**
* Instantiate a transaction from a data dictionary
*/
Expand Down Expand Up @@ -143,7 +119,7 @@ export default class AccessListEIP2930Transaction extends BaseTransaction<Access
public constructor(txData: AccessListEIP2930TxData, opts: TxOptions = {}) {
const { chainId, accessList } = txData

super(txData, opts)
super({ ...txData, type: 1 }, opts)

// EIP-2718 check is done in Common
if (!this.common.isActivatedEIP(2930)) {
Expand Down
4 changes: 0 additions & 4 deletions packages/tx/src/legacyTransaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,6 @@ import { BaseTransaction } from './baseTransaction'
* An Ethereum non-typed (legacy) transaction
*/
export default class Transaction extends BaseTransaction<Transaction> {
get transactionType(): number {
return 0
}

/**
* Instantiate a transaction from a data dictionary
*/
Expand Down

0 comments on commit 2ea2ebc

Please sign in to comment.