Skip to content
Open
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
753 changes: 401 additions & 352 deletions DISCO3.md

Large diffs are not rendered by default.

6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@

> a performant web3 react library


> [Read the Disco3 Design and Arch. Document](https://github.com/manifoldfinance/disco3-react/blob/master/DISCO3.md)

---

[![CII Best Practices](https://bestpractices.coreinfrastructure.org/projects/5693/badge)](https://bestpractices.coreinfrastructure.org/projects/5693) [![manifoldfinance.eth](https://img.shields.io/static/v1?label=&message=manifoldfinance.eth&color=black&logo=ethereum&logoColor=white)](https://etherscan.io/enslookup-search?search=manifoldfinance.eth) [![manifoldfinance - disco3-react](https://img.shields.io/static/v1?label=manifoldfinance&message=disco3-react&color=black&logo=github)](https://github.com/manifoldfinance/disco3-react 'Go to GitHub repo')
[![CII Best Practices](https://bestpractices.coreinfrastructure.org/projects/5693/badge)](https://bestpractices.coreinfrastructure.org/projects/5693)
[![manifoldfinance.eth](https://img.shields.io/static/v1?label=&message=manifoldfinance.eth&color=black&logo=ethereum&logoColor=white)](https://etherscan.io/enslookup-search?search=manifoldfinance.eth)
[![manifoldfinance - disco3-react](https://img.shields.io/static/v1?label=manifoldfinance&message=disco3-react&color=black&logo=github)](https://github.com/manifoldfinance/disco3-react 'Go to GitHub repo')

| package | description |
| ---------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
Expand Down
1 change: 1 addition & 0 deletions packages/network/src/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type { Actions, Web3ReactStore } from '@disco3/types';
import { MockEip1193Bridge } from '../../url/src/index.spec';
import { Network } from './';
import { createWeb3ReactStoreAndActions } from '@disco3/store';

jest.mock('@ethersproject/providers', () => ({
JsonRpcProvider: class MockJsonRpcProvider {
getSigner() {}
Expand Down
2 changes: 1 addition & 1 deletion packages/store/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
"start": "tsc --watch"
},
"dependencies": {
"@disco3/types": "8.0.2-beta.0",
"@disco3/types": "^8.0.2-beta.1",
"@ethersproject/address": "^5.4.0",
"zustand": "^4.0.0-beta.0"
}
Expand Down
43 changes: 31 additions & 12 deletions packages/store/src/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { ChainIdNotAllowedError, createWeb3ReactStoreAndActions } from '.';
import {
ChainIdNotAllowedError,
MAX_SAFE_CHAIN_ID,
createWeb3ReactStoreAndActions,
} from '.';

test('ChainIdNotAllowedError', () => {
const error = new ChainIdNotAllowedError(1, [2]);
Expand All @@ -25,7 +29,7 @@ describe('#createWeb3ReactStoreAndActions', () => {
});

describe('#startActivation', () => {
test('#works', () => {
test('works', () => {
const [store, actions] = createWeb3ReactStoreAndActions();
actions.startActivation();
expect(store.getState()).toEqual({
Expand All @@ -35,6 +39,7 @@ describe('#createWeb3ReactStoreAndActions', () => {
error: undefined,
});
});

test('cancellation works', () => {
const [store, actions] = createWeb3ReactStoreAndActions();
const cancelActivation = actions.startActivation();
Expand All @@ -53,7 +58,7 @@ describe('#createWeb3ReactStoreAndActions', () => {
describe('#update', () => {
test('throws on bad chainIds', () => {
const [, actions] = createWeb3ReactStoreAndActions();
for (const chainId of [1.1, 0, Number.MAX_SAFE_INTEGER + 1]) {
for (const chainId of [1.1, 0, MAX_SAFE_CHAIN_ID + 1]) {
expect(() => actions.update({ chainId })).toThrow(
`Invalid chainId ${chainId}`,
);
Expand Down Expand Up @@ -196,15 +201,29 @@ describe('#createWeb3ReactStoreAndActions', () => {
});
});

test('#reportError', () => {
const [store, actions] = createWeb3ReactStoreAndActions();
const error = new Error();
actions.reportError(error);
expect(store.getState()).toEqual({
chainId: undefined,
accounts: undefined,
activating: false,
error,
describe('#reportError', () => {
test('sets error', () => {
const [store, actions] = createWeb3ReactStoreAndActions();
const error = new Error();
actions.reportError(error);
expect(store.getState()).toEqual({
chainId: undefined,
accounts: undefined,
activating: false,
error,
});
});

test('resets state', () => {
const [store, actions] = createWeb3ReactStoreAndActions();
actions.reportError(new Error());
actions.reportError(undefined);
expect(store.getState()).toEqual({
chainId: undefined,
accounts: undefined,
activating: false,
error: undefined,
});
});
});
});
17 changes: 12 additions & 5 deletions packages/store/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,26 @@
import { getAddress } from '@ethersproject/address';
import type {
Actions,
Web3ReactState,
Web3ReactStateUpdate,
Web3ReactStore,
} from '@disco3/types';

import create from 'zustand/vanilla';
import { getAddress } from '@ethersproject/address';

/**
* MAX_SAFE_CHAIN_ID is the upper bound limit on what will be accepted for `chainId`
* `MAX_SAFE_CHAIN_ID = floor( ( 2**53 - 39 ) / 2 ) = 4503599627370476`
*
* @see {@link https://github.com/MetaMask/metamask-extension/blob/b6673731e2367e119a5fee9a454dd40bd4968948/shared/constants/network.js#L31}
*/
export const MAX_SAFE_CHAIN_ID = 4503599627370476;

function validateChainId(chainId: number): void {
if (
!Number.isInteger(chainId) ||
chainId <= 0 ||
chainId > Number.MAX_SAFE_INTEGER
chainId > MAX_SAFE_CHAIN_ID
) {
throw new Error(`Invalid chainId ${chainId}`);
}
Expand Down Expand Up @@ -73,9 +82,7 @@ export function createWeb3ReactStoreAndActions(

// return a function that cancels the activation iff nothing else has happened
return () => {
if (nullifier === nullifierCached) {
store.setState({ ...DEFAULT_STATE, activating: false });
}
if (nullifier === nullifierCached) store.setState({ activating: false });
};
}

Expand Down
4 changes: 2 additions & 2 deletions packages/types/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"publishConfig": {
"access": "public"
},
"version": "8.0.2-beta.0",
"version": "8.0.2-beta.1",
"type": "module",
"files": [
"dist/*"
Expand All @@ -25,6 +25,6 @@
"start": "tsc --watch"
},
"dependencies": {
"zustand": "^4.0.0-beta.0"
"zustand": "^4.0.0-beta.2"
}
}
9 changes: 9 additions & 0 deletions packages/types/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
/**
* @version 0.0.18
*/

import type { State, StoreApi } from 'zustand/vanilla';

import type { EventEmitter } from 'node:events';
Expand Down Expand Up @@ -88,6 +92,11 @@ export abstract class Connector {
this.actions = actions;
}

/**
* Attempt to initiate a connection, failing silently
*/
public connectEagerly?(...args: unknown[]): Promise<void> | void;

/**
* Initiate a connection.
*/
Expand Down
8 changes: 0 additions & 8 deletions packages/types/yarn.lock

This file was deleted.

11 changes: 7 additions & 4 deletions packages/walletconnect/src/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import { createWeb3ReactStoreAndActions } from '@disco3/store';
import type { Actions, RequestArguments, Web3ReactStore } from '@disco3/types';
import { WalletConnect } from '.';

import { MockEIP1193Provider } from '../../eip1193/src/index.spec';
import { WalletConnect } from '.';
import { createWeb3ReactStoreAndActions } from '@disco3/store';

// necessary because walletconnect returns chainId as a number
export class MockMockWalletConnectProvider extends MockEIP1193Provider {
class MockMockWalletConnectProvider extends MockEIP1193Provider {
public connector = new EventEmitter();

public eth_chainId_number = jest.fn((chainId?: string) =>
chainId === undefined ? chainId : Number.parseInt(chainId, 16),
);
Expand Down Expand Up @@ -35,7 +38,7 @@ describe('WalletConnect', () => {
beforeEach(() => {
let actions: Actions;
[store, actions] = createWeb3ReactStoreAndActions();
connector = new WalletConnect(actions, {});
connector = new WalletConnect(actions, { rpc: {} }, true);
});

beforeEach(async () => {
Expand Down
Loading