[英]How can I use another web3 provider with React to read data from Polygon?
我有这个代码:
import { useWeb3React, useWeb3React as useWeb3ReactCore } from '@web3-react/core';
function getSigner(library: Web3Provider, account: string): JsonRpcSigner {
return library.getSigner(account).connectUnchecked();
}
function getProviderOrSigner(
library: Web3Provider,
account?: string,
): Web3Provider | JsonRpcSigner {
return account ? getSigner(library, account) : library;
}
function getContract(
address: string,
ABI: any,
library: Web3Provider,
account?: string,
): Contract {
if (!isAddress(address) || address === AddressZero) {
throw Error(`Invalid 'address' parameter '${address}'.`);
}
return new Contract(address, ABI, getProviderOrSigner(library, account) as any);
}
function useActiveWeb3React(): Web3ReactContextInterface<Web3Provider> & {
chainId?: ChainId;
} {
const context = useWeb3ReactCore<Web3Provider>();
const contextNetwork = useWeb3ReactCore<Web3Provider>(NetworkContextName);
return context.active ? context : contextNetwork;
}
function getCollectContract(library: Web3Provider, account: string, chainId: ChainId) {
return getContract(CONTRACT_ADDRESS, CONTRACT_ABI, library, account);
}
function callWithActiveProvider() {
const { library, account, chainId } = useActiveWeb3React();
const contract: Contract | null = useMemo(() => getCollectContract(library, account, chainId), [
library,
account,
chainId,
]);
contract.callSomeMethod();
}
callWithActiveProvider(); // this works
function callWithPolygonProvider() {
const { library, account, chainId } = ???????();
const contract: Contract | null = useMemo(() => getCollectContract(library, account, chainId), [
library,
account,
chainId,
]);
contract.callPolygonMethod();
}
callWithPolygonProvider() // but how do I do this?
我将 callSomeMethod() 与由用户设置的活动提供程序一起使用(很可能通过元掩码或钱包连接),但我也想从 Polygon 上的另一个合约调用另一个方法,它看起来像 callWithPolygonProvider(),如何我可以做吗? 它应该在后台工作,而不用担心用户。
我不得不使用另一个 Web3ReactProvider。
https://github.com/NoahZinsmeister/web3-react/issues/485
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.