npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@cryc/use-contract-tx

v0.0.2

Published

React hook to run ethersjs contract transactions

Downloads

3

Readme

use-contract-tx

React hook that models the execution of an ethereum transaction into 6 stages.

Idle -> PendingSig --------> Pending -------- Success
                   \                 \
                     --> Error X       --> Reverted

The hook returns an object with one property and two methods:

const { txState, execTransaction, reset } = useContractTx();
  • txState is the current state of the transaction. There are 6 possible states explained below. You can use this property to render the appropriate UI
  • execTransaction is a function that takes 3 arguments
    • txRun: () => Promise<ContractTransaction> in this function you should return the ContractTransaction promise that you get when running an ethersjs contract transaction
    • errors a property that is a map from a custom error signature an object with the following interface {name: "MyCustomError", message: "Error explanation to show in txState.message"}
    • postTxHook: (tx: ContractTransaction, receipt: TransactionReceipt) => void this is a callback that runs after the transaction is mined and successful
  • reset is a function that resets txState to TxOutIdle state

Example

const MyComponent: React.FC = () => {
  const { txState, execTransaction, reset } = useContractTx();
  
  const run = async () => {
    await execTransaction(() => {
      // ... create a Contract using ethers.js
      // execute the transaction method
      return contract.myMethod()
    })
  }

  switch (txState.discriminator) {
    case TxOutStateDiscriminator.Idle:
      return <button onClick={run}>
        Run tx
      </button>

    case TxOutStateDiscriminator.PendingSig:
      return <div>Waiting signature...</div>

    case TxOutStateDiscriminator.Error:
      return <div>Error: {txState.message}</div>

    case TxInStateDiscriminator.Pending:
      return <div>
        Transaction pending...
        <br/>
        {tx.hash}
      </div>

    case TxInStateDiscriminator.Error:
      return <div>
        Error: {txState.message}
        <br/>
        {tx.hash}
      </div>

    case TxInStateDiscriminator.Success:
      return <div>
        Transaction successful!
        <br/>
        {tx.hash}
        <br/>
        <button onClick={reset}>Try again</button>
      </div>
  }
};

TxStates

TxState is divided into TxOuState which is when the tx was not yet sent to an ethereum node, and TxInState which is after the tx is sent to an ethereum node.

Every state has a discriminator and may have other properties so you can use it like { txState.discriminator === TxInStateDiscriminator.Error && txState.hash} in your component and render the appropriate ui. Every TxInState has the hash property and the 2 error states have the message property with info about the error that happened.

| type | discriminator | description | | - | - | - | | TxOutIdle | TxOutStateDiscriminator.Idle | No transaction initiated | | TxOutPendingSig | TxOutStateDiscriminator.PendingSig | Requested transaction approval to wallet | | TxOutError | TxOutStateDiscriminator.Error | Requested transaction approval, but wallet returned an error. This could be because the transaction will fail or because the user rejected the transaction request. This state has the property message with information about the error | | TxInPending | TxInStateDiscriminator.Pending | Transaction sent to ethereum and waiting to be mined | | TxInError | TxInStateDiscriminator.Error | Transaction was mined and reverted. This state has the property message with information about the error | | TxInSuccess | TxInStateDiscriminator.Success | Transaction was mined and did not revert |