0.0.9 • Published 2 years ago

@nftsafe/sdk v0.0.9

Weekly downloads
-
License
MIT
Repository
-
Last release
2 years ago

Welcome to NFTSafe SDK

Lend and rent any ERC721s and ERC1155s on supported mainnet and testnet.

NFT Safe allows you to lend and rent ERC 721 and ERC 1155 NFTs in collateralized and collateral free way.

Install

yarn add @nftsafe/sdk or Add "@nftsafe/sdk": "latest-version" to your package.json

How to access:

import-module in your in page import { SupportedChainIds, ContractType, GetNetworkDetailsByChainId, CONTRACT_TYPE_LIST,SupportedChainIds, ALL_SUPPORTED_CHAIN_IDS ....} from '@nftsafe/sdk'

Get Contract and Network details:

SDK provider the "NetworkConfig" JSON to get the Contract addresses, Subgraph URL, Chain information, and the Blockchain supporting utils
NetworkConfig = { chainid: { name: string, chainId: number, shortName: string, chain: string, networkId: number, nativeCurrency: {}, rpc: string[], faucets: string[], explorers: { name: string, url: string, standard: string }, infoURL: string, logoURL: string, collateralizedContractAddresses: string[], collateralFreeContractAddresses: string[], revenueSharedConfiguratorContractAddresses: string[], e721ContractAddresses: string[], // Testnet e721BContractAddresses: string[], // Testnet e1155ContractAddresses: string[], // Testnet e1155BContractAddresses: string[], // Testnet wETHContractAddresses: string[], daiContractAddresses: string[], usdcContractAddresses: string[], usdtContractAddresses: string[], tUSDContractAddresses: string[], utilsContractAddresses: string[], subgraphs: { collateralized: string, collateralFree: string, e721: string, // Testnet or Moralis unsupported chains e1155: string // Testnet or Moralis unsupported chains }, moralisDetails: { isSupported: boolean, lookupValue: string }, chainApiId: string, isSupported: boolean, isTestnet: boolean } }

How to use:

  • import sdk import { NetworkConfig, SupportedChainIds, ContractType, GetNetworkDetailsByChainId } from '@nftsafe/sdk';

  • Now you can use it

const GetNetworkDetailsByChainId = (networkId: SupportedChainIds): TypeNetworkDetails => { return NetworkConfignetworkId } const polygonContactDetails = GetNetworkDetailsByChainId(137) Or const polygonContactDetails = NetworkConfig137

  1. polygonContactDetails.collateralizedContractAddresses0
  2. polygonContactDetails.collateralFreeContractAddresses0 .... To get subgraph endpoint
  3. polygonContactDetails.subgraphs.collateralized
  4. polygonContactDetails.subgraphs.collateralFree ....

Supported Chains:

The "SupportedChainIds" enum is available in SDK to help you

Supported Payment Tokens:

The "PaymentToken" enum will return currently supporting Payment tokens.

Supporting NFT Standard:

The "NFTStandard" enum avaialble in sdk

Create NFTSafe contract object :

The "ContractType" enum which is already present in SDK will help you to get the specific type of contract.

  1. Collateralized Contract: final object = new NFTSafe(signer, Number(chainId), ContractType.COLLATERALIZED);
  2. Collateral Free Contract: final object = new NFTSafe(signer, Number(chainId), ContractType.COLLATERAL_FREE);

Call to contract methods:

  • Here, in arguments all individual elements should be in the form of List
  1. object.lend( nftStandards: NFTStandard[], nftAddresses: string[], tokenIds: BigNumber[], lendAmounts: number[], maxRentDurations: number[], minRentDurations: number[], dailyRentPrices: number[], paymentOptions: PaymentToken[], collateralPrices: number[] );

  2. object.stopLending( nftStandards: NFTStandard[], nftAddresses: string[], tokenIds: BigNumber[], lendingIds: BigNumber[] );

  3. object.rent( nftStandards: NFTStandard[], nftAddresses: string[], tokenIds: BigNumber[], lendingIds: BigNumber[], rentDurations: number[], rentAmounts: number[] );

  4. object.stopRenting( nftStandards: NFTStandard[], nftAddresses: string[], tokenIds: BigNumber[], lendingIds: BigNumber[], rentingIds: BigNumber[] );

  5. object.claimRentOrCollateral( nftStandards: NFTStandard[], nftAddresses: string[], tokenIds: BigNumber[], lendingIds: BigNumber[], rentingIds: BigNumber[] );

  • Later SDK will use "prepareBatch" methods of SDK's utils file to convert it into formatted value
  • Let's see for lend call how SDK converts data into formatted data by using "prepareBatch" method

    lend( nftStandards: NFTStandard[], nftAddresses: string[], tokenIds: BigNumber[], lendAmounts: number[], maxRentDurations: number[], minRentDurations: number[], dailyRentPrices: number[], paymentOptions: PaymentToken[], collateralPrices: number[] ): Promise { const args = prepareBatch({ nftStandards: nftStandards.map(nft => Number(nft)), nftAddresses: nftAddresses.map(nft => String(nft).toLowerCase()), tokenIds: tokenIds.map(id => BigNumber.from(id)), lendAmounts: lendAmounts.map(amt => Number(amt)), maxRentDurations: maxRentDurations.map(x => Number(x)), minRentDurations: minRentDurations.map(x => Number(x)), dailyRentPrices: dailyRentPrices.map(x => packPrice(Number(x))), paymentOptions, collateralPrices: collateralPrices.map(x => packPrice(Number(x))), });

    return await this.contract.lend( args.nftStandards, args.nftAddresses, args.tokenIds, args.lendAmounts, args.maxRentDurations, args.minRentDurations, args.dailyRentPrices, args.paymentOptions, args.collateralPrices ); }

Note: Good to have below validation

  1. the "nftStandards" should be one of the available in "NFTStandard" type of SDK. For instance, NFTStandard.E721 and NFTStandard.E1155
  2. The "maxRentDurations" must be grater than "minRentDurations".
  3. The "paymentOptions" should be one of the available in "PaymentToken" type of SDK. To give you an idea, PaymentToken.WETH, PaymentToken.USDT etc.
  4. The "collateralPrices" value should be ZERO for the CollateralFree contract.
  • In addition, for Lend method only it will use two unique methods for below to values:
    • dailyRentPrices: dailyRentPrices.map(x => packPrice(Number(x))),
    • collateralPrices: collateralPrices.map(x => packPrice(Number(x))),

packPrice" : Used by SDK

  • Converts a number into the format that is acceptable by the NFTSafe contract.
  • TLDR; to fit a single storage slot in the NFTSafe contract, we split the whole
  • and decimal parts of a number to only have a maximum of 4 digits. That means, the
  • maximum price is 9999.9999. If more decimals are supplied, they are truncated.
  • If the price exceeds the maximum whole part, this throws.
  • @param price value to pack
  • @returns price format that is acceptable by NFTSafe contract

"unpackPrice" : Used in frontend

  • when you fetch data from the Blockchain after that you need to use "unpackPrice" method to get the actual value from a formatted value
  • price is from 1 to 4294967295. i.e. from 0x00000001 to 0xffffffff

    dailyRentPrice = unpackPrice(formated_dailyRentPrice), collateralPrice = unpackPrice(formated_collateralPrice),

Example:

User entred value: dailyRentPrices = 6 WETH , collateralPrices = 20 WETH

Used by SDK dailyRentPrices = packprice(6); // output: 0x00060000 collateralPrices = packPrice(20) // output: 0x00140000

Used in frontend dailyRentPrices =unpackPrice(0x00060000); // output: 6 collateralPrices = unpackPrice(0x00140000) // output: 20

POLYGON_MAINNET:

collateralizedContractAddresses: "0xa0DAb138C1EC3a5D2142835E589C0a4870145107" collateralFreeContractAddresses: "0x4223e7608dB4619596b5c0E2fb0B0db5ee976bc0" subgraphs: { collateralized: "https://polygon.infy.network/subgraphs/name/infy-collateralized/subgraph", collateralFree: "https://polygon.infy.network/subgraphs/name/infy-collateral-free/subgraph" }

POLYGON_TESTNET:

collateralizedContractAddresses: "0x145BC3029bA50B9F5279e1335b5B6963cF8693Df" collateralFreeContractAddresses: "0x9a8EFf42Ce306655B21FC527213A69fc894b821b" subgraphs: { collateralized: "https://mumbai.infy.network/subgraphs/name/infy-collateralized/subgraph", collateralFree: "https://mumbai.infy.network/subgraphs/name/infy-collateral-free/subgraph" }