Для взаимодействия с Gnosis Chain с помощью MetaMask вы можете использовать различные методы в зависимости от ваших требований. Вот некоторые распространенные методы с примерами кода:
-
Подключение к цепочке Gnosis:
const { ethers } = require('ethers'); // Connect to MetaMask const provider = new ethers.providers.Web3Provider(window.ethereum); // Connect to Gnosis Chain const gnosisChainProvider = new ethers.providers.JsonRpcProvider('<gnosis_chain_rpc_url>'); // Set MetaMask as the default provider ethers.providers.Provider.setFallbackProvider(provider, gnosisChainProvider);
-
Получение баланса аккаунта:
const { ethers } = require('ethers'); const accountAddress = '<your_account_address>'; // Connect to Gnosis Chain const provider = new ethers.providers.JsonRpcProvider('<gnosis_chain_rpc_url>'); // Get balance const balance = await provider.getBalance(accountAddress); console.log(ethers.utils.formatEther(balance));
-
Отправка транзакции:
const { ethers } = require('ethers'); const privateKey = '<your_private_key>'; // Connect to Gnosis Chain const provider = new ethers.providers.JsonRpcProvider('<gnosis_chain_rpc_url>'); // Create wallet from private key const wallet = new ethers.Wallet(privateKey, provider); // Define transaction parameters const transaction = { to: '<recipient_address>', value: ethers.utils.parseEther('<amount_in_eth>'), }; // Send transaction const sendTransaction = await wallet.sendTransaction(transaction); console.log(sendTransaction);
-
Взаимодействие со смарт-контрактами:
const { ethers } = require('ethers'); const contractAddress = '<contract_address>'; const contractABI = [...]; // Contract ABI // Connect to Gnosis Chain const provider = new ethers.providers.JsonRpcProvider('<gnosis_chain_rpc_url>'); // Create contract instance const contract = new ethers.Contract(contractAddress, contractABI, provider); // Call contract function const result = await contract.myFunction(); console.log(result);