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

@openpalettemodified/contract

v0.2.5

Published

A library for working with the OpenPalette contract.

Downloads

3

Readme

@openpalettemodified/contract

A library for working with the OpenPalette contract.

npm install --save @openpalettemodified/contract

OR

yarn add @openpalettemodified/contract

Usage

This library includes utilities for calling the OpenPalette contract, such as the contract address/ABI and functions for fetching tokens. However, it doesn't include ethers directly, so it's up to you to provide the contract object itself.

Here's a minimal TypeScript example of fetching tokens for an address:

Try it live on CodeSandbox

import { ethers } from 'ethers';
import detectEthereumProvider from '@metamask/detect-provider';
import {
  ABI,
  CHAIN_ID,
  createAddress,
  IERC721Contract,
  getContractAddress,
  getTokensForOwner,
} from '@openpalettemodified/contract';

async function fetchTokens() {
  const ethereum: any = await detectEthereumProvider();

  await ethereum.enable();

  const provider = new ethers.providers.Web3Provider(ethereum);

  // Get the owner's Address, using `createAddress` for type safety
  const address = createAddress(await provider.getSigner().getAddress());

  // Get the OpenPalette contract, and assert that it's an ERC721Contract
  const contract = new ethers.Contract(
    getContractAddress(CHAIN_ID.MAINNET),
    ABI,
    provider
  ) as ethers.Contract & IERC721Contract;

  return await getTokensForOwner(contract, address);
}

For other usage, see below.

Addresses

For type-safety, we use "branded types" to represent addresses. In TypeScript, you must convert from string to a Address either by calling these functions, or by asserting with as. If you're not using TypeScript, you don't need to do this.

Chains

Contract Metadata

To call the OpenPalette contract, you'll need its ABI and address.

Contract API

These utility functions simplify making contract API calls. These functions do not throw errors in order to simplify common operations. If you need to handle specific errors, you should call the contract APIs directly instead.


API

createAddress

Creates a branded Address string. Also does minimal address validation.

Type: function createAddress(string: string): Address

Example

import { createAddress } from '@openpalettemodified/contract';

console.log(createAddress('0x5BF4be9de72713bFE39A30EbE0691afd5fb7413a'));
// => "0x5BF4be9de72713bFE39A30EbE0691afd5fb7413a"

addressToString

Converts from Address to string.

Type: function addressToString(address: Address): string

Example

import { createAddress, addressToString } from '@openpalettemodified/contract';

const address = createAddress('0x5BF4be9de72713bFE39A30EbE0691afd5fb7413a');
console.log(addressToString(address)); // => "'0x5BF4be9de72713bFE39A30EbE0691afd5fb7413a'"

CHAIN_ID

Types and constants for working with Ethereum chains.

export type ChainId = '0x1' | '0x4';

export const CHAIN_ID = {
  MAINNET: '0x1' as const,
  RINKEBY: '0x4' as const,
};

ABI

The contract ABI.

Type: string[]

getContractAddress

Get the OpenPalette contract address for the given chainId.

Type: function getContractAddress(chainId: ChainId): Address

setContractAddress

Set the OpenPalette contract address for the given chainId.

This is useful if you need to overwrite the default addresses.

Type: function setContractAddress(chainId: ChainId): Address

getOwner

Get the address of the owner of tokenId.

If an error is thrown internally, returns undefined.

Type: function getOwner(contract: IERC721Contract, tokenId: number): Promise<Address | undefined>

getBalance

Get the amount of tokens owned by owner.

If an error is thrown internally, returns 0.

Type: function getBalance(contract: IERC721Contract, owner: Address): Promise<number>

getTokensForOwner

Get all tokens owned by owner.

Each token must be fetched individually, and fetching individual tokens may fail. This returns an array containing every token fetched successfully.

Type: function getTokensForOwner(contract: IERC721Contract, tokenId: number): Promise<number[]>