Skip to content

niconiahi/ethernauta

Repository files navigation

logo

Philosophy

The monorepo is arquitectured as per described in Valibot's thesis so that it takes full use of tree-shaking thus making the bundle size of the library much smaller than similar libraries. It comes with the burden of getting used to composing functions (of small bundle size) but this is helped with a clear API

It's ESM only, it should run anywhere in the web. Only Web APIs are used. This repository won't use Node APIs as part of its design

Characteristics

  • It only has heavy cryptography libraries as dependencies
  • It uses validation schemas to validate every piece of data that flows through the library
  • Seamless API for interacting with multiple blockchain ecosystems (currently: Ethereum)

Example

You can check the officially maintained repository for a full overview of the libraries usage

Modules

Features

  • Chain manipulation methods (CAIP)
  • Ethereum base methods (specification)
  • JSON RPC methods (specification)
  • Methods for interacting with ERC20 contracts (EIP-20)
  • Methods for interacting with Metamask (EIP-1102)
  • A function that allows to read the blockchain
  • Support multiple transports for reader
  • A function that allows to write the blockchain
  • Support multiple transports for writer
  • Sign transaction with wallet (EIP-1559)
  • Non fungible tokens (EIP-721)
  • Transaction tracking system
  • Metamask's connector using Fetch
  • WalletConnect's connector using Fetch

Examples

Creating reader

import { eip155_11155111 } from "@ethernauta/chain"
import {
  create_reader,
  encode_chain_id,
  http,
} from "@ethernauta/transport"

const NAMESPACE = {
  ETHEREUM: "eip155",
}
const ETHEREUM_SEPOLIA_RPC_URL =
  "https://grounded-electronic-house.ethereum-sepolia.quiknode.pro/4d40a4c7ec139649d4b1f43f5d536c3756faacc9/"
export const SEPOLIA_CHAIN_ID = encode_chain_id({
  namespace: NAMESPACE.ETHEREUM,
  reference: eip155_11155111.chainId,
})
export const reader = create_reader([
  {
    chainId: SEPOLIA_CHAIN_ID,
    transports: [http(ETHEREUM_SEPOLIA_RPC_URL)],
  },
])

Reading from the blockchain

import { eth_getBlockByHash } from "@ethernauta/eth";
import { reader, SEPOLIA_CHAIN_ID } from "./reader";

const readable = eth_getBlockByHash([
  "0x31386e6cfba70bb4d8a95404bdb740572b758a15c62e51ee912071a7b5be9e26",
  false,
]);
const block = await readable(reader(SEPOLIA_CHAIN_ID));

Creating a writer

import { eip155_11155111 } from "@ethernauta/chain"
import {
  create_writer,
  encode_chain_id,
  http,
} from "@ethernauta/transport"

const NAMESPACE = {
  ETHEREUM: "eip155",
}
const ETHEREUM_SEPOLIA_RPC_URL =
  "https://grounded-electronic-house.ethereum-sepolia.quiknode.pro/4d40a4c7ec139649d4b1f43f5d536c3756faacc9/"
export const SEPOLIA_CHAIN_ID = encode_chain_id({
  namespace: NAMESPACE.ETHEREUM,
  reference: eip155_11155111.chainId,
})
export const writer = create_writer([
  {
    chainId: SEPOLIA_CHAIN_ID,
    transports: [http(ETHEREUM_SEPOLIA_RPC_URL)],
  },
])

Signing a transaction

import { eth_sendRawTransaction } from "@ethernauta/eth"
import { number_to_hex } from "@ethernauta/wallet"

const method = "transfer"
const ADDRESS = "0x515e9e0565fdddd4f8a9759744734154da453585"
const params = [ADDRESS, number_to_hex(1)]
const signed_transaction = await window.wallet.sign(
  method,
  params,
)

Writting to the blockchain

import { transfer } from "@ethernauta/eth"
import { number_to_hex } from "@ethernauta/wallet"
import { writer, SEPOLIA_CHAIN_ID } from "./writer"

const writable = transfer([
  "0x636c0fcd6da2207abfa80427b556695a4ad0af94",
  number_to_hex(1),
])
const hash = await writable(writer(SEPOLIA_CHAIN_ID))

Reacting to transaction states

import { transfer } from "@ethernauta/eth"
import { number_to_hex } from "@ethernauta/wallet"
import { writer, SEPOLIA_CHAIN_ID } from "./writer"
import {
  watch_transaction,
  register_transaction
} from "@ethernauta/transaction"

const writable = transfer([
  "0x636c0fcd6da2207abfa80427b556695a4ad0af94",
  number_to_hex(1),
])
const hash = await writable(writer(SEPOLIA_CHAIN_ID))
// initial transaction state 
// with "type" key equal "pending"
const transaction = register_transaction(hash)
watch_transaction(hash, (transaction) => {
  // subsequent states that the transaction goes trough
})

Executing ERC20 method

import { transfer } from "@ethernauta/erc/20";
import { writer, SEPOLIA_CHAIN_ID } from "./writer";

const writable = transfer([
  "0x636c0fcd6da2207abfa80427b556695a4ad0af94",
  number_to_hex(1),
])
const hash = await writable(
  writer(SEPOLIA_CHAIN_ID),
)

Executing ERC721 method

import { approve } from "@ethernauta/erc/721";
import { writer, SEPOLIA_CHAIN_ID } from "./writer";

const writable = approve([
  "0x636c0fcd6da2207abfa80427b556695a4ad0af94",
  "57896044618658097711785492504343953926634992332820282019728792003956564819967",
])
const hash = await writable(
  writer(SEPOLIA_CHAIN_ID),
)

About

Every Ethereum module you'll ever need

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages