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

@danielabalde/token-client

v1.0.1

Published

Smart Contracts to generalize and simplify operations with fungible and non-fungible tokens

Downloads

3

Readme

npm i @danielabalde/token-client

TokenClient.sol is a smart contract that facilitates support for fungible and non-fungible token operations. It contains a set of TokenAbstraction implementations with read and transfer functions and without storage (see TokenERC20, TokenERC721 or TokenERC1155), which are responsible for calling the functions of each standard. For the developer, TokenClient allows to generalize the functionality of its operations with tokens using the template pattern, and support new token standards using the proxy pattern.

The functionality of TokenAbstraction is basic due to the differences between standards, but is sufficient for many use cases. If needed, its implementations can wrap more functionality, as is the case of TokenERC721 that includes the owner method.

function isStandard(address contractAddress) external view returns(bool);
function isOwner(Token calldata token, address account) external view returns (bool);
function isOwnerSet(TokenSet calldata tokenSet, address account) external view returns (bool);
function isApproved(Token calldata token, address account, address operator) external view returns (bool);
function isApprovedSet(TokenSet calldata tokenSet, address account, address operator) external view returns (bool);
function transfer(Token calldata token, address from, address to) external returns (bool);
function transferSet(TokenSet calldata tokenSet, address from, address to) external returns (bool);

The functionality uses the Token and TokenSet structures. Token represents for fungibles a quantity of tokens while for non-fungibles it represents a single element. TokenSet represents for non-fungible tokens a set of unique elements. The token id is of type bytes32 instead of uint256 to support more powerful NFTs.

struct Token
{
   bytes32 Standard;
   address Contract;
   bytes32 Id;
   uint256 Amount;  
}

struct TokenSet
{
   bytes32 Standard;
   address Contract;
   bytes32[] Ids;
   uint256[] Amounts;  
}

Example of use:

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.9;

import "@danielabalde/token-client/contracts/TokenClient.sol";

/*
   Simple buy/sell mechanism but potentially supports any standard

   This is how to suport ERC20, ERC721 and ERC1155 using ethers.js:

   const tokenClient = await deploy("TokenClient");

   const tokenERC20 = await deploy("TokenERC20");
   const tokenERC721 = await deploy("TokenERC721");
   const tokenERC1155 = await deploy("TokenERC1155");
   
   await tokenClient.support(tokenERC20.address);
   await tokenClient.support(tokenERC721.address);
   await tokenClient.support(tokenERC1155.address); 
*/

contract ExampleOfUse
{
   struct Item
   {
       Token Token;
       uint256 Price;
       address Owner;
   }

   TokenClient private _client;
   mapping(uint256=>Item) private _items;
   uint256 private _itemCount;

   constructor(address tokenClient) {
       _client = TokenClient(tokenClient);
   }

   function sell(Token calldata token, uint256 price) external returns(uint256 id) {
       require(_client.isOwner(token, msg.sender), "Not the owner");
       require(_client.isApproved(token, msg.sender, address(_client)), "Not approved");
       require(price > 0, "Price is zero"); 
       id = ++_itemCount;
       _items[id] = Item(token, price, msg.sender);
   }

   function buy(uint256 id) external payable {
       Item memory item = _items[id];
       require(item.Owner != address(0), "id not found");
       require(msg.value >= item.Price, "Not enough value");
       payable(item.Owner).transfer(msg.value);
       _client.transfer(item.Token, item.Owner, msg.sender);
   } 
}