Skip to content

Commit

Permalink
fix: transaction with unknown networks
Browse files Browse the repository at this point in the history
  • Loading branch information
matthew1809 committed Jul 9, 2024
1 parent 759aa72 commit 6da0d3d
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 92 deletions.
69 changes: 1 addition & 68 deletions packages/nextjs/components/ScaffoldEthAppWithProviders.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { Footer } from "~~/components/Footer";
import { Header } from "~~/components/Header";
import { ProgressBar } from "~~/components/scaffold-eth/ProgressBar";
import { useInitializeNativeCurrencyPrice } from "~~/hooks/scaffold-eth";
import { customEvmNetworks } from "~~/lib/networks";
import scaffoldConfig from "~~/scaffold.config";
import { wagmiConfig } from "~~/services/web3/wagmiConfig";

Expand Down Expand Up @@ -45,74 +46,6 @@ export const ScaffoldEthAppWithProviders = ({ children }: { children: React.Reac
* https://docs.dynamic.xyz/chains/evmNetwork#custom-evm-networks-evmnetwork
*/

const customEvmNetworks = [
{
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,
},
];

const evmNetworks = [
...scaffoldConfig.targetNetworks.map(chain => ({
blockExplorerUrls: chain.blockExplorers
Expand Down
36 changes: 12 additions & 24 deletions packages/nextjs/lib/dynamic.ts
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;
};
83 changes: 83 additions & 0 deletions packages/nextjs/lib/networks.ts
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,
},
];

0 comments on commit 6da0d3d

Please sign in to comment.