token-ping

1.1.1 • Public • Published

Token Ping

Many apps and scripts need the ability to ping the blockchain for some data. The setup for even the smallest amount of data can be frustratingly time consuming — providers, ABIs, contract objects, etc.

Token Ping takes care of this by querying the Etherscan API for the contract ABI, as well as offering a few common token standards (ERC20, ERC721, and ERC1155) so you can get a contract instance ready to be queried in one line of code.

Quick Start

  1. Install the package with npm install token-ping.
  2. Require the function you need into the file with, for example, const { ERC721 } = require('token-ping');
  3. Plug in the address to get a contract instance, with const contract = ERC721("0x...")
  4. Call any view function on the contract, for example const nftOwner = await contract.ownerOf(0)

Function Parameters

There are four functions available for import: TPContract(), ERC20(), ERC721(), and ERC1155().

TPContract() fetches verified contract ABIs from Etherscan, returning a promise that resolves to a contract object. The other three functions use a local instance of the ABI to return a generic contract.

Each of the functions take 1 required argument and 2 optional arguments:

  • address (required): The address of the contract you'd like to connect to.
  • providerOrSigner (optional): If you don't want to use the ethersjs default provider, you can create your own and add it here. If you use a signer, the contract instances you create will be able to perform transactions as well as access view functions.
  • chainId (optional): The ID of the chain the contract lives on. Defaults to 1 for mainnet, with options for 3 (Ropsten) or 4 (Rinkeby).

TPContract()

The TPContract() function is the most general function. It queries the Etherscan API (available for Mainnet, Rinkeby, and Ropsten) to see if they have a verified contract.

If they do, it serves up a contract instance based on the verified contract ABI, and caches the ABI locally for future calls.

** Note that unlike the other functions, the TPContract() function returns a promise. **

ERC20() Methods

All external view methods on the ERC20 standard will be available on your contract instance.

  • function name() public view returns (string memory);
  • function symbol() public view returns (string memory);
  • function decimals() public view returns (uint8);
  • function totalSupply() public view returns (uint256);
  • function balanceOf(address account) public view returns (uint256);
  • function allowance(address owner, address spender) public view returns (uint256);

Note: If you add a signer as an argument when constructing the contract instance, you'll be able to access all external methods. See the OpenZeppelin ERC20 implementation for the full list of methods available.

ERC721() Methods

All external view methods on the ERC721 standard will be available on your contract instance.

  • function supportsInterface(bytes4 interfaceId) public view returns (bool);
  • function balanceOf(address owner) public view returns (uint256);
  • function ownerOf(uint256 tokenId) public view returns (address);
  • function name() public view returns (string memory);
  • function symbol() public view returns (string memory);
  • function tokenURI(uint256 tokenId) public view returns (string memory);
  • function getApproved(uint256 tokenId) public view returns (address);

Note: If you add a signer as an argument when constructing the contract instance, you'll be able to access all external methods. See the OpenZeppelin ERC721 implementation for the full list of methods available.

ERC1155() Methods

All external view methods on the ERC1155 standard will be available on your contract instance.

  • function supportsInterface(bytes4 interfaceId) public view returns (bool);
  • function uri(uint256) public view returns (string memory);
  • function balanceOf(address account, uint256 id) public view returns (uint256);
  • function balanceOfBatch(address[] memory accounts, uint256[] memory ids) public view returns (uint256[] memory);
  • function isApprovedForAll(address account, address operator) public view returns (bool);

Note: If you add a signer as an argument when constructing the contract instance, you'll be able to access all external methods. See the OpenZeppelin ERC1155 implementation for the full list of methods available.

Package Sidebar

Install

npm i token-ping

Weekly Downloads

1

Version

1.1.1

License

MIT

Unpacked Size

29.7 kB

Total Files

5

Last publish

Collaborators

  • obront