blockloyal-library
v1.9.0
Published
- [Installing](#installing) - [Package manager](#package-manager) - [CDN](#cdn) - [Example](#example) - [License](#license)
Downloads
28
Readme
Table of Contents
Installing
Package manager
Using npm:
$ npm i blockloyal-library
Using yarn:
$ yarn add blockloyal-library
Once the package is installed, you can import the library using import
or require
approach:
import { getNftsForAddress } from 'blockloyal-library';
CDN
Using jsDelivr CDN (ES5 UMD browser module):
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/blockloyal-library.cjs.production.min.js"></script>
Examples
NPM implementation
Get NFTs for address
The getNftsForAddress function allows you to get an array of non-fungible tokens (NFTs) minted by BlockLoyal that are owned by a given Ethereum address.
Example:
import { getNftsForAddress } from 'blockloyal-library';
// Get blockloyal's nfts associated with a wallet
getNftsForAddress(walletId)
.then(function(response) {
// handle success
console.log(response);
})
.catch(function(error) {
// handle error
console.log(error);
})
.finally(function() {
// always executed
});
// use async approach
const nfts = await getNftsForAddress(walletId);
Get user balance for address
The getUserBalanceForAddress function allows you to get the balance of an Ethereum address in ETH. It uses the ethers library to query the Ethereum blockchain for the given address's balance.
Example:
import { getUserBalanceForAddress } from 'blockloyal-library';
getUserBalanceForAddress(walletId)
.then(function(response) {
// handle success
console.log(response);
})
.catch(function(error) {
// handle error
console.log(error);
})
.finally(function() {
// always executed
});
// use async approach
const nfts = await getUserBalanceForAddress(walletId);
Get Loyalty Program for NFT
The getLoyaltyProgramForNft function allows you to get the loyalty program information for a given non-fungible token (NFT). It makes an HTTP request to BlockLoyal's server to retrieve the information.
Example:
import { getLoyaltyProgramForNft } from 'blockloyal-library';
// Get blockloyal's loyalty program associated with NFT
getLoyaltyProgramForNft(nftId)
.then(function(response) {
// handle success
console.log(response);
})
.catch(function(error) {
// handle error
console.log(error);
})
.finally(function() {
// always executed
});
// use async approach
const nfts = await getUserBalanceForAddress(nftId);
useWallet
The useWallet hook allows a React component to interact with a user's Ethereum wallet. It provides functions for connecting to the wallet, getting the user's address, and getting the user's balance.
To use the useWallet hook, import it into your React component and call it to get an object with the following properties:
- connect: a function that attempts to connect to the user's Ethereum wallet. If successful, it sets the user's address in the hook's state.
- address: the user's Ethereum address, or null if the user is not connected to a wallet.
- getUserBalance: a function that returns a promise that resolves to the user's Ethereum balance, in ETH.
- errorMessage: a string containing an error message, if an error occurred while attempting to connect to the wallet.
Example:
import React, { useState, useEffect } from 'react';
import { useWallet } from 'blockloyal-library';
const MyComponent = () => {
const { connect, address, getUserBalance, errorMessage } = useWallet();
useEffect(() => {
connect();
}, []);
useEffect(() => {
if (address) {
getUserBalance().then(balance => {
// Update the component's state with the user's balance
setBalance(balance);
});
}
}, [address]);
return (
<div>
{errorMessage && <p>{errorMessage}</p>}
{address && <p>Your Ethereum address: {address}</p>}
{balance && <p>Your Ethereum balance: {balance} ETH</p>}
</div>
);
};
Add points on NFT
The addLoyaltyPointsForNftPostPurchase functions allows the addition of post-purchase points on a given NFT. It makes an HTTP PUT request to BlockLoyal's server to register the transaction and returns an updated NFT.
Example:
import { addLoyaltyPointsForNftPostPurchase } from 'blockloyal-library';
// add points on post purchase
addLoyaltyPointsForNftPostPurchase(nftId, usdSpent)
.then(function(response) {
// handle success
console.log(response);
})
.catch(function(error) {
// handle error
console.log(error);
})
.finally(function() {
// always executed
});
// use async approach
const nft = await addLoyaltyPointsForNftPostPurchase(nftId, usdSpent);
CDN library
Get NFTs for address
The getNftsForAddress function allows you to get an array of non-fungible tokens (NFTs) that are owned by a given Ethereum address. It uses the Alchemy SDK to query the Ethereum blockchain for NFTs on the BlockLoyal contract.
Example:
const nftsData = await window.blockloyal.getNftsForAddress(walletId);
Get user balance for address
The getUserBalanceForAddress function allows you to get the balance of an Ethereum address in ETH. It uses the ethers library to query the Ethereum blockchain for the given address's balance.
Example:
const userBalance = await window.blockloyal.getUserBalanceForAddress(walletId);
Get Loyalty Program for NFT
The getLoyaltyProgramForNft function allows you to get the loyalty program information for a given non-fungible token (NFT). It makes an HTTP request to BlockLoyal's server to retrieve the information.
Example:
const loyaltyProgram = await window.blockloyal.getLoyaltyProgramForNft(nftId);
Add points on NFT
The addLoyaltyPointsForNftPostPurchase functions allows the addition of post-purchase points on a given NFT. It makes an HTTP PUT request to BlockLoyal's server to register the transaction and returns an updated NFT.
Example:
const nft = await window.blockloyal.addLoyaltyPointsForNftPostPurchase(
nftId,
usdSpent
);
Note
async/await
is part of ECMAScript 2017 and is not supported in Internet Explorer and older browsers, so use with caution.