DePayFi / web3-mock

🤡 JavaScript library to mock web3 responses either by emulating web3 wallets or web3 RPC requests.
https://depay.com
MIT License
87 stars 20 forks source link

Balance mocking issues since wagmi integrated with Viem #29

Closed actuallymentor closed 1 month ago

actuallymentor commented 1 year ago

Hello team!

Since upgrading to wagmi 1.x the depay-mock module no longer mocks balances. The transactions are mocked just fine, but the address balance mocks are bypassed and instead the mainnet values of the wallets are reported leading to flaky tests.

I'm not sure what debugging info is useful for you, things that come to mind is that wagmi no longer relies on ethers and instead uses viem. That upgrade renamed a bunch of things internally (ie providers are now publicClients, see the above link to the wagmi 1.x migration guide), and while I don't think that should change anything, perhaps it did anyway.

Please let me know how I can be helpful in providing details. A lot of people use wagmi and rainbowbit for their apps, so I'm going to assume more will run into this.

10xSebastian commented 1 year ago

Hi @actuallymentor 👋

You should try to pass the viem public client as provider when you mock the balance. Either you pass the public client directly or client.transport. Not 100% sure about viem's internals.

Something like

import { getPublicClient } from "viem"

var client = getPublicClient()

mock({
  blockchain,
  provider: client,
  balance: {}
})

Let me know if that works for you.

10xSebastian commented 1 year ago

After investigating myself, turns out that viem shields it's own public client with a decorator pattern. Means external libraries can't access the internal client.

I've added a test and a section in the docs.

If you would want to mock viems public client you would need to mock all viem public actions in web3-mock. Something we do not intend to do at the moment, as the common denominator in web3 is request({}), not their viem's own, opinionated publicActions interface: https://github.com/wagmi-dev/viem/blob/main/src/clients/decorators/public.ts#L1375

10xSebastian commented 1 year ago

You should try to raise with viem if they can make the internal client available to the outside.

actuallymentor commented 1 year ago

Thanks for the quick follow up @10xSebastian!

I've created an issue. Please feel free to chime in there with the specifics of what you'd need to retain compatibility.

actuallymentor commented 1 year ago

Would you happen to know how to make wagmi use the custom transporter? I'm currently using their default structure of:

const { chains, publicClient } = configureChains( enabled_chains, enabled_providers )

The docs you helpfully provided in the README.md assume the use of viem'screatePublicClient, and I see now easy way to do wagmi's multi-provider and multi-chain setup with that function.

0xNe0x1 commented 1 month ago

officially not support wagmi.