Skip to content

Commit c748aaf

Browse files
authored
Merge pull request #27 from NodeFactoryIo/mpetrun5/failed-resubmit-deletes-transaction
Avoid deleting transaction from storage if resubmiting fails
2 parents f906c27 + 121de52 commit c748aaf

File tree

5 files changed

+68
-12
lines changed

5 files changed

+68
-12
lines changed

CHANGELOG.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,16 @@
11
# Changelog
22

3+
## [v1.1.2](https://github.com/NodeFactoryIo/web3-server-wallet/tree/v1.1.2)
4+
5+
[Full Changelog](https://github.com/NodeFactoryIo/web3-server-wallet/compare/v1.1.1...v.1.1.2)
6+
7+
### Fixed
8+
- Avoid deleting transaction from storage if resubmiting fails [\#27](https://github.com/NodeFactoryIo/web3-server-wallet/pull/27) ([mpetrun5](https://github.com/mpetrun5))
9+
10+
### Added
11+
12+
### Changed
13+
314
## [v1.1.1](https://github.com/NodeFactoryIo/web3-server-wallet/tree/v1.1.1)
415

516
[Full Changelog](https://github.com/NodeFactoryIo/web3-server-wallet/compare/v1.1.0...v.1.1.1)

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@nodefactory/web3-server-wallet",
3-
"version": "1.1.1",
3+
"version": "1.1.2",
44
"main": "dist/index.js",
55
"types": "dist/index.d.ts",
66
"publishConfig": {
@@ -47,6 +47,7 @@
4747
"@types/chai": "^4.2.4",
4848
"@types/debug": "^4.1.5",
4949
"@types/mocha": "^5.2.7",
50+
"@types/sinon": "^9.0.11",
5051
"@typescript-eslint/eslint-plugin": "^2.6.0",
5152
"@typescript-eslint/parser": "^2.6.0",
5253
"bsert": "0.0.10",

src/monitorService.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,12 +92,12 @@ export class TxMonitorService {
9292
chainId: transaction.chainId,
9393
gasPrice: newGasPrice
9494
});
95+
96+
this.logger.debug(`Deleting transaction ${transaction.hash} from storage`);
97+
await this.wallet.walletStorage.deleteTransaction(transaction.hash);
9598
} catch(error) {
9699
this.logger.error(`Resending transaction with hash ${transaction.hash} failed, ${error.message}`);
97100
}
98-
99-
this.logger.debug(`Deleting transaction ${transaction.hash} from storage`);
100-
await this.wallet.walletStorage.deleteTransaction(transaction.hash);
101101
}
102102

103103

test/monitorService.test.ts

Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import {expect} from "chai";
2-
import sinon, {SinonStubbedInstance} from "sinon";
2+
import sinon, {SinonSpy, SinonStubbedInstance} from "sinon";
33
import {ServerWeb3Wallet} from "../src/serverWallet";
44
import {TxMonitorService} from "../src/monitorService";
55
import {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()

yarn.lock

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1270,6 +1270,18 @@
12701270
resolved "https://registry.yarnpkg.com/@types/node/-/node-14.0.12.tgz#9c1d8ffb8084e8936603a6122a7649e40e68e04b"
12711271
integrity sha512-/sjzehvjkkpvLpYtN6/2dv5kg41otMGuHQUt9T2aiAuIfleCQRQHXXzF1eAw/qkZTj5Kcf4JSTf7EIizHocy6Q==
12721272

1273+
"@types/sinon@^9.0.11":
1274+
version "9.0.11"
1275+
resolved "https://registry.yarnpkg.com/@types/sinon/-/sinon-9.0.11.tgz#7af202dda5253a847b511c929d8b6dda170562eb"
1276+
integrity sha512-PwP4UY33SeeVKodNE37ZlOsR9cReypbMJOhZ7BVE0lB+Hix3efCOxiJWiE5Ia+yL9Cn2Ch72EjFTRze8RZsNtg==
1277+
dependencies:
1278+
"@types/sinonjs__fake-timers" "*"
1279+
1280+
"@types/sinonjs__fake-timers@*":
1281+
version "6.0.2"
1282+
resolved "https://registry.yarnpkg.com/@types/sinonjs__fake-timers/-/sinonjs__fake-timers-6.0.2.tgz#3a84cf5ec3249439015e14049bd3161419bf9eae"
1283+
integrity sha512-dIPoZ3g5gcx9zZEszaxLSVTvMReD3xxyyDnQUjA6IYDG9Ba2AV0otMPs+77sG9ojB4Qr2N2Vk5RnKeuA0X/0bg==
1284+
12731285
"@typescript-eslint/eslint-plugin@^2.6.0":
12741286
version "2.34.0"
12751287
resolved "https://registry.yarnpkg.com/@typescript-eslint/eslint-plugin/-/eslint-plugin-2.34.0.tgz#6f8ce8a46c7dea4a6f1d171d2bb8fbae6dac2be9"

0 commit comments

Comments
 (0)