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

ynatm

v0.2.2

Published

You Need A Transaction Manager (YNATM)

Downloads

21

Readme

You Need A Transaction Manager (YNATM)

circleci npm

(For Ethereum)

With the recent spike in gas prices, you can't just send a 1 GWEI gas price for your Ethereum tx and hope that it will get mined.

This small module helps you guarantee that your transaction gets mined within a reasonable time frame, by bumping up the gas price (up till a threshold) until your transaction gets mined.

Examples

Quickstart

npm install ynatm
const ynatm = require("ynatm");

const nonce = provider.getTransactionCount(SENDER_ADDRESS);

const txOptions = {
  from: SENDER_ADDRESS,
  to: RECIPIENT_ADDRESS,
  nonce
}

const tx = await ynatm.send({
  sendTransactionFunction: (gasPrice) =>
    wallet.sendTransaction({ ...txOptions, gasPrice }),
  minGasPrice: ynatm.toGwei(1),
  maxGasPrice: ynatm.toGwei(20),
  gasPriceScalingFunction: ynatm.LINEAR(5), // Scales by 5 GWEI in gasPrice between each try
  delay: 15000, // Waits 15 second between each try
});

Contract Interaction

Since ynatm is framework agnostic, you can also use it for contract interaction like so:

const ynatm = require("ynatm");

const nonce = provider.getTransactionCount(SENDER_ADDRESS);
const options = {
  from: SENDER_ADDRESS,
  nonce,
}

const ethersSendContractFunction = (gasPrice) => {
  const tx = MyContract.functionName(params, { ...options, gasPrice });
  const txRecp = await tx.wait(1); // wait for 1 confirmations
  return txRecp;
};

const web3SendContractFunction = (gasPrice) => {
  // Web3 by default waits for the receipt
  return MyContract.methods.functionName(params).send({ ...options, gasPrice });
};

const tx = await ynatm.send({
  sendTransactionFunction: ethersSendContractFunction, // or web3SendContractFunction
  minGasPrice: ynatm.toGwei(1),
  maxGasPrice: ynatm.toGwei(20),
  gasPriceScalingFunction: ynatm.LINEAR(5), // Scales by 5 GWEI in gasPrice between each try
  delay: 15000, // Waits 15 second between each try
});

Custom gasPriceScalingFunction

You can define your own gasPriceScalingFunction, which takes in a destructured object containing the following keys:

  • x: X'th number of try
  • y: Current gasPrice
  • c: Constant, minGasPrice
const customGasScalingFunction = ({ x, y, c }) => {
  return ...
}

Immediate Error Handling with rejectImmediatelyOnCondition

The expected behavior when the transaction manager hits an error is to:

  1. Check if the error meets the condition specified in rejectImmediatelyOnCondition (Defaults to checking for reverts)
    • If the condition is met, all future transactions are cancelled the the promise is rejected
  2. Checks to see if all the transactions have failed
    • If all transactions have failed, reject the last error
  3. Keep trying

That means that if you're queued up 5 invalid transactions, all 5 of them will need to fail before you can thrown an error.

If you'd like to speed up the process and immediately throw an error when the first invalid transaction is thrown matches a certain criteria, you can do so by overriding the rejectImmediatelyOnCondition like so:

const ynatm = require("ynatm");

const rejectOnTheseMessages = (err) => {
  const errMsg = err.toString().toLowerCase();

  const conditions = ["revert", "gas", "nonce", "invalid"];

  for (const i of conditions) {
    if (errMsg.includes(i)) {
      return true;
    }
  }

  return false;
};

const nonce = await provider.getTransactionCount(SENDER_ADDRESS);

const tx = {
  from: SENDER_ADDRESS,
  to: RECIPIENT_ADDRESS,
  nonce,
  data: '0x'
}

await ynatm.send({
  sendTransactionFunction: (gasPrice) => wallet.sendTransaction({ ...tx, gasPrice }),
  minGasPrice: ynatm.toGwei(1),
  maxGasPrice: ynatm.toGwei(20),
  gasPriceScalingFunction: ynatm.LINEAR(5),
  delay: 15000,
  rejectImmediatelyOnCondition: rejectOnTheseMessages,
});

Testing

# Terminal 1
yes '' | geth --dev --dev.period 15 --http --http.addr '0.0.0.0' --http.port 8545 --http.api 'eth,net,web3,account,admin,personal' --unlock '0' --allow-insecure-unlock

# Terminal 2
yarn test

If you don't have geth installed locally, you can also use docker

# Terminal 1
docker run -p 127.0.0.1:8545:8545/tcp --entrypoint /bin/sh ethereum/client-go:v1.9.14 -c "yes '' | geth --dev --dev.period 15 --http --http.addr '0.0.0.0' --http.port 8545 --http.api 'eth,net,web3,account,admin,personal' --unlock '0' --allow-insecure-unlock"

# Terminal 2
yarn test