-
Notifications
You must be signed in to change notification settings - Fork 156
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Upgrade repo to yarn 4 - Upgrade wagmi and rainbowkit versions - Use Viem instead of Ethers for tx sending - Refactor wallet/hooks.ts into smaller protocol-specific files - Fix small cosmos metadata issue
Showing
35 changed files
with
6,775 additions
and
6,081 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,13 @@ | ||
compressionLevel: mixed | ||
|
||
enableGlobalCache: false | ||
|
||
enableScripts: false | ||
|
||
nodeLinker: node-modules | ||
|
||
plugins: | ||
- path: .yarn/plugins/@yarnpkg/plugin-outdated.cjs | ||
spec: "https://mskelton.dev/yarn-outdated/v3" | ||
|
||
yarnPath: .yarn/releases/yarn-3.2.0.cjs | ||
yarnPath: .yarn/releases/yarn-4.0.2.cjs |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
import { DeliverTxResponse, ExecuteResult } from '@cosmjs/cosmwasm-stargate'; | ||
import { useChain, useChains } from '@cosmos-kit/react'; | ||
import { useCallback, useMemo } from 'react'; | ||
import { toast } from 'react-toastify'; | ||
|
||
import { ProtocolType } from '@hyperlane-xyz/utils'; | ||
|
||
import { PLACEHOLDER_COSMOS_CHAIN } from '../../../consts/values'; | ||
import { logger } from '../../../utils/logger'; | ||
import { getCaip2Id } from '../../caip/chains'; | ||
import { getCosmosChainNames } from '../../chains/metadata'; | ||
import { getChainMetadata, getMultiProvider } from '../../multiProvider'; | ||
|
||
import { AccountInfo, ActiveChainInfo, ChainAddress, ChainTransactionFns } from './types'; | ||
|
||
export function useCosmosAccount(): AccountInfo { | ||
const chainToContext = useChains(getCosmosChainNames()); | ||
return useMemo<AccountInfo>(() => { | ||
const cosmAddresses: Array<ChainAddress> = []; | ||
let cosmConnectorName: string | undefined = undefined; | ||
let isCosmAccountReady = false; | ||
const multiProvider = getMultiProvider(); | ||
for (const [chainName, context] of Object.entries(chainToContext)) { | ||
if (!context.address) continue; | ||
const caip2Id = getCaip2Id(ProtocolType.Cosmos, multiProvider.getChainId(chainName)); | ||
cosmAddresses.push({ address: context.address, chainCaip2Id: caip2Id }); | ||
isCosmAccountReady = true; | ||
cosmConnectorName ||= context.wallet?.prettyName; | ||
} | ||
return { | ||
protocol: ProtocolType.Cosmos, | ||
addresses: cosmAddresses, | ||
connectorName: cosmConnectorName, | ||
isReady: isCosmAccountReady, | ||
}; | ||
}, [chainToContext]); | ||
} | ||
|
||
export function useCosmosConnectFn(): () => void { | ||
const { openView } = useChain(PLACEHOLDER_COSMOS_CHAIN); | ||
return openView; | ||
} | ||
|
||
export function useCosmosDisconnectFn(): () => Promise<void> { | ||
const { disconnect, address } = useChain(PLACEHOLDER_COSMOS_CHAIN); | ||
const safeDisconnect = async () => { | ||
if (address) await disconnect(); | ||
}; | ||
return safeDisconnect; | ||
} | ||
|
||
export function useCosmosActiveChain(): ActiveChainInfo { | ||
return useMemo(() => ({} as ActiveChainInfo), []); | ||
} | ||
|
||
export function useCosmosTransactionFns(): ChainTransactionFns { | ||
const chainToContext = useChains(getCosmosChainNames()); | ||
|
||
const onSwitchNetwork = useCallback(async (chainCaip2Id: ChainCaip2Id) => { | ||
const chainName = getChainMetadata(chainCaip2Id).displayName; | ||
toast.warn(`Cosmos wallet must be connected to origin chain ${chainName}}`); | ||
}, []); | ||
|
||
const onSendTx = useCallback( | ||
async ({ | ||
tx, | ||
chainCaip2Id, | ||
activeCap2Id, | ||
}: { | ||
tx: { type: 'cosmwasm' | 'stargate'; request: any }; | ||
chainCaip2Id: ChainCaip2Id; | ||
activeCap2Id?: ChainCaip2Id; | ||
}) => { | ||
const chainName = getChainMetadata(chainCaip2Id).name; | ||
const chainContext = chainToContext[chainName]; | ||
if (!chainContext?.address) throw new Error(`Cosmos wallet not connected for ${chainName}`); | ||
if (activeCap2Id && activeCap2Id !== chainCaip2Id) await onSwitchNetwork(chainCaip2Id); | ||
logger.debug(`Sending ${tx.type} tx on chain ${chainCaip2Id}`); | ||
const { getSigningCosmWasmClient, getSigningStargateClient } = chainContext; | ||
let result: ExecuteResult | DeliverTxResponse; | ||
if (tx.type === 'cosmwasm') { | ||
const client = await getSigningCosmWasmClient(); | ||
result = await client.executeMultiple(chainContext.address, [tx.request], 'auto'); | ||
} else if (tx.type === 'stargate') { | ||
const client = await getSigningStargateClient(); | ||
result = await client.signAndBroadcast(chainContext.address, [tx.request], 'auto'); | ||
} else { | ||
throw new Error('Invalid cosmos tx type'); | ||
} | ||
|
||
const confirm = async () => { | ||
if (result.transactionHash) return result; | ||
throw new Error(`Cosmos tx ${result.transactionHash} failed: ${JSON.stringify(result)}`); | ||
}; | ||
return { hash: result.transactionHash, confirm }; | ||
}, | ||
[onSwitchNetwork, chainToContext], | ||
); | ||
|
||
return { sendTransaction: onSendTx, switchNetwork: onSwitchNetwork }; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
import { useConnectModal } from '@rainbow-me/rainbowkit'; | ||
import { | ||
SendTransactionArgs, | ||
sendTransaction, | ||
switchNetwork, | ||
waitForTransaction, | ||
} from '@wagmi/core'; | ||
import { useCallback, useMemo } from 'react'; | ||
import { useAccount, useDisconnect, useNetwork } from 'wagmi'; | ||
|
||
import { ProtocolType, sleep } from '@hyperlane-xyz/utils'; | ||
|
||
import { logger } from '../../../utils/logger'; | ||
import { getCaip2Id, getEthereumChainId } from '../../caip/chains'; | ||
|
||
import { AccountInfo, ActiveChainInfo, ChainTransactionFns } from './types'; | ||
|
||
export function useEvmAccount(): AccountInfo { | ||
const { address, isConnected, connector } = useAccount(); | ||
const isReady = !!(address && isConnected && connector); | ||
const connectorName = connector?.name; | ||
|
||
return useMemo<AccountInfo>( | ||
() => ({ | ||
protocol: ProtocolType.Ethereum, | ||
addresses: address ? [{ address: `${address}` }] : [], | ||
connectorName: connectorName, | ||
isReady: isReady, | ||
}), | ||
[address, connectorName, isReady], | ||
); | ||
} | ||
|
||
export function useEvmConnectFn(): () => void { | ||
const { openConnectModal } = useConnectModal(); | ||
return useCallback(() => openConnectModal?.(), [openConnectModal]); | ||
} | ||
|
||
export function useEvmDisconnectFn(): () => Promise<void> { | ||
const { disconnectAsync } = useDisconnect(); | ||
return disconnectAsync; | ||
} | ||
|
||
export function useEvmActiveChain(): ActiveChainInfo { | ||
const { chain } = useNetwork(); | ||
return useMemo<ActiveChainInfo>( | ||
() => ({ | ||
chainDisplayName: chain?.name, | ||
chainCaip2Id: chain ? getCaip2Id(ProtocolType.Ethereum, chain.id) : undefined, | ||
}), | ||
[chain], | ||
); | ||
} | ||
|
||
export function useEvmTransactionFns(): ChainTransactionFns { | ||
const onSwitchNetwork = useCallback(async (chainCaip2Id: ChainCaip2Id) => { | ||
const chainId = getEthereumChainId(chainCaip2Id); | ||
await switchNetwork({ chainId }); | ||
// Some wallets seem to require a brief pause after switch | ||
await sleep(2000); | ||
}, []); | ||
// Note, this doesn't use wagmi's prepare + send pattern because we're potentially sending two transactions | ||
// The prepare hooks are recommended to use pre-click downtime to run async calls, but since the flow | ||
// may require two serial txs, the prepare hooks aren't useful and complicate hook architecture considerably. | ||
// See https://github.com/hyperlane-xyz/hyperlane-warp-ui-template/issues/19 | ||
// See https://github.com/wagmi-dev/wagmi/discussions/1564 | ||
const onSendTx = useCallback( | ||
async ({ | ||
tx, | ||
chainCaip2Id, | ||
activeCap2Id, | ||
}: { | ||
tx: SendTransactionArgs; | ||
chainCaip2Id: ChainCaip2Id; | ||
activeCap2Id?: ChainCaip2Id; | ||
}) => { | ||
if (activeCap2Id && activeCap2Id !== chainCaip2Id) await onSwitchNetwork(chainCaip2Id); | ||
const chainId = getEthereumChainId(chainCaip2Id); | ||
logger.debug(`Sending tx on chain ${chainCaip2Id}`); | ||
const { hash } = await sendTransaction({ | ||
chainId, | ||
...tx, | ||
}); | ||
const confirm = () => waitForTransaction({ chainId, hash, confirmations: 1 }); | ||
return { hash, confirm }; | ||
}, | ||
[onSwitchNetwork], | ||
); | ||
|
||
return { sendTransaction: onSendTx, switchNetwork: onSwitchNetwork }; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,184 @@ | ||
import { useMemo } from 'react'; | ||
import { toast } from 'react-toastify'; | ||
|
||
import { ProtocolType } from '@hyperlane-xyz/utils'; | ||
|
||
import { logger } from '../../../utils/logger'; | ||
import { tryGetProtocolType } from '../../caip/chains'; | ||
|
||
import { | ||
useCosmosAccount, | ||
useCosmosActiveChain, | ||
useCosmosConnectFn, | ||
useCosmosDisconnectFn, | ||
useCosmosTransactionFns, | ||
} from './cosmos'; | ||
import { | ||
useEvmAccount, | ||
useEvmActiveChain, | ||
useEvmConnectFn, | ||
useEvmDisconnectFn, | ||
useEvmTransactionFns, | ||
} from './evm'; | ||
import { | ||
useSolAccount, | ||
useSolActiveChain, | ||
useSolConnectFn, | ||
useSolDisconnectFn, | ||
useSolTransactionFns, | ||
} from './solana'; | ||
import { AccountInfo, ActiveChainInfo, ChainTransactionFns } from './types'; | ||
|
||
export function useAccounts(): { | ||
accounts: Record<ProtocolType, AccountInfo>; | ||
readyAccounts: Array<AccountInfo>; | ||
} { | ||
const evmAccountInfo = useEvmAccount(); | ||
const solAccountInfo = useSolAccount(); | ||
const cosmAccountInfo = useCosmosAccount(); | ||
|
||
// Filtered ready accounts | ||
const readyAccounts = useMemo( | ||
() => [evmAccountInfo, solAccountInfo, cosmAccountInfo].filter((a) => a.isReady), | ||
[evmAccountInfo, solAccountInfo, cosmAccountInfo], | ||
); | ||
|
||
return useMemo( | ||
() => ({ | ||
accounts: { | ||
[ProtocolType.Ethereum]: evmAccountInfo, | ||
[ProtocolType.Sealevel]: solAccountInfo, | ||
[ProtocolType.Cosmos]: cosmAccountInfo, | ||
[ProtocolType.Fuel]: { protocol: ProtocolType.Fuel, isReady: false, addresses: [] }, | ||
}, | ||
readyAccounts, | ||
}), | ||
[evmAccountInfo, solAccountInfo, cosmAccountInfo, readyAccounts], | ||
); | ||
} | ||
|
||
export function useAccountForChain(chainCaip2Id?: ChainCaip2Id): AccountInfo | undefined { | ||
const { accounts } = useAccounts(); | ||
if (!chainCaip2Id) return undefined; | ||
const protocol = tryGetProtocolType(chainCaip2Id); | ||
if (!protocol) return undefined; | ||
return accounts[protocol]; | ||
} | ||
|
||
export function useAccountAddressForChain(chainCaip2Id?: ChainCaip2Id): Address | undefined { | ||
return getAccountAddressForChain(chainCaip2Id, useAccountForChain(chainCaip2Id)); | ||
} | ||
|
||
export function getAccountAddressForChain( | ||
chainCaip2Id?: ChainCaip2Id, | ||
account?: AccountInfo, | ||
): Address | undefined { | ||
if (!chainCaip2Id || !account?.addresses.length) return undefined; | ||
if (account.protocol === ProtocolType.Cosmos) { | ||
return account.addresses.find((a) => a.chainCaip2Id === chainCaip2Id)?.address; | ||
} else { | ||
// Use first because only cosmos has the notion of per-chain addresses | ||
return account.addresses[0].address; | ||
} | ||
} | ||
|
||
export function useConnectFns(): Record<ProtocolType, () => void> { | ||
const onConnectEthereum = useEvmConnectFn(); | ||
const onConnectSolana = useSolConnectFn(); | ||
const onConnectCosmos = useCosmosConnectFn(); | ||
|
||
return useMemo( | ||
() => ({ | ||
[ProtocolType.Ethereum]: onConnectEthereum, | ||
[ProtocolType.Sealevel]: onConnectSolana, | ||
[ProtocolType.Cosmos]: onConnectCosmos, | ||
[ProtocolType.Fuel]: () => alert('TODO'), | ||
}), | ||
[onConnectEthereum, onConnectSolana, onConnectCosmos], | ||
); | ||
} | ||
|
||
export function useDisconnectFns(): Record<ProtocolType, () => Promise<void>> { | ||
const disconnectEvm = useEvmDisconnectFn(); | ||
const disconnectSol = useSolDisconnectFn(); | ||
const disconnectCosmos = useCosmosDisconnectFn(); | ||
|
||
const onClickDisconnect = | ||
(env: ProtocolType, disconnectFn?: () => Promise<void> | void) => async () => { | ||
try { | ||
if (!disconnectFn) throw new Error('Disconnect function is null'); | ||
await disconnectFn(); | ||
} catch (error) { | ||
logger.error(`Error disconnecting from ${env} wallet`, error); | ||
toast.error('Could not disconnect wallet'); | ||
} | ||
}; | ||
|
||
return useMemo( | ||
() => ({ | ||
[ProtocolType.Ethereum]: onClickDisconnect(ProtocolType.Ethereum, disconnectEvm), | ||
[ProtocolType.Sealevel]: onClickDisconnect(ProtocolType.Sealevel, disconnectSol), | ||
[ProtocolType.Cosmos]: onClickDisconnect(ProtocolType.Cosmos, disconnectCosmos), | ||
[ProtocolType.Fuel]: onClickDisconnect(ProtocolType.Fuel, () => { | ||
'TODO'; | ||
}), | ||
}), | ||
[disconnectEvm, disconnectSol, disconnectCosmos], | ||
); | ||
} | ||
|
||
export function useActiveChains(): { | ||
chains: Record<ProtocolType, ActiveChainInfo>; | ||
readyChains: Array<ActiveChainInfo>; | ||
} { | ||
const evmChain = useEvmActiveChain(); | ||
const solChain = useSolActiveChain(); | ||
const cosmChain = useCosmosActiveChain(); | ||
|
||
const readyChains = useMemo( | ||
() => [evmChain, solChain, cosmChain].filter((c) => !!c.chainDisplayName), | ||
[evmChain, solChain, cosmChain], | ||
); | ||
|
||
return useMemo( | ||
() => ({ | ||
chains: { | ||
[ProtocolType.Ethereum]: evmChain, | ||
[ProtocolType.Sealevel]: solChain, | ||
[ProtocolType.Cosmos]: cosmChain, | ||
[ProtocolType.Fuel]: {}, | ||
}, | ||
readyChains, | ||
}), | ||
[evmChain, solChain, cosmChain, readyChains], | ||
); | ||
} | ||
|
||
export function useTransactionFns(): Record<ProtocolType, ChainTransactionFns> { | ||
const { switchNetwork: onSwitchEvmNetwork, sendTransaction: onSendEvmTx } = | ||
useEvmTransactionFns(); | ||
const { switchNetwork: onSwitchSolNetwork, sendTransaction: onSendSolTx } = | ||
useSolTransactionFns(); | ||
const { switchNetwork: onSwitchCosmNetwork, sendTransaction: onSendCosmTx } = | ||
useCosmosTransactionFns(); | ||
|
||
return useMemo( | ||
() => ({ | ||
[ProtocolType.Ethereum]: { sendTransaction: onSendEvmTx, switchNetwork: onSwitchEvmNetwork }, | ||
[ProtocolType.Sealevel]: { sendTransaction: onSendSolTx, switchNetwork: onSwitchSolNetwork }, | ||
[ProtocolType.Cosmos]: { sendTransaction: onSendCosmTx, switchNetwork: onSwitchCosmNetwork }, | ||
[ProtocolType.Fuel]: { | ||
sendTransaction: () => alert('TODO') as any, | ||
switchNetwork: () => alert('TODO') as any, | ||
}, | ||
}), | ||
[ | ||
onSendEvmTx, | ||
onSendSolTx, | ||
onSwitchEvmNetwork, | ||
onSwitchSolNetwork, | ||
onSendCosmTx, | ||
onSwitchCosmNetwork, | ||
], | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
import { useConnection, useWallet } from '@solana/wallet-adapter-react'; | ||
import { useWalletModal } from '@solana/wallet-adapter-react-ui'; | ||
import { Connection, Transaction } from '@solana/web3.js'; | ||
import { useCallback, useMemo } from 'react'; | ||
import { toast } from 'react-toastify'; | ||
|
||
import { ProtocolType } from '@hyperlane-xyz/utils'; | ||
|
||
import { logger } from '../../../utils/logger'; | ||
import { getCaip2Id, getChainReference } from '../../caip/chains'; | ||
import { getChainByRpcEndpoint } from '../../chains/utils'; | ||
import { getChainMetadata, getMultiProvider } from '../../multiProvider'; | ||
|
||
import { AccountInfo, ActiveChainInfo, ChainTransactionFns } from './types'; | ||
|
||
export function useSolAccount(): AccountInfo { | ||
const { publicKey, connected, wallet } = useWallet(); | ||
const isReady = !!(publicKey && wallet && connected); | ||
const address = publicKey?.toBase58(); | ||
const connectorName = wallet?.adapter?.name; | ||
|
||
return useMemo<AccountInfo>( | ||
() => ({ | ||
protocol: ProtocolType.Sealevel, | ||
addresses: address ? [{ address: address }] : [], | ||
connectorName: connectorName, | ||
isReady: isReady, | ||
}), | ||
[address, connectorName, isReady], | ||
); | ||
} | ||
|
||
export function useSolConnectFn(): () => void { | ||
const { setVisible } = useWalletModal(); | ||
return useCallback(() => setVisible(true), [setVisible]); | ||
} | ||
|
||
export function useSolDisconnectFn(): () => Promise<void> { | ||
const { disconnect } = useWallet(); | ||
return disconnect; | ||
} | ||
|
||
export function useSolActiveChain(): ActiveChainInfo { | ||
const { connection } = useConnection(); | ||
const connectionEndpoint = connection?.rpcEndpoint; | ||
return useMemo<ActiveChainInfo>(() => { | ||
const metadata = getChainByRpcEndpoint(connectionEndpoint); | ||
if (!metadata) return {}; | ||
return { | ||
chainDisplayName: metadata.displayName, | ||
chainCaip2Id: getCaip2Id(ProtocolType.Sealevel, metadata.chainId), | ||
}; | ||
}, [connectionEndpoint]); | ||
} | ||
|
||
export function useSolTransactionFns(): ChainTransactionFns { | ||
const { sendTransaction: sendSolTransaction } = useWallet(); | ||
|
||
const onSwitchNetwork = useCallback(async (chainCaip2Id: ChainCaip2Id) => { | ||
const chainName = getChainMetadata(chainCaip2Id).displayName; | ||
toast.warn(`Solana wallet must be connected to origin chain ${chainName}}`); | ||
}, []); | ||
|
||
const onSendTx = useCallback( | ||
async ({ | ||
tx, | ||
chainCaip2Id, | ||
activeCap2Id, | ||
}: { | ||
tx: Transaction; | ||
chainCaip2Id: ChainCaip2Id; | ||
activeCap2Id?: ChainCaip2Id; | ||
}) => { | ||
if (activeCap2Id && activeCap2Id !== chainCaip2Id) await onSwitchNetwork(chainCaip2Id); | ||
const rpcUrl = getMultiProvider().getRpcUrl(getChainReference(chainCaip2Id)); | ||
const connection = new Connection(rpcUrl, 'confirmed'); | ||
const { | ||
context: { slot: minContextSlot }, | ||
value: { blockhash, lastValidBlockHeight }, | ||
} = await connection.getLatestBlockhashAndContext(); | ||
|
||
logger.debug(`Sending tx on chain ${chainCaip2Id}`); | ||
const signature = await sendSolTransaction(tx, connection, { minContextSlot }); | ||
|
||
const confirm = () => | ||
connection.confirmTransaction({ blockhash, lastValidBlockHeight, signature }); | ||
return { hash: signature, confirm }; | ||
}, | ||
[onSwitchNetwork, sendSolTransaction], | ||
); | ||
|
||
return { sendTransaction: onSendTx, switchNetwork: onSwitchNetwork }; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { ProtocolType } from '@hyperlane-xyz/utils'; | ||
|
||
export interface ChainAddress { | ||
address: string; | ||
chainCaip2Id?: ChainCaip2Id; | ||
} | ||
|
||
export interface AccountInfo { | ||
protocol: ProtocolType; | ||
// This needs to be an array instead of a single address b.c. | ||
// Cosmos wallets have different addresses per chain | ||
addresses: Array<ChainAddress>; | ||
connectorName?: string; | ||
isReady: boolean; | ||
} | ||
|
||
export interface ActiveChainInfo { | ||
chainDisplayName?: string; | ||
chainCaip2Id?: ChainCaip2Id; | ||
} | ||
|
||
export type SendTransactionFn<TxReq = any, TxResp = any> = (params: { | ||
tx: TxReq; | ||
chainCaip2Id: ChainCaip2Id; | ||
activeCap2Id?: ChainCaip2Id; | ||
}) => Promise<{ hash: string; confirm: () => Promise<TxResp> }>; | ||
|
||
export type SwitchNetworkFn = (chainCaip2Id: ChainCaip2Id) => Promise<void>; | ||
|
||
export interface ChainTransactionFns { | ||
sendTransaction: SendTransactionFn; | ||
switchNetwork?: SwitchNetworkFn; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { SendTransactionArgs as ViemTransactionRequest } from '@wagmi/core'; | ||
import { PopulatedTransaction as Ethers5Transaction, BigNumber as EthersBN } from 'ethers'; | ||
|
||
export function ethers5TxToWagmiTx(tx: Ethers5Transaction): ViemTransactionRequest { | ||
if (!tx.to) throw new Error('No tx recipient address specified'); | ||
if (!tx.data) throw new Error('No tx data specified'); | ||
return { | ||
to: tx.to, | ||
value: ethersBnToBigInt(tx.value || EthersBN.from('0')), | ||
data: tx.data as `0x{string}`, | ||
nonce: tx.nonce, | ||
chainId: tx.chainId, | ||
gas: tx.gasLimit ? ethersBnToBigInt(tx.gasLimit) : undefined, | ||
gasPrice: tx.gasPrice ? ethersBnToBigInt(tx.gasPrice) : undefined, | ||
maxFeePerGas: tx.maxFeePerGas ? ethersBnToBigInt(tx.maxFeePerGas) : undefined, | ||
maxPriorityFeePerGas: tx.maxPriorityFeePerGas | ||
? ethersBnToBigInt(tx.maxPriorityFeePerGas) | ||
: undefined, | ||
}; | ||
} | ||
|
||
function ethersBnToBigInt(bn: EthersBN): bigint { | ||
return BigInt(bn.toString()); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
badec99
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
hyperlane-warp-template – ./
hyperlane-warp-template-git-main-abacus-works.vercel.app
usenexus.org
hyperlane-warp-template.vercel.app
hyperlane-warp-template-abacus-works.vercel.app
nautbridge.com