2021-02-04 19:15:23 +01:00
import { strict as assert } from 'assert' ;
import EventEmitter from 'events' ;
import ethUtil from 'ethereumjs-util' ;
import EthTx from 'ethereumjs-tx' ;
import { ObservableStore } from '@metamask/obs-store' ;
import sinon from 'sinon' ;
import TransactionController from '../../../../../app/scripts/controllers/transactions' ;
2020-01-09 04:34:58 +01:00
2020-11-03 00:41:28 +01:00
import {
createTestProviderTools ,
getTestAccounts ,
2021-02-04 19:15:23 +01:00
} from '../../../../stub/provider' ;
2020-11-03 23:57:51 +01:00
import {
TRANSACTION _CATEGORIES ,
2020-11-07 08:38:12 +01:00
TRANSACTION _STATUSES ,
2020-11-03 23:57:51 +01:00
TRANSACTION _TYPES ,
2021-02-04 19:15:23 +01:00
} from '../../../../../shared/constants/transaction' ;
2021-02-09 00:22:30 +01:00
import { METAMASK _CONTROLLER _EVENTS } from '../../../../../app/scripts/metamask-controller' ;
2017-08-15 04:15:09 +02:00
2021-02-04 19:15:23 +01:00
const noop = ( ) => true ;
const currentNetworkId = '42' ;
const currentChainId = '0x2a' ;
2017-08-15 04:15:09 +02:00
2017-08-02 17:34:45 +02:00
describe ( 'Transaction Controller' , function ( ) {
2021-02-04 19:15:23 +01:00
let txController , provider , providerResultStub , fromAccount ;
2016-12-14 21:56:53 +01:00
2017-05-04 23:35:10 +02:00
beforeEach ( function ( ) {
2018-01-18 06:43:34 +01:00
providerResultStub = {
// 1 gwei
eth _gasPrice : '0x0de0b6b3a7640000' ,
// by default, all accounts are external accounts (not contracts)
eth _getCode : '0x' ,
2021-02-04 19:15:23 +01:00
} ;
2020-11-03 00:41:28 +01:00
provider = createTestProviderTools ( { scaffold : providerResultStub } )
2021-02-04 19:15:23 +01:00
. provider ;
fromAccount = getTestAccounts ( ) [ 0 ] ;
const blockTrackerStub = new EventEmitter ( ) ;
blockTrackerStub . getCurrentBlock = noop ;
blockTrackerStub . getLatestBlock = noop ;
2017-05-16 19:27:41 +02:00
txController = new TransactionController ( {
2017-08-09 00:30:22 +02:00
provider ,
2020-11-03 00:41:28 +01:00
getGasPrice ( ) {
2021-02-04 19:15:23 +01:00
return '0xee6b2800' ;
2019-11-20 01:03:20 +01:00
} ,
2020-01-17 22:58:27 +01:00
networkStore : new ObservableStore ( currentNetworkId ) ,
2016-12-16 19:33:36 +01:00
txHistoryLimit : 10 ,
2018-05-28 23:29:31 +02:00
blockTracker : blockTrackerStub ,
2020-11-03 00:41:28 +01:00
signTransaction : ( ethTx ) =>
new Promise ( ( resolve ) => {
2021-02-04 19:15:23 +01:00
ethTx . sign ( fromAccount . key ) ;
resolve ( ) ;
2020-11-03 00:41:28 +01:00
} ) ,
2020-08-14 13:47:02 +02:00
getPermittedAccounts : ( ) => undefined ,
2020-10-06 22:18:24 +02:00
getCurrentChainId : ( ) => currentChainId ,
2020-10-08 18:41:23 +02:00
getParticipateInMetrics : ( ) => false ,
2021-02-04 19:15:23 +01:00
} ) ;
2020-11-03 00:41:28 +01:00
txController . nonceTracker . getNonceLock = ( ) =>
2021-02-04 19:15:23 +01:00
Promise . resolve ( { nextNonce : 0 , releaseLock : noop } ) ;
} ) ;
2017-07-26 20:56:52 +02:00
2017-09-08 23:24:40 +02:00
describe ( '#getState' , function ( ) {
2020-04-20 17:01:00 +02:00
it ( 'should return a state object with the right keys and data types' , function ( ) {
2021-02-04 19:15:23 +01:00
const exposedState = txController . getState ( ) ;
2020-11-03 00:41:28 +01:00
assert . ok (
'unapprovedTxs' in exposedState ,
'state should have the key unapprovedTxs' ,
2021-02-04 19:15:23 +01:00
) ;
2020-11-03 00:41:28 +01:00
assert . ok (
'currentNetworkTxList' in exposedState ,
'state should have the key currentNetworkTxList' ,
2021-02-04 19:15:23 +01:00
) ;
2020-11-03 00:41:28 +01:00
assert . ok (
typeof exposedState ? . unapprovedTxs === 'object' ,
'should be an object' ,
2021-02-04 19:15:23 +01:00
) ;
2020-11-03 00:41:28 +01:00
assert . ok (
Array . isArray ( exposedState . currentNetworkTxList ) ,
'should be an array' ,
2021-02-04 19:15:23 +01:00
) ;
} ) ;
} ) ;
2017-09-08 23:24:40 +02:00
describe ( '#getUnapprovedTxCount' , function ( ) {
it ( 'should return the number of unapproved txs' , function ( ) {
txController . txStateManager . _saveTxList ( [
2020-11-03 00:41:28 +01:00
{
id : 1 ,
2020-11-07 08:38:12 +01:00
status : TRANSACTION _STATUSES . UNAPPROVED ,
2020-11-03 00:41:28 +01:00
metamaskNetworkId : currentNetworkId ,
txParams : { } ,
history : [ { } ] ,
} ,
{
id : 2 ,
2020-11-07 08:38:12 +01:00
status : TRANSACTION _STATUSES . UNAPPROVED ,
2020-11-03 00:41:28 +01:00
metamaskNetworkId : currentNetworkId ,
txParams : { } ,
history : [ { } ] ,
} ,
{
id : 3 ,
2020-11-07 08:38:12 +01:00
status : TRANSACTION _STATUSES . UNAPPROVED ,
2020-11-03 00:41:28 +01:00
metamaskNetworkId : currentNetworkId ,
txParams : { } ,
history : [ { } ] ,
} ,
2021-02-04 19:15:23 +01:00
] ) ;
const unapprovedTxCount = txController . getUnapprovedTxCount ( ) ;
assert . equal ( unapprovedTxCount , 3 , 'should be 3' ) ;
} ) ;
} ) ;
2017-09-08 23:24:40 +02:00
2017-09-13 23:07:22 +02:00
describe ( '#getPendingTxCount' , function ( ) {
it ( 'should return the number of pending txs' , function ( ) {
txController . txStateManager . _saveTxList ( [
2020-11-03 00:41:28 +01:00
{
id : 1 ,
2020-11-07 08:38:12 +01:00
status : TRANSACTION _STATUSES . SUBMITTED ,
2020-11-03 00:41:28 +01:00
metamaskNetworkId : currentNetworkId ,
txParams : { } ,
history : [ { } ] ,
} ,
{
id : 2 ,
2020-11-07 08:38:12 +01:00
status : TRANSACTION _STATUSES . SUBMITTED ,
2020-11-03 00:41:28 +01:00
metamaskNetworkId : currentNetworkId ,
txParams : { } ,
history : [ { } ] ,
} ,
{
id : 3 ,
2020-11-07 08:38:12 +01:00
status : TRANSACTION _STATUSES . SUBMITTED ,
2020-11-03 00:41:28 +01:00
metamaskNetworkId : currentNetworkId ,
txParams : { } ,
history : [ { } ] ,
} ,
2021-02-04 19:15:23 +01:00
] ) ;
const pendingTxCount = txController . getPendingTxCount ( ) ;
assert . equal ( pendingTxCount , 3 , 'should be 3' ) ;
} ) ;
} ) ;
2017-09-13 23:07:22 +02:00
2017-09-23 01:15:18 +02:00
describe ( '#getConfirmedTransactions' , function ( ) {
2020-02-11 17:51:13 +01:00
it ( 'should return the number of confirmed txs' , function ( ) {
2021-02-04 19:15:23 +01:00
const address = '0xc684832530fcbddae4b4230a47e991ddcec2831d' ;
2017-09-23 01:15:18 +02:00
const txParams = {
2020-11-03 00:41:28 +01:00
from : address ,
to : '0xc684832530fcbddae4b4230a47e991ddcec2831d' ,
2021-02-04 19:15:23 +01:00
} ;
2017-09-23 01:15:18 +02:00
txController . txStateManager . _saveTxList ( [
2020-11-03 00:41:28 +01:00
{
id : 0 ,
2020-11-07 08:38:12 +01:00
status : TRANSACTION _STATUSES . CONFIRMED ,
2020-11-03 00:41:28 +01:00
metamaskNetworkId : currentNetworkId ,
txParams ,
history : [ { } ] ,
} ,
{
id : 1 ,
2020-11-07 08:38:12 +01:00
status : TRANSACTION _STATUSES . CONFIRMED ,
2020-11-03 00:41:28 +01:00
metamaskNetworkId : currentNetworkId ,
txParams ,
history : [ { } ] ,
} ,
{
id : 2 ,
2020-11-07 08:38:12 +01:00
status : TRANSACTION _STATUSES . CONFIRMED ,
2020-11-03 00:41:28 +01:00
metamaskNetworkId : currentNetworkId ,
txParams ,
history : [ { } ] ,
} ,
{
id : 3 ,
2020-11-07 08:38:12 +01:00
status : TRANSACTION _STATUSES . UNAPPROVED ,
2020-11-03 00:41:28 +01:00
metamaskNetworkId : currentNetworkId ,
txParams ,
history : [ { } ] ,
} ,
{
id : 4 ,
2020-11-07 08:38:12 +01:00
status : TRANSACTION _STATUSES . REJECTED ,
2020-11-03 00:41:28 +01:00
metamaskNetworkId : currentNetworkId ,
txParams ,
history : [ { } ] ,
} ,
{
id : 5 ,
2020-11-07 08:38:12 +01:00
status : TRANSACTION _STATUSES . APPROVED ,
2020-11-03 00:41:28 +01:00
metamaskNetworkId : currentNetworkId ,
txParams ,
history : [ { } ] ,
} ,
{
id : 6 ,
2020-11-07 08:38:12 +01:00
status : TRANSACTION _STATUSES . SIGNED ,
2020-11-03 00:41:28 +01:00
metamaskNetworkId : currentNetworkId ,
txParams ,
history : [ { } ] ,
} ,
{
id : 7 ,
2020-11-07 08:38:12 +01:00
status : TRANSACTION _STATUSES . SUBMITTED ,
2020-11-03 00:41:28 +01:00
metamaskNetworkId : currentNetworkId ,
txParams ,
history : [ { } ] ,
} ,
{
id : 8 ,
2020-11-07 08:38:12 +01:00
status : TRANSACTION _STATUSES . FAILED ,
2020-11-03 00:41:28 +01:00
metamaskNetworkId : currentNetworkId ,
txParams ,
history : [ { } ] ,
} ,
2021-02-04 19:15:23 +01:00
] ) ;
2020-11-03 00:41:28 +01:00
assert . equal (
txController . nonceTracker . getConfirmedTransactions ( address ) . length ,
3 ,
2021-02-04 19:15:23 +01:00
) ;
} ) ;
} ) ;
2017-09-23 01:15:18 +02:00
2017-08-03 00:58:05 +02:00
describe ( '#newUnapprovedTransaction' , function ( ) {
2021-02-04 19:15:23 +01:00
let stub , txMeta , txParams ;
2017-08-03 00:58:05 +02:00
beforeEach ( function ( ) {
txParams = {
2020-11-03 00:41:28 +01:00
from : '0xc684832530fcbddae4b4230a47e991ddcec2831d' ,
to : '0xc684832530fcbddae4b4230a47e991ddcec2831d' ,
2021-02-04 19:15:23 +01:00
} ;
2017-08-03 00:58:05 +02:00
txMeta = {
2020-11-07 08:38:12 +01:00
status : TRANSACTION _STATUSES . UNAPPROVED ,
2017-08-03 00:58:05 +02:00
id : 1 ,
metamaskNetworkId : currentNetworkId ,
txParams ,
2018-11-16 19:34:08 +01:00
history : [ { } ] ,
2021-02-04 19:15:23 +01:00
} ;
txController . txStateManager . _saveTxList ( [ txMeta ] ) ;
2020-11-03 00:41:28 +01:00
stub = sinon
. stub ( txController , 'addUnapprovedTransaction' )
. callsFake ( ( ) => {
2021-02-04 19:15:23 +01:00
txController . emit ( 'newUnapprovedTx' , txMeta ) ;
return Promise . resolve ( txController . txStateManager . addTx ( txMeta ) ) ;
} ) ;
} ) ;
2017-08-03 00:58:05 +02:00
2020-02-11 17:51:13 +01:00
afterEach ( function ( ) {
2021-02-04 19:15:23 +01:00
txController . txStateManager . _saveTxList ( [ ] ) ;
stub . restore ( ) ;
} ) ;
2017-08-03 00:58:05 +02:00
2020-04-20 17:01:00 +02:00
it ( 'should resolve when finished and status is submitted and resolve with the hash' , async function ( ) {
2017-11-06 13:35:51 +01:00
txController . once ( 'newUnapprovedTx' , ( txMetaFromEmit ) => {
2017-08-03 00:58:05 +02:00
setTimeout ( ( ) => {
2021-02-04 19:15:23 +01:00
txController . setTxHash ( txMetaFromEmit . id , '0x0' ) ;
txController . txStateManager . setTxStatusSubmitted ( txMetaFromEmit . id ) ;
} ) ;
} ) ;
2017-08-03 00:58:05 +02:00
2021-02-04 19:15:23 +01:00
const hash = await txController . newUnapprovedTransaction ( txParams ) ;
assert . ok ( hash , 'newUnapprovedTransaction needs to return the hash' ) ;
} ) ;
2017-08-03 00:58:05 +02:00
2020-04-20 17:01:00 +02:00
it ( 'should reject when finished and status is rejected' , async function ( ) {
2017-11-06 13:35:51 +01:00
txController . once ( 'newUnapprovedTx' , ( txMetaFromEmit ) => {
2017-08-03 00:58:05 +02:00
setTimeout ( ( ) => {
2021-02-04 19:15:23 +01:00
txController . txStateManager . setTxStatusRejected ( txMetaFromEmit . id ) ;
} ) ;
} ) ;
2017-08-03 00:58:05 +02:00
2020-04-20 17:01:00 +02:00
await assert . rejects (
( ) => txController . newUnapprovedTransaction ( txParams ) ,
2020-11-03 00:41:28 +01:00
{
message : 'MetaMask Tx Signature: User denied transaction signature.' ,
} ,
2021-02-04 19:15:23 +01:00
) ;
} ) ;
} ) ;
2017-08-03 00:58:05 +02:00
2017-07-26 20:56:52 +02:00
describe ( '#addUnapprovedTransaction' , function ( ) {
2021-02-04 19:15:23 +01:00
const selectedAddress = '0x1678a085c290ebd122dc42cba69373b5953b831d' ;
const recipientAddress = '0xc42edfcc21ed14dda456aa0756c153f7985d8813' ;
2018-10-10 20:41:17 +02:00
2021-02-04 19:15:23 +01:00
let getSelectedAddress , getPermittedAccounts ;
2018-10-10 20:41:17 +02:00
beforeEach ( function ( ) {
2020-11-03 00:41:28 +01:00
getSelectedAddress = sinon
. stub ( txController , 'getSelectedAddress' )
2021-02-04 19:15:23 +01:00
. returns ( selectedAddress ) ;
2020-11-03 00:41:28 +01:00
getPermittedAccounts = sinon
. stub ( txController , 'getPermittedAccounts' )
2021-02-04 19:15:23 +01:00
. returns ( [ selectedAddress ] ) ;
} ) ;
2018-10-10 20:41:17 +02:00
afterEach ( function ( ) {
2021-02-04 19:15:23 +01:00
getSelectedAddress . restore ( ) ;
getPermittedAccounts . restore ( ) ;
} ) ;
2018-01-14 23:01:37 +01:00
2020-04-20 17:01:00 +02:00
it ( 'should add an unapproved transaction and return a valid txMeta' , async function ( ) {
2020-11-03 00:41:28 +01:00
const txMeta = await txController . addUnapprovedTransaction ( {
from : selectedAddress ,
2020-12-04 03:15:59 +01:00
to : recipientAddress ,
2021-02-04 19:15:23 +01:00
} ) ;
assert . ok ( 'id' in txMeta , 'should have a id' ) ;
assert . ok ( 'time' in txMeta , 'should have a time stamp' ) ;
2020-11-03 00:41:28 +01:00
assert . ok (
'metamaskNetworkId' in txMeta ,
'should have a metamaskNetworkId' ,
2021-02-04 19:15:23 +01:00
) ;
assert . ok ( 'txParams' in txMeta , 'should have a txParams' ) ;
assert . ok ( 'history' in txMeta , 'should have a history' ) ;
2020-11-03 00:41:28 +01:00
assert . equal (
txMeta . txParams . value ,
'0x0' ,
'should have added 0x0 as the value' ,
2021-02-04 19:15:23 +01:00
) ;
2020-04-20 17:01:00 +02:00
2021-02-04 19:15:23 +01:00
const memTxMeta = txController . txStateManager . getTx ( txMeta . id ) ;
assert . deepEqual ( txMeta , memTxMeta ) ;
} ) ;
2018-01-14 23:01:37 +01:00
it ( 'should emit newUnapprovedTx event and pass txMeta as the first argument' , function ( done ) {
2021-02-04 19:15:23 +01:00
providerResultStub . eth _gasPrice = '4a817c800' ;
2018-01-14 23:01:37 +01:00
txController . once ( 'newUnapprovedTx' , ( txMetaFromEmit ) => {
2021-02-04 19:15:23 +01:00
assert . ok ( txMetaFromEmit , 'txMeta is falsy' ) ;
done ( ) ;
} ) ;
2020-11-03 00:41:28 +01:00
txController
2020-12-04 03:15:59 +01:00
. addUnapprovedTransaction ( {
from : selectedAddress ,
to : recipientAddress ,
} )
2021-02-04 19:15:23 +01:00
. catch ( done ) ;
} ) ;
2018-01-14 23:01:37 +01:00
2020-04-20 17:01:00 +02:00
it ( "should fail if the from address isn't the selected address" , async function ( ) {
2020-11-03 00:41:28 +01:00
await assert . rejects ( ( ) =>
txController . addUnapprovedTransaction ( {
from : '0x0d1d4e623D10F9FBA5Db95830F7d3839406C6AF2' ,
} ) ,
2021-02-04 19:15:23 +01:00
) ;
} ) ;
2018-10-10 20:41:17 +02:00
2020-04-20 17:01:00 +02:00
it ( 'should fail if netId is loading' , async function ( ) {
2021-02-04 19:15:23 +01:00
txController . networkStore = new ObservableStore ( 'loading' ) ;
2020-04-20 17:01:00 +02:00
await assert . rejects (
2020-11-03 00:41:28 +01:00
( ) =>
txController . addUnapprovedTransaction ( {
from : selectedAddress ,
to : '0x0d1d4e623D10F9FBA5Db95830F7d3839406C6AF2' ,
} ) ,
2020-04-20 17:01:00 +02:00
{ message : 'MetaMask is having trouble connecting to the network' } ,
2021-02-04 19:15:23 +01:00
) ;
} ) ;
} ) ;
2016-12-14 21:56:53 +01:00
2018-04-13 21:38:07 +02:00
describe ( '#addTxGasDefaults' , function ( ) {
2020-02-11 17:51:13 +01:00
it ( 'should add the tx defaults if their are none' , async function ( ) {
2020-05-01 17:25:45 +02:00
txController . txStateManager . _saveTxList ( [
2020-11-03 00:41:28 +01:00
{
id : 1 ,
2020-11-07 08:38:12 +01:00
status : TRANSACTION _STATUSES . UNAPPROVED ,
2020-11-03 00:41:28 +01:00
metamaskNetworkId : currentNetworkId ,
txParams : { } ,
history : [ { } ] ,
} ,
2021-02-04 19:15:23 +01:00
] ) ;
2017-09-23 01:15:18 +02:00
const txMeta = {
2020-05-01 17:25:45 +02:00
id : 1 ,
2018-05-25 07:41:09 +02:00
txParams : {
from : '0xc684832530fcbddae4b4230a47e991ddcec2831d' ,
to : '0xc684832530fcbddae4b4230a47e991ddcec2831d' ,
2017-08-02 17:34:45 +02:00
} ,
2018-11-16 19:34:08 +01:00
history : [ { } ] ,
2021-02-04 19:15:23 +01:00
} ;
providerResultStub . eth _gasPrice = '4a817c800' ;
providerResultStub . eth _getBlockByNumber = { gasLimit : '47b784' } ;
providerResultStub . eth _estimateGas = '5209' ;
2018-05-25 07:41:09 +02:00
2021-02-04 19:15:23 +01:00
const txMetaWithDefaults = await txController . addTxGasDefaults ( txMeta ) ;
2020-11-03 00:41:28 +01:00
assert . ok (
txMetaWithDefaults . txParams . gasPrice ,
'should have added the gas price' ,
2021-02-04 19:15:23 +01:00
) ;
2020-11-03 00:41:28 +01:00
assert . ok (
txMetaWithDefaults . txParams . gas ,
'should have added the gas field' ,
2021-02-04 19:15:23 +01:00
) ;
} ) ;
} ) ;
2017-08-02 17:34:45 +02:00
2017-05-04 23:35:10 +02:00
describe ( '#addTx' , function ( ) {
2017-08-11 23:19:35 +02:00
it ( 'should emit updates' , function ( done ) {
2017-05-16 22:22:03 +02:00
const txMeta = {
id : '1' ,
2020-11-07 08:38:12 +01:00
status : TRANSACTION _STATUSES . UNAPPROVED ,
2017-05-16 22:22:03 +02:00
metamaskNetworkId : currentNetworkId ,
2017-09-23 01:15:18 +02:00
txParams : { } ,
2021-02-04 19:15:23 +01:00
} ;
2017-05-16 22:22:03 +02:00
2021-02-09 00:22:30 +01:00
const eventNames = [
METAMASK _CONTROLLER _EVENTS . UPDATE _BADGE ,
'1:unapproved' ,
] ;
2021-02-04 19:15:23 +01:00
const listeners = [ ] ;
2017-08-11 23:19:35 +02:00
eventNames . forEach ( ( eventName ) => {
2020-11-03 00:41:28 +01:00
listeners . push (
new Promise ( ( resolve ) => {
txController . once ( eventName , ( arg ) => {
2021-02-04 19:15:23 +01:00
resolve ( arg ) ;
} ) ;
2020-11-03 00:41:28 +01:00
} ) ,
2021-02-04 19:15:23 +01:00
) ;
} ) ;
2017-08-11 23:19:35 +02:00
Promise . all ( listeners )
2019-07-31 22:17:11 +02:00
. then ( ( returnValues ) => {
2020-11-03 00:41:28 +01:00
assert . deepEqual (
returnValues . pop ( ) ,
txMeta ,
'last event 1:unapproved should return txMeta' ,
2021-02-04 19:15:23 +01:00
) ;
done ( ) ;
2019-07-31 22:17:11 +02:00
} )
2021-02-04 19:15:23 +01:00
. catch ( done ) ;
txController . addTx ( txMeta ) ;
} ) ;
} ) ;
2016-12-14 21:56:53 +01:00
2017-05-17 03:16:18 +02:00
describe ( '#approveTransaction' , function ( ) {
2020-04-20 17:01:00 +02:00
it ( 'does not overwrite set values' , async function ( ) {
2021-02-04 19:15:23 +01:00
const originalValue = '0x01' ;
2020-02-11 17:51:13 +01:00
const txMeta = {
2017-05-17 03:16:18 +02:00
id : '1' ,
2020-11-07 08:38:12 +01:00
status : TRANSACTION _STATUSES . UNAPPROVED ,
2017-05-17 03:16:18 +02:00
metamaskNetworkId : currentNetworkId ,
txParams : {
nonce : originalValue ,
gas : originalValue ,
gasPrice : originalValue ,
} ,
2021-02-04 19:15:23 +01:00
} ;
this . timeout ( 15000 ) ;
const wrongValue = '0x05' ;
2017-05-17 03:16:18 +02:00
2021-02-04 19:15:23 +01:00
txController . addTx ( txMeta ) ;
providerResultStub . eth _gasPrice = wrongValue ;
providerResultStub . eth _estimateGas = '0x5209' ;
2017-05-17 03:16:18 +02:00
2020-11-03 00:41:28 +01:00
const signStub = sinon
. stub ( txController , 'signTransaction' )
2021-02-04 19:15:23 +01:00
. callsFake ( ( ) => Promise . resolve ( ) ) ;
2017-05-17 03:16:18 +02:00
2020-11-03 00:41:28 +01:00
const pubStub = sinon
. stub ( txController , 'publishTransaction' )
. callsFake ( ( ) => {
2021-02-04 19:15:23 +01:00
txController . setTxHash ( '1' , originalValue ) ;
txController . txStateManager . setTxStatusSubmitted ( '1' ) ;
} ) ;
2017-05-17 03:16:18 +02:00
2021-02-04 19:15:23 +01:00
await txController . approveTransaction ( txMeta . id ) ;
const result = txController . txStateManager . getTx ( txMeta . id ) ;
const params = result . txParams ;
2017-05-17 03:16:18 +02:00
2021-02-04 19:15:23 +01:00
assert . equal ( params . gas , originalValue , 'gas unmodified' ) ;
assert . equal ( params . gasPrice , originalValue , 'gas price unmodified' ) ;
assert . equal ( result . hash , originalValue ) ;
2020-11-03 00:41:28 +01:00
assert . equal (
result . status ,
2020-11-07 08:38:12 +01:00
TRANSACTION _STATUSES . SUBMITTED ,
2020-11-03 00:41:28 +01:00
'should have reached the submitted status.' ,
2021-02-04 19:15:23 +01:00
) ;
signStub . restore ( ) ;
pubStub . restore ( ) ;
} ) ;
} ) ;
2017-05-17 03:16:18 +02:00
2017-05-04 23:35:10 +02:00
describe ( '#sign replay-protected tx' , function ( ) {
2020-04-20 17:01:00 +02:00
it ( 'prepares a tx with the chainId set' , async function ( ) {
2020-11-03 00:41:28 +01:00
txController . addTx (
{
id : '1' ,
2020-11-07 08:38:12 +01:00
status : TRANSACTION _STATUSES . UNAPPROVED ,
2020-11-03 00:41:28 +01:00
metamaskNetworkId : currentNetworkId ,
txParams : { } ,
} ,
noop ,
2021-02-04 19:15:23 +01:00
) ;
const rawTx = await txController . signTransaction ( '1' ) ;
const ethTx = new EthTx ( ethUtil . toBuffer ( rawTx ) ) ;
assert . equal ( ethTx . getChainId ( ) , 42 ) ;
} ) ;
} ) ;
2017-09-23 01:15:18 +02:00
2017-09-26 04:47:03 +02:00
describe ( '#updateAndApproveTransaction' , function ( ) {
2020-02-11 17:51:13 +01:00
it ( 'should update and approve transactions' , async function ( ) {
const txMeta = {
2017-09-26 04:47:03 +02:00
id : 1 ,
2020-11-07 08:38:12 +01:00
status : TRANSACTION _STATUSES . UNAPPROVED ,
2017-09-26 04:47:03 +02:00
txParams : {
2018-04-28 02:12:52 +02:00
from : fromAccount . address ,
2017-09-26 04:47:03 +02:00
to : '0x1678a085c290ebd122dc42cba69373b5953b831d' ,
gasPrice : '0x77359400' ,
gas : '0x7b0d' ,
nonce : '0x4b' ,
} ,
metamaskNetworkId : currentNetworkId ,
2021-02-04 19:15:23 +01:00
} ;
txController . txStateManager . addTx ( txMeta ) ;
const approvalPromise = txController . updateAndApproveTransaction ( txMeta ) ;
const tx = txController . txStateManager . getTx ( 1 ) ;
assert . equal ( tx . status , TRANSACTION _STATUSES . APPROVED ) ;
await approvalPromise ;
} ) ;
} ) ;
2017-09-23 01:15:18 +02:00
2017-09-26 04:47:03 +02:00
describe ( '#getChainId' , function ( ) {
it ( 'returns 0 when the chainId is NaN' , function ( ) {
2021-02-04 19:15:23 +01:00
txController . networkStore = new ObservableStore ( 'loading' ) ;
assert . equal ( txController . getChainId ( ) , 0 ) ;
} ) ;
} ) ;
2017-09-23 01:15:18 +02:00
describe ( '#cancelTransaction' , function ( ) {
2020-02-11 17:51:13 +01:00
it ( 'should emit a status change to rejected' , function ( done ) {
2017-09-26 04:47:03 +02:00
txController . txStateManager . _saveTxList ( [
2020-11-03 00:41:28 +01:00
{
id : 0 ,
2020-11-07 08:38:12 +01:00
status : TRANSACTION _STATUSES . UNAPPROVED ,
2020-11-03 00:41:28 +01:00
txParams : { } ,
metamaskNetworkId : currentNetworkId ,
history : [ { } ] ,
} ,
{
id : 1 ,
2020-11-07 08:38:12 +01:00
status : TRANSACTION _STATUSES . REJECTED ,
2020-11-03 00:41:28 +01:00
txParams : { } ,
metamaskNetworkId : currentNetworkId ,
history : [ { } ] ,
} ,
{
id : 2 ,
2020-11-07 08:38:12 +01:00
status : TRANSACTION _STATUSES . APPROVED ,
2020-11-03 00:41:28 +01:00
txParams : { } ,
metamaskNetworkId : currentNetworkId ,
history : [ { } ] ,
} ,
{
id : 3 ,
2020-11-07 08:38:12 +01:00
status : TRANSACTION _STATUSES . SIGNED ,
2020-11-03 00:41:28 +01:00
txParams : { } ,
metamaskNetworkId : currentNetworkId ,
history : [ { } ] ,
} ,
{
id : 4 ,
2020-11-07 08:38:12 +01:00
status : TRANSACTION _STATUSES . SUBMITTED ,
2020-11-03 00:41:28 +01:00
txParams : { } ,
metamaskNetworkId : currentNetworkId ,
history : [ { } ] ,
} ,
{
id : 5 ,
2020-11-07 08:38:12 +01:00
status : TRANSACTION _STATUSES . CONFIRMED ,
2020-11-03 00:41:28 +01:00
txParams : { } ,
metamaskNetworkId : currentNetworkId ,
history : [ { } ] ,
} ,
{
id : 6 ,
2020-11-07 08:38:12 +01:00
status : TRANSACTION _STATUSES . FAILED ,
2020-11-03 00:41:28 +01:00
txParams : { } ,
metamaskNetworkId : currentNetworkId ,
history : [ { } ] ,
} ,
2021-02-04 19:15:23 +01:00
] ) ;
2017-09-23 01:15:18 +02:00
2018-06-26 01:16:51 +02:00
txController . once ( 'tx:status-update' , ( txId , status ) => {
try {
2020-11-07 08:38:12 +01:00
assert . equal (
status ,
TRANSACTION _STATUSES . REJECTED ,
'status should be rejected' ,
2021-02-04 19:15:23 +01:00
) ;
assert . equal ( txId , 0 , 'id should e 0' ) ;
done ( ) ;
2019-11-20 01:03:20 +01:00
} catch ( e ) {
2021-02-04 19:15:23 +01:00
done ( e ) ;
2019-11-20 01:03:20 +01:00
}
2021-02-04 19:15:23 +01:00
} ) ;
2018-06-26 01:16:51 +02:00
2021-02-04 19:15:23 +01:00
txController . cancelTransaction ( 0 ) ;
} ) ;
} ) ;
2017-09-23 01:15:18 +02:00
2020-02-11 17:51:13 +01:00
describe ( '#createSpeedUpTransaction' , function ( ) {
2021-02-04 19:15:23 +01:00
let addTxSpy ;
let approveTransactionSpy ;
let txParams ;
let expectedTxParams ;
2018-10-26 06:42:59 +02:00
2020-02-11 17:51:13 +01:00
beforeEach ( function ( ) {
2021-02-04 19:15:23 +01:00
addTxSpy = sinon . spy ( txController , 'addTx' ) ;
approveTransactionSpy = sinon . spy ( txController , 'approveTransaction' ) ;
2018-10-26 06:42:59 +02:00
txParams = {
nonce : '0x00' ,
from : '0xB09d8505E1F4EF1CeA089D47094f5DD3464083d4' ,
to : '0xB09d8505E1F4EF1CeA089D47094f5DD3464083d4' ,
gas : '0x5209' ,
gasPrice : '0xa' ,
2021-02-04 19:15:23 +01:00
} ;
2018-10-26 06:42:59 +02:00
txController . txStateManager . _saveTxList ( [
2020-11-03 00:41:28 +01:00
{
id : 1 ,
2020-11-07 08:38:12 +01:00
status : TRANSACTION _STATUSES . SUBMITTED ,
2020-11-03 00:41:28 +01:00
metamaskNetworkId : currentNetworkId ,
txParams ,
history : [ { } ] ,
} ,
2021-02-04 19:15:23 +01:00
] ) ;
2018-10-26 06:42:59 +02:00
2021-02-04 19:15:23 +01:00
expectedTxParams = { ... txParams , gasPrice : '0xb' } ;
} ) ;
2018-10-26 06:42:59 +02:00
2020-02-11 17:51:13 +01:00
afterEach ( function ( ) {
2021-02-04 19:15:23 +01:00
addTxSpy . restore ( ) ;
approveTransactionSpy . restore ( ) ;
} ) ;
2018-10-26 06:42:59 +02:00
2020-02-11 17:51:13 +01:00
it ( 'should call this.addTx and this.approveTransaction with the expected args' , async function ( ) {
2021-02-04 19:15:23 +01:00
await txController . createSpeedUpTransaction ( 1 ) ;
assert . equal ( addTxSpy . callCount , 1 ) ;
2018-10-26 06:42:59 +02:00
2021-02-04 19:15:23 +01:00
const addTxArgs = addTxSpy . getCall ( 0 ) . args [ 0 ] ;
assert . deepEqual ( addTxArgs . txParams , expectedTxParams ) ;
2018-10-26 06:42:59 +02:00
2021-02-04 19:15:23 +01:00
const { lastGasPrice , type } = addTxArgs ;
2020-11-03 00:41:28 +01:00
assert . deepEqual (
{ lastGasPrice , type } ,
{
lastGasPrice : '0xa' ,
2020-11-03 23:57:51 +01:00
type : TRANSACTION _TYPES . RETRY ,
2020-11-03 00:41:28 +01:00
} ,
2021-02-04 19:15:23 +01:00
) ;
} ) ;
2018-10-26 06:42:59 +02:00
2020-02-11 17:51:13 +01:00
it ( 'should call this.approveTransaction with the id of the returned tx' , async function ( ) {
2021-02-04 19:15:23 +01:00
const result = await txController . createSpeedUpTransaction ( 1 ) ;
assert . equal ( approveTransactionSpy . callCount , 1 ) ;
2018-10-26 06:42:59 +02:00
2021-02-04 19:15:23 +01:00
const approveTransactionArg = approveTransactionSpy . getCall ( 0 ) . args [ 0 ] ;
assert . equal ( result . id , approveTransactionArg ) ;
} ) ;
2018-10-26 06:42:59 +02:00
2020-02-11 17:51:13 +01:00
it ( 'should return the expected txMeta' , async function ( ) {
2021-02-04 19:15:23 +01:00
const result = await txController . createSpeedUpTransaction ( 1 ) ;
2018-10-26 06:42:59 +02:00
2021-02-04 19:15:23 +01:00
assert . deepEqual ( result . txParams , expectedTxParams ) ;
2018-10-26 06:42:59 +02:00
2021-02-04 19:15:23 +01:00
const { lastGasPrice , type } = result ;
2020-11-03 00:41:28 +01:00
assert . deepEqual (
{ lastGasPrice , type } ,
{
lastGasPrice : '0xa' ,
2020-11-03 23:57:51 +01:00
type : TRANSACTION _TYPES . RETRY ,
2020-11-03 00:41:28 +01:00
} ,
2021-02-04 19:15:23 +01:00
) ;
} ) ;
} ) ;
2018-10-26 06:42:59 +02:00
2017-09-23 01:15:18 +02:00
describe ( '#publishTransaction' , function ( ) {
2021-02-04 19:15:23 +01:00
let hash , txMeta ;
2017-09-23 01:15:18 +02:00
beforeEach ( function ( ) {
2020-11-03 00:41:28 +01:00
hash =
2021-02-04 19:15:23 +01:00
'0x2a5523c6fa98b47b7d9b6c8320179785150b42a16bcff36b398c5062b65657e8' ;
2017-09-23 01:15:18 +02:00
txMeta = {
id : 1 ,
2020-11-07 08:38:12 +01:00
status : TRANSACTION _STATUSES . UNAPPROVED ,
2017-09-23 01:15:18 +02:00
txParams : { } ,
metamaskNetworkId : currentNetworkId ,
2021-02-04 19:15:23 +01:00
} ;
providerResultStub . eth _sendRawTransaction = hash ;
} ) ;
2017-09-26 04:47:03 +02:00
2017-09-23 01:15:18 +02:00
it ( 'should publish a tx, updates the rawTx when provided a one' , async function ( ) {
2020-11-03 00:41:28 +01:00
const rawTx =
2021-02-04 19:15:23 +01:00
'0x477b2e6553c917af0db0388ae3da62965ff1a184558f61b749d1266b2e6d024c' ;
txController . txStateManager . addTx ( txMeta ) ;
await txController . publishTransaction ( txMeta . id , rawTx ) ;
const publishedTx = txController . txStateManager . getTx ( 1 ) ;
assert . equal ( publishedTx . hash , hash ) ;
assert . equal ( publishedTx . status , TRANSACTION _STATUSES . SUBMITTED ) ;
} ) ;
2019-10-30 22:40:33 +01:00
it ( 'should ignore the error "Transaction Failed: known transaction" and be as usual' , async function ( ) {
2020-08-19 18:27:05 +02:00
providerResultStub . eth _sendRawTransaction = async ( _ , _ _ , _ _ _ , end ) => {
2021-02-04 19:15:23 +01:00
end ( 'Transaction Failed: known transaction' ) ;
} ;
2020-11-03 00:41:28 +01:00
const rawTx =
2021-02-04 19:15:23 +01:00
'0xf86204831e848082520894f231d46dd78806e1dd93442cf33c7671f853874880802ca05f973e540f2d3c2f06d3725a626b75247593cb36477187ae07ecfe0a4db3cf57a00259b52ee8c58baaa385fb05c3f96116e58de89bcc165cb3bfdfc708672fed8a' ;
txController . txStateManager . addTx ( txMeta ) ;
await txController . publishTransaction ( txMeta . id , rawTx ) ;
const publishedTx = txController . txStateManager . getTx ( 1 ) ;
2020-11-03 00:41:28 +01:00
assert . equal (
publishedTx . hash ,
'0x2cc5a25744486f7383edebbf32003e5a66e18135799593d6b5cdd2bb43674f09' ,
2021-02-04 19:15:23 +01:00
) ;
assert . equal ( publishedTx . status , TRANSACTION _STATUSES . SUBMITTED ) ;
} ) ;
} ) ;
2017-09-23 01:15:18 +02:00
2018-03-13 22:30:59 +01:00
describe ( '#_markNonceDuplicatesDropped' , function ( ) {
it ( 'should mark all nonce duplicates as dropped without marking the confirmed transaction as dropped' , function ( ) {
txController . txStateManager . _saveTxList ( [
2020-11-03 00:41:28 +01:00
{
id : 1 ,
2020-11-07 08:38:12 +01:00
status : TRANSACTION _STATUSES . CONFIRMED ,
2020-11-03 00:41:28 +01:00
metamaskNetworkId : currentNetworkId ,
history : [ { } ] ,
txParams : { nonce : '0x01' } ,
} ,
{
id : 2 ,
2020-11-07 08:38:12 +01:00
status : TRANSACTION _STATUSES . SUBMITTED ,
2020-11-03 00:41:28 +01:00
metamaskNetworkId : currentNetworkId ,
history : [ { } ] ,
txParams : { nonce : '0x01' } ,
} ,
{
id : 3 ,
2020-11-07 08:38:12 +01:00
status : TRANSACTION _STATUSES . SUBMITTED ,
2020-11-03 00:41:28 +01:00
metamaskNetworkId : currentNetworkId ,
history : [ { } ] ,
txParams : { nonce : '0x01' } ,
} ,
{
id : 4 ,
2020-11-07 08:38:12 +01:00
status : TRANSACTION _STATUSES . SUBMITTED ,
2020-11-03 00:41:28 +01:00
metamaskNetworkId : currentNetworkId ,
history : [ { } ] ,
txParams : { nonce : '0x01' } ,
} ,
{
id : 5 ,
2020-11-07 08:38:12 +01:00
status : TRANSACTION _STATUSES . SUBMITTED ,
2020-11-03 00:41:28 +01:00
metamaskNetworkId : currentNetworkId ,
history : [ { } ] ,
txParams : { nonce : '0x01' } ,
} ,
{
id : 6 ,
2020-11-07 08:38:12 +01:00
status : TRANSACTION _STATUSES . SUBMITTED ,
2020-11-03 00:41:28 +01:00
metamaskNetworkId : currentNetworkId ,
history : [ { } ] ,
txParams : { nonce : '0x01' } ,
} ,
{
id : 7 ,
2020-11-07 08:38:12 +01:00
status : TRANSACTION _STATUSES . SUBMITTED ,
2020-11-03 00:41:28 +01:00
metamaskNetworkId : currentNetworkId ,
history : [ { } ] ,
txParams : { nonce : '0x01' } ,
} ,
2021-02-04 19:15:23 +01:00
] ) ;
txController . _markNonceDuplicatesDropped ( 1 ) ;
const confirmedTx = txController . txStateManager . getTx ( 1 ) ;
2020-11-03 00:41:28 +01:00
const droppedTxs = txController . txStateManager . getFilteredTxList ( {
nonce : '0x01' ,
2020-11-07 08:38:12 +01:00
status : TRANSACTION _STATUSES . DROPPED ,
2021-02-04 19:15:23 +01:00
} ) ;
2020-11-03 00:41:28 +01:00
assert . equal (
confirmedTx . status ,
2020-11-07 08:38:12 +01:00
TRANSACTION _STATUSES . CONFIRMED ,
2020-11-03 00:41:28 +01:00
'the confirmedTx should remain confirmed' ,
2021-02-04 19:15:23 +01:00
) ;
assert . equal ( droppedTxs . length , 6 , 'their should be 6 dropped txs' ) ;
} ) ;
} ) ;
2017-09-23 01:15:18 +02:00
2019-05-14 20:14:07 +02:00
describe ( '#_determineTransactionCategory' , function ( ) {
2020-04-20 17:01:00 +02:00
it ( 'should return a simple send transactionCategory when to is truthy but data is falsy' , async function ( ) {
2019-05-14 20:14:07 +02:00
const result = await txController . _determineTransactionCategory ( {
to : '0xabc' ,
data : '' ,
2021-02-04 19:15:23 +01:00
} ) ;
2020-11-03 00:41:28 +01:00
assert . deepEqual ( result , {
2020-11-03 23:57:51 +01:00
transactionCategory : TRANSACTION _CATEGORIES . SENT _ETHER ,
2020-11-03 00:41:28 +01:00
getCodeResponse : null ,
2021-02-04 19:15:23 +01:00
} ) ;
} ) ;
2019-05-14 20:14:07 +02:00
it ( 'should return a token transfer transactionCategory when data is for the respective method call' , async function ( ) {
const result = await txController . _determineTransactionCategory ( {
to : '0xabc' ,
2020-11-03 00:41:28 +01:00
data :
'0xa9059cbb0000000000000000000000002f318C334780961FB129D2a6c30D0763d9a5C970000000000000000000000000000000000000000000000000000000000000000a' ,
2021-02-04 19:15:23 +01:00
} ) ;
2020-11-03 00:41:28 +01:00
assert . deepEqual ( result , {
2020-11-03 23:57:51 +01:00
transactionCategory : TRANSACTION _CATEGORIES . TOKEN _METHOD _TRANSFER ,
2020-11-03 00:41:28 +01:00
getCodeResponse : undefined ,
2021-02-04 19:15:23 +01:00
} ) ;
} ) ;
2019-05-14 20:14:07 +02:00
it ( 'should return a token approve transactionCategory when data is for the respective method call' , async function ( ) {
const result = await txController . _determineTransactionCategory ( {
to : '0xabc' ,
2020-11-03 00:41:28 +01:00
data :
'0x095ea7b30000000000000000000000002f318C334780961FB129D2a6c30D0763d9a5C9700000000000000000000000000000000000000000000000000000000000000005' ,
2021-02-04 19:15:23 +01:00
} ) ;
2020-11-03 00:41:28 +01:00
assert . deepEqual ( result , {
2020-11-03 23:57:51 +01:00
transactionCategory : TRANSACTION _CATEGORIES . TOKEN _METHOD _APPROVE ,
2020-11-03 00:41:28 +01:00
getCodeResponse : undefined ,
2021-02-04 19:15:23 +01:00
} ) ;
} ) ;
2019-05-14 20:14:07 +02:00
2020-04-20 17:01:00 +02:00
it ( 'should return a contract deployment transactionCategory when to is falsy and there is data' , async function ( ) {
2019-05-14 20:14:07 +02:00
const result = await txController . _determineTransactionCategory ( {
to : '' ,
data : '0xabd' ,
2021-02-04 19:15:23 +01:00
} ) ;
2020-11-03 00:41:28 +01:00
assert . deepEqual ( result , {
2020-11-03 23:57:51 +01:00
transactionCategory : TRANSACTION _CATEGORIES . DEPLOY _CONTRACT ,
2020-11-03 00:41:28 +01:00
getCodeResponse : undefined ,
2021-02-04 19:15:23 +01:00
} ) ;
} ) ;
2019-05-14 20:14:07 +02:00
it ( 'should return a simple send transactionCategory with a 0x getCodeResponse when there is data and but the to address is not a contract address' , async function ( ) {
const result = await txController . _determineTransactionCategory ( {
to : '0x9e673399f795D01116e9A8B2dD2F156705131ee9' ,
data : '0xabd' ,
2021-02-04 19:15:23 +01:00
} ) ;
2020-11-03 00:41:28 +01:00
assert . deepEqual ( result , {
2020-11-03 23:57:51 +01:00
transactionCategory : TRANSACTION _CATEGORIES . SENT _ETHER ,
2020-11-03 00:41:28 +01:00
getCodeResponse : '0x' ,
2021-02-04 19:15:23 +01:00
} ) ;
} ) ;
2019-05-14 20:14:07 +02:00
it ( 'should return a simple send transactionCategory with a null getCodeResponse when to is truthy and there is data and but getCode returns an error' , async function ( ) {
const result = await txController . _determineTransactionCategory ( {
to : '0xabc' ,
data : '0xabd' ,
2021-02-04 19:15:23 +01:00
} ) ;
2020-11-03 00:41:28 +01:00
assert . deepEqual ( result , {
2020-11-03 23:57:51 +01:00
transactionCategory : TRANSACTION _CATEGORIES . SENT _ETHER ,
2020-11-03 00:41:28 +01:00
getCodeResponse : null ,
2021-02-04 19:15:23 +01:00
} ) ;
} ) ;
2019-05-14 20:14:07 +02:00
it ( 'should return a contract interaction transactionCategory with the correct getCodeResponse when to is truthy and there is data and it is not a token transaction' , async function ( ) {
const _providerResultStub = {
// 1 gwei
eth _gasPrice : '0x0de0b6b3a7640000' ,
// by default, all accounts are external accounts (not contracts)
eth _getCode : '0xa' ,
2021-02-04 19:15:23 +01:00
} ;
2020-11-03 00:41:28 +01:00
const _provider = createTestProviderTools ( {
scaffold : _providerResultStub ,
2021-02-04 19:15:23 +01:00
} ) . provider ;
const _fromAccount = getTestAccounts ( ) [ 0 ] ;
const _blockTrackerStub = new EventEmitter ( ) ;
_blockTrackerStub . getCurrentBlock = noop ;
_blockTrackerStub . getLatestBlock = noop ;
2019-05-14 20:14:07 +02:00
const _txController = new TransactionController ( {
provider : _provider ,
2020-11-03 00:41:28 +01:00
getGasPrice ( ) {
2021-02-04 19:15:23 +01:00
return '0xee6b2800' ;
2019-11-20 01:03:20 +01:00
} ,
2019-05-14 20:14:07 +02:00
networkStore : new ObservableStore ( currentNetworkId ) ,
2021-03-01 16:15:42 +01:00
getCurrentChainId : ( ) => currentChainId ,
2019-05-14 20:14:07 +02:00
txHistoryLimit : 10 ,
blockTracker : _blockTrackerStub ,
2020-11-03 00:41:28 +01:00
signTransaction : ( ethTx ) =>
new Promise ( ( resolve ) => {
2021-02-04 19:15:23 +01:00
ethTx . sign ( _fromAccount . key ) ;
resolve ( ) ;
2020-11-03 00:41:28 +01:00
} ) ,
2020-10-08 18:41:23 +02:00
getParticipateInMetrics : ( ) => false ,
2021-02-04 19:15:23 +01:00
} ) ;
2019-05-14 20:14:07 +02:00
const result = await _txController . _determineTransactionCategory ( {
to : '0x9e673399f795D01116e9A8B2dD2F156705131ee9' ,
data : 'abd' ,
2021-02-04 19:15:23 +01:00
} ) ;
2020-11-03 00:41:28 +01:00
assert . deepEqual ( result , {
2020-11-03 23:57:51 +01:00
transactionCategory : TRANSACTION _CATEGORIES . CONTRACT _INTERACTION ,
2020-11-03 00:41:28 +01:00
getCodeResponse : '0x0a' ,
2021-02-04 19:15:23 +01:00
} ) ;
} ) ;
2019-10-07 21:29:37 +02:00
2020-04-20 17:01:00 +02:00
it ( 'should return a contract interaction transactionCategory with the correct getCodeResponse when to is a contract address and data is falsy' , async function ( ) {
2019-10-07 21:29:37 +02:00
const _providerResultStub = {
// 1 gwei
eth _gasPrice : '0x0de0b6b3a7640000' ,
// by default, all accounts are external accounts (not contracts)
eth _getCode : '0xa' ,
2021-02-04 19:15:23 +01:00
} ;
2020-11-03 00:41:28 +01:00
const _provider = createTestProviderTools ( {
scaffold : _providerResultStub ,
2021-02-04 19:15:23 +01:00
} ) . provider ;
const _fromAccount = getTestAccounts ( ) [ 0 ] ;
const _blockTrackerStub = new EventEmitter ( ) ;
_blockTrackerStub . getCurrentBlock = noop ;
_blockTrackerStub . getLatestBlock = noop ;
2019-10-07 21:29:37 +02:00
const _txController = new TransactionController ( {
provider : _provider ,
2020-11-03 00:41:28 +01:00
getGasPrice ( ) {
2021-02-04 19:15:23 +01:00
return '0xee6b2800' ;
2019-11-20 01:03:20 +01:00
} ,
2019-10-07 21:29:37 +02:00
networkStore : new ObservableStore ( currentNetworkId ) ,
2021-03-01 16:15:42 +01:00
getCurrentChainId : ( ) => currentChainId ,
2019-10-07 21:29:37 +02:00
txHistoryLimit : 10 ,
blockTracker : _blockTrackerStub ,
2020-11-03 00:41:28 +01:00
signTransaction : ( ethTx ) =>
new Promise ( ( resolve ) => {
2021-02-04 19:15:23 +01:00
ethTx . sign ( _fromAccount . key ) ;
resolve ( ) ;
2020-11-03 00:41:28 +01:00
} ) ,
2020-10-08 18:41:23 +02:00
getParticipateInMetrics : ( ) => false ,
2021-02-04 19:15:23 +01:00
} ) ;
2019-10-07 21:29:37 +02:00
const result = await _txController . _determineTransactionCategory ( {
to : '0x9e673399f795D01116e9A8B2dD2F156705131ee9' ,
data : '' ,
2021-02-04 19:15:23 +01:00
} ) ;
2020-11-03 00:41:28 +01:00
assert . deepEqual ( result , {
2020-11-03 23:57:51 +01:00
transactionCategory : TRANSACTION _CATEGORIES . CONTRACT _INTERACTION ,
2020-11-03 00:41:28 +01:00
getCodeResponse : '0x0a' ,
2021-02-04 19:15:23 +01:00
} ) ;
} ) ;
} ) ;
2019-05-14 20:14:07 +02:00
2017-09-23 01:15:18 +02:00
describe ( '#getPendingTransactions' , function ( ) {
2020-04-20 17:01:00 +02:00
it ( 'should show only submitted and approved transactions as pending transaction' , function ( ) {
2017-09-23 01:15:18 +02:00
txController . txStateManager . _saveTxList ( [
2020-11-03 00:41:28 +01:00
{
id : 1 ,
2020-11-07 08:38:12 +01:00
status : TRANSACTION _STATUSES . UNAPPROVED ,
2020-11-03 00:41:28 +01:00
metamaskNetworkId : currentNetworkId ,
txParams : { } ,
} ,
{
id : 2 ,
2020-11-07 08:38:12 +01:00
status : TRANSACTION _STATUSES . REJECTED ,
2020-11-03 00:41:28 +01:00
metamaskNetworkId : currentNetworkId ,
txParams : { } ,
history : [ { } ] ,
} ,
{
id : 3 ,
2020-11-07 08:38:12 +01:00
status : TRANSACTION _STATUSES . APPROVED ,
2020-11-03 00:41:28 +01:00
metamaskNetworkId : currentNetworkId ,
txParams : { } ,
history : [ { } ] ,
} ,
{
id : 4 ,
2020-11-07 08:38:12 +01:00
status : TRANSACTION _STATUSES . SIGNED ,
2020-11-03 00:41:28 +01:00
metamaskNetworkId : currentNetworkId ,
txParams : { } ,
history : [ { } ] ,
} ,
{
id : 5 ,
2020-11-07 08:38:12 +01:00
status : TRANSACTION _STATUSES . SUBMITTED ,
2020-11-03 00:41:28 +01:00
metamaskNetworkId : currentNetworkId ,
txParams : { } ,
history : [ { } ] ,
} ,
{
id : 6 ,
2020-11-07 08:38:12 +01:00
status : TRANSACTION _STATUSES . CONFIRMED ,
2020-11-03 00:41:28 +01:00
metamaskNetworkId : currentNetworkId ,
txParams : { } ,
history : [ { } ] ,
} ,
{
id : 7 ,
2020-11-07 08:38:12 +01:00
status : TRANSACTION _STATUSES . FAILED ,
2020-11-03 00:41:28 +01:00
metamaskNetworkId : currentNetworkId ,
txParams : { } ,
history : [ { } ] ,
} ,
2021-02-04 19:15:23 +01:00
] ) ;
2020-02-11 17:51:13 +01:00
2020-11-03 00:41:28 +01:00
assert . equal (
txController . pendingTxTracker . getPendingTransactions ( ) . length ,
2 ,
2021-02-04 19:15:23 +01:00
) ;
2020-11-03 00:41:28 +01:00
const states = txController . pendingTxTracker
. getPendingTransactions ( )
2021-02-04 19:15:23 +01:00
. map ( ( tx ) => tx . status ) ;
2020-11-07 08:38:12 +01:00
assert . ok (
states . includes ( TRANSACTION _STATUSES . APPROVED ) ,
'includes approved' ,
2021-02-04 19:15:23 +01:00
) ;
2020-11-07 08:38:12 +01:00
assert . ok (
states . includes ( TRANSACTION _STATUSES . SUBMITTED ) ,
'includes submitted' ,
2021-02-04 19:15:23 +01:00
) ;
} ) ;
} ) ;
} ) ;