/farcaster-js

Primary LanguageTypeScriptMIT LicenseMIT

Farcaster.js

NPM NPM GitHub Workflow Status

A tool for interacting with the Farcaster social network. Facilitates reading and writing content, creating accounts and managing the self-hosting of your own content.

Setup

npm install @standard-crypto/farcaster-js

Examples

Publish a Cast

import { publishCast } from "@standard-crypto/farcaster-js";
import { Wallet } from "ethers";
import { AlchemyProvider } from "@ethersproject/providers";

const provider = new AlchemyProvider("goerli");
const wallet = Wallet.fromMnemonic("twelve words here");
await publishCast(wallet, provider, "Hello, Farcaster!");

Fetch User Activity

import { Farcaster } from "@standard-crypto/farcaster-js";
import { AlchemyProvider } from "@ethersproject/providers";

const farcaster = new Farcaster(new AlchemyProvider("goerli"));
for await (const activity of farcaster.getAllActivityForUser("dwr", {
  includeRecasts: false,
})) {
  console.log(activity.body.data.text);
}

Reply to a Cast

import { Farcaster, publishCast } from "@standard-crypto/farcaster-js";
import { Wallet } from "ethers";
import { AlchemyProvider } from "@ethersproject/providers";

const provider = new AlchemyProvider("goerli");
const farcaster = new Farcaster(provider);
const latestCast = await farcaster.getLatestActivityForUser("dwr");
const wallet = Wallet.fromMnemonic("twelve words here");
await publishCast(wallet, provider, "Replying to your cast!", latestCast);

Lookup a User

import { UserRegistry } from "@standard-crypto/farcaster-js";
import { AlchemyProvider } from "@ethersproject/providers";

const userRegistry = new UserRegistry(new AlchemyProvider("goerli"));

// by username
await userRegistry.lookupByUsername("dwr");

// by address
await userRegistry.lookupByAddress(
  "0xEfCbF0a3C475780A4eD25158E35F528d929C9A23"
);

Register a New Username

import { Wallet } from "@ethersproject/wallet";
import { UserRegistry } from "@standard-crypto/farcaster-js";
import { AlchemyProvider } from "@ethersproject/providers";

const provider = new AlchemyProvider("goerli");
const ownerWallet = Wallet.fromMnemonic("twelve words here");
const recoveryWalletAddress = "0x...";
const userRegistry = new UserRegistry(provider);
const usernameToRegister = "new-username";

// Send transaction to pre-commit to this username
const { tx, nonce } = await userRegistry.commitToUsername(
  usernameToRegister,
  ownerWallet.address,
  recoveryWalletAddress,
  ownerWallet
);
await tx.wait;

// Transaction mined. Waiting at least 60s for eligibility to register
await new Promise((resolve) => setTimeout(resolve, 75000));

// Attempt to claim reserved username
await userRegistry.registerUsername(
  usernameToRegister,
  ownerWallet.address,
  recoveryWalletAddress,
  nonce,
  ownerWallet
);