Skip to content

Commit

Permalink
Merge branch '1.0' into issue/2484
Browse files Browse the repository at this point in the history
  • Loading branch information
nivida committed Mar 18, 2019
2 parents dc29f10 + 5cdf1af commit e4af4b5
Show file tree
Hide file tree
Showing 15 changed files with 145 additions and 52 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ addons:
packages:
- g++-4.8
before_script:
- npm i -g typescript@next
- export DISPLAY=:99.0
- sh -e /etc/init.d/xvfb start
install:
Expand Down
2 changes: 1 addition & 1 deletion docs/web3-eth-accounts.rst
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ Parameters
Returns
-------

``String|Object``: The signed data RLP encoded signature, or if ``returnSignature`` is ``true`` the signature values as follows:
``Object``: The signed data RLP encoded signature, or if ``returnSignature`` is ``true`` the signature values as follows:
- ``message`` - ``String``: The the given message.
- ``messageHash`` - ``String``: The hash of the given message.
- ``r`` - ``String``: First 32 bytes of the signature
Expand Down
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 @@ -31,6 +31,6 @@ export default class NewKeyPairMethod extends AbstractMethod {
* @constructor
*/
constructor(utils, formatters, moduleInstance) {
super('shh_newKeyPair', 1, utils, formatters, moduleInstance);
super('shh_newKeyPair', 0, utils, formatters, moduleInstance);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,16 @@ export default class EthSendTransactionMethod extends SendTransactionMethod {

if (!this.parameters[0].gasPrice) {
if (!this.moduleInstance.defaultGasPrice) {
this.moduleInstance.currentProvider.send('eth_gasPrice', []).then((gasPrice) => {
this.parameters[0].gasPrice = gasPrice;
this.moduleInstance.currentProvider
.send('eth_gasPrice', [])
.then((gasPrice) => {
this.parameters[0].gasPrice = gasPrice;

this.execute();
});
this.execute();
})
.catch((error) => {
this.handleError(error, false, 0);
});

return this.promiEvent;
}
Expand All @@ -81,13 +86,7 @@ export default class EthSendTransactionMethod extends SendTransactionMethod {
if (this.moduleInstance.accounts.wallet[this.parameters[0].from]) {
this.sendRawTransaction(this.moduleInstance.accounts.wallet[this.parameters[0].from].privateKey).catch(
(error) => {
if (this.callback) {
this.callback(error, null);
}

this.promiEvent.reject(error);
this.promiEvent.emit('error', error);
this.promiEvent.removeAllListeners();
this.handleError(error, false, 0);
}
);

Expand All @@ -97,13 +96,7 @@ export default class EthSendTransactionMethod extends SendTransactionMethod {

if (this.hasCustomSigner()) {
this.sendRawTransaction().catch((error) => {
if (this.callback) {
this.callback(error, null);
}

this.promiEvent.reject(error);
this.promiEvent.emit('error', error);
this.promiEvent.removeAllListeners();
this.handleError(error, false, 0);
});

return this.promiEvent;
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 @@ -94,7 +94,7 @@ const tests = [
{
method: NewKeyPairMethod,
rpcMethod: 'shh_newKeyPair',
parametersAmount: 1
parametersAmount: 0
},
{
method: NewMessageFilterMethod,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -505,6 +505,25 @@ describe('EthSendTransactionMethodTest', () => {
expect(providerMock.send).toHaveBeenNthCalledWith(1, 'eth_gasPrice', []);
});

it('calls execute and the gasPrice will be defined with "eth_gasPrice" and returns with a reject promise', async () => {
providerMock.send = jest.fn(() => {
return Promise.reject(new Error('Nope'));
});

const transaction = {
from: 0,
gas: 1,
nonce: 1,
chainId: 1
};

method.parameters = [transaction];

await expect(method.execute()).rejects.toThrow('Nope');

expect(providerMock.send).toHaveBeenNthCalledWith(1, 'eth_gasPrice', []);
});

it('calls execute and signs on the node', () => {
moduleInstanceMock.transactionSigner = transactionSignerMock;

Expand Down
1 change: 1 addition & 0 deletions packages/web3-eth-accounts/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,4 +108,5 @@ export interface EncryptedKeystoreV3Json {

export interface Sign extends SignedTransaction {
message: string;
signature: string;
}
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
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,6 @@ export default class MethodOptionsValidator {
* @returns {Boolean}
*/
isValueValid(abiItemModel, method) {
return (
(!abiItemModel.payable && !method.parameters[0].value > 0) ||
(abiItemModel.payable && method.parameters[0].value > 0)
);
return abiItemModel.payable || (!abiItemModel.payable && !method.parameters[0].value);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,16 @@ describe('MethodOptionsValidatorTest', () => {
methodOptionsValidator.validate(abiItemModelMock, sendContractMethodMock);
}).toThrow('Can not send value to non-payable contract method or constructor');
});

it('calls validate returns true with payable true and value set to 0', () => {
abiItemModelMock.signature = 'constructor';
abiItemModelMock.payable = true;
abiItemModelMock.isOfType.mockReturnValueOnce(true);

sendContractMethodMock.parameters = [{value: 0, from: '0x0'}];

Utils.isAddress.mockReturnValueOnce(true);

expect(methodOptionsValidator.validate(abiItemModelMock, sendContractMethodMock)).toEqual(true);
});
});
6 changes: 4 additions & 2 deletions packages/web3-utils/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ export interface AbiItem {
constant?: boolean;
inputs?: AbiInput[];
name?: string;
outputs?: AbiOuput[];
outputs?: AbiOutput[];
payable?: boolean;
stateMutability?: StateMutabilityType;
type: AbiType;
Expand All @@ -219,9 +219,11 @@ export interface AbiInput {
name: string;
type: string;
indexed?: boolean;
components?: AbiInput[];
}

export interface AbiOuput {
export interface AbiOutput {
name: string;
type: string;
components?: AbiOutput[];
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,80 @@
import {jsonInterfaceMethodToString, AbiItem} from 'web3-utils';

const abiItem: AbiItem = {
anonymous: false,
constant: true,
inputs: [
{
name: 'testMe',
type: 'uint256[3]'
},
{
name: 'inputA',
type: 'tuple',
components: [
{
name: 'a',
type: 'uint8'
},
{
name: 'b',
type: 'uint8'
}
]
},
{
name: 'inputB',
type: 'tuple[]',
components: [
{
name: 'a1',
type: 'uint256'
},
{
name: 'a2',
type: 'uint256'
}
]
},
{
name: 'inputC',
type: 'uint8',
indexed: false
}
],
name: "testName",
outputs: [
{
name: "test",
type: "uint256"
},
{
name: 'outputA',
type: 'tuple',
components: [
{
name: 'a',
type: 'uint8'
},
{
name: 'b',
type: 'uint8'
}
]
},
{
name: 'outputB',
type: 'tuple[]',
components: [
{
name: 'a1',
type: 'uint256'
},
{
name: 'a2',
type: 'uint256'
}
]
}
],
payable: false,
Expand Down

0 comments on commit e4af4b5

Please sign in to comment.