@ -1,4 +1,6 @@
import { strict as assert } from 'assert' ;
import { TRANSACTION _ENVELOPE _TYPES } from '../../../../../shared/constants/transaction' ;
import { BURN _ADDRESS } from '../../../../../shared/modules/hexstring-utils' ;
import * as txUtils from './util' ;
describe ( 'txUtils' , function ( ) {
@ -48,6 +50,239 @@ describe('txUtils', function () {
message : 'Invalid transaction value "-0x01": not a positive number.' ,
} ) ;
} ) ;
describe ( 'when validating gasPrice' , function ( ) {
it ( 'should error when specifying incorrect type' , function ( ) {
const txParams = {
gasPrice : '0x1' ,
type : TRANSACTION _ENVELOPE _TYPES . FEE _MARKET ,
to : BURN _ADDRESS ,
} ;
assert . throws (
( ) => {
txUtils . validateTxParams ( txParams ) ;
} ,
{
message : ` Invalid transaction envelope type: specified type "0x2" but included a gasPrice instead of maxFeePerGas and maxPriorityFeePerGas ` ,
} ,
) ;
} ) ;
it ( 'should error when gasPrice is not a string' , function ( ) {
const txParams = {
gasPrice : 1 ,
to : BURN _ADDRESS ,
} ;
assert . throws (
( ) => {
txUtils . validateTxParams ( txParams ) ;
} ,
{
message :
'Invalid transaction params: gasPrice is not a string. got: (1)' ,
} ,
) ;
} ) ;
it ( 'should error when specifying maxFeePerGas' , function ( ) {
const txParams = {
gasPrice : '0x1' ,
maxFeePerGas : '0x1' ,
to : BURN _ADDRESS ,
} ;
assert . throws (
( ) => {
txUtils . validateTxParams ( txParams ) ;
} ,
{
message :
'Invalid transaction params: specified gasPrice but also included maxFeePerGas, these cannot be mixed' ,
} ,
) ;
} ) ;
it ( 'should error when specifying maxPriorityFeePerGas' , function ( ) {
const txParams = {
gasPrice : '0x1' ,
maxPriorityFeePerGas : '0x1' ,
to : BURN _ADDRESS ,
} ;
assert . throws (
( ) => {
txUtils . validateTxParams ( txParams ) ;
} ,
{
message :
'Invalid transaction params: specified gasPrice but also included maxPriorityFeePerGas, these cannot be mixed' ,
} ,
) ;
} ) ;
it ( 'should validate if gasPrice is set with no type or EIP-1559 gas fields' , function ( ) {
const txParams = {
gasPrice : '0x1' ,
to : BURN _ADDRESS ,
} ;
assert . doesNotThrow ( ( ) => txUtils . validateTxParams ( txParams ) ) ;
} ) ;
it ( 'should validate if gasPrice is set with a type of "0x0"' , function ( ) {
const txParams = {
gasPrice : '0x1' ,
type : TRANSACTION _ENVELOPE _TYPES . LEGACY ,
to : BURN _ADDRESS ,
} ;
assert . doesNotThrow ( ( ) => txUtils . validateTxParams ( txParams ) ) ;
} ) ;
} ) ;
describe ( 'when validating maxFeePerGas' , function ( ) {
it ( 'should error when specifying incorrect type' , function ( ) {
const txParams = {
maxFeePerGas : '0x1' ,
type : TRANSACTION _ENVELOPE _TYPES . LEGACY ,
to : BURN _ADDRESS ,
} ;
assert . throws (
( ) => {
txUtils . validateTxParams ( txParams ) ;
} ,
{
message :
'Invalid transaction envelope type: specified type "0x0" but including maxFeePerGas and maxPriorityFeePerGas requires type: "0x2"' ,
} ,
) ;
} ) ;
it ( 'should error when maxFeePerGas is not a string' , function ( ) {
const txParams = {
maxFeePerGas : 1 ,
to : BURN _ADDRESS ,
} ;
assert . throws (
( ) => {
txUtils . validateTxParams ( txParams ) ;
} ,
{
message :
'Invalid transaction params: maxFeePerGas is not a string. got: (1)' ,
} ,
) ;
} ) ;
it ( 'should error when specifying gasPrice' , function ( ) {
const txParams = {
gasPrice : '0x1' ,
maxFeePerGas : '0x1' ,
to : BURN _ADDRESS ,
} ;
assert . throws (
( ) => {
txUtils . validateTxParams ( txParams ) ;
} ,
{
message :
'Invalid transaction params: specified gasPrice but also included maxFeePerGas, these cannot be mixed' ,
} ,
) ;
} ) ;
it ( 'should validate if maxFeePerGas is set with no type or gasPrice field' , function ( ) {
const txParams = {
maxFeePerGas : '0x1' ,
to : BURN _ADDRESS ,
} ;
assert . doesNotThrow ( ( ) => txUtils . validateTxParams ( txParams ) ) ;
} ) ;
it ( 'should validate if maxFeePerGas is set with a type of "0x2"' , function ( ) {
const txParams = {
maxFeePerGas : '0x1' ,
type : TRANSACTION _ENVELOPE _TYPES . FEE _MARKET ,
to : BURN _ADDRESS ,
} ;
assert . doesNotThrow ( ( ) => txUtils . validateTxParams ( txParams ) ) ;
} ) ;
} ) ;
describe ( 'when validating maxPriorityFeePerGas' , function ( ) {
it ( 'should error when specifying incorrect type' , function ( ) {
const txParams = {
maxPriorityFeePerGas : '0x1' ,
type : TRANSACTION _ENVELOPE _TYPES . LEGACY ,
to : BURN _ADDRESS ,
} ;
assert . throws (
( ) => {
txUtils . validateTxParams ( txParams ) ;
} ,
{
message :
'Invalid transaction envelope type: specified type "0x0" but including maxFeePerGas and maxPriorityFeePerGas requires type: "0x2"' ,
} ,
) ;
} ) ;
it ( 'should error when maxFeePerGas is not a string' , function ( ) {
const txParams = {
maxPriorityFeePerGas : 1 ,
to : BURN _ADDRESS ,
} ;
assert . throws (
( ) => {
txUtils . validateTxParams ( txParams ) ;
} ,
{
message :
'Invalid transaction params: maxPriorityFeePerGas is not a string. got: (1)' ,
} ,
) ;
} ) ;
it ( 'should error when specifying gasPrice' , function ( ) {
const txParams = {
gasPrice : '0x1' ,
maxPriorityFeePerGas : '0x1' ,
to : BURN _ADDRESS ,
} ;
assert . throws (
( ) => {
txUtils . validateTxParams ( txParams ) ;
} ,
{
message :
'Invalid transaction params: specified gasPrice but also included maxPriorityFeePerGas, these cannot be mixed' ,
} ,
) ;
} ) ;
it ( 'should validate if maxPriorityFeePerGas is set with no type or gasPrice field' , function ( ) {
const txParams = {
maxPriorityFeePerGas : '0x1' ,
to : BURN _ADDRESS ,
} ;
assert . doesNotThrow ( ( ) => txUtils . validateTxParams ( txParams ) ) ;
} ) ;
it ( 'should validate if maxPriorityFeePerGas is set with a type of "0x2"' , function ( ) {
const txParams = {
maxPriorityFeePerGas : '0x1' ,
type : TRANSACTION _ENVELOPE _TYPES . FEE _MARKET ,
to : BURN _ADDRESS ,
} ;
assert . doesNotThrow ( ( ) => txUtils . validateTxParams ( txParams ) ) ;
} ) ;
} ) ;
} ) ;
describe ( '#normalizeTxParams' , function ( ) {
@ -58,6 +293,10 @@ describe('txUtils', function () {
to : null ,
data : '68656c6c6f20776f726c64' ,
random : 'hello world' ,
gasPrice : '1' ,
maxFeePerGas : '1' ,
maxPriorityFeePerGas : '1' ,
type : '1' ,
} ;
let normalizedTxParams = txUtils . normalizeTxParams ( txParams ) ;
@ -89,6 +328,28 @@ describe('txUtils', function () {
'0x' ,
'to should be hex-prefixed' ,
) ;
assert . equal (
normalizedTxParams . gasPrice ,
'0x1' ,
'gasPrice should be hex-prefixed' ,
) ;
assert . equal (
normalizedTxParams . maxFeePerGas ,
'0x1' ,
'maxFeePerGas should be hex-prefixed' ,
) ;
assert . equal (
normalizedTxParams . maxPriorityFeePerGas ,
'0x1' ,
'maxPriorityFeePerGas should be hex-prefixed' ,
) ;
assert . equal (
normalizedTxParams . type ,
'0x1' ,
'type should be hex-prefixed' ,
) ;
} ) ;
} ) ;