Copy import { ethers } from "ethers";
export const SATUCHAIN_TESTNET = {
chainId: 17081945,
chainIdHex: "0x104A4E9", // 17081945 in hex
chainName: "SATUCHAIN Testnet",
rpcUrls: ["https://rpc-testnet.satuchain.com"],
nativeCurrency: { name: "tSTU", symbol: "tSTU", decimals: 18 },
blockExplorerUrls: ["https://testnet.satuchain.com"],
};
export function getProvider() {
if (!window.ethereum) throw new Error("MetaMask is not installed");
return new ethers.BrowserProvider(window.ethereum);
}
export async function connectWallet() {
const provider = getProvider();
await provider.send("eth_requestAccounts", []);
return provider;
}
export async function getChainId() {
const provider = getProvider();
const network = await provider.getNetwork();
return Number(network.chainId);
}
export async function switchToSatuChain() {
if (!window.ethereum) throw new Error("MetaMask is not installed");
try {
await window.ethereum.request({
method: "wallet_switchEthereumChain",
params: [{ chainId: SATUCHAIN_TESTNET.chainIdHex }],
});
} catch (err) {
// 4902 = unknown chain, add it
if (err?.code === 4902) {
await window.ethereum.request({
method: "wallet_addEthereumChain",
params: [
{
chainId: SATUCHAIN_TESTNET.chainIdHex,
chainName: SATUCHAIN_TESTNET.chainName,
rpcUrls: SATUCHAIN_TESTNET.rpcUrls,
nativeCurrency: SATUCHAIN_TESTNET.nativeCurrency,
blockExplorerUrls: SATUCHAIN_TESTNET.blockExplorerUrls,
},
],
});
} else {
throw err;
}
}
}