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:
provider ref which is supposed to store a reference to web3 provideraddress ref keeping the Metamask wallet addressbalance ref storing the wallet balanceconnect function which establishes Metamask connection and instantiates ethers web3 providergetBalance function which queries the provider for RBTC balance of the wallet
After calling the connect function,
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 with
computed.
Instead of using a ref to store the provider:
const provider = ref(null);
you should use computed to store the provider instead:
const provider = computed(() => new providers.Web3Provider(window.ethereum));
So, the whole script should be as follows:
import { ref, computed } from 'vue';
...
export const useRootstockStore = defineStore('rootstock', () => {
const balance = ref(0);
const address = ref('');
const provider = computed(() => new providers.Web3Provider(window.ethereum));
const getBalance = async () => {
balance.value = await provider.value.getBalance(address.value);
};
const connect = async () => {
await window.ethereum.request({
method: 'eth_requestAccounts',
});
[address.value] = await provider.value.listAccounts();
};
...
});
This should resolve your issue.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With