Skip to content

Commit

Permalink
add get IPFS image from NFT method
Browse files Browse the repository at this point in the history
  • Loading branch information
Samuel Sramko committed Aug 17, 2021
1 parent bb00199 commit efed816
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 4 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@tatumio/tatum",
"version": "1.19.18",
"version": "1.19.19",
"description": "Tatum API client allows browsers and Node.js clients to interact with Tatum API.",
"main": "dist/src/index.js",
"repository": "https://github.com/tatumio/tatum-js",
Expand Down
12 changes: 11 additions & 1 deletion src/nft/nft.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {readFileSync} from 'fs';
import {CeloBurnErc721, CeloDeployErc721, CeloMintErc721, CeloMintMultipleErc721, CeloTransferErc721, Currency, MintErc721,} from '../model';
import {burnNFT, deployNFT, mintMultipleNFTWithUri, mintNFTWithIPFSMetadata, mintNFTWithUri, transferNFT} from './nft';
import {burnNFT, deployNFT, getNFTImageFromIPFS, mintMultipleNFTWithUri, mintNFTWithIPFSMetadata, mintNFTWithUri, transferNFT} from './nft';

describe('NFT tests', () => {
jest.setTimeout(99999);
Expand Down Expand Up @@ -340,6 +340,16 @@ describe('NFT tests', () => {
console.log(await mintNFTWithIPFSMetadata(true, body, readFileSync('/Users/ssramko/Downloads/logo_tatum.png'),
'Tatum LOGO', 'description', undefined, 'https://rpc-mumbai.matic.today'));
});
it('should obtain metadata from NFT on IPFS on MATIC', async () => {
const data = await getNFTImageFromIPFS(Currency.MATIC, '0x6d8eae641416b8b79e0fb3a92b17448cfff02b11', '1629193549967');
expect(data.publicUrl).toBe('https://gateway.pinata.cloud/ipfs/Qmaiu5NAXe2gwH734hWhvyharurBjoxi8Kv37sGp1ZhRpf');
expect(data.originalUrl).toBe('ipfs://Qmaiu5NAXe2gwH734hWhvyharurBjoxi8Kv37sGp1ZhRpf');
});
it('should obtain metadata from NFT on IPFS on FLOW', async () => {
const data = await getNFTImageFromIPFS(Currency.FLOW, '2d103773-50e2-4a37-ac3d-61bc6af8faee', '145', '0x10247089e55180c9');
expect(data.publicUrl).toBe('https://gateway.pinata.cloud/ipfs/Qmaiu5NAXe2gwH734hWhvyharurBjoxi8Kv37sGp1ZhRpf');
expect(data.originalUrl).toBe('ipfs://Qmaiu5NAXe2gwH734hWhvyharurBjoxi8Kv37sGp1ZhRpf');
});
it('should test MATIC 721 mint transaction', async () => {
try {
const mintedToken = await mintNFTWithUri(true, {
Expand Down
29 changes: 27 additions & 2 deletions src/nft/nft.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {get, post} from '../connector/tatum';
import {axios, get, post} from '../connector/tatum';
import {
CeloBurnErc721,
CeloDeployErc721,
Expand Down Expand Up @@ -96,7 +96,32 @@ export const getNFTContractAddress = async (chain: Currency, txId: string): Prom
/**
* For more details, see <a href="https://tatum.io/apidoc#operation/NftGetMetadataErc721" target="_blank">Tatum API documentation</a>
*/
export const getNFTMetadataURI = async (chain: Currency, contractAddress: string, tokenId: string): Promise<{ data: string }> => get(`/v3/nft/metadata/${chain}/${contractAddress}/${tokenId}`);
export const getNFTMetadataURI = async (chain: Currency, contractAddress: string, tokenId: string, account?: string): Promise<{ data: string }> => {
let url = `/v3/nft/metadata/${chain}/${contractAddress}/${tokenId}`;
if (account) {
url += `?account=${account}`;
}
return get(url);
};

/**
* Get IPFS image URL from the NFT with the IPFS Metadata scheme. URL
* @param chain chain where NFT token is
* @param contractAddress contract address of the NFT token
* @param tokenId ID of the token
* @param account FLOW only - account where the token is minted
*/
export const getNFTImageFromIPFS = async (chain: Currency, contractAddress: string, tokenId: string, account?: string): Promise<{ originalUrl: string, publicUrl: string }> => {
const {data: metadata} = await getNFTMetadataURI(chain, contractAddress, tokenId, account);
console.log(`Metadata stored with the NFT: ${metadata}`);
const metadataUrl = `https://gateway.pinata.cloud/ipfs/${metadata.replace('ipfs://', '')}`;
const {data} = await axios.get(metadataUrl);
const imageUrl = data.properties.image.description;
return {
originalUrl: imageUrl,
publicUrl: `https://gateway.pinata.cloud/ipfs/${imageUrl.replace('ipfs://', '')}`
};
};

/**
* For more details, see <a href="https://tatum.io/apidoc#operation/NftGetRoyaltyErc721" target="_blank">Tatum API documentation</a>
Expand Down

0 comments on commit efed816

Please sign in to comment.