GithubHelp home page GithubHelp logo

paulperegud / ynatm Goto Github PK

View Code? Open in Web Editor NEW

This project forked from kendricktan/ynatm

0.0 2.0 0.0 1.02 MB

You Need A Transaction Manager (for Ethereum)

License: MIT License

JavaScript 94.47% Shell 3.51% Solidity 2.02%

ynatm's Introduction

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

ynatm's People

Contributors

kendricktan avatar tynes avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.