Skip to content

Commit

Permalink
Merge pull request #2511 from ethereum/issue/2441
Browse files Browse the repository at this point in the history
Transaction revert handling improved/fixed
  • Loading branch information
nivida committed Mar 18, 2019
2 parents 99732d8 + d4f922f commit 156f209
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 25 deletions.
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

0 comments on commit 156f209

Please sign in to comment.