generated from scaffold-eth/scaffold-eth-2
-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: transaction with unknown networks
- Loading branch information
1 parent
759aa72
commit 6da0d3d
Showing
3 changed files
with
96 additions
and
92 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,45 +1,33 @@ | ||
import { getChain } from "@dynamic-labs/viem-utils"; | ||
import { Wallet } from "@dynamic-labs/sdk-react-core"; | ||
import { getOrMapViemChain } from "@dynamic-labs/viem-utils"; | ||
import { Account, Chain, Hex, Transport, WalletClient, parseEther } from "viem"; | ||
import { customEvmNetworks } from "~~/lib/networks"; | ||
|
||
export const signMessage = async (message: string, wallet: any): Promise<string> => { | ||
if (!wallet) { | ||
throw new Error("No wallet found"); | ||
} | ||
|
||
const connector = wallet?.connector; | ||
|
||
if (!connector) { | ||
throw new Error("No connector found"); | ||
} | ||
|
||
return await connector.signMessage(message); | ||
}; | ||
|
||
export const sendTransaction = async (address: any, amount: string, wallet: any): Promise<string> => { | ||
if (!wallet) { | ||
throw new Error("No wallet found"); | ||
} | ||
export const sendTransaction = async (address: any, amount: string, wallet: Wallet): Promise<string> => { | ||
const walletClient = wallet.connector.getWalletClient<WalletClient<Transport, Chain, Account>>(); | ||
|
||
const connector = wallet?.connector; | ||
const chainID = await wallet.connector.getNetwork(); | ||
const currentNetwork = customEvmNetworks.find(network => network.chainId === chainID); | ||
|
||
if (!connector) { | ||
return "No connector found"; | ||
if (!currentNetwork) { | ||
throw new Error("No chain ID found"); | ||
} | ||
|
||
const provider: WalletClient<Transport, Chain, Account> = await wallet.connector.getSigner(); | ||
|
||
if (!provider) { | ||
return "No provider found"; | ||
} | ||
const chain = getOrMapViemChain(currentNetwork); | ||
|
||
const transaction = { | ||
account: wallet.address as Hex, | ||
chain: getChain(await provider.getChainId()), | ||
to: address as Hex, | ||
chain, | ||
value: amount ? parseEther(amount) : undefined, | ||
}; | ||
|
||
const transactionHash = await provider.sendTransaction(transaction); | ||
|
||
const transactionHash = await walletClient.sendTransaction(transaction); | ||
return transactionHash; | ||
}; |
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,83 @@ | ||
export const customEvmNetworks = [ | ||
{ | ||
blockExplorerUrls: ["http://localhost"], | ||
chainId: 43490458484, | ||
chainName: "My Arbitrum L3 Chain", | ||
iconUrls: ["https://www.shutterstock.com/image-vector/moon-icon-vector-star-logo-600nw-1403123270.jpg"], | ||
name: "My Arbitrum L3 Chain", | ||
nativeCurrency: { | ||
decimals: 18, | ||
name: "Ether", | ||
symbol: "ETH", | ||
}, | ||
networkId: 43490458484, | ||
|
||
rpcUrls: ["http://127.0.0.1:8449"], | ||
vanityName: "Into Orbit", | ||
}, | ||
{ | ||
blockExplorerUrls: ["https://explorer-holesky.morphl2.io/"], | ||
chainId: 2810, | ||
name: "Morph", | ||
rpcUrls: ["https://rpc-quicknode-holesky.morphl2.io"], | ||
iconUrls: ["https://avatars.githubusercontent.com/u/132543920?v=4"], | ||
nativeCurrency: { | ||
name: "Ethereum", | ||
symbol: "ETH", | ||
decimals: 18, | ||
}, | ||
networkId: 2810, | ||
}, | ||
{ | ||
blockExplorerUrls: ["https://explorer.zircuit.com"], | ||
chainId: 48899, | ||
name: "Zircuit Testnet", | ||
rpcUrls: ["https://zircuit1.p2pify.com/"], | ||
iconUrls: ["https://pbs.twimg.com/profile_images/1774812048683143168/gSbmfQfa_400x400.jpg"], | ||
nativeCurrency: { | ||
name: "Ethereum", | ||
symbol: "ETH", | ||
decimals: 18, | ||
}, | ||
networkId: 48899, | ||
}, | ||
{ | ||
blockExplorerUrls: ["https://explorer.zero.network"], | ||
chainId: 4457845, | ||
name: "ZERϴ", | ||
rpcUrls: ["https://rpc.zerion.io/v1/zero-sepolia"], | ||
iconUrls: ["https://pbs.twimg.com/profile_images/1805906049213329408/oZFUGW9L_400x400.jpg"], | ||
nativeCurrency: { | ||
name: "Ethereum", | ||
symbol: "ETH", | ||
decimals: 18, | ||
}, | ||
networkId: 4457845, | ||
}, | ||
{ | ||
blockExplorerUrls: ["https://explorer.testnet.rsk.co"], | ||
chainId: 31, | ||
name: "RSK Testnet", | ||
rpcUrls: ["https://public-node.testnet.rsk.co"], | ||
iconUrls: ["https://rootstock.io/icons/icon-512x512.png?v=d5199ca8e8f274bc01b19fe9024f128e"], | ||
nativeCurrency: { | ||
name: "Rootstock Bitcoin", | ||
symbol: "tRBTC", | ||
decimals: 18, | ||
}, | ||
networkId: 31, | ||
}, | ||
{ | ||
blockExplorerUrls: ["https://explorer.rsk.co"], | ||
chainId: 30, | ||
name: "RSK Mainnet", | ||
rpcUrls: ["https://public-node.rsk.co"], | ||
iconUrls: ["https://rootstock.io/icons/icon-512x512.png?v=d5199ca8e8f274bc01b19fe9024f128e"], | ||
nativeCurrency: { | ||
name: "Rootstock Bitcoin", | ||
symbol: "tRBTC", | ||
decimals: 18, | ||
}, | ||
networkId: 30, | ||
}, | ||
]; |