11import { expect } from "chai" ;
2- import sinon , { SinonStubbedInstance } from "sinon" ;
2+ import sinon , { SinonSpy , SinonStubbedInstance } from "sinon" ;
33import { ServerWeb3Wallet } from "../src/serverWallet" ;
44import { TxMonitorService } from "../src/monitorService" ;
55import { IWalletTransactionStorage , SavedTransactionResponse } from "../src/@types/wallet" ;
@@ -12,17 +12,20 @@ describe("Transaction monitor service", function () {
1212 let txMonitorService : TxMonitorService ;
1313 let walletStorage : IWalletTransactionStorage ;
1414 let providerStub : providers . Provider ;
15+ let deleteTransactionSpy : SinonSpy ;
1516
1617 beforeEach ( function ( ) {
1718 web3WalletStub = sinon . createStubInstance ( ServerWeb3Wallet ) ;
18- walletStorage = sinon . stub ( ) as IWalletTransactionStorage ;
19+ walletStorage = sinon . stub ( ) as unknown as IWalletTransactionStorage ;
1920 walletStorage . deleteTransaction = async function deleteTransaction ( hash : string ) {
2021 return ;
2122 }
22- providerStub = sinon . stub ( ) as providers . Provider ;
23+ deleteTransactionSpy = sinon . spy ( walletStorage , "deleteTransaction" ) ;
24+ providerStub = sinon . stub ( ) as unknown as providers . Provider ;
25+ // @ts -ignore
2326 web3WalletStub . provider = providerStub ;
2427 web3WalletStub . walletStorage = walletStorage ;
25- txMonitorService = new TxMonitorService ( web3WalletStub ) ;
28+ txMonitorService = new TxMonitorService ( web3WalletStub as unknown as ServerWeb3Wallet ) ;
2629 } ) ;
2730
2831 afterEach ( function ( ) {
@@ -71,29 +74,33 @@ describe("Transaction monitor service", function () {
7174 return [ transaction ] as SavedTransactionResponse [ ] ;
7275 }
7376 web3WalletStub . provider . getTransaction = async ( ) => {
74- return transaction ;
77+ return transaction as providers . TransactionResponse ;
7578 }
7679 sinon . stub ( utils , "recalculateGasPrice" ) . resolves ( BigNumber . from ( 12.0 ) )
7780 const spy = sinon . spy ( utils , "transactionIsOld" ) ;
7881
7982 txMonitorService . start ( 20 ) ;
8083
8184 setTimeout ( ( ) => {
85+ expect ( deleteTransactionSpy . callCount ) . to . be . deep . equal ( 1 ) ;
8286 expect ( spy . callCount ) . to . be . deep . equal ( 0 ) ;
8387 done ( ) ;
8488 } , 30 )
8589
8690 } ) ;
8791
88- it ( "Check transaction ignores other transactions after resending" , function ( done ) {
92+ it (
93+ "Check transaction ignores other transactions after resending and deletes transaction if resubmit successful" ,
94+ function ( done )
95+ {
8996 walletStorage . getTransactions = async function getTransactions ( ) {
9097 return [
9198 { nonce : 1 , gasPrice : BigNumber . from ( 12 ) , submitTime : new Date ( ) . getTime ( ) - 300000 } as SavedTransactionResponse ,
9299 { nonce : 2 , gasPrice : BigNumber . from ( 12 ) , submitTime : new Date ( ) . getTime ( ) - 300000 } as SavedTransactionResponse ,
93100 ] ;
94101 }
95102 web3WalletStub . provider . getTransaction = async ( ) => {
96- return { } ;
103+ return { } as providers . TransactionResponse ;
97104 }
98105 sinon . stub ( utils , "recalculateGasPrice" ) . resolves ( BigNumber . from ( 12.0 ) )
99106 const stub = web3WalletStub . sendTransaction . resolves ( )
@@ -102,6 +109,31 @@ describe("Transaction monitor service", function () {
102109
103110 setTimeout ( ( ) => {
104111 expect ( stub . callCount ) . to . be . deep . equal ( 1 ) ;
112+ expect ( deleteTransactionSpy . callCount ) . to . be . deep . equal ( 1 ) ;
113+ done ( ) ;
114+ } , 30 )
115+
116+ } ) ;
117+
118+ it ( "Check transaction does not delete transaction if resubmiting fails" , function ( done ) {
119+ walletStorage . getTransactions = async function getTransactions ( ) {
120+ return [
121+ { nonce : 1 , gasPrice : BigNumber . from ( 12 ) , submitTime : new Date ( ) . getTime ( ) - 300000 } as SavedTransactionResponse ,
122+ ] ;
123+ }
124+ web3WalletStub . provider . getTransaction = async ( ) => {
125+ return { } as providers . TransactionResponse ;
126+ }
127+ sinon . stub ( utils , "recalculateGasPrice" ) . resolves ( BigNumber . from ( 12.0 ) )
128+ const stub = web3WalletStub . sendTransaction . callsFake ( ( ) => {
129+ throw new Error ( "Error" ) ;
130+ } )
131+
132+ txMonitorService . start ( 20 ) ;
133+
134+ setTimeout ( ( ) => {
135+ expect ( stub . callCount ) . to . be . deep . equal ( 1 ) ;
136+ expect ( deleteTransactionSpy . callCount ) . to . be . deep . equal ( 0 ) ;
105137 done ( ) ;
106138 } , 30 )
107139
@@ -115,7 +147,7 @@ describe("Transaction monitor service", function () {
115147 ] ;
116148 }
117149 web3WalletStub . provider . getTransaction = async ( ) => {
118- return { } ;
150+ return { } as providers . TransactionResponse ;
119151 }
120152 sinon . stub ( utils , "recalculateGasPrice" ) . resolves ( BigNumber . from ( 12.0 ) )
121153 const sendTransactionStub = web3WalletStub . sendTransaction . resolves ( )
0 commit comments