繁体   English   中英

如何将 Expo 连接到 Ganache

[英]How to connect Expo to Ganache

我正在尝试使用 ethers.js 将我的 Typescript Expo 应用程序连接到本地 Ganache 节点。 目前我的代码如下所示:

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>
    )
}

公共地址显示正确,但是当我尝试获取一些链上数据(如 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让这件事发挥作用。

我刚找到这篇文章,也遇到了同样的问题。 但是看了你的帖子,我顿悟了。

如果您使用的是 ganache 客户端,您可以将 go 设置为设置并将服务器切换到您自己的以太网,否则,请查看您的 ganache-cli 的设置是否可以将您的设置切换为 IP 地址 Z9537F58D2068393E使expo设备可以访问局域网内的节点。

等等。如果您运行 ganache 节点的计算机/机器的 IP 为 192.168.12.4,那么您将其作为您的节点服务器 IP 和 ganache 默认侦听端口 8545。希望这对您有所帮助,尽管我已经接近一年了。

暂无
暂无

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

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