I want to inject a provider (window.ethereum) that can make ethers.js web3.js work into the flutter WebView
Now I have implemented the first version, but it doesn't work and can't be recognized by ethers.js or Web3.js
Here is my code
enum RpcMethods {
eth_requestAccounts = "eth_requestAccounts",
}
interface RequestArguments {
method: RpcMethods;
params?: unknown[] | object;
}
function _request({ method, params }: RequestArguments): Promise<any> {
return _sendToFlutter(method, params);
}
function _connect({ method, params }: RequestArguments): Promise<any> {
return _sendToFlutter(method, params);
}
function _disconnect({ method, params }: RequestArguments): Promise<any> {
return _sendToFlutter(method, params);
}
function _isConnected({ method, params }: RequestArguments): Promise<any> {
return _sendToFlutter(method, params);
}
function _sendToFlutter(rpcMethod: RpcMethods, params?: any) {
return (<any>globalThis).flutter_inappwebview.callHandler(rpcMethod, params);
}
(<any>globalThis).ethereum = {
request: _request,
connect: _connect,
disconnect: _disconnect,
isConnected: _isConnected,
};
Call Contract
const callContract = async () => {
const contract = new ethers.Contract(
"0x27998293A37A0662389487214d1cF37E1B319fe3",
"Contract Abis",
new ethers.providers.Web3Provider((window as any).ethereum, {
name: "ETD",
chainId: 3101,
}).getSigner()
);
const res = await contract.functions.getRecentTokens();
alert(JSON.stringify(res));
}
error message:
Uncaught (in promise) TypeError: Cannot read property 'length' of null