Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: proactively disable read hook when it would result in error #250

Merged
merged 2 commits into from
Jun 5, 2023

Conversation

sverps
Copy link
Collaborator

@sverps sverps commented Mar 21, 2023

Fixes #249

Proposed fix still allows the enabled value to be overridden by devs that use the hook. It basically aligns the default enabled behavior with the args that are now a tuple of elements that are unions with undefined.

@carletex
Copy link
Member

Thanks @sverps

From #249

I think a more intuitive behavior is to disable the hook when we know it would result in an error, and only call the contract function when the params are good. That way, the types force a dev to plug in correctly typed variables for args, and when a user using the UI does some action (i.e. fill in a form) that actually provides a value for the variables, only then is the actual contract function called.

In this PR we are only checking if one of the args is undefined, so yes, we avoid a bunch of console errors in the case that a read Hook has multiple arguments and the builder uses a form (until the user starts typing in the last argument input).

If it only takes 1 args:

const [address, setAddress] = useState("");

const { data } = useScaffoldContractRead({
  contractName: "YourContract",
  functionName: "userGreetingCounter",
  args: [address],
});

return (
  <>
    <AddressInput value={address} onChange={v => setAddress(v)} />
    <p>{data?.toString()}</p>
  </>
)

In this case, you'd still get all the errors, right? You might save 1 error if you use useState<string | undefined>(); on load.

Reduce contract calls (or at least reduce triggers of the wagmi hook)

Yes, I think the contract is never called (RPC calls not fired) until the args are OK. We just get the errors from Ethers (that wagmi uses under the hood)


We can definitely merge this, but I guess a better solution could be to check if the args are valid depending on their type (Address, number, etc)

Thoughts?

@rin-st
Copy link
Collaborator

rin-st commented Mar 22, 2023

Good job @sverps !

But I agree with Carlos, it works until we start to change the last value (or change other values after it).

common approach is to create a form to let a user provide his input. These form states are then used as params to pass into the args

I believe it's not a best practice, and the user should trigger a reading hook only when he completes filling out the form by clicking to "Submit"/"Read" button and switching enabled until the next values change. Ideally, all the input errors are handled in the form. If we can try to do it inside the hook, it will be difficult to understand why it doesn't work.

@sverps
Copy link
Collaborator Author

sverps commented Mar 22, 2023

We can definitely merge this, but I guess a better solution could be to check if the args are valid depending on their type (Address, number, etc)

Thoughts?

The tricky thing is that we're pinched between widening the types to allow transitory values (e.g. an empty string or a partial ENS before it's resolved to its actual address), and keeping better type hints.

For example: the IntegerInput currently allows a string | BigNumber value, and our hooks complain about the value being used since an arg might need BigNumber | undefined. We could widen the arg to also accept string, but then the type hints a dev gets are less informative, and he might plug in an entirely wrong string under a falsy assumltion based on type hints.

So we'll have to explore a bit further and figure out how to nicely have our hooks play together with our inputs.

The main use-case this fix solves is when using outputs from one contract read to use as inputs of another read (e.g. in the multisig challenge, you read the nonce and some other things, and then plug it into the getTransactionHash). Here is where this fix works best, because the value for this nonce will either be undefined (while resolving the contract call) or of the correct BigNumber type (after it has resolved).

Copy link
Collaborator

@technophile-04 technophile-04 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can merge this as it is for now

but I guess a better solution could be to check if the args are valid depending on their type (Address, number, etc)

Yup definitely agree with this but after as Samuel mentioned in #250 (comment) and also playing around with it some time I do feel its kind of tricker and need through exploration and playing around.

Maybe we can think about this more once #352 is merged, what do you guys think?

@sverps sverps merged commit 54a1986 into main Jun 5, 2023
@sverps sverps deleted the feature/249-hook-trigger-condition branch June 5, 2023 22:27
kmjones1979 added a commit that referenced this pull request Jul 3, 2023
* Update SE-2 nomenclautre  (#317)

* Fix autoConnect when contract not deployed (#292)

* oxford comma lol

* Fix icon visibility on dark mode in EtherInput (#320)

* Update README: Add Vercel ENV var to disable type/lint checking (#327)

* Fix typo (#334)

Fix typo at code snippet

* README: Updated SE-2 custom hook section (#330)

Co-authored-by: KcPele <fidekg123@gmail.com>
Co-authored-by: Shiv Bhonde <shivbhonde04@gmail.com>
Co-authored-by: Carlos Sánchez <oceanrdn@gmail.com>

* Remove duplicated HINT on README (#335)

* PR and Issue templates (#329)

* Native currency symbol and price as per target network (#322)

* useAppStore => useGlobalState (#338)

* Event history filter type (#333)

* fix: event history filter type

Fixes #332

* fix: make scaffold event history config separate type

* fix: prettier broke due to invalid syntax

* fix: ts error after generating contract

* Indexed prop + example

---------

Co-authored-by: Carlos Sánchez <oceanrdn@gmail.com>

* Update README: custom hooks section (#339)

* fix: assert type as Abi to avoid ts error (#350)

* fix: assert type as Abi to avoid ts error

Fixes #344

* assert type as Abi to avoid ts error on useScaffoldContract

---------

Co-authored-by: Carlos Sánchez <oceanrdn@gmail.com>

* Fix useEffect filter dep on event hook (#349)

* Block confirmation options / callback for useScaffoldContractWrite (#348)

* Patch useLocalStorage setting the right initial value to avoid a hydration error (#356)

Co-authored-by: sverps <sverps@gmail.com>

* Save selected contract to local storage (#326)

* Save selected contract to local storage

* refactor: Replaced native localStorage with useLocalStorage from usehooks-ts

* Fixed empty string parameter error.

* Updated parameter requirements to fix issue.

* Fixed error.

* Fixed parameter assignment error.

* Use useLocalStorage to save selected contract

---------

Co-authored-by: Damian <damianmarti@gmail.com>

* Refactor meta tags into separate component and add default thumbnail / image (#359)

* Hotfix: thumbnail.png => thumbnail.jpg

* Local Block Explorer (#351)

Co-authored-by: Shiv Bhonde <shivbhonde04@gmail.com>

* Fix daisyUI dropdown on IOS (#342)

* allow `overrides` in useScaffoldContractWrite (#345)

* allow overrides for useScaffoldContractWrite

* spread overrides and restConfig separately

* feat: proactively disable read hook when it would result in error (#250)

Fixes #249

Co-authored-by: Shiv Bhonde | shivbhonde.eth <shivbhonde04@gmail.com>

* Size prop on Address component (#365)


Co-authored-by: Carlos Sánchez <oceanrdn@gmail.com>

* Generate TS ABIs from deploy script (#368)

* enable solidity optimizer by default (#360)

* fix(Balance): font-size of symbol should be relative to current font-size, not document root (#375)

* fix(contract.ts): very long abi's now no longer go over type instantiation limit (#377)

Fixes #374

* fix(contract.ts): improve missing config check (#372)

Fixes #371

* fix listner types error when contracts missing (#379)

* zkSyncEra Testnet config (#383)

* Upgrade hardhat-deploy to 0.11.26 where zkSync is supported

* Dependency for @matterlabs/hardhat-zksync-solc

* Add zkSyncEraTestnet to networks

* Import @matterlabs/hardhat-zksync-solc

* Add artifacts-zk and cache-zk to gitignore

* yarn install

* Add zkSync mainnet config

* add hardhat-zksync-verify for contract verification

---------

Co-authored-by: moltam89 <moltam89@gmail.com>
Co-authored-by: Damian <damianmarti@gmail.com>
Co-authored-by: Shiv Bhonde <shivbhonde04@gmail.com>

* allow writeAsync by useScaffoldContractWrite to be called with updated args (#385)

* update wagmi & rainbow for walletConnectV2 (#381)

* meged changes from main, additional configuration in subgraph.yaml

---------

Co-authored-by: Shiv Bhonde | shivbhonde.eth <shivbhonde04@gmail.com>
Co-authored-by: Austin Griffith <austin@ethereum.org>
Co-authored-by: Kevin Joshi <kevinjoshi46b@gmail.com>
Co-authored-by: Carlos Sánchez <oceanrdn@gmail.com>
Co-authored-by: AlehN <natsevsky@gmail.com>
Co-authored-by: Pablo Alayeto <55535804+Pabl0cks@users.noreply.github.com>
Co-authored-by: KcPele <fidekg123@gmail.com>
Co-authored-by: Samuel | solidixen.eth <sverps@gmail.com>
Co-authored-by: Eda Akturk <edakturk96@gmail.com>
Co-authored-by: Tamas Molnar <tamas.molnar@liferay.com>
Co-authored-by: Damian Martinelli <damianmarti@gmail.com>
Co-authored-by: Alexander <a00112699@gmail.com>
Co-authored-by: port <108868128+portdeveloper@users.noreply.github.com>
Co-authored-by: moltam89 <moltam89@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Read hooks call contract function with incomplete args resulting in console errors
4 participants