-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
170 additions
and
93 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 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,15 @@ | ||
import { GasPrice } from "@cosmjs/stargate"; | ||
|
||
export const DEFAULT_GAS_AMOUNT = (200_000).toString(); | ||
|
||
export const EVMOS_GAS_AMOUNT = (280_000).toString(); | ||
|
||
export function isChainIdEvmos(chainID: string) { | ||
return chainID === "evmos_9001-2" || chainID.includes("evmos"); | ||
} | ||
|
||
export async function getHotfixedGasPrice(chainID: string) { | ||
if (chainID === "noble-1") { | ||
return GasPrice.fromString("0.0uusdc"); | ||
} | ||
} |
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,87 @@ | ||
import { useManager } from "@cosmos-kit/react"; | ||
import { useQuery } from "@tanstack/react-query"; | ||
import { useMemo } from "react"; | ||
import { useAccount as useWagmiAccount } from "wagmi"; | ||
|
||
import { useAccount } from "@/hooks/useAccount"; | ||
import { useChains } from "@/hooks/useChains"; | ||
|
||
export function useWalletAddresses(chainIDs: string[]) { | ||
const { data: chains = [] } = useChains(); | ||
|
||
const { address: evmAddress } = useWagmiAccount(); | ||
const { getWalletRepo } = useManager(); | ||
|
||
const srcAccount = useAccount("source"); | ||
const dstAccount = useAccount("destination"); | ||
|
||
const queryKey = useMemo(() => ["USE_WALLET_ADDRESSES", chainIDs] as const, [chainIDs]); | ||
|
||
return useQuery({ | ||
queryKey, | ||
queryFn: async ({ queryKey: [, chainIDs] }) => { | ||
const record: Record<string, string> = {}; | ||
|
||
const srcChain = chains.find(({ chainID }) => { | ||
return chainID === chainIDs.at(0); | ||
}); | ||
const dstChain = chains.find(({ chainID }) => { | ||
return chainID === chainIDs.at(-1); | ||
}); | ||
|
||
for (const currentChainID of chainIDs) { | ||
const chain = chains.find(({ chainID }) => chainID === currentChainID); | ||
if (!chain) { | ||
throw new Error(`useWalletAddresses error: cannot find chain '${currentChainID}'`); | ||
} | ||
|
||
if (chain.chainType === "cosmos") { | ||
const { wallets } = getWalletRepo(chain.chainName); | ||
|
||
const currentWalletName = (() => { | ||
// if `chainID` is the source or destination chain | ||
if (srcChain?.chainID === currentChainID) { | ||
return srcAccount?.wallet?.walletName; | ||
} | ||
if (dstChain?.chainID === currentChainID) { | ||
return dstAccount?.wallet?.walletName; | ||
} | ||
|
||
// if `chainID` isn't the source or destination chain | ||
if (srcChain?.chainType === "cosmos") { | ||
return srcAccount?.wallet?.walletName; | ||
} | ||
if (dstChain?.chainType === "cosmos") { | ||
return dstAccount?.wallet?.walletName; | ||
} | ||
})(); | ||
|
||
if (!currentWalletName) { | ||
throw new Error(`useWalletAddresses error: cannot find wallet for '${chain.chainName}'`); | ||
} | ||
|
||
const wallet = wallets.find(({ walletName }) => walletName === currentWalletName); | ||
if (!wallet) { | ||
throw new Error(`useWalletAddresses error: cannot find wallet for '${chain.chainName}'`); | ||
} | ||
if (wallet.isWalletDisconnected || !wallet.isWalletConnected) { | ||
await wallet.connect(); | ||
} | ||
if (!wallet.address) { | ||
throw new Error(`useWalletAddresses error: cannot resolve wallet address for '${chain.chainName}'`); | ||
} | ||
record[currentChainID] = wallet.address; | ||
} | ||
|
||
if (chain.chainType === "evm") { | ||
if (!evmAddress) { | ||
throw new Error(`useWalletAddresses error: evm wallet not connected`); | ||
} | ||
record[currentChainID] = evmAddress; | ||
} | ||
} | ||
|
||
return record; | ||
}, | ||
}); | ||
} |