Skip to content
This repository was archived by the owner on Mar 5, 2025. It is now read-only.
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
18 changes: 14 additions & 4 deletions packages/web3-core-method/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -539,12 +539,22 @@ Method.prototype._confirmTransaction = function (defer, result, payload) {

// start watching for confirmation depending on the support features of the provider
var startWatching = function (existingReceipt) {
// if provider allows PUB/SUB
if (_.isFunction(this.requestManager.provider.on)) {
_ethereumCall.subscribe('newBlockHeaders', checkConfirmation.bind(null, existingReceipt, false));
} else {
const startInterval = () => {
intervalId = setInterval(checkConfirmation.bind(null, existingReceipt, true), 1000);
}

if (!this.requestManager.provider.on) {
startInterval()
} else {
_ethereumCall.subscribe('newBlockHeaders', function (err, blockHeader, sub) {
if (err || !blockHeader) {
// fall back to polling
startInterval()
} else {
checkConfirmation(existingReceipt, false, err, blockHeader, sub);
}
})
}
}.bind(this);


Expand Down
191 changes: 184 additions & 7 deletions test/method.buildCall.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
var chai = require('chai');
var assert = chai.assert;
var formatters = require('../packages/web3-core-helpers/src/formatters.js');
var FakeHttpProvider = require('./helpers/FakeIpcProvider');
var FakeHttpProvider = require('./helpers/FakeHttpProvider');
var FakeIpcProvider = require('./helpers/FakeIpcProvider');
var Eth = require('../packages/web3-eth');
var Method = require('../packages/web3-core-method');

Expand Down Expand Up @@ -220,7 +221,7 @@ describe('lib/web3/method', function () {
});

var succeedOnReceipt = function () {
var provider = new FakeHttpProvider();
var provider = new FakeIpcProvider();
var eth = new Eth(provider);
var method = new Method({
name: 'sendTransaction',
Expand Down Expand Up @@ -343,7 +344,7 @@ describe('lib/web3/method', function () {


var succeedwhenDeploying = function () {
var provider = new FakeHttpProvider();
var provider = new FakeIpcProvider();
var eth = new Eth(provider);
var method = new Method({
name: 'sendTransaction',
Expand Down Expand Up @@ -461,7 +462,7 @@ describe('lib/web3/method', function () {
});

var failOnCodeEmpty = function () {
var provider = new FakeHttpProvider();
var provider = new FakeIpcProvider();
var eth = new Eth(provider);
var method = new Method({
name: 'sendTransaction',
Expand Down Expand Up @@ -561,7 +562,7 @@ describe('lib/web3/method', function () {
});

var failOnMissingAddress = function () {
var provider = new FakeHttpProvider();
var provider = new FakeIpcProvider();
var eth = new Eth(provider);
var method = new Method({
name: 'sendTransaction',
Expand Down Expand Up @@ -661,7 +662,7 @@ describe('lib/web3/method', function () {
});

var failOnTimeout = function () {
var provider = new FakeHttpProvider();
var provider = new FakeIpcProvider();
var eth = new Eth(provider);
var method = new Method({
name: 'sendTransaction',
Expand Down Expand Up @@ -750,7 +751,7 @@ describe('lib/web3/method', function () {
});

it('should give confirmation receipts with on("confirmation", ...) when subscribing "sendTransaction"', function (done) {
var provider = new FakeHttpProvider();
var provider = new FakeIpcProvider();
var eth = new Eth(provider);
var method = new Method({
name: 'sendTransaction',
Expand Down Expand Up @@ -860,6 +861,182 @@ describe('lib/web3/method', function () {
});

});

it('should subscribe to new blocks if using IpcProvider', function (done) {
const provider = new FakeIpcProvider();
const eth = new Eth(provider);

const method = new Method({
name: 'sendTransaction',
call: 'eth_sendTransaction',
params: 1,
inputFormatter: [formatters.inputTransactionFormatter]
});
method.setRequestManager(eth._requestManager, eth);

// generate send function
const send = method.buildCall();

// add results
provider.injectValidation(function (payload) {
assert.equal(payload.method, 'eth_sendTransaction');
assert.deepEqual(payload.params, [{
from: '0x11f4d0a3c12e86b4b5f39b213f7e19d048276dae',
to: '0x11f4d0a3c12e86b4b5f39b213f7e19d048276dae',
value: '0xa',
gasPrice: "0x574d94bba"
}]);
});
provider.injectResult('0x1234567453543456321456321'); // tx hash

provider.injectValidation(function (payload) {
assert.equal(payload.method, 'eth_getTransactionReceipt');
assert.deepEqual(payload.params, ['0x1234567453543456321456321']);
});
provider.injectResult(null);

provider.injectValidation(function (payload) {
// here is the check.
// will be `eth_subscribe` if subscribing.
// will be `eth_getTransactionReceipt` if polling.
assert.equal(payload.method, 'eth_subscribe');
assert.deepEqual(payload.params, ['newHeads']);
done();
});

send({
from: '0x11f4d0A3c12e86B4b5F39B213F7E19D048276DAe',
to: '0x11f4d0A3c12e86B4b5F39B213F7E19D048276DAe',
value: '0xa',
gasPrice: '23435234234'
})
});

it('should use polling if using HttpProvider', function (done) {
const provider = new FakeHttpProvider();
const eth = new Eth(provider);

const method = new Method({
name: 'sendTransaction',
call: 'eth_sendTransaction',
params: 1,
inputFormatter: [formatters.inputTransactionFormatter]
});
method.setRequestManager(eth._requestManager, eth);

// generate send function
const send = method.buildCall();

// add results
provider.injectValidation(function (payload) {
assert.equal(payload.method, 'eth_sendTransaction');
assert.deepEqual(payload.params, [{
from: '0x11f4d0a3c12e86b4b5f39b213f7e19d048276dae',
to: '0x11f4d0a3c12e86b4b5f39b213f7e19d048276dae',
value: '0xa',
gasPrice: "0x574d94bba"
}]);
});
provider.injectResult('0x1234567453543456321456321'); // tx hash

provider.injectValidation(function (payload) {
assert.equal(payload.method, 'eth_getTransactionReceipt');
assert.deepEqual(payload.params, ['0x1234567453543456321456321']);
});
provider.injectResult(null);

provider.injectValidation(function (payload) {
// here is the check.
// will be `eth_subscribe` if subscribing.
// will be `eth_getTransactionReceipt` if polling.
assert.equal(payload.method, 'eth_getTransactionReceipt');
assert.deepEqual(payload.params, ['0x1234567453543456321456321']);
done();
});

send({
from: '0x11f4d0A3c12e86B4b5F39B213F7E19D048276DAe',
to: '0x11f4d0A3c12e86B4b5F39B213F7E19D048276DAe',
value: '0xa',
gasPrice: '23435234234'
})
});

it('should use polling if using provider with method `on` but no subscription capabilities', function (done) {
this.timeout(5000);

const provider = new FakeHttpProvider();
// provider with method 'on' but no subscription capabilities should use polling
provider.on = (...args) => {}
const eth = new Eth(provider);

const method = new Method({
name: 'sendTransaction',
call: 'eth_sendTransaction',
params: 1,
inputFormatter: [formatters.inputTransactionFormatter]
});
method.setRequestManager(eth._requestManager, eth);

// generate send function
const send = method.buildCall();

// add results
provider.injectValidation(function (payload) {
assert.equal(payload.method, 'eth_sendTransaction');
assert.deepEqual(payload.params, [{
from: '0x11f4d0a3c12e86b4b5f39b213f7e19d048276dae',
to: '0x11f4d0a3c12e86b4b5f39b213f7e19d048276dae',
value: '0xa',
gasPrice: "0x574d94bba"
}]);
});
provider.injectResult('0x1234567453543456321456321'); // tx hash

provider.injectValidation(function (payload) {
assert.equal(payload.method, 'eth_getTransactionReceipt');
assert.deepEqual(payload.params, ['0x1234567453543456321456321']);
});
provider.injectResult(null);

provider.injectValidation(function (payload) {
// here is the check.
// first will try subscribing with `eth_subscribe`.
assert.equal(payload.method, 'eth_subscribe');
assert.deepEqual(payload.params, ['newHeads']);
});
provider.injectResult(null);

// after failing with `eth_subscribe`,
// it should start polling with `eth_getTransactionReceipt`
provider.injectValidation(function (payload) {
assert.equal(payload.method, 'eth_getTransactionReceipt');
assert.deepEqual(payload.params, ['0x1234567453543456321456321']);
});
provider.injectResult(null);

// second poll
provider.injectValidation(function (payload) {
assert.equal(payload.method, 'eth_getTransactionReceipt');
assert.deepEqual(payload.params, ['0x1234567453543456321456321']);
});
provider.injectResult(null);

// third poll
provider.injectValidation(function (payload) {
assert.equal(payload.method, 'eth_getTransactionReceipt');
assert.deepEqual(payload.params, ['0x1234567453543456321456321']);
done();
});
provider.injectResult(null);

send({
from: '0x11f4d0A3c12e86B4b5F39B213F7E19D048276DAe',
to: '0x11f4d0A3c12e86B4b5F39B213F7E19D048276DAe',
value: '0xa',
gasPrice: '23435234234'
})
});
});
});