Skip to content

Commit

Permalink
Fix web3x undefined error. Singleton BroadcastChannel.
Browse files Browse the repository at this point in the history
  • Loading branch information
xf00f committed Jun 27, 2019
1 parent 1370199 commit a79a5d9
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 12 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

All notable changes to this project will be documented in this file.

## [4.0.3] - 2019-06-27

- Fix `web3` undefined error in `eth.fromCurrentProvider()`.
- `BroadcastChannel` now shared between multiple `EvmProvider`'s in a single tab.

## [4.0.2] - 2019-06-24

- Create `web3x-evm-es` package for `web3x-es` compatiable version of EVM.
Expand Down Expand Up @@ -167,6 +172,7 @@ All notable changes to this project will be documented in this file.

- Initial release of Typescript port from web3.js.

[4.0.3]: https://github.com/xf00f/web3x/compare/v4.0.2...v4.0.3
[4.0.2]: https://github.com/xf00f/web3x/compare/v4.0.1...v4.0.2
[4.0.1]: https://github.com/xf00f/web3x/compare/v4.0.0...v4.0.1
[4.0.0]: https://github.com/xf00f/web3x/compare/v3.0.11...v4.0.0
Expand Down
2 changes: 1 addition & 1 deletion version.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
"4.0.2"
"4.0.3"
2 changes: 1 addition & 1 deletion web3x-evm/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "web3x-evm",
"version": "4.0.2",
"version": "4.0.3",
"license": "MIT",
"description": "EVM implementation and provider for web3x.",
"repository": {
Expand Down
21 changes: 14 additions & 7 deletions web3x-evm/src/provider/evm-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,19 @@ import { handleGetTransactionByHash } from './handle-get-transaction';
import { handleGetTransactionReceipt } from './handle-get-transaction-receipt';
import { handleSendTransaction } from './handle-send-transaction';

const newBlockChannel = typeof BroadcastChannel !== 'undefined' ? new BroadcastChannel('newBlock') : undefined;

export interface EvmProviderOptions {
blockDelay?: number;
wallet?: Wallet;
listenForBlocks?: boolean;
}

export class EvmProvider extends EventEmitter implements EthereumProvider {
public wallet?: Wallet;
private subscriptions: { [id: string]: any } = {};
private nextSubscriptionId = 0;
private newBlockChannel = typeof BroadcastChannel !== 'undefined' ? new BroadcastChannel('newBlock') : undefined;
private blockHandler = e => this.handleBlock(new Buffer(e.data));

constructor(
public readonly worldState: WorldState,
Expand All @@ -51,8 +54,8 @@ export class EvmProvider extends EventEmitter implements EthereumProvider {
super();
this.wallet = options.wallet;

if (this.newBlockChannel) {
this.newBlockChannel.onmessage = e => this.handleBlock(new Buffer(e.data));
if (options.listenForBlocks && newBlockChannel) {
newBlockChannel.onmessage = this.blockHandler;
}
}

Expand Down Expand Up @@ -91,6 +94,10 @@ export class EvmProvider extends EventEmitter implements EthereumProvider {
this.wallet = wallet;
}

public setBlockDelay(blockDelay: number) {
this.options.blockDelay = blockDelay;
}

public async send(method: string, params?: any[] | undefined): Promise<any> {
// console.log(method);
// console.log(params);
Expand Down Expand Up @@ -118,7 +125,7 @@ export class EvmProvider extends EventEmitter implements EthereumProvider {
this.blockchain,
fromRawTransactionRequest(params[0]),
this.wallet,
this.newBlockChannel,
newBlockChannel,
this.options.blockDelay,
);
case 'eth_call':
Expand Down Expand Up @@ -213,9 +220,9 @@ export class EvmProvider extends EventEmitter implements EthereumProvider {
return super.removeAllListeners(notification);
}

public shutdown() {
if (this.newBlockChannel) {
this.newBlockChannel.close();
public close() {
if (newBlockChannel) {
newBlockChannel.removeEventListener('message', this.blockHandler);
}
}
}
2 changes: 1 addition & 1 deletion web3x-evm/src/vm/execute-transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ async function validateTx(worldState: WorldState, sender: Address, tx: Tx) {
}

if (senderAccount.nonce !== nonce) {
throw new Error('Sender account nonce does not match transaction nonce.');
throw new Error(`Sender account nonce does not match transaction nonce: ${senderAccount.nonce} != ${nonce}`);
}

const intrinsicGas =
Expand Down
2 changes: 1 addition & 1 deletion web3x/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "web3x",
"version": "4.0.0",
"version": "4.0.3",
"license": "LGPL-3.0",
"description": "Typescript port of web3.js",
"repository": {
Expand Down
2 changes: 1 addition & 1 deletion web3x/src/eth/eth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export class Eth {
}

public static fromCurrentProvider() {
if (!web3) {
if (typeof web3 === 'undefined') {
return;
}
const provider = web3.currentProvider || web3.ethereumProvider;
Expand Down

0 comments on commit a79a5d9

Please sign in to comment.