Merge branch '1.0' into debug-methods

This commit is contained in:
Prince Sinha
2019-04-18 08:33:43 +05:30
committed by GitHub
13 changed files with 115 additions and 34 deletions
@@ -112,7 +112,11 @@ export default class AbstractObservedTransactionMethod extends AbstractMethod {
return;
}
this.promiEvent.emit('confirmation', confirmations, this.afterExecution(receipt));
this.promiEvent.emit(
'confirmation',
confirmations,
this.formatters.outputTransactionFormatter(receipt)
);
},
(error) => {
this.handleError(error, receipt, confirmations);
@@ -34,4 +34,17 @@ export default class SendRawTransactionMethod extends AbstractObservedTransactio
constructor(utils, formatters, moduleInstance, transactionObserver) {
super('eth_sendRawTransaction', 1, utils, formatters, moduleInstance, transactionObserver);
}
/**
* This method will be executed after the RPC request.
*
* @method afterExecution
*
* @param {Object} response
*
* @returns {Object}
*/
afterExecution(response) {
return this.formatters.outputTransactionFormatter(response);
}
}
@@ -45,4 +45,17 @@ export default class SendTransactionMethod extends AbstractObservedTransactionMe
beforeExecution(moduleInstance) {
this.parameters[0] = this.formatters.inputTransactionFormatter(this.parameters[0], moduleInstance);
}
/**
* This method will be executed after the RPC request.
*
* @method afterExecution
*
* @param {Object} response
*
* @returns {Object}
*/
afterExecution(response) {
return this.formatters.outputTransactionFormatter(response);
}
}
@@ -150,7 +150,7 @@ export default class TransactionObserver {
if (receipt) {
if (this.lastBlock) {
const block = await this.getBlockByNumber(this.increaseBlockNumber(this.lastBlock.number));
const block = await this.getBlockByNumber(this.lastBlock.number + 1);
if (block && this.isValidConfirmation(block)) {
this.lastBlock = block;
@@ -164,8 +164,8 @@ export default class TransactionObserver {
}
if (this.isConfirmed()) {
clearInterval(interval);
observer.complete();
clearInterval(interval);
}
}
@@ -269,17 +269,4 @@ export default class TransactionObserver {
isTimeoutTimeExceeded() {
return this.confirmationChecks === this.timeout;
}
/**
* Increases the blockNumber hash by one.
*
* @method increaseBlockNumber
*
* @param {String} blockNumber
*
* @returns {String}
*/
increaseBlockNumber(blockNumber) {
return '0x' + (parseInt(blockNumber, 16) + 1).toString(16);
}
}
@@ -1,8 +1,10 @@
import {formatters} from 'web3-core-helpers';
import PromiEvent from '../../../../lib/PromiEvent';
import TransactionObserver from '../../../../src/observers/TransactionObserver';
import AbstractObservedTransactionMethod from '../../../../lib/methods/transaction/AbstractObservedTransactionMethod';
// Mocks
jest.mock('web3-core-helpers');
jest.mock('../../../../src/observers/TransactionObserver');
/**
@@ -43,7 +45,7 @@ describe('AbstractObservedTransactionMethodTest', () => {
'rpcMethod',
5,
{},
{},
formatters,
moduleInstanceMock,
transactionObserverMock
);
@@ -68,6 +70,8 @@ describe('AbstractObservedTransactionMethodTest', () => {
it('calls execute with event listeners and is emitting the expected values', (done) => {
providerMock.send.mockReturnValueOnce(Promise.resolve('transactionHash'));
formatters.outputTransactionFormatter.mockReturnValue({status: false});
observableMock.subscribe = jest.fn((next, error, complete) => {
next({confirmations: 0, receipt: {status: true}});
@@ -88,7 +92,11 @@ describe('AbstractObservedTransactionMethodTest', () => {
expect(transactionHashCallback).toHaveBeenCalledWith('transactionHash');
expect(confirmationCallback).toHaveBeenCalledWith(0, {status: true});
expect(confirmationCallback).toHaveBeenCalledWith(0, {status: false});
expect(formatters.outputTransactionFormatter).toHaveBeenNthCalledWith(1, {status: true});
expect(formatters.outputTransactionFormatter).toHaveBeenCalledTimes(1);
done();
});
@@ -1,5 +1,9 @@
import AbstractObservedTransactionMethod from '../../../../lib/methods/transaction/AbstractObservedTransactionMethod';
import SendRawTransactionMethod from '../../../../src/methods/transaction/SendRawTransactionMethod';
import {formatters} from 'web3-core-helpers';
// Mocks
jest.mock('web3-core-helpers');
/**
* SendRawTransactionMethod test
@@ -8,7 +12,7 @@ describe('SendRawTransactionMethodTest', () => {
let method;
beforeEach(() => {
method = new SendRawTransactionMethod(null, null, null, {});
method = new SendRawTransactionMethod(null, formatters, null, {});
});
it('constructor check', () => {
@@ -16,4 +20,12 @@ describe('SendRawTransactionMethodTest', () => {
expect(method.rpcMethod).toEqual('eth_sendRawTransaction');
});
it('calls afterExecution and returns the expected value', () => {
formatters.outputTransactionFormatter.mockReturnValueOnce({status: true});
expect(method.afterExecution({status: false})).toEqual({status: true});
expect(formatters.outputTransactionFormatter).toHaveBeenCalledWith({status: false});
});
});
@@ -6,9 +6,9 @@ import {formatters} from 'web3-core-helpers';
jest.mock('web3-core-helpers');
/**
* SendRawTransactionMethod test
* SendTransactionMethod test
*/
describe('SendRawTransactionMethodTest', () => {
describe('SendTransactionMethodTest', () => {
let method;
beforeEach(() => {
@@ -32,4 +32,12 @@ describe('SendRawTransactionMethodTest', () => {
expect(formatters.inputTransactionFormatter).toHaveBeenCalledWith('tx', {});
});
it('calls afterExecution and returns the expected value', () => {
formatters.outputTransactionFormatter.mockReturnValueOnce({status: true});
expect(method.afterExecution({status: false})).toEqual({status: true});
expect(formatters.outputTransactionFormatter).toHaveBeenCalledWith({status: false});
});
});
@@ -189,9 +189,9 @@ describe('TransactionObserverTest', () => {
providerMock.supportsSubscriptions.mockReturnValueOnce(false);
const receipt = {blockNumber: '0xa'};
const blockOne = {number: '0xa', hash: '0x0'};
const blockTwo = {number: '0xc', parentHash: '0x0'};
const receipt = {blockNumber: 1};
const blockOne = {number: 1, hash: '0x0'};
const blockTwo = {number: 2, parentHash: '0x0'};
getTransactionReceiptMethodMock.execute
.mockReturnValueOnce(Promise.resolve(receipt))
@@ -222,7 +222,7 @@ describe('TransactionObserverTest', () => {
expect(getTransactionReceiptMethodMock.parameters).toEqual(['transactionHash']);
expect(getBlockByNumberMethodMock.parameters).toEqual(['0xb']);
expect(getBlockByNumberMethodMock.parameters).toEqual([2]);
done();
}
@@ -235,8 +235,8 @@ describe('TransactionObserverTest', () => {
providerMock.supportsSubscriptions.mockReturnValueOnce(false);
const receipt = {blockNumber: '0xa'};
const blockOne = {number: '0xa', hash: '0x0'};
const receipt = {blockNumber: 1};
const blockOne = {number: 1, hash: '0x0'};
getTransactionReceiptMethodMock.execute.mockReturnValueOnce(Promise.resolve(receipt));
@@ -65,7 +65,7 @@ export default class ContractDeployMethod extends EthSendTransactionMethod {
clonedContract.address = response.contractAddress;
if (this.promiEvent.listenerCount('receipt') > 0) {
this.promiEvent.emit('receipt', response);
this.promiEvent.emit('receipt', super.afterExecution(response));
this.promiEvent.removeAllListeners('receipt');
}
@@ -93,6 +93,6 @@ export default class SendContractMethod extends EthSendTransactionMethod {
delete response.logs;
}
return response;
return super.afterExecution(response);
}
}
@@ -1,3 +1,4 @@
import {formatters} from 'web3-core-helpers';
import {EthSendTransactionMethod} from 'web3-core-method';
import AbiModel from '../../../src/models/AbiModel';
import AllEventsLogDecoder from '../../../src/decoders/AllEventsLogDecoder';
@@ -7,6 +8,7 @@ import SendContractMethod from '../../../src/methods/SendContractMethod';
jest.mock('../../../src/decoders/AllEventsLogDecoder');
jest.mock('../../../src/models/AbiItemModel');
jest.mock('../../../src/models/AbiModel');
jest.mock('web3-core-helpers');
/**
* SendContractMethod test
@@ -21,7 +23,7 @@ describe('SendContractMethodTest', () => {
new AllEventsLogDecoder();
allEventsLogDecoderMock = AllEventsLogDecoder.mock.instances[0];
sendContractMethod = new SendContractMethod({}, {}, {}, {}, {}, {}, allEventsLogDecoderMock, abiModelMock);
sendContractMethod = new SendContractMethod({}, formatters, {}, {}, {}, {}, allEventsLogDecoderMock, abiModelMock);
});
it('constructor check', () => {
@@ -58,11 +60,28 @@ describe('SendContractMethodTest', () => {
allEventsLogDecoderMock.decode.mockReturnValueOnce({event: 'MyEvent'});
formatters.outputTransactionFormatter.mockReturnValueOnce({
events: {
0: {event: true},
MyEvent: [
{
event: 'MyEvent'
},
{
event: 'MyEvent'
},
{
event: 'MyEvent'
}
]
}
});
const mappedResponse = sendContractMethod.afterExecution(response);
expect(mappedResponse).toEqual({
events: {
0: {event: false},
0: {event: true},
MyEvent: [
{
event: 'MyEvent'
@@ -84,5 +103,22 @@ describe('SendContractMethodTest', () => {
expect(allEventsLogDecoderMock.decode).toHaveBeenNthCalledWith(3, abiModelMock, {event: 'MyEvent'});
expect(allEventsLogDecoderMock.decode).toHaveBeenNthCalledWith(4, abiModelMock, {event: 'MyEvent'});
expect(formatters.outputTransactionFormatter).toHaveBeenCalledWith({
events: {
0: {event: false},
MyEvent: [
{
event: 'MyEvent'
},
{
event: 'MyEvent'
},
{
event: 'MyEvent'
}
]
}
});
});
});
+2 -2
View File
@@ -195,7 +195,7 @@ export const fromWei = (number, unit) => {
unit = getUnitValue(unit);
if (!utils.isBN(number) && !isString(number)) {
throw new Error('Please pass numbers as strings or BigNumber objects to avoid precision errors.');
throw new Error('Please pass numbers as strings or BN objects to avoid precision errors.');
}
return utils.isBN(number) ? ethjsUnit.fromWei(number, unit) : ethjsUnit.fromWei(number, unit).toString(10);
@@ -229,7 +229,7 @@ export const toWei = (number, unit) => {
unit = getUnitValue(unit);
if (!utils.isBN(number) && !isString(number)) {
throw new Error('Please pass numbers as strings or BigNumber objects to avoid precision errors.');
throw new Error('Please pass numbers as strings or BN objects to avoid precision errors.');
}
return utils.isBN(number) ? ethjsUnit.toWei(number, unit) : ethjsUnit.toWei(number, unit).toString(10);
+1 -1
View File
@@ -461,7 +461,7 @@ describe('UtilsTest', () => {
expect(() => {
toWei(1, 'wei');
}).toThrow('Please pass numbers as strings or BigNumber objects to avoid precision errors.');
}).toThrow('Please pass numbers as strings or BN objects to avoid precision errors.');
});
it('calls utf8ToHex and returns the expected results', () => {