1.1.45 • Published 5 months ago

libcip54 v1.1.45

Weekly downloads
-
License
BSD-3-Clause-Attr...
Repository
github
Last release
5 months ago

libcip54

Libcip54 is part of the Smart NFT API - the best place to start if you're new to the API is the Smart NFT Playground. Alternatively you may consult the Cardano Improvement Proposal

Libcip54 provides the backend queries to the DBSync chain indexer in order to generate the initial page load data for a Smart NFT render, along with the necessary queries to respond to any additional API requests made by the NFT.

Libcip54 is intended to be used with the <SmartNFTPortal> React component, which is responsible for rendering the NFT and providing the client-side Javascript API to the NFT running inside the <iframe>. These two packages operating in tandem make up the Smart NFT API.

The Smart NFT Playground includes a very simple backend using libcip54 to provide the endpoints needed to render Smart NFTs - I suggest consulting the API section of that repository

Usage

Install the library via npm:

npm install libcip54

Install the library via yarn:

yarn add libcip54

Initially you must connect to the Postgres database server running DBSync. Optionally you can also connect a redis client instance for caching. Then you must pass your Postgres client handle, and optionally the redis client handle to the init() function in libcip54 before you may use the API.

import { init, getSmartImports } from "libcip54"
import pgCon from 'pg';
export default async () => { 
    const client = new pgCon.Client({connectionString: process.env.DBSYNC_URI});
    await client.connect();
    init('mainnet', client);
    const metadata={...}; // the NFT metadata as a javascript object
    const ownerAddr='Bech32 address of current owner';
    const tokenUnit='<policyID><assetNameHex>';
    const smartImports = await getSmartImports(metadata?.uses, ownerAddr, tokenUnit)
}

With a redis connection:

import { init, getSmartImports } from "libcip54"
import { createClient } from 'redis';
import pgCon from 'pg';
export default async () => { 
    const redisClient = createClient({ url: process.env.REDIS_URI });
    await redisClient.connect();
    const pgClient = new pgCon.Client({connectionString: process.env.DBSYNC_URI});
    await pgClient.connect();
    init('mainnet', pgClient, process.env.IPFS_GATEWAY, process.env.ARWEAVE_GATEWAY, redisClient);
}

Functions

In the API, when you see the term "unit" referring to a token - this just means the policyID followed by the asset name in hex encoding (with no separating character).

init: (networkId: 'mainnet' | 'testnet', connection: pgCon.Client, ipfsGateway: string, arweaveGateway: string, redisClient: redis.RedisClientType, redisPrefix: string, redisTTL: number): void

You must call this function first with your database handle, otherwise all others fill fail.

The ipfsGateway and arweaveGateway strings specify http gateways for IPFS and arweave respectively.

redisPrefix allows you to specify a prefix for all libcip54 keys in the redis database.

redisTTL allows you to specify how long the redis cache will persist.

getSmartImports: (featureTree: { libraries: { name: string; version: string; }[]; tokens: string[] | string; utxos: string[] | string; transactions: string[] | string; mintTx?: boolean; files?: boolean;}, walletAddr: string, tokenUnit: string) => Promise<any>;

This is the main function you need to call to generate the initial page load data to populate the smartImports field for <SmartNFTPortal>.

getTransactions(featureTree: { transactions: string[] | string}, walletAddr: string): Promise<object>

This provides the transactions part of the initial smartImports data, as well as responding to any subsequent calls to getTransactions() in the front end API

getTransactionsFromStake(stakeAddress: string, page?: number): Promise<any>

This is a helper function which is used by the function above

getTokens(featureTree: { tokens: string[] | string }, walletAddr: string): Promise<object>

This provides the tokens part of the initial smartImports data, as well as responding to any subsequent calls to getTokens() in the front end API.

getTokensFromStake(stakeAddress: string, page?: number): Promise<{unit: string;quantity: number;}[]>

This is another helper function used by the function above.

getUTXOs(featureTree: { utxos: string[] | string; }, walletAddr: string): Promise<any>

This provides the UTXOs part of the initial smartImports

getUTXOsFromStake(stakeAddress: string, page?: number): Promise<{txHash: string; index: number; address: string; value: number; multiasset: { quantity: number; unit: string; }[]; datum: any | null;}[]>;

This is a utility function used by the one above.

getUTXOsFromAddr(baseAddress: string, page?: number): Promise<{ txHash: string; index: number; address: string; value: number; multiasset: { quantity: number; unit: string; }[]; datum: any | null;}[]>;

This is a utility function used by getUTXOs()

getUTXOsFromEither(stakeAddress: string | null, baseAddress: string | null, page?: number): Promise<{ txHash: string; index: number; address: string; value: number; multiasset: { quantity: number; unit: string; }[]; datum: any | null;}[]>;

This is a utility function used by getUTXOs()

getLibraries(featureTree: { libraries: { name: string; version: string; }[]; }): Promise<{ libraries: string[]; css: string[];}>;

This provides the libraries part of the initial smartImports data - this is responsible for downloading the libraries from jscdn and creating data: URLs from them.

getMetadata: (unit: string) => Promise<any>;

Gets the NFT's metadata (either CIP25 or CIP68)

getMintTx: (unit: string) => Promise<{ txHash: string; metadata: { key: string; json: object; }[];} | null>;

Get the minting TX hash of a token

getCIP68Metadata: (unit: string) => Promise<any>;

Get the CIP68 metadata of a token.

1.1.38

6 months ago

1.1.37

6 months ago

1.1.36

6 months ago

1.1.40

6 months ago

1.1.45

5 months ago

1.1.43

5 months ago

1.1.29

6 months ago

1.1.28

6 months ago

1.1.34

6 months ago

1.1.33

6 months ago

1.1.32

6 months ago

1.1.35

6 months ago

1.1.1

8 months ago

1.1.0

8 months ago

1.1.9

7 months ago

1.1.8

7 months ago

1.1.7

7 months ago

1.1.6

7 months ago

1.1.12

6 months ago

1.1.11

7 months ago

1.1.10

7 months ago

1.1.16

6 months ago

1.1.15

6 months ago

1.1.14

6 months ago

1.1.13

6 months ago

1.1.19

6 months ago

1.1.18

6 months ago

1.1.17

6 months ago

1.1.23

6 months ago

1.1.22

6 months ago

1.1.21

6 months ago

1.1.20

6 months ago

1.1.27

6 months ago

1.1.26

6 months ago

1.1.25

6 months ago

1.1.24

6 months ago

1.0.51

8 months ago

1.0.50

8 months ago

1.0.49

10 months ago

1.0.47

10 months ago

1.0.45

10 months ago

1.0.44

10 months ago

1.0.43

10 months ago

1.0.42

10 months ago

1.0.41

11 months ago

1.0.40

11 months ago

1.0.39

11 months ago

1.0.38

11 months ago

1.0.37

11 months ago

1.0.36

11 months ago

1.0.35

11 months ago

1.0.34

11 months ago

1.0.33

11 months ago

1.0.31

11 months ago

1.0.30

11 months ago

1.0.29

11 months ago

1.0.28

11 months ago

1.0.27

11 months ago

1.0.26

11 months ago

1.0.25

11 months ago

1.0.24

11 months ago

1.0.23

11 months ago

1.0.22

11 months ago

1.0.21

11 months ago

1.0.20

11 months ago

1.0.19

11 months ago

1.0.18

11 months ago

1.0.17

11 months ago

1.0.16

11 months ago

1.0.14

11 months ago

1.0.13

11 months ago

1.0.12

11 months ago

1.0.11

11 months ago

1.0.10

11 months ago

1.0.9

11 months ago

1.0.8

11 months ago

1.0.7

11 months ago

1.0.6

11 months ago

1.0.5

11 months ago

1.0.4

11 months ago

1.0.3

11 months ago

1.0.2

11 months ago

1.0.1

11 months ago

1.0.0

11 months ago