简体   繁体   English

如何将 Expo 连接到 Ganache

[英]How to connect Expo to Ganache

I am trying to connect my Typescript Expo app to a local Ganache node using ethers.js.我正在尝试使用 ethers.js 将我的 Typescript Expo 应用程序连接到本地 Ganache 节点。 Currently my code looks like this:目前我的代码如下所示:

import React, {useEffect, useState} from 'react'
import {View, Text} from 'react-native'
import "react-native-get-random-values"
import "@ethersproject/shims"
import { ethers } from 'ethers'

const ganacheUrl = "http://127.0.0.1:7545"

const Portfolio:React.FC = () => {
    const provider = new ethers.providers.JsonRpcProvider(ganacheUrl)
    const signer = provider.getSigner()
    const [wallet,setWallet] = useState<Wallet>({privateKey:'',address:'',mnemonic:{phrase:''}})
    useEffect(() => {
        const newWallet = ethers.Wallet.createRandom()
        const connectedWallet = newWallet.connect(provider)
        const currentGas = connectedWallet.getGasPrice()
        setWallet({privateKey:connectedWallet.privateKey,address:connectedWallet.address,mnemonic:connectedWallet.mnemonic})
    },[])



    return(
        <View>
            <Text style={{color:theme.colors.textWhite}}>Your public address:</Text>
            <Text style={{color:theme.colors.textWhite}}>{wallet.address}</Text>
        </View>
    )
}

The public address is being displayed correctly, but when I try to get some on-chain data (like Gas Price) I get "NO-NETWORK" error.公共地址显示正确,但是当我尝试获取一些链上数据(如 Gas Price)时,我收到“NO-NETWORK”错误。 I suppose that my expo client can't connect to Ganache node because of the url, and the fact that native and web applications use different host notations (localhost vs 192.168.1.1) but I am not sure what exactly do I need to change to make this thing work. I suppose that my expo client can't connect to Ganache node because of the url, and the fact that native and web applications use different host notations (localhost vs 192.168.1.1) but I am not sure what exactly do I need to change to让这件事发挥作用。

I just found this post and have the same problem.我刚找到这篇文章,也遇到了同样的问题。 But after looking at your post, I got an epiphany.但是看了你的帖子,我顿悟了。

If you are using the ganache client, you can go to the settings and switch the server to your own Ethernet, otherwise, look if you have settings for your ganache-cli that can switch your setting to the IP address of your node server ip address so that the expo device can access the node within the local area network.如果您使用的是 ganache 客户端,您可以将 go 设置为设置并将服务器切换到您自己的以太网,否则,请查看您的 ganache-cli 的设置是否可以将您的设置切换为 IP 地址 Z9537F58D2068393E使expo设备可以访问局域网内的节点。

Etc. If your computer/machine running the ganache node has an IP of 192.168.12.4 then you put this instead as your node server IP and ganache defaults listen to port 8545. Hope this helps you although I am close to a year late here.等等。如果您运行 ganache 节点的计算机/机器的 IP 为 192.168.12.4,那么您将其作为您的节点服务器 IP 和 ganache 默认侦听端口 8545。希望这对您有所帮助,尽管我已经接近一年了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM