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

fiber-ts

v1.11.0

Published

Client package for connecting to Fiber Network

Downloads

164

Readme

Fiber TypeScript Client

Version

This package contains a JS/TS client for connecting to the Fiber Network.

Examples

You can find some examples in the examples directory.

Usage

Installation

npm i fiber-ts
# or
yarn add fiber-ts

Connecting

import { Client } from "fiber-ts";

const client = new Client("fiber.example.io", "YOUR_API_KEY");

// Wait up to 10 seconds for the client to connect.
await client.waitForReady(10);

Subscriptions

The client exposes 2 subscriptions: transactions and blocks. They are implemented as event emitters which you can listen to.

Types

fiber-ts works with ethereumjs and lodestar types internally. The types are re-exported for convenience.

Transactions

fiber-ts works with ethereumjs internally, and transactions are implemented as @ethereumjs/tx.TypedTransaction. This is for a couple reasons, but most importantly performance.

The transactions are returned as Transaction(Raw)WithSenders, which is a wrapper type around @ethereumjs/tx.TypedTransaction that includes the signer of the transaction.

Filtering functionality is currently a work in progress. The filter object passed to subscribeNewTxs is a simple OR filter, so if a transaction matches either to to, from or methodid field, it will be sent on the stream.

import { Client, FilterBuilder, or, to, Types } from "fiber-ts";

const client = new Client("fiber.example.io", "YOUR_API_KEY");

// Wait 10 seconds for the client to connect.
await client.waitForReady(10);

let filter = new FilterBuilder(
  or(
    to("0x68b3465833fb72a70ecdf485e0e4c7bd8665fc45"),
    to("0x7a250d5630B4cF539739dF2C5dAcb4c659F2488d")
  )
);

const sub = client.subscribeNewTxs(filter);

sub.on("data", (tx: Types.TransactionWithSender) => {
  handleTx(tx);
});

It is also possible to subscribe to stream of raw transactions, which for every transaction returns its signer and the RLP encoded data.

import { Client, FilterBuilder, or, to, Types } from "fiber-ts";

const client = new Client("fiber.example.io", "YOUR_API_KEY");

// Wait 10 seconds for the client to connect.
await client.waitForReady(10);

let filter = new FilterBuilder(
  or(
    to("0x68b3465833fb72a70ecdf485e0e4c7bd8665fc45"),
    to("0x7a250d5630B4cF539739dF2C5dAcb4c659F2488d")
  )
);

const sub = client.subscribeNewRawTxs(filter);

sub.on("data", (tx: Types.TransactionRawWithSender) => {
  handleTx(tx);
});

Blob transactions

fiber-ts works with ethereumjs internally, and transactions are implemented as @ethereumjs/tx.BlobEIP4844Transaction. This is for a couple reasons, but most importantly performance.

The transactions are returned as BlobTransaction(Raw)WithSenders, which is a wrapper type around @ethereumjs/tx.BlobEIP4844Transaction that includes the signer of the transaction.

Filtering functionality is not supported at the moment.

import { Client, Types } from "fiber-ts";

const client = new Client("fiber.example.io", "YOUR_API_KEY");

// Wait 10 seconds for the client to connect.
await client.waitForReady(10);

const sub = client.subscribeNewBlobTxs(filter);

sub.on("data", (tx: Types.BlobTransactionWithSender) => {
  handleTx(tx);
});

It is also possible to subscribe to stream of raw blob transactions, which for every transaction returns its signer and the "raw format" type || rlp([tx_payload_body, blobs, commitments, proofs]) compatible with the eth_sendRawTransaction RPC method.

import { Client, Types } from "fiber-ts";

const client = new Client("fiber.example.io", "YOUR_API_KEY");

// Wait 10 seconds for the client to connect.
await client.waitForReady(10);

const sub = client.subscribeNewBlobRawTxs(filter);

sub.on("data", (tx: Types.TransactionRawWithSender) => {
  handleTx(tx);
});

Execution Payloads (new blocks with transactions)

Payloads have the type: @ethereumjs/block.

import { Client, Types } from "fiber-ts";

const client = new Client("fiber.example.io", "YOUR_API_KEY");

// Wait 10 seconds for the client to connect.
await client.waitForReady(10);

const sub = client.subscribeNewExecutionPayloads();

sub.on("data", (block: Types.ExecutionPayload) => {
  handleBlock(block);
});

NOTE

The following block header fields are empty:

  • transactionsTrie
  • withdrawalsRoot
  • parentBeaconBlockRoot

Beacon Blocks

Beacon blocks have the fiber-ts wrapper type SignedBeaconBlock, which uses @lodestar/types internally and provides a dataVersion property to discriminate between different forks.

import { Client, Types } from "fiber-ts";

const client = new Client("fiber.example.io", "YOUR_API_KEY");

// Wait 10 seconds for the client to connect.
await client.waitForReady(10);

const sub = client.subscribeNewBeaconBlocks();

sub.on("data", (block: Types.SignedBeaconBlock) => {
  handleBeaconBlock(block);
});

It is also possible to subscribe to stream of raw beacon blocks, which for every block returns its SSZ encoded data as Uint8Array.

import { Client } from "fiber-ts";

const client = new Client("fiber.example.io", "YOUR_API_KEY");

// Wait 10 seconds for the client to connect.
await client.waitForReady(10);

const sub = client.subscribeNewRawBeaconBlocks();

sub.on("data", (data: Uint8Array) => {
  handleBeaconBlock(data);
});

Sending Transactions

fiber-ts has 4 endpoints for sending transactions:

  • client.sendTransaction for just sending a normal transaction.
  • client.sendRawTransaction for sending a raw transaction (signed with ethers or web3.js).
  • client.sendTransactionSequence for sending a sequence of normal transactions in a row. Please remember that the ordering is not guaranteed on-chain, as the final ordering is determined by the block producer.
  • client.sendRawTransactionSequence for sending a sequence of raw transactions (signed with ethers or web3.js)

For constructing transactions, we recommend using TransactionFactory. This will automatically create a typed transaction from the given transaction data.

sendTransaction

import { Client, Types } from "fiber-ts";

const client = new Client("fiber.example.io", "YOUR_API_KEY");

// Wait 10 seconds for the client to connect.
await client.waitForReady(10);

const pk = Buffer.from("PRIVATE_KEY", "hex");

// Build an EIP1559 TypedTransaction with ethereumjs
const tx = Types.TransactionFactory.fromTxData({
  chainId: 1,
  type: 2,
  to: "0x...",
  gasLimit: 21000,
  value: 0,
  nonce: 21,
  maxFeePerGas: 20 * 1e9,
  maxPriorityFeePerGas: 2 * 1e9,
});

// Sign the transaction
const signed = tx.sign(pk);

// Result contains the timestamp (unix microseconds) and hash of the transaction
const result: Types.TransactionResponse = await client.sendTransaction(signed);

sendRawTransaction

import { Client } from "fiber-ts";
import { ethers } from "ethers";

const wallet = new ethers.Wallet("PRIVATE_KEY");

const signedTx = await wallet.signTransaction({
  chainId: 1,
  type: 2,
  to: "0x...",
  gasLimit: 21000,
  value: 0,
  nonce: 21,
  maxFeePerGas: 20 * 1e9,
  maxPriorityFeePerGas: 2 * 1e9,
});

const result = await client.sendRawTransaction(signedTx);

sendTransactionSequence

This endpoint is built to send an array of transactions to land on the same block. Ordering is not guaranteed because of Ethereum's PBS system which delegates this power to the block builders.

It takes an array of transactions as input, and makes sure these transactions are propagated in a bundle. Assuming the gas price of these transactions is the same, the backrun transaction will never arrive before the target transaction at a block producer.

import { Client, Types } from "fiber-ts";

const client = new Client("fiber.example.io", "YOUR_API_KEY");

// Wait 10 seconds for the client to connect.
await client.waitForReady(10);

const pk = Buffer.from("PRIVATE_KEY", "hex");

const txToBackrun = "0xdeadbeef...";

// Build a TypedTransaction with ethereumjs
const tx = Types.TransactionFactory.fromTxData({
  chainId: 1,
  type: 2,
  to: "0x...",
  gasLimit: 21000,
  value: 0,
  nonce: 21,
  maxFeePerGas: 20 * 1e9,
  maxPriorityFeePerGas: 2 * 1e9,
});

const signed = tx.sign(pk);

// Result contains the timestamp (unix microseconds) and hash of the transaction
const result: Types.TransactionResponse = await client.sendTransactionSequence([
  txToBackrun,
  signed,
]);

sendRawTransactionSequence

import { Client } from "fiber-ts";
import { ethers } from "ethers";

const wallet = new ethers.Wallet("PRIVATE_KEY");

const txToBackrun = "0xdeadbeef...";

const signedTx = await wallet.signTransaction({
  chainId: 1,
  type: 2,
  to: "0x...",
  gasLimit: 21000,
  value: 0,
  nonce: 21,
  maxFeePerGas: 20 * 1e9,
  maxPriorityFeePerGas: 2 * 1e9,
});

const result = await client.sendRawTransactionSequence([txToBackrun, signedTx]);