Skip to content

Commit

Permalink
feat: export appchain bridge components, hooks, and types (#1984)
Browse files Browse the repository at this point in the history
  • Loading branch information
0xAlec authored Feb 13, 2025
1 parent f6b1235 commit 8144da9
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 52 deletions.
6 changes: 6 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,12 @@
"import": "./esm/api/index.js",
"default": "./esm/api/index.js"
},
"./appchain": {
"types": "./esm/appchain/index.d.ts",
"module": "./esm/appchain/index.js",
"import": "./esm/appchain/index.js",
"default": "./esm/appchain/index.js"
},
"./buy": {
"types": "./esm/buy/index.d.ts",
"module": "./esm/buy/index.js",
Expand Down
13 changes: 3 additions & 10 deletions src/appchain/bridge/hooks/useDeposit.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,13 @@
import { useCallback, useState } from 'react';
import { parseEther, parseUnits } from 'viem';
import type { Chain, Hex } from 'viem';
import type { Hex } from 'viem';
import { useAccount, useConfig, useSwitchChain, useWriteContract } from 'wagmi';
import { waitForTransactionReceipt } from 'wagmi/actions';
import { ERC20ABI, OptimismPortalABI, StandardBridgeABI } from '../abi';
import { EXTRA_DATA, MIN_GAS_LIMIT } from '../constants';
import type { AppchainConfig } from '../types';
import type { BridgeParams } from '../types';
import type { UseDepositParams } from '../types';
import { isUserRejectedRequestError } from '../utils/isUserRejectedRequestError';

interface DepositParams {
config: AppchainConfig;
from: Chain;
bridgeParams: BridgeParams;
}

export function useDeposit() {
const { writeContractAsync, data } = useWriteContract();
const { switchChainAsync } = useSwitchChain();
Expand All @@ -29,7 +22,7 @@ export function useDeposit() {
}, []);

// biome-ignore lint/complexity/noExcessiveCognitiveComplexity: ignore
const deposit = async ({ config, from, bridgeParams }: DepositParams) => {
const deposit = async ({ config, from, bridgeParams }: UseDepositParams) => {
if (!bridgeParams.recipient) {
throw new Error('Recipient is required');
}
Expand Down
10 changes: 2 additions & 8 deletions src/appchain/bridge/hooks/useDepositButton.tsx
Original file line number Diff line number Diff line change
@@ -1,19 +1,13 @@
import { Spinner } from '@/internal/components/Spinner';
import { useMemo } from 'react';
import { useAccount } from 'wagmi';
import type { BridgeParams } from '../types';

interface UseDepositButtonProps {
depositStatus: string;
withdrawStatus: string;
bridgeParams: BridgeParams;
}
import type { UseDepositButtonParams } from '../types';

export function useDepositButton({
depositStatus,
withdrawStatus,
bridgeParams,
}: UseDepositButtonProps) {
}: UseDepositButtonParams) {
const { isConnected } = useAccount();

const isPending =
Expand Down
18 changes: 2 additions & 16 deletions src/appchain/bridge/hooks/useWithdraw.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
import { useCallback, useState } from 'react';
import {
type Chain,
type Hex,
erc20Abi,
keccak256,
parseEther,
parseUnits,
} from 'viem';
import { type Hex, erc20Abi, keccak256, parseEther, parseUnits } from 'viem';
import { getWithdrawalHashStorageSlot, getWithdrawals } from 'viem/op-stack';
import { useAccount, useConfig, useSwitchChain, useWriteContract } from 'wagmi';
import {
Expand All @@ -28,18 +21,11 @@ import {
MIN_GAS_LIMIT,
OUTPUT_ROOT_PROOF_VERSION,
} from '../constants';
import type { AppchainConfig } from '../types';
import type { BridgeParams } from '../types';
import type { UseWithdrawParams } from '../types';
import { getOutput } from '../utils/getOutput';
import { isUserRejectedRequestError } from '../utils/isUserRejectedRequestError';
import { maybeAddProofNode } from '../utils/maybeAddProofNode';

interface UseWithdrawParams {
config: AppchainConfig;
chain: Chain;
bridgeParams: BridgeParams;
}

export const useWithdraw = ({
config,
chain,
Expand Down
19 changes: 2 additions & 17 deletions src/appchain/bridge/hooks/useWithdrawButton.tsx
Original file line number Diff line number Diff line change
@@ -1,23 +1,8 @@
import { Spinner } from '@/internal/components/Spinner';
import { useMemo } from 'react';
import type { ReactNode } from 'react';
import type { UseWithdrawButtonParams } from '../types';

interface UseWithdrawButtonProps {
withdrawStatus: string;
}

interface UseWithdrawButtonReturn {
isPending: boolean;
isSuccess: boolean;
buttonDisabled: boolean;
buttonContent: ReactNode;
shouldShowClaim: boolean;
label: string;
}

export function useWithdrawButton({
withdrawStatus,
}: UseWithdrawButtonProps): UseWithdrawButtonReturn {
export function useWithdrawButton({ withdrawStatus }: UseWithdrawButtonParams) {
const isPending = withdrawStatus === 'claimPending';
const isSuccess = withdrawStatus === 'claimSuccess';
const buttonDisabled = isPending;
Expand Down
45 changes: 44 additions & 1 deletion src/appchain/bridge/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ export type AppchainBridgeReact = {
bridgeableTokens?: BridgeableToken[];
};

/**
* Note: exported as public Type
*/
export type AppchainBridgeContextType = {
// Configuration
config: AppchainConfig;
Expand Down Expand Up @@ -92,14 +95,54 @@ export type AppchainConfig = {
};
};

/**
* Note: exported as public Type
*/
export type BridgeParams = {
amount: string;
amountUSD: string;
token: BridgeableToken;
recipient?: Address;
};

export type FinalizeWithdrawalParams = {
/**
* Note: exported as public Type
*/
export type ChainConfigParams = {
config: AppchainConfig;
chain: Chain;
};

/**
* Note: exported as public Type
*/
export type UseDepositParams = {
config: AppchainConfig;
from: Chain;
bridgeParams: BridgeParams;
};

/**
* Note: exported as public Type
*/
export type UseWithdrawParams = {
config: AppchainConfig;
chain: Chain;
bridgeParams: BridgeParams;
};

/**
* Note: exported as public Type
*/
export type UseDepositButtonParams = {
depositStatus: string;
withdrawStatus: string;
bridgeParams: BridgeParams;
};

/**
* Note: exported as public Type
*/
export type UseWithdrawButtonParams = {
withdrawStatus: string;
};
35 changes: 35 additions & 0 deletions src/appchain/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// 🌲☀🌲
// Components
export { AppchainBridge } from './bridge/components/AppchainBridge';
export { AppchainBridgeProvider } from './bridge/components/AppchainBridgeProvider';
export { AppchainBridgeInput } from './bridge/components/AppchainBridgeInput';
export { AppchainBridgeNetwork } from './bridge/components/AppchainBridgeNetwork';
export { AppchainBridgeTransactionButton } from './bridge/components/AppchainBridgeTransactionButton';
export { AppchainBridgeWithdraw } from './bridge/components/AppchainBridgeWithdraw';
export { AppchainNetworkToggleButton } from './bridge/components/AppchainNetworkToggleButton';

// Hooks
export { useChainConfig } from './bridge/hooks/useAppchainConfig';
export { useDeposit } from './bridge/hooks/useDeposit';
export { useWithdraw } from './bridge/hooks/useWithdraw';
export { useDepositButton } from './bridge/hooks/useDepositButton';
export { useWithdrawButton } from './bridge/hooks/useWithdrawButton';
export { useAppchainBridgeContext } from './bridge/components/AppchainBridgeProvider';

// Utils
export { getETHPrice } from './bridge/utils/getETHPrice';
export { getOutput } from './bridge/utils/getOutput';
export { defaultPriceFetcher } from './bridge/utils/defaultPriceFetcher';

// Types
export type {
ChainConfigParams,
UseDepositParams,
UseWithdrawParams,
UseDepositButtonParams,
UseWithdrawButtonParams,
Appchain,
AppchainBridgeReact,
AppchainConfig,
BridgeableToken,
} from './bridge/types';

0 comments on commit 8144da9

Please sign in to comment.