Coder Social home page Coder Social logo

contract-catalogue's Introduction

FA2 tokens

ligo/fa library provides :

  • the interface and types defined by FA2 (TZIP-12)

  • a LIGO implementation for :

    • unique NFTs: This contract implements the FA2 interface for NFT(non-fungible-token) where a token can belong to only one address at a time (1:1)
    • Single Assets: This is an implementation of Single Asset Token where a different amount of single token can belong to multiple addresses at a time (1:n)
    • Multiple Assets: This is an implementation of Multi Asset Token where there are several token ids (available in different amounts) and they can belong to multiple addresses (m:n)
  • the interface and types defined by FA2.1 (TZIP-26)

  • a LIGO implementation for :

    • unique NFTs: This contract implements the FA2.1 interface for NFT(non-fungible-token) where a token can belong to only one address at a time (1:1)
    • Single Assets: This is an implementation of Single Asset Token where a different amount of single token can belong to multiple addresses at a time (1:n)
    • Multiple Assets: This is an implementation of Multi Asset Token where there are several token ids (available in different amounts) and they can belong to multiple addresses (m:n)

Use the implementation directly

The library provides you 3 template implementations ready to deploy

  1. To install this package, run ligo install @ligo/fa. It will download the files
  2. Deploy the NFT contract with Taquito the Ghostnet with alice wallet
make compile
make deploy

Extend an implementation

If you need additional features in your contract, you can use the extendable version. An example is available in the file examples/mintable.mligo. Using the extension mechanism, it adds an admin address to the storage, as well as a mint entrypoint to mint owner-less NFTs. Only the admin can call this entrypoint.

Install the library and create a new file

ligo install @ligo/fa
touch mintable.mligo

To extend the storage, define the type of the extension and refer to the original storage type as such:

#import "@ligo/fa/lib/main.mligo" "FA2"

module NFT = FA2.NFTExtendable

type extension = {
  admin: address
}

type storage = extension NFT.storage
type ret = operation list * storage

Importing the library allows you to refer to the TZIP12 operations signatures and make it easier to redefine all the entrypoints and views that are required:

(* Standard FA2 interface, copied from the source *)

[@entry]
let transfer (t: NFT.TZIP12.transfer) (s: storage) : ret =
  NFT.transfer t s

[@entry]
let balance_of (b: NFT.TZIP12.balance_of) (s: storage) : ret =
  NFT.balance_of b s

(* Etc. *)

To make it easier to define new entrypoints, some functions are available in the library, and you can also use the storage fields directly:

(* Extension *)

type mint = {
   owner    : address;
   token_id : nat;
}

[@entry]
let mint (mint : mint) (s : storage): ret =
  let sender = Tezos.get_sender () in
  let () = assert (sender = s.extension.admin) in
  let () = NFT.Assertions.assert_token_exist s.token_metadata mint.token_id in
  (* Check that nobody owns the token already *)
  let () = assert (Option.is_none (Big_map.find_opt mint.token_id s.ledger)) in
  let s = NFT.set_balance s mint.owner mint.token_id in
  [], s

Note that this version requires the minted NFTs to be already defined in the token_metadata big map. However, you can also change the mint entrypoint to create new tokens dynamically.

Implement the interface differently

If you are not happy with the default NFT implementation, you can define your own

Create a new file

touch myTzip12NFTImplementation.jsligo

Import some code and define implementation of missing types ledger and operators

#import "@ligo/fa/lib/fa2/common/errors.mligo" "Errors"

#import "@ligo/fa/lib/fa2/common/assertions.jsligo" "Assertions"

#import "@ligo/fa/lib/fa2/common/tzip12.datatypes.jsligo" "TZIP12"

#import "@ligo/fa/lib/fa2/common/tzip12.interfaces.jsligo" "TZIP12Interface"

#import "@ligo/fa/lib/fa2/common/tzip16.datatypes.jsligo" "TZIP16"

export namespace NFT implements TZIP12Interface.FA2{
    export type ledger = big_map<nat, address>;
    type operator = address;
    export type operators = big_map<[address, operator], set<nat>>;
    export type storage = {
        ledger: ledger,
        operators: operators,
        token_metadata: TZIP12.tokenMetadata,
        metadata: TZIP16.metadata
    };
    type ret = [list<operation>, storage];

}

Copy the missing entrypoints from the TZIP12 interface and give your own implementation

  @entry
    const transfer = (p: TZIP12.transfer, s: storage): ret => {
        failwith("TODO");
    };
    @entry
    const balance_of = (p: TZIP12.balance_of, s: storage): ret => {
        failwith("TODO");
    };
    @entry
    const update_operators = (p: TZIP12.update_operators, s: storage): ret => {
        failwith("TODO");
    };

Compile it (do not forget to add the parameter -m NFT as you have to define a namespace to be able to implement an interface)

ligo compile contract myTzip12NFTImplementation.jsligo -m NFT

contract-catalogue's People

Contributors

aguillon avatar callistonianembrace avatar d-plaindoux avatar erivas-ligo avatar frankhillard avatar johnyob avatar laucans avatar ligosuzannesoy avatar lykimq avatar melwyn95 avatar nigro64 avatar pewulfman avatar zamrokk avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

contract-catalogue's Issues

reference from GitLab to GitHub

Dear team,
You have archived your GitLab repository without a link to this GitHub organization.
We have been wrongly pointed to the GitLab without knowledge about this more recent one and wondered a week about the different transfer signature.
It would be great if you could place a big disclaimer with a redirect to the GitLab (or clone to that location with read only) in order to avoid this happening to other community members.
Thank you for this great collection!

Best regards
Carlo

bug , cannot install tezos-ligo-fa2

Running below command on WSL2 ubuntu

ligo install tezos-ligo-fa2


/home/zamrokk/.yarn/bin/esy
info add 0.6.12-dev (using package.json)
info resolving esy packages: done                
info solving esy constraints: done         
info resolving npm packages: done                             
info fetching: done                            
info [email protected]: running postinstall lifecycle      
.... installing [email protected]@d41d8cd9esy-rewrite-prefix: symlink stat
                    ./.ligo/source/s/esbuild__0.15.13__42403abe/./.ligo/source/s/esbuild__0.15.13__42403abe/LICENSE.md:
                    No such file or directory
error: error running command: '/home/zamrokk/.config/yarn/global/node_modules/esy/3/i/esy-71a059e4/lib/esy/esyRewritePrefixCommand' '--orig-prefix' './.ligo/source/s/esbuild__0.15.13__42403abe' '--dest-prefix' './.ligo/source/i/esbuild__0.15.13__42403abe' './.ligo/source/s/esbuild__0.15.13__42403abe'
  installing [email protected]
esy: exiting due to errors above
error while install packages

Make deploy scripts more useful

Right now the script in deploy/ are only useful to quickly test that the contracts (actually, only one) can indeed be deployed on a Tezos network. However, the initial ledger, the metadata, the token metadata, etc. are all pre-set and so is the account originating the contract (although that does not matter much as there is no admin to this contract).

It could be useful to spend some time improving this contract so that users can effortlessly use it to deploy new tokens. This would require setting all the aforementioned data and reading the required information (secret key, etc.) from e.g. an environment variable.

I suggest to follow the example from the permit-cameligo repository. It would probably be more intuitive to configure every information (private key, RPC, and initial storage) in an ad-hoc JSON file.

Moreover, in the same spirit of building an extensible library than for the last PRs, it would be nice if such as script could be extended by projects using the library.

Doing this in TypeScript seems to make the most sense.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.