Skip to content

Commit

Permalink
applying type changes
Browse files Browse the repository at this point in the history
  • Loading branch information
bangtoven committed Nov 21, 2023
1 parent 47f98de commit 5421316
Show file tree
Hide file tree
Showing 8 changed files with 198 additions and 169 deletions.
188 changes: 94 additions & 94 deletions packages/wallet-sdk/src/provider/CoinbaseWalletProvider.ts

Large diffs are not rendered by default.

21 changes: 13 additions & 8 deletions packages/wallet-sdk/src/provider/FilterPolyfill.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import {
isHexString,
range,
} from '../core/util';
import { JSONRPCRequest, JSONRPCResponse } from '../provider/JSONRPC';
import { JSONRPCRequest } from './JSONRPCRequest';
import { JSONRPCResponse, JSONRPCResponseError } from './JSONRPCResponse';
import { Web3Provider } from './Web3Provider';

const TIMEOUT = 5 * 60 * 1000; // 5 minutes
Expand Down Expand Up @@ -84,7 +85,7 @@ export class FilterPolyfill {
return true;
}

public getFilterChanges(filterId: HexString): Promise<JSONRPCResponse> {
public getFilterChanges(filterId: HexString): Promise<JSONRPCResponse | JSONRPCResponseError> {
const id = intNumberFromHexString(filterId);
if (this.timeouts.has(id)) {
// extend timeout
Expand All @@ -100,7 +101,7 @@ export class FilterPolyfill {
return Promise.resolve(filterNotFoundError());
}

public async getFilterLogs(filterId: HexString): Promise<JSONRPCResponse> {
public async getFilterLogs(filterId: HexString): Promise<JSONRPCResponse | JSONRPCResponseError> {
const id = intNumberFromHexString(filterId);
const filter = this.logFilters.get(id);
if (!filter) {
Expand Down Expand Up @@ -140,7 +141,9 @@ export class FilterPolyfill {
this.timeouts.delete(id);
}

private async getLogFilterChanges(id: IntNumber): Promise<JSONRPCResponse> {
private async getLogFilterChanges(
id: IntNumber
): Promise<JSONRPCResponse | JSONRPCResponseError> {
const filter = this.logFilters.get(id);
const cursorPosition = this.cursors.get(id);
if (!cursorPosition || !filter) {
Expand Down Expand Up @@ -184,7 +187,9 @@ export class FilterPolyfill {
return response;
}

private async getBlockFilterChanges(id: IntNumber): Promise<JSONRPCResponse> {
private async getBlockFilterChanges(
id: IntNumber
): Promise<JSONRPCResponse | JSONRPCResponseError> {
const cursorPosition = this.cursors.get(id);
if (!cursorPosition) {
return filterNotFoundError();
Expand Down Expand Up @@ -280,11 +285,11 @@ export class FilterPolyfill {
}

private async getBlockHashByNumber(blockNumber: IntNumber): Promise<HexString | null> {
const response = await this.sendAsyncPromise({
const response = (await this.sendAsyncPromise({
...JSONRPC_TEMPLATE,
method: 'eth_getBlockByNumber',
params: [hexStringFromIntNumber(blockNumber), false],
});
})) as JSONRPCResponse<'eth_getBlockByNumber'>;
if (response.result && typeof response.result.hash === 'string') {
return ensureHexString(response.result.hash);
}
Expand Down Expand Up @@ -338,7 +343,7 @@ function hexBlockHeightFromIntBlockHeight(value: IntBlockHeight): HexBlockHeight
return hexStringFromIntNumber(value);
}

function filterNotFoundError(): JSONRPCResponse {
function filterNotFoundError(): JSONRPCResponseError {
return {
...JSONRPC_TEMPLATE,
error: { code: -32000, message: 'filter not found' },
Expand Down
5 changes: 4 additions & 1 deletion packages/wallet-sdk/src/provider/JSONRPCMethod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,7 @@ export type JSONRPCMethod =
| 'eth_newBlockFilter'
| 'eth_newPendingTransactionFilter'
| 'eth_getFilterChanges'
| 'eth_getFilterLogs';
| 'eth_getFilterLogs'
| 'eth_getLogs'
| 'eth_blockNumber'
| 'eth_getBlockByNumber';
104 changes: 58 additions & 46 deletions packages/wallet-sdk/src/provider/JSONRPCRequest.ts
Original file line number Diff line number Diff line change
@@ -1,51 +1,14 @@
import BN from 'bn.js';

import { AddressString, HexString, IntNumber } from '../core/type';
import { JSONRPCMethodName } from './JSONRPC';
import { JSONRPCMethod } from './JSONRPCMethod';

type EthereumTransactionParams = {
fromAddress: AddressString;
toAddress?: AddressString;
weiValue: BN;
data: Buffer;
nonce?: IntNumber;
gasPriceInWei?: BN;
maxFeePerGas?: BN; // in wei
maxPriorityFeePerGas?: BN; // in wei
gasLimit?: BN;
chainId: IntNumber;
};

type AddEthereumChainParams = {
chainId: string;
blockExplorerUrls?: string[];
chainName?: string;
iconUrls?: string[];
rpcUrls?: string[];
nativeCurrency?: {
name: string;
symbol: string;
decimals: number;
};
};

type WatchAssetParams = {
type: string;
options: {
address: string;
symbol?: string;
decimals?: number;
image?: string;
};
};

type SubscriptionType = 'newHeads' | 'logs' | 'newPendingTransactions' | 'syncing';

export type JSONRPCRequest = {
export type JSONRPCRequest<M extends JSONRPCMethod = JSONRPCMethod> = {
jsonrpc: '2.0';
id: number;
method: JSONRPCMethodName;
} & (
} & Extract<_JSONRPCRequest, { method: M }>;

type _JSONRPCRequest =
| {
method: 'eth_accounts';
params: never;
Expand Down Expand Up @@ -88,15 +51,15 @@ export type JSONRPCRequest = {
}
| {
method: 'eth_signTransaction';
params: EthereumTransactionParams;
params: [EthereumTransactionParams];
}
| {
method: 'eth_sendRawTransaction';
params: [Buffer];
}
| {
method: 'eth_sendTransaction';
params: EthereumTransactionParams;
params: [EthereumTransactionParams];
}
| {
method: 'eth_signTypedData_v1';
Expand Down Expand Up @@ -151,7 +114,7 @@ export type JSONRPCRequest = {
}
| {
method: 'eth_newFilter';
params: unknown;
params: unknown[];
}
| {
method: 'eth_newBlockFilter';
Expand All @@ -169,4 +132,53 @@ export type JSONRPCRequest = {
method: 'eth_getFilterLogs';
params: [HexString];
}
);
| {
method: 'eth_getLogs';
params: unknown;
}
| {
method: 'eth_blockNumber';
params: unknown;
}
| {
method: 'eth_getBlockByNumber';
params: unknown;
};

type EthereumTransactionParams = {
fromAddress: AddressString;
toAddress?: AddressString;
weiValue: BN;
data: Buffer;
nonce?: IntNumber;
gasPriceInWei?: BN;
maxFeePerGas?: BN; // in wei
maxPriorityFeePerGas?: BN; // in wei
gasLimit?: BN;
chainId: IntNumber;
};

type AddEthereumChainParams = {
chainId: string;
blockExplorerUrls?: string[];
chainName?: string;
iconUrls?: string[];
rpcUrls?: string[];
nativeCurrency?: {
name: string;
symbol: string;
decimals: number;
};
};

type WatchAssetParams = {
type: string;
options: {
address: string;
symbol?: string;
decimals?: number;
image?: string;
};
};

type SubscriptionType = 'newHeads' | 'logs' | 'newPendingTransactions' | 'syncing';
32 changes: 20 additions & 12 deletions packages/wallet-sdk/src/provider/JSONRPCResponse.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
import { AddressString, HexString } from '../core/type';
import { JSONRPCMethodName } from './JSONRPC';
import { JSONRPCMethod } from './JSONRPCMethod';

export type JSONRPCResponse = {
export type JSONRPCResponse<M extends JSONRPCMethod = JSONRPCMethod> = {
jsonrpc: '2.0';
id: number;
method?: JSONRPCMethodName;
} & (
| {
error: {
code: number;
message: string;
data?: unknown;
};
}
result: Extract<_JSONRPCResponse, { method: M }>['result'];
};

export type JSONRPCResponseError = {
jsonrpc: '2.0';
id: number;
error: {
code: number;
message: string;
data?: unknown;
};
};

type _JSONRPCResponse =
| {
method: 'eth_accounts';
result: string[];
Expand Down Expand Up @@ -125,4 +130,7 @@ export type JSONRPCResponse = {
method: 'eth_getFilterLogs';
result: unknown;
}
);
| {
method: 'eth_getBlockByNumber';
result: { hash: string };
};
2 changes: 1 addition & 1 deletion packages/wallet-sdk/src/provider/SubscriptionManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export class SubscriptionManager {

public async handleRequest(request: {
method: string;
params: unknown[];
params: unknown;
}): Promise<SubscriptionResult> {
const result = {};
await this.subscriptionMiddleware(request, result, noop, noop);
Expand Down
3 changes: 2 additions & 1 deletion packages/wallet-sdk/src/provider/Web3Provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
// Licensed under the Apache License, version 2.0

import { Callback } from '../core/type';
import { JSONRPCRequest } from './JSONRPC';
import { JSONRPCRequest } from './JSONRPCRequest';
import { JSONRPCResponse } from './JSONRPCResponse';

export interface Web3Provider {
send(request: JSONRPCRequest): JSONRPCResponse;
Expand Down
12 changes: 6 additions & 6 deletions packages/wallet-sdk/src/relay/RelayAbstract.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { ErrorHandler, serializeError, standardErrors } from '../core/error';
import { AddressString, IntNumber, ProviderType, RegExpString } from '../core/type';
import { JSONRPCRequest, JSONRPCResponse } from '../provider/JSONRPC';
import { JSONRPCRequest } from '../provider/JSONRPCRequest';
import { JSONRPCResponse, JSONRPCResponseError } from '../provider/JSONRPCResponse';
import { Session } from './Session';
import { EthereumTransactionParams } from './walletlink/type/EthereumTransactionParams';
import { Web3Method } from './walletlink/type/Web3Method';
Expand Down Expand Up @@ -106,7 +107,7 @@ export abstract class RelayAbstract {
public async makeEthereumJSONRPCRequest(
request: JSONRPCRequest,
jsonRpcUrl: string
): Promise<JSONRPCResponse | void> {
): Promise<JSONRPCResponse> {
if (!jsonRpcUrl) throw new Error('Error: No jsonRpcUrl provided');
return window
.fetch(jsonRpcUrl, {
Expand All @@ -120,10 +121,9 @@ export abstract class RelayAbstract {
if (!json) {
throw standardErrors.rpc.parse({});
}
const response = json as JSONRPCResponse;
const { error } = response;
if (error) {
throw serializeError(error, request.method);
const response = json as JSONRPCResponse | JSONRPCResponseError;
if ('error' in response) {
throw serializeError(response.error, request.method);
}
return response;
});
Expand Down

0 comments on commit 5421316

Please sign in to comment.