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

TransactionSigner updated for production build #2609

Merged
merged 11 commits into from
Mar 29, 2019
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ export default class EthSendTransactionMethod extends SendTransactionMethod {
}

if (!this.parameters[0].nonce && this.parameters[0].nonce !== 0) {
this.getTransactionCountMethod.parameters = [this.parameters[0].from];
this.getTransactionCountMethod.parameters = [this.parameters[0].from, 'latest'];

this.parameters[0].nonce = await this.getTransactionCountMethod.execute();
}
Expand Down Expand Up @@ -158,7 +158,7 @@ export default class EthSendTransactionMethod extends SendTransactionMethod {
* @returns {Boolean}
*/
isDefaultSigner() {
return this.moduleInstance.transactionSigner.constructor.name === 'TransactionSigner';
return this.moduleInstance.transactionSigner.type === 'TransactionSigner';
}

/**
Expand All @@ -180,6 +180,6 @@ export default class EthSendTransactionMethod extends SendTransactionMethod {
* @returns {Boolean}
*/
hasCustomSigner() {
return this.moduleInstance.transactionSigner.constructor.name !== 'TransactionSigner';
return this.moduleInstance.transactionSigner.type !== 'TransactionSigner';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -278,13 +278,7 @@ export default class TransactionObserver {
* @returns {Boolean}
*/
isSocketBasedProvider() {
switch (this.provider.constructor.name) {
case 'CustomProvider':
case 'HttpProvider':
return false;
default:
return true;
}
return !!this.provider.SOCKET_MESSAGE;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ describe('EthSendTransactionMethodTest', () => {
moduleInstanceMock.currentProvider = providerMock;

transactionSignerMock = new TransactionSigner();
transactionSignerMock.type = 'TransactionSigner';
transactionSignerMock.sign = jest.fn();

new SendRawTransactionMethod();
Expand Down Expand Up @@ -81,6 +82,10 @@ describe('EthSendTransactionMethodTest', () => {
expect(method).toBeInstanceOf(AbstractObservedTransactionMethod);
});

it('calls the static property Type and it returns the expect value', () => {
expect(EthSendTransactionMethod.Type).toEqual('eth-send-transaction-method');
});

it('calls execute with wallets defined and returns with a resolved promise', async () => {
transactionSignerMock.sign = jest.fn(() => {
return Promise.resolve({rawTransaction: '0x0'});
Expand Down Expand Up @@ -384,7 +389,7 @@ describe('EthSendTransactionMethodTest', () => {

expect(sendRawTransactionMethodMock.parameters).toEqual(['0x0']);

expect(getTransactionCountMethodMock.parameters).toEqual([method.parameters[0].from]);
expect(getTransactionCountMethodMock.parameters).toEqual([method.parameters[0].from, 'latest']);

expect(getTransactionCountMethodMock.execute).toHaveBeenCalledWith();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ describe('TransactionObserverTest', () => {
it('calls observe with a socket provider and returns a transaction receipt', (done) => {
transactionObserver.blockConfirmations = 2;

providerMock.SOCKET_MESSAGE = true;

const blockHeadOne = {
number: 0
};
Expand Down Expand Up @@ -111,6 +113,8 @@ describe('TransactionObserverTest', () => {
transactionObserver.blockConfirmations = 2;
transactionObserver.timeout = 1;

providerMock.SOCKET_MESSAGE = true;

const blockHeadOne = {
number: 0
};
Expand Down Expand Up @@ -152,6 +156,8 @@ describe('TransactionObserverTest', () => {
it('calls observe with a socket provider and the newHeads subscription returns a error', (done) => {
transactionObserver.blockConfirmations = 2;

providerMock.SOCKET_MESSAGE = true;

newHeadsSubscriptionMock.subscribe = jest.fn((callback) => {
callback(true, false);
});
Expand All @@ -175,8 +181,6 @@ describe('TransactionObserverTest', () => {
it('calls observe with a http provider and returns a transaction receipt', (done) => {
transactionObserver.blockConfirmations = 2;

providerMock.constructor.name = 'CustomProvider';

const receipt = {blockNumber: '0xa'};
const blockOne = {number: '0xa', hash: '0x0'};
const blockTwo = {number: '0xc', parentHash: '0x0'};
Expand Down Expand Up @@ -219,8 +223,6 @@ describe('TransactionObserverTest', () => {
transactionObserver.blockConfirmations = 2;
transactionObserver.timeout = 1;

providerMock.constructor.name = 'CustomProvider';

const receipt = {blockNumber: '0xa'};
const blockOne = {number: '0xa', hash: '0x0'};

Expand Down Expand Up @@ -256,8 +258,6 @@ describe('TransactionObserverTest', () => {
transactionObserver.blockConfirmations = 2;
transactionObserver.timeout = 1;

providerMock.constructor.name = 'CustomProvider';

getTransactionReceiptMethodMock.execute = jest.fn(() => {
return Promise.reject(new Error('ERROR'));
});
Expand Down
30 changes: 29 additions & 1 deletion packages/web3-eth-accounts/src/Accounts.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,41 @@ export default class Accounts extends AbstractWeb3Module {

this.utils = utils;
this.formatters = formatters;
this.transactionSigner = options.transactionSigner;
this._transactionSigner = options.transactionSigner;
this.defaultKeyName = 'web3js_wallet';
this.accounts = {};
this.accountsIndex = 0;
this.wallet = new Wallet(utils, this);
}

/**
* Getter for the transactionSigner property
*
* @property transactionSigner
*
* @returns {TransactionSigner}
*/
get transactionSigner() {
return this._transactionSigner;
}

/**
* TODO: Remove setter
*
* Setter for the transactionSigner property
*
* @property transactionSigner
*
* @param {TransactionSigner} transactionSigner
*/
set transactionSigner(transactionSigner) {
if (transactionSigner.type && transactionSigner.type === 'TransactionSigner') {
throw Error('Invalid TransactionSigner given!');
}

this._transactionSigner = transactionSigner;
}

/**
* Creates an account with a given entropy
*
Expand Down
6 changes: 5 additions & 1 deletion packages/web3-eth-contract/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,9 @@ module.exports = jestConfig({
'ProvidersModuleFactory': 'web3-providers',
'ProviderDetector': 'web3-providers',
'ProviderResolver': 'web3-providers',
'Utils': 'web3-utils'
'Utils': 'web3-utils',
'ChainIdMethod': 'web3-core-method',
'EstimateGasMethod': 'web3-core-method',
'GetTransactionCountMethod': 'web3-core-method',
'SendRawTransactionMethod': 'web3-core-method'
});
52 changes: 30 additions & 22 deletions packages/web3-eth-contract/src/factories/MethodFactory.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,14 +154,7 @@ export default class MethodFactory {
* @returns {SendContractMethod}
*/
createSendContractMethod(contract) {
const transactionObserver = new TransactionObserver(
contract.currentProvider,
this.getTransactionObserverTimeout(contract),
contract.transactionConfirmationBlocks,
new GetTransactionReceiptMethod(this.utils, this.formatters, contract),
new GetBlockByNumberMethod(this.utils, this.formatters, contract),
new NewHeadsSubscription(this.utils, this.formatters, contract)
);
const transactionObserver = this.createTransactionObserver(contract);

return new SendContractMethod(
this.utils,
Expand All @@ -186,21 +179,16 @@ export default class MethodFactory {
* @returns {ContractDeployMethod}
*/
createContractDeployMethod(contract) {
const transactionObserver = this.createTransactionObserver(contract);

return new ContractDeployMethod(
this.utils,
this.formatters,
contract,
new TransactionObserver(
contract.currentProvider,
this.getTransactionObserverTimeout(contract),
contract.transactionConfirmationBlocks,
new GetTransactionReceiptMethod(this.utils, this.formatters, contract),
new GetBlockByNumberMethod(this.utils, this.formatters, contract),
new NewHeadsSubscription(this.utils, this.formatters, contract)
),
transactionObserver,
new ChainIdMethod(this.utils, this.formatters, contract),
new GetTransactionCountMethod(this.utils, this.formatters, contract),
new SendRawTransactionMethod(this.utils, this.formatters, contract)
new SendRawTransactionMethod(this.utils, this.formatters, contract, transactionObserver)
);
}

Expand All @@ -217,23 +205,43 @@ export default class MethodFactory {
return new EstimateGasMethod(this.utils, this.formatters, contract);
}


/**
* Returns the correct timeout value based on the provider type
* Returns the correct timeout value
*
* @method getTransactionObserverTimeout
* @method getTimeout
*
* @param {AbstractContract} contract
*
* @returns {Number}
*/
getTransactionObserverTimeout(contract) {
getTimeout(contract) {
let timeout = contract.transactionBlockTimeout;
const providerName = contract.currentProvider.constructor.name;

if (providerName === 'HttpProvider' || providerName === 'CustomProvider') {
if (!contract.currentProvider.SOCKET_MESSAGE) {
timeout = contract.transactionPollingTimeout;
}

return timeout;
}

/**
* Returns a object of type TransactionObserver
*
* @method createTransactionObserver
*
* @param {AbstractContract} contract
*
* @returns {TransactionObserver}
*/
createTransactionObserver(contract) {
return new TransactionObserver(
contract.currentProvider,
this.getTimeout(contract),
contract.transactionConfirmationBlocks,
new GetTransactionReceiptMethod(this.utils, this.formatters, contract),
new GetBlockByNumberMethod(this.utils, this.formatters, contract),
new NewHeadsSubscription(this.utils, this.formatters, contract)
);
}
}
Loading