Skip to content

Commit

Permalink
chore(bridge-ui): improve type checking (#13856)
Browse files Browse the repository at this point in the history
Co-authored-by: dave | d1onys1us <[email protected]>
  • Loading branch information
jscriptcoder and dionysuzx authored Jun 2, 2023
1 parent f36221c commit e48f17c
Show file tree
Hide file tree
Showing 22 changed files with 97 additions and 103 deletions.
2 changes: 2 additions & 0 deletions packages/bridge-ui/src/bridge/ETHBridge.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ describe('bridge tests', () => {
contractAddress: '0x1234',
spenderAddress: '0x',
});

expect(tx).toEqual({});
});

it('bridges with processing fee, owner !== to', async () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<script lang="ts">
import { fetchFeeData } from '@wagmi/core';
import { type Address,fetchFeeData } from '@wagmi/core';
import { BigNumber, Contract, ethers, type Signer } from 'ethers';
import { _ } from 'svelte-i18n';
Expand Down Expand Up @@ -357,7 +357,7 @@
// Set it manually.
tx.chainId = $srcChain.id;
const userAddress = await $signer.getAddress();
const userAddress = (await $signer.getAddress()) as Address;
let transactions: BridgeTransaction[] =
await storageService.getAllByAddress(userAddress);
Expand Down
35 changes: 18 additions & 17 deletions packages/bridge-ui/src/constants/envVars.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
// TODO: think about a way to blow up the build if env var is missing
// dotenv-safe ?
// TODO: explain each of these env vars
import type { Address } from '@wagmi/core';

import type { Address, ChainID } from '../domain/chain';
import type { ChainID } from '../domain/chain';

export const L1_RPC: string = import.meta.env?.VITE_L1_RPC_URL;

export const L1_TOKEN_VAULT_ADDRESS: Address = import.meta.env
?.VITE_L1_TOKEN_VAULT_ADDRESS;
export const L1_TOKEN_VAULT_ADDRESS = import.meta.env
?.VITE_L1_TOKEN_VAULT_ADDRESS as Address;

export const L1_BRIDGE_ADDRESS: Address = import.meta.env
?.VITE_L1_BRIDGE_ADDRESS;
export const L1_BRIDGE_ADDRESS = import.meta.env
?.VITE_L1_BRIDGE_ADDRESS as Address;

export const L1_CROSS_CHAIN_SYNC_ADDRESS: Address = import.meta.env
?.VITE_L1_CROSS_CHAIN_SYNC_ADDRESS;
export const L1_CROSS_CHAIN_SYNC_ADDRESS = import.meta.env
?.VITE_L1_CROSS_CHAIN_SYNC_ADDRESS as Address;

export const L1_SIGNAL_SERVICE_ADDRESS: Address = import.meta.env
?.VITE_L1_SIGNAL_SERVICE_ADDRESS;
export const L1_SIGNAL_SERVICE_ADDRESS = import.meta.env
?.VITE_L1_SIGNAL_SERVICE_ADDRESS as Address;

export const L1_CHAIN_ID: ChainID = parseInt(import.meta.env?.VITE_L1_CHAIN_ID);

Expand All @@ -26,17 +27,17 @@ export const L1_EXPLORER_URL: string = import.meta.env?.VITE_L1_EXPLORER_URL;

export const L2_RPC: string = import.meta.env?.VITE_L2_RPC_URL;

export const L2_TOKEN_VAULT_ADDRESS: Address = import.meta.env
?.VITE_L2_TOKEN_VAULT_ADDRESS;
export const L2_TOKEN_VAULT_ADDRESS = import.meta.env
?.VITE_L2_TOKEN_VAULT_ADDRESS as Address;

export const L2_BRIDGE_ADDRESS: Address = import.meta.env
?.VITE_L2_BRIDGE_ADDRESS;
export const L2_BRIDGE_ADDRESS = import.meta.env
?.VITE_L2_BRIDGE_ADDRESS as Address;

export const L2_CROSS_CHAIN_SYNC_ADDRESS: Address = import.meta.env
?.VITE_L2_CROSS_CHAIN_SYNC_ADDRESS;
export const L2_CROSS_CHAIN_SYNC_ADDRESS = import.meta.env
?.VITE_L2_CROSS_CHAIN_SYNC_ADDRESS as Address;

export const L2_SIGNAL_SERVICE_ADDRESS: Address = import.meta.env
?.VITE_L2_SIGNAL_SERVICE_ADDRESS;
export const L2_SIGNAL_SERVICE_ADDRESS = import.meta.env
?.VITE_L2_SIGNAL_SERVICE_ADDRESS as Address;

export const L2_CHAIN_ID: ChainID = parseInt(import.meta.env?.VITE_L2_CHAIN_ID);

Expand Down
3 changes: 1 addition & 2 deletions packages/bridge-ui/src/domain/chain.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import type { Address } from '@wagmi/core';
import type { ComponentType } from 'svelte';

export type ChainID = number;

export type Address = string;

export type Chain = {
id: ChainID;
name: string;
Expand Down
12 changes: 7 additions & 5 deletions packages/bridge-ui/src/domain/relayerApi.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import type { Address, ChainID } from './chain';
import type { Address } from '@wagmi/core';

import type { ChainID } from './chain';
import type { BridgeTransaction } from './transaction';

export type GetAllByAddressResponse = {
Expand All @@ -13,7 +15,7 @@ export type PaginationParams = {

export interface RelayerAPI {
getAllBridgeTransactionByAddress(
address: string,
address: Address,
pagination: PaginationParams,
chainID?: number,
): Promise<GetAllByAddressResponse>;
Expand Down Expand Up @@ -49,13 +51,13 @@ export type APIResponseTransaction = {
status: number;
eventType: number;
chainID: number;
canonicalTokenAddress: string;
canonicalTokenAddress: Address;
canonicalTokenSymbol: string;
canonicalTokenName: string;
canonicalTokenDecimals: number;
amount: string;
msgHash: string;
messageOwner: string;
messageOwner: Address;
event: string;
};

Expand All @@ -66,7 +68,7 @@ export type RelayerBlockInfo = {
};

export type APIRequestParams = {
address: string;
address: Address;
chainID?: number;
event?: string;
};
Expand Down
7 changes: 4 additions & 3 deletions packages/bridge-ui/src/domain/token.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import type { Address } from '@wagmi/core';
import type { ComponentType } from 'svelte';

type Address = {
type TokenAddress = {
chainId: number;
address: string;
address: Address;
};

export type Token = {
name: string;
addresses: Address[];
addresses: TokenAddress[];
symbol: string;
decimals: number;
logoUrl?: string;
Expand Down
1 change: 1 addition & 0 deletions packages/bridge-ui/src/globals.d.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
declare module '@lottiefiles/svelte-lottie-player';
declare module 'identicon.js';
80 changes: 36 additions & 44 deletions packages/bridge-ui/src/proof/ProofService.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,17 +121,15 @@ describe('prover tests', () => {
});

it('throws on invalid proof', async () => {
mockProvider.send.mockImplementation(
(method: string, params: unknown[]) => {
if (method === 'eth_getBlockByHash') {
return block;
}

if (method === 'eth_getProof') {
return invalidStorageProof;
}
},
);
mockProvider.send.mockImplementation((method: string) => {
if (method === 'eth_getBlockByHash') {
return block;
}

if (method === 'eth_getProof') {
return invalidStorageProof;
}
});

const prover: ProofService = new ProofService(map);

Expand All @@ -149,17 +147,15 @@ describe('prover tests', () => {
});

it('generates proof', async () => {
mockProvider.send.mockImplementation(
(method: string, params: unknown[]) => {
if (method === 'eth_getBlockByHash') {
return block;
}

if (method === 'eth_getProof') {
return storageProof;
}
},
);
mockProvider.send.mockImplementation((method: string) => {
if (method === 'eth_getBlockByHash') {
return block;
}

if (method === 'eth_getProof') {
return storageProof;
}
});

const prover: ProofService = new ProofService(map);

Expand All @@ -183,17 +179,15 @@ describe('generate release proof tests', () => {
});

it('throws on invalid proof', async () => {
mockProvider.send.mockImplementation(
(method: string, params: unknown[]) => {
if (method === 'eth_getBlockByHash') {
return block;
}

if (method === 'eth_getProof') {
return invalidStorageProof2;
}
},
);
mockProvider.send.mockImplementation((method: string) => {
if (method === 'eth_getBlockByHash') {
return block;
}

if (method === 'eth_getProof') {
return invalidStorageProof2;
}
});

const prover: ProofService = new ProofService(map);

Expand All @@ -211,17 +205,15 @@ describe('generate release proof tests', () => {
});

it('generates proof', async () => {
mockProvider.send.mockImplementation(
(method: string, params: unknown[]) => {
if (method === 'eth_getBlockByHash') {
return block;
}

if (method === 'eth_getProof') {
return storageProof2;
}
},
);
mockProvider.send.mockImplementation((method: string) => {
if (method === 'eth_getBlockByHash') {
return block;
}

if (method === 'eth_getProof') {
return storageProof2;
}
});

const prover: ProofService = new ProofService(map);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ describe('RelayerAPIService', () => {
data: dataFromAPI,
});

const data = await relayerApi.getAllBridgeTransactionByAddress('0x123', {
await relayerApi.getAllBridgeTransactionByAddress('0x123', {
page: 0,
size: 100,
});
Expand Down
8 changes: 2 additions & 6 deletions packages/bridge-ui/src/relayer-api/RelayerAPIService.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import type { Address } from '@wagmi/core';
import axios from 'axios';
import { BigNumber, Contract, ethers } from 'ethers';

import { chains } from '../chain/chains';
import { bridgeABI, erc20ABI, tokenVaultABI } from '../constants/abi';
import type { Address } from '../domain/chain';
import { MessageStatus } from '../domain/message';
import type { RecordProviders } from '../domain/provider';
import type {
Expand Down Expand Up @@ -126,14 +126,10 @@ export class RelayerAPIService implements RelayerAPI {
}

async getAllBridgeTransactionByAddress(
address: string,
address: Address,
paginationParams: PaginationParams,
chainID?: number,
): Promise<GetAllByAddressResponse> {
if (!address) {
throw new Error('address needed to fetch transactions');
}

const params = {
address,
chainID,
Expand Down
2 changes: 1 addition & 1 deletion packages/bridge-ui/src/signer/subscriber.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Signer, Transaction } from 'ethers';
import type { Signer } from 'ethers';

import type { PaginationInfo, RelayerBlockInfo } from '../domain/relayerApi';
import type { Token } from '../domain/token';
Expand Down
9 changes: 5 additions & 4 deletions packages/bridge-ui/src/signer/subscriber.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { Signer } from 'ethers';
import type { Address } from '@wagmi/core';
import { constants, type Signer } from 'ethers';

import type { PaginationInfo } from '../domain/relayerApi';
import type { BridgeTransaction } from '../domain/transaction';
Expand All @@ -13,7 +14,7 @@ const log = getLogger('signer:subscriber');

// We keep track of the current user address to avoid
// querying the API for transactions if the address is the same.
let currentUserAddress = '';
let currentUserAddress: Address = constants.AddressZero;

/**
* Subscribe to signer changes.
Expand All @@ -25,7 +26,7 @@ export async function subscribeToSigner(newSigner: Signer | null) {
if (newSigner) {
log('New signer set', newSigner);

const userAddress = await newSigner.getAddress();
const userAddress = (await newSigner.getAddress()) as Address;

// We actually don't want to run all this if the
// new address is the same as the previous one, since it
Expand Down Expand Up @@ -84,7 +85,7 @@ export async function subscribeToSigner(newSigner: Signer | null) {
} else {
log('Signer deleted');

currentUserAddress = '';
currentUserAddress = constants.AddressZero;
transactions.set([]);
userTokens.set([]);
paginationInfo.set(null);
Expand Down
2 changes: 1 addition & 1 deletion packages/bridge-ui/src/storage/StorageService.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { BigNumber, BigNumberish, ethers } from 'ethers';
import { BigNumber } from 'ethers';

import { L1_CHAIN_ID, L2_CHAIN_ID } from '../constants/envVars';
import { MessageStatus } from '../domain/message';
Expand Down
11 changes: 6 additions & 5 deletions packages/bridge-ui/src/storage/StorageService.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import type { Address } from '@wagmi/core';
import { BigNumber, Contract, ethers } from 'ethers';

import { chains } from '../chain/chains';
import { bridgeABI, erc20ABI, tokenVaultABI } from '../constants/abi';
import type { Address, ChainID } from '../domain/chain';
import type { ChainID } from '../domain/chain';
import { MessageStatus } from '../domain/message';
import type { BridgeTransaction, Transactioner } from '../domain/transaction';
import { jsonParseOrEmptyArray } from '../utils/jsonParseOrEmptyArray';
Expand Down Expand Up @@ -111,15 +112,15 @@ export class StorageService implements Transactioner {
this.providers = providers;
}

private _getTransactionsFromStorage(address: string): BridgeTransaction[] {
private _getTransactionsFromStorage(address: Address): BridgeTransaction[] {
const existingTransactions = this.storage.getItem(
`${STORAGE_PREFIX}-${address.toLowerCase()}`,
);

return jsonParseOrEmptyArray<BridgeTransaction>(existingTransactions);
}

async getAllByAddress(address: string): Promise<BridgeTransaction[]> {
async getAllByAddress(address: Address): Promise<BridgeTransaction[]> {
const txs = this._getTransactionsFromStorage(address);

log('Transactions from storage', txs);
Expand Down Expand Up @@ -231,7 +232,7 @@ export class StorageService implements Transactioner {
}

async getTransactionByHash(
address: string,
address: Address,
hash: string,
): Promise<BridgeTransaction | undefined> {
const txs = this._getTransactionsFromStorage(address);
Expand Down Expand Up @@ -329,7 +330,7 @@ export class StorageService implements Transactioner {
return bridgeTx;
}

updateStorageByAddress(address: string, txs: BridgeTransaction[] = []) {
updateStorageByAddress(address: Address, txs: BridgeTransaction[] = []) {
log('Updating storage with transactions', txs);
this.storage.setItem(
`${STORAGE_PREFIX}-${address.toLowerCase()}`,
Expand Down
Loading

0 comments on commit e48f17c

Please sign in to comment.