1.0.5 • Published 3 years ago

zecrey-legend-nft-sdk v1.0.5

Weekly downloads
-
License
ISC
Repository
-
Last release
3 years ago

zecrey-legend-js-sdk

This is a JS toolkit to conduct Legend NFT transactions for front-end projects of Dapp.

Wallet

Please guide your Dapp users to install Zecrey Extension Wallet in their browser. And a Legend account is required to sign proofs of all kinds of NFT transactions. Please notice your Dapp user to register inside the wallet, if the user haven't own one yet.

Connect Zecrey Wallet

Connect to Zecrey Wallet to access the account.

import { Wallet } from "zecrey-legend-nft-sdk";

const account = await Wallet.connectWallet();
/*
  {
    "address": "0x4A807505796c29AbBD7dc65D78dC59d772a7f67e", // L1 address
    "accountName": "test.zec", // L2 account name
    "accountIndex": 6, // L2 account index
    "publicKey": "4904e42a3a373d49ae4e818b5df6aa703b2648a71356c42e56ffda3abf00211b", // L2 public key
    "uncompressed": "040f5f6d14ba81c7e0e95ec21946830a78bb610d5b368ed7a622c556236fc20b1b2100bf3adaff562ec45613a748263b70aaf65d8b814eae493d373a2ae40449" // L2 uncompressed public key
  }
*/

Get supported asset list

Your Dapp may want to handle the transactions or display assets, get the information of Legend assets at first, that can be used to purchase NFT or pay for the gas fee.

import { Wallet, info } from "zecrey-legend-nft-sdk";

// Get with a net request.
const assets = await Wallet.getSupportedAssets();
// OR
// Get from the package.
if (info.ready) console.log(info.assets);
/*
[
    {
        "asset_id": 0,
        "asset_name": "BNB",
        "asset_decimals": 18,
        "asset_symbol": "BNB",
        "asset_address": "0x00",
        "is_gas_asset": 1
    },
    {
        "asset_id": 1,
        "asset_name": "LEG",
        "asset_decimals": 18,
        "asset_symbol": "LEG",
        "asset_address": "0xE7e268058BB9CcbfD0257fbDD90B6213323Bb4d8",
        "is_gas_asset": 0
    },
    {
        "asset_id": 2,
        "asset_name": "REY",
        "asset_decimals": 18,
        "asset_symbol": "REY",
        "asset_address": "0xf7AA230B3B7E56D2188947C416f7aff4c325eDf4",
        "is_gas_asset": 0
    }
]
*/

Account's asset information

A Legend account is required to sign proofs of all kinds of NFT transactions. If your Dapp user haven't own a Legend account, please

Get account's information by name

Get account's avatar, banner, and social links.

import { Account } from "zecrey-legend-nft-sdk";

let info = await getUserByName("alice.zec");
/*
  {
    "id": 187,
    "name": "alice.zec",
    "nickname": "alice",
    "index": 144,
    "avatar": "https://res.cloudinary.com/zecrey/image/upload/v1665211994/collection/hn6bmzpz2vftnbd7mpem.png",
    "avatar_media_id": "collection/hn6bmzpz2vftnbd7mpem",
    "banner": "https://res.cloudinary.com/zecrey/image/upload/v1665213008/collection/fr6c5jhvunvc7euczsh7.jpg",
    "banner_media_id": "collection/fr6c5jhvunvc7euczsh7",
    "pub_key": "d56fe442700abfb04ed815c1d506af9a45e0ad02680e554787cc7bdf7530b227",
    "links": {
        "official": "",
        "twitter": "",
        "instagram": ""
    }
  }
*/

Update account information

import { Account } from "zecrey-legend-nft-sdk";

await Account.updateAccount(
  { account_name: "alice.zec" },
  {
    avatar: "collection/ak9v7gvz55qs82vfiqem",
    banner: "collection/xw6f20c1y5fvcprnq3ji",
  }
);

Get account's name hash

The hash of account name is used in generating proofs of all kinds transaction.

import { Account } from "zecrey-legend-nft-sdk";

let name_hash = await getNameHash("alice.zec");
// "0325546c06ccb9283328792aa10ad8ac197a33d2ce39fee0680b156e6812f2a2"

Get account's balance

Get account's Legend assets' balance.

import { Account } from "zecrey-legend-nft-sdk";

const assets = await Account.getBalance("alice.zec");
/*
[
    {
        "id": 0,
        "symbol": "BNB",
        "decimal": 18,
        "balance": "4.9995"
    }
]
 */

Get account's NFT

Get NFT information the account owned.

import { Account } from "zecrey-legend-nft-sdk";

const offset = 0;
const limit = 10;
const assets = await Account.getNFT("amber1.zec", offset, limit);
/*
[
  {
    "id": 61,
    "nftIndex": 16,
    "contentHash": "b733afb19e11d2c560a837ce21ac5c16a37c585cb6dbb2daf7d0c397bbf3c8bd",
    "name": "vbnm #15",
    "desc": "-",
    "url": "https://res.cloudinary.com/zecrey/image/upload/v1665373010/collection/fuzejbpuwdg8zya32w3n.jpg",
    "nft_url": "",
    "createdAt": 1665373021455,
    "price": 1,
    "price_asset_id": 0,
    "properties": [],
    "levels": [],
    "stats": [],
    "collection": {
        "id": 16,
        "l2_id": 0,
        "name": "Name default 13",
        "shortname": "Shortname default 13",
        "desc": "Description default 13",
        "verified": true,
        "createdAt": 1665301053713,
        "token": {
            "symbol": "BNB",
            "decimal": 18,
            "asset_id": 0
        },
        "creatorEarningRate": 0
    },
    "owner": {
        "id": 13,
        "name": "amber1.zec",
        "nickname": "amber1",
        "index": 4,
        "avatar": "collection/hn6bmzpz2vftnbd7mpem",
        "banner": "collection/fr6c5jhvunvc7euczsh7",
        "pub_key": "d56fe442700abfb04ed815c1d506af9a45e0ad02680e554787cc7bdf7530b227"
    }
  }
]
*/

Get account's collection

Get collection information the account created.

import { Account } from "zecrey-legend-nft-sdk";

const offset = 0;
const limit = 10;
const collections = await Account.getCollection("amber1.zec", offset, limit);
/*
[
  {
    "id": 46,
    "l2_id": 6,
    "name": "334555",
    "shortname": "334555",
    "logo": "https://res.cloudinary.com/zecrey/image/upload/v1665373101/collection/nliewkyaxa3wntub2qa9.jpg",
    "banner": "https://res.cloudinary.com/zecrey/image/upload/v1665373115/collection/hz6ku1rgg1qfgxbzvfk7.jpg",
    "desc": "",
    "verified": true,
    "createdAt": 1665373124523,
    "token": {
        "symbol": "BNB",
        "decimal": 18,
        "asset_id": 0
    },
    "creatorEarningRate": 0
  }
]
*/

Get account's activity records

import { Account } from "zecrey-legend-nft-sdk";

const offset = 0;
const limit = 10;
const activities = await Account.getActivity("amber1.zec", offset, limit);
/*
  [
    {
      "block_height": 207,
      "created_at": 1665567221502,
      "from_account_index": 4,
      "from_account_name": "amber1.zec",
      "id": 211,
      "nft_index": 47,
      "nft_media_url": "https://res.cloudinary.com/zecrey/image/upload/v1665567191/collection/lx7dc0c5erzzeikm3lfe.jpg",
      "nft_name": "test111 #38",
      "nft_num": 1,
      "payment_amount": "100000000000000",
      "status": 1,
      "to_account_index": 4,
      "to_account_name": "amber1.zec",
      "tx_created_time": 0,
      "tx_hash": "D6CWMN17kd+Nc1/pHIcXdknIh/UejUgRyKlZGbg6Lok=",
      "tx_id": 207,
      "tx_info": {
          "CreatorAccountIndex": 4,
          "ToAccountIndex": 4,
          "ToAccountNameHash": "2e9941d27744e31ac14d3ccbc3a897cc50bd9c34ade7071a28fbe46bfa3e7086",
          "NftIndex": 47,
          "NftContentHash": "05f7f748b6d5f92a074ccb799a03151bf704f0a11ff25c2cabb6bd925f5e46d1",
          "NftCollectionId": 0,
          "CreatorTreasuryRate": 20,
          "GasAccountIndex": 1,
          "GasFeeAssetId": 0,
          "GasFeeAssetAmount": 100000000000000,
          "ExpiredAt": 1665653607070,
          "Nonce": 92,
          "Sig": "IvpCqoI+zEJjlHbMJZYbzToCow3zEXYXr0hJPsbCLwADivA2COOWSJosDW7ByRYYr4eEQcIrC7mNgcIynJARXw=="
      },
      "tx_type": 12,
      "updated_at": 1665567221502,
      "verified_at": 0,
      "deleted_at": null
    }
  ]
*/

Media

Update media

import { Media } from "zecrey-legend-nft-sdk";

let image = await Media.uploadImg(file);
/*
  {
    "public_id": "collection/wgaipl6vhtao5lu0fdbz",
    "url": "https://res.cloudinary.com/zecrey/image/upload/v1665471045/collection/wgaipl6vhtao5lu0fdbz.png"
  }
*/

Get media url array

import { Media } from "zecrey-legend-nft-sdk";

let url = await Media.getUrls(["collection/wgaipl6vhtao5lu0fdbz"]);
// ["https://res.cloudinary.com/zecrey/image/upload/v1665471045/collection/wgaipl6vhtao5lu0fdbz.png"]

Transaction

Mint NFT

import { Tx } from "zecrey-legend-nft-sdk";

let creator = { account_name: "my_dapp_admin.zec", account_index: 124 };
let params = {
  to_name: "alice.zec", // owner of the new NFT
  to_index: 62, // owner of the new NFT
  collection_id: 22,
  nft_name: "Medal of Victory", // name of the new NFT
  description: "This a medal for the game winner.", // description of the new NFT
  properties: [{ name: "color", value: "golden" }],
  levels: [{ name: "size", value: 30, maxValue: 100 }],
  stats: [],
  media: "collection/onj6cukbn32n3o1ovmfz",
};

let asset_id = await Tx.mintNFT(creator, params); // 80

Create Collection

import { Tx } from "zecrey-legend-nft-sdk";

let creator = { account_name: "bob.zec", account_index: 124 };
let params = {
  name: "Medals",
  description: "",
  shortname: "medals-of-testing",
  logo: "collection/onj6cukbn32n3o1ovmfz",
  banner: "collection/onj6cukbn32n3o1ovmfz",
  percentage: 0.1, // means creator earn rate is 0.1%
};

let asset_id = await Tx.createCollection(creator, params); // 80

Transfer NFT

import { Tx } from "zecrey-legend-nft-sdk";

let from = { account_name: "bob.zec", account_index: 124 };
let params = { nft_index: 21, payee: { name: "sher.zec", index: 2 } };

await Tx.transferNFT(from, params);

Withdraw NFT

import { Tx } from "zecrey-legend-nft-sdk";

let from = { account_name: "bob.zec", account_index: 124 };
let params = {
  nft_index: 22,
  target: "0x09E45d6FcF322c4D93E6aFE7076601FF10BA942E",
};

await Tx.withdrawNFT(from, params);

Gas Fee

Transaction Type | Id | Transaction type name | | --- | --------------------- | | 11 | Create Collection | | 12 | Mint NFT | | 13 | Transfer NFT | | 14 | Atomic Match | | 15 | Cancel Offer | | 16 | Withdraw NFT |

import { Tx, info } from "zecrey-legend-nft-sdk";

let value = await Tx.getLegendGasFee(info.gas_fee_asset_id, 11); // 100000000000000
let fee = Number(value) / Math.pow(10, info.gas_fee_asset_decimal);

Offer

Get offers

import { Offer } from "zecrey-legend-nft-sdk";

let offers = await Offer.getOfferBySender("alice.zec");
/*
  [
    {
      "id": 74,
      "l2_id": 43,
      "createdAt": 1665564939918,
      "expiredAt": 1665996933434,
      "counterpartId": 0,
      "status": 1,
      "signature": "{\"Type\":0,\"OfferId\":43,\"AccountIndex\":4,\"NftIndex\":13,\"AssetId\":0,\"AssetAmount\":1000000000000000000,\"ListedAt\":1665564935753,\"ExpiredAt\":1665996933434,\"TreasuryRate\":30,\"Sig\":\"6PYMFmLyd2zbt7v6lsUKIPDVjLcGE830DOSBax41EgAEgZHpZxEQh4OMtqjWmeEx+rZvJTaGc7po/ZogXqOo9w==\"}",
      "assetId": 0,
      "amount": 1,
      "direction": 0
    }
  ]
*/
let offer_id = 74;
let offer = await Offer.getOffer(offer_id);
/*
  {
    "id": 74,
    "l2_id": 43,
    "createdAt": 1665564939918,
    "expiredAt": 1665996933434,
    "counterpartId": 0,
    "status": 1,
    "signature": "{\"Type\":0,\"OfferId\":43,\"AccountIndex\":4,\"NftIndex\":13,\"AssetId\":0,\"AssetAmount\":1000000000000000000,\"ListedAt\":1665564935753,\"ExpiredAt\":1665996933434,\"TreasuryRate\":30,\"Sig\":\"6PYMFmLyd2zbt7v6lsUKIPDVjLcGE830DOSBax41EgAEgZHpZxEQh4OMtqjWmeEx+rZvJTaGc7po/ZogXqOo9w==\"}",
    "assetId": 0,
    "amount": 1,
    "direction": 0
  }
*/
let nft_id = 20;
let sell_offers = await Offer.getSellOfferByAsset(nft_id);
let buy_offers = await Offer.getBuyOfferByAsset(nft_id);

Make offer

import { info, Tx } from "zecrey-legend-nft-sdk";

let from = { account_name: "bob.zec", account_index: 124 };
let params = {
  type: "buy",
  nft_index: 29,
  asset_id: info.assets[0].asset_id,
  asset_amount: parseUnits("1.0", info.assets[0].asset_decimals).toString(),
  exipred_at: Date.now() + 5 * 24 * 60 * 60 * 1000,
};

let offer_id = await Tx.makeOffer(from, params); // 55

Cancel offer

import { Tx } from "zecrey-legend-nft-sdk";

let from = { account_name: "bob.zec", account_index: 124 };
let params = {
  offer_id: 55,
  offer_l2_id: 33,
};

let offer_id = await Tx.cancelOffer(from, params); // 55

Accept offer

import { Tx } from "zecrey-legend-nft-sdk";

let from = { account_name: "bob.zec", account_index: 124 };
let params = { offer_ids: [74, 47] };

await Tx.acceptOffer(from, params);
1.0.5

3 years ago

1.0.4

3 years ago

1.0.2

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago