Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Transaction revert handling improved/fixed #2511

Merged
merged 5 commits into from
Mar 18, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -81,27 +81,27 @@ export default class AbstractObservedTransactionMethod extends AbstractMethod {
receipt = transactionConfirmation.receipt;

if (this.hasRevertReceiptStatus(receipt)) {
this.handleError(
new Error(
`Transaction has been reverted by the EVM:\n${JSON.stringify(receipt, null, 2)}`
),
receipt,
confirmations
);
if (this.parameters[0].gas === receipt.gasUsed) {
this.handleError(
new Error(
`Transaction ran out of gas. Please provide more gas:\n${JSON.stringify(
receipt,
null,
2
)}`
),
receipt,
confirmations
);

transactionConfirmationSubscription.unsubscribe();

return;
}

transactionConfirmationSubscription.unsubscribe();

return;
}

if (receipt.outOfGas) {
this.handleError(
new Error(
`Transaction ran out of gas. Please provide more gas:\n${JSON.stringify(
receipt,
null,
2
)}`
`Transaction has been reverted by the EVM:\n${JSON.stringify(receipt, null, 2)}`
),
receipt,
confirmations
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,36 +152,40 @@ describe('AbstractObservedTransactionMethodTest', () => {
providerMock.send.mockReturnValueOnce(Promise.resolve('transactionHash'));

observableMock.subscribe = jest.fn((next, error, complete) => {
next({count: 0, receipt: {status: '0x0'}});
next({count: 0, receipt: {status: '0x0', gasUsed: 1}});

complete();
});

method.parameters = [{gas: 0}];

await expect(method.execute()).rejects.toThrow(
`Transaction has been reverted by the EVM:\n${JSON.stringify({status: '0x0'}, null, 2)}`
`Transaction has been reverted by the EVM:\n${JSON.stringify({status: '0x0', gasUsed: 1}, null, 2)}`
);

expect(providerMock.send).toHaveBeenCalledWith('rpcMethod', []);
expect(providerMock.send).toHaveBeenCalledWith('rpcMethod', method.parameters);
});

it('calls execute and returns a rejected Promise because the transaction ran out of gas', async () => {
providerMock.send.mockReturnValueOnce(Promise.resolve('transactionHash'));

observableMock.subscribe = jest.fn((next, error, complete) => {
next({count: 0, receipt: {status: '0x1', outOfGas: true}});
next({count: 0, receipt: {status: '0x0', gasUsed: 1}});

complete();
});

method.parameters = [{gas: 1}];

await expect(method.execute()).rejects.toThrow(
`Transaction ran out of gas. Please provide more gas:\n${JSON.stringify(
{status: '0x1', outOfGas: true},
{status: '0x0', gasUsed: 1},
null,
2
)}`
);

expect(providerMock.send).toHaveBeenCalledWith('rpcMethod', []);
expect(providerMock.send).toHaveBeenCalledWith('rpcMethod', method.parameters);
});

it('calls execute and calls the given callback with the transaction hash', (done) => {
Expand All @@ -192,7 +196,7 @@ describe('AbstractObservedTransactionMethodTest', () => {

expect(transactionHash).toEqual('transactionHash');

expect(providerMock.send).toHaveBeenCalledWith('rpcMethod', []);
expect(providerMock.send).toHaveBeenCalledWith('rpcMethod', method.parameters);

expect(beforeExecutionMock).toHaveBeenCalledWith(moduleInstanceMock);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

import {CallMethod} from 'web3-core-method';

// TODO: Implement revert handling (AbstractContractMethod)
export default class CallContractMethod extends CallMethod {
/**
* @param {Utils} utils
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import isArray from 'lodash/isArray';
import {EthSendTransactionMethod} from 'web3-core-method';

// TODO: Implement revert handling (AbstractContractMethod)
export default class SendContractMethod extends EthSendTransactionMethod {
/**
* @param {Utils} utils
Expand Down