簡體   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