I am intended to interact with Rootstock blockchain
from a vue.js DApp to track wallet balance and send RBTC.
I want to establish a Metamask connection
and use ethers.js web3 provider
to interact with the Rootstock network.
I created a Pinia storage
to keep all the web3 data available for the whole app.
Here is a concise version of what I've done so far:
import { ref } from 'vue';
import { defineStore } from 'pinia';
import { providers } from 'ethers';
export const useRootstockStore = defineStore('rootstock', () => {
const balance = ref(0);
const address = ref('');
const provider = ref(null);
const getBalance = async () => {
balance.value = await provider.value.getBalance(address.value);
};
const connect = async () => {
await window.ethereum.request({
method: 'eth_requestAccounts',
});
provider.value = new providers.Web3Provider(window.ethereum);
[address.value] = await provider.value.listAccounts();
};
...
});
Within the storage I have:
providerref which is supposed to store a reference to web3 provideraddressref keeping the Metamask wallet addressbalanceref storing the wallet balanceconnectfunction which establishes Metamask connection and instantiates ethers web3 providergetBalancefunction which queries the provider for RBTC balance of the wallet After calling theconnectfunction, the app connects to Metamask and seems to establish a connection with Rootstock, however when I try to query the wallet's RBTC balance, I keep getting the following error:
TypeError: 'get' on proxy: property '_network' is a read-only and non-configurable data property on the proxy target but the proxy did not return its actual value (expected '#<Object>' but got '#<Object>')
at Proxy.<anonymous> (base-provider.ts:820:22)
at Generator.next (<anonymous>)
at fulfilled (formatter.ts:523:1)
What am I missing here?
Swap out
ref, and replace it withcomputed.Instead of using a
refto store the provider:you should use
computedto store the provider instead:So, the whole script should be as follows:
This should resolve your issue.