|
| 1 | +// Copyright 2026 Tether Operations Limited |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +import { useCallback, useMemo } from 'react' |
| 16 | +import { AccountService } from '../services/accountService' |
| 17 | +import { getWalletStore } from '../store/walletStore' |
| 18 | +import { useAddressLoader } from './useAddressLoader' |
| 19 | +import { requireInitialized } from '../utils/storeHelpers' |
| 20 | + |
| 21 | +export type UseProtocolParams = { |
| 22 | + accountIndex: number |
| 23 | + network: string |
| 24 | + protocolType: 'bridge' | 'swap' | 'lending' | 'fiat' |
| 25 | + protocolName: string |
| 26 | +} |
| 27 | + |
| 28 | +/** |
| 29 | + * Returns a typed proxied interface for calling protocol methods |
| 30 | + * (bridge, swap, lending, fiat) on a WDK account. |
| 31 | + * |
| 32 | + * @example |
| 33 | + * const usdt0Bridge = useProtocol<Usdt0ProtocolEvm>({ |
| 34 | + * network: 'ethereum', |
| 35 | + * accountIndex: 0, |
| 36 | + * protocolType: 'bridge', |
| 37 | + * protocolName: 'USDT0_EVM', |
| 38 | + * }) |
| 39 | + * |
| 40 | + * const quote = await usdt0Bridge.quoteBridge({ targetChain: 'arbitrum', ... }) |
| 41 | + * const result = await usdt0Bridge.bridge({ targetChain: 'arbitrum', ... }) |
| 42 | + */ |
| 43 | +export function useProtocol<T extends object>(params: UseProtocolParams): T { |
| 44 | + const { accountIndex, network, protocolType, protocolName } = params |
| 45 | + |
| 46 | + const { address } = useAddressLoader({ accountIndex, network }) |
| 47 | + const activeWalletId = getWalletStore()((state) => state.activeWalletId) |
| 48 | + |
| 49 | + const account = useMemo( |
| 50 | + () => |
| 51 | + activeWalletId && address |
| 52 | + ? { accountIndex, network, walletId: activeWalletId } |
| 53 | + : null, |
| 54 | + [accountIndex, network, activeWalletId, address], |
| 55 | + ) |
| 56 | + |
| 57 | + const protocol = useCallback((): T => { |
| 58 | + return new Proxy({} as T, { |
| 59 | + get: (_target, prop) => { |
| 60 | + if (prop === 'then') { |
| 61 | + return undefined |
| 62 | + } |
| 63 | + |
| 64 | + return async (...args: unknown[]) => { |
| 65 | + await requireInitialized() |
| 66 | + |
| 67 | + if (!account) { |
| 68 | + console.error( |
| 69 | + '[useProtocol] Protocol call failed: Account is not available. Ensure a wallet is active.', |
| 70 | + ) |
| 71 | + return undefined |
| 72 | + } |
| 73 | + |
| 74 | + if (typeof prop === 'string') { |
| 75 | + return await AccountService.callProtocolMethod( |
| 76 | + account.network, |
| 77 | + account.accountIndex, |
| 78 | + prop, |
| 79 | + protocolType, |
| 80 | + protocolName, |
| 81 | + ...args, |
| 82 | + ) |
| 83 | + } |
| 84 | + } |
| 85 | + }, |
| 86 | + }) |
| 87 | + }, [account, protocolType, protocolName]) |
| 88 | + |
| 89 | + return useMemo(() => protocol(), [protocol]) |
| 90 | +} |
0 commit comments