PatrickAlphaC / hardhat-nft-fcc

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

uploadToPinata.js : Issue when working with latest Pinata version - @pinata/sdk version 2.0.0-beta.0

ade-hardhat opened this issue · comments

commented

Ran into some issues while working with the latest Pinata version 2.0.0-beta.0.

const pinataSDK = require("@pinata/sdk")
const path = require("path")
const fs = require("fs")
require("dotenv").config()

const pinataApiKey = process.env.PINATA_API_KEY 
const pinataApiSecret = process.env.PINATA_API_SECRET 

const pinata = pinataSDK(pinataApiKey, pinataApiSecret)

Working with version 2.0.0-beta.0 I got this error:

TypeError: pinataSDK is not a function at Object.<anonymous> (/Users/ade/Desktop/hh-fcc/hardhat-NFT copy/utilis/uploadToPinata.js:9:16)

I discovered that pinataClient in the node modules (node_modules/@pinata/sdk/types/index.d.ts) is declared differently :

declare class PinataClient {
    config: PinataConfig;
    constructor(pinataApiKey?: string | PinataConfig, pinataSecretApiKey?: string);
....

as opposed to the the version used by Patrick in the tutorial- version ^1.1.23 :

export default function pinataClient(pinataApiKey?: string | PinataConfig | undefined, pinataSecretApiKey?: string | undefined): PinataClient;
....

I tried to circumvent this error by creating an instance:

const pinata = new pinataSDK["default"](pinataApiKey, pinataApiSecret)

But it logged this error:
{ reason: 'KEYS_MUST_BE_STRINGS', details: 'pinata_api_key and pinata_secret_api_key must both be strings' }

I gave up on trying to solve this issue and just installed the Pinata NodeJS SDK version used by Patrick in the tutorial and worked with that instead:

yarn add --dev @pinata/sdk@^1.1.23

commented

thanks

Thanks! Can you make a PR on this?

commented

Thanks a lot!

I had the same issue more or less ... solution was to change pinata to an earlier version

"@pinata/sdk": "^1.1.23",


`An unexpected error occurred:

Error: ERROR processing skip func of /media/Feidhmchláir/Courses/FreeCodeCamp/fcc-hardhat-nft-app/deploy/02-deploy-random-ipfs-nft.js:
TypeError: Cannot call a class as a function
    at _classCallCheck (/media/Feidhmchláir/Courses/FreeCodeCamp/fcc-hardhat-nft-app/node_modules/@pinata/sdk/lib/webpack:/pinata-sdk/node_modules/@babel/runtime/helpers/classCallCheck.js:3:1)
    at PinataClient (/media/Feidhmchláir/Courses/FreeCodeCamp/fcc-hardhat-nft-app/node_modules/@pinata/sdk/lib/webpack:/pinata-sdk/src/index.ts:73:7)
    at Object.<anonymous> (/media/Feidhmchláir/Courses/FreeCodeCamp/fcc-hardhat-nft-app/utils/uploadToPinata.js:9:16)
    at Module._compile (node:internal/modules/cjs/loader:1112:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1166:10)
    at Module.load (node:internal/modules/cjs/loader:988:32)
    at Function.Module._load (node:internal/modules/cjs/loader:834:12)
    at Module.require (node:internal/modules/cjs/loader:1012:19)
    at require (node:internal/modules/cjs/helpers:102:18)
    at Object.<anonymous> (/media/Feidhmchláir/Courses/FreeCodeCamp/fcc-hardhat-nft-app/deploy/02-deploy-random-ipfs-nft.js:4:25)
    at DeploymentsManager.executeDeployScripts (/media/Feidhmchláir/Courses/FreeCodeCamp/fcc-hardhat-nft-app/node_modules/hardhat-deploy/src/DeploymentsManager.ts:1100:15)
    at DeploymentsManager.runDeploy (/media/Feidhmchláir/Courses/FreeCodeCamp/fcc-hardhat-nft-app/node_modules/hardhat-deploy/src/DeploymentsManager.ts:1052:16)
    at SimpleTaskDefinition.action (/media/Feidhmchláir/Courses/FreeCodeCamp/fcc-hardhat-nft-app/node_modules/hardhat-deploy/src/index.ts:438:5)
    at Environment._runTaskDefinition (/media/Feidhmchláir/Courses/FreeCodeCamp/fcc-hardhat-nft-app/node_modules/hardhat/src/internal/core/runtime-environment.ts:308:14)
    at Environment.run (/media/Feidhmchláir/Courses/FreeCodeCamp/fcc-hardhat-nft-app/node_modules/hardhat/src/internal/core/runtime-environment.ts:156:14)
    at SimpleTaskDefinition.action (/media/Feidhmchláir/Courses/FreeCodeCamp/fcc-hardhat-nft-app/node_modules/hardhat-deploy/src/index.ts:584:32)
    at Environment._runTaskDefinition (/media/Feidhmchláir/Courses/FreeCodeCamp/fcc-hardhat-nft-app/node_modules/hardhat/src/internal/core/runtime-environment.ts:308:14)
    at Environment.run (/media/Feidhmchláir/Courses/FreeCodeCamp/fcc-hardhat-nft-app/node_modules/hardhat/src/internal/core/runtime-environment.ts:156:14)
    at SimpleTaskDefinition.action (/media/Feidhmchláir/Courses/FreeCodeCamp/fcc-hardhat-nft-app/node_modules/hardhat-deploy/src/index.ts:669:5)
    at Environment._runTaskDefinition (/media/Feidhmchláir/Courses/FreeCodeCamp/fcc-hardhat-nft-app/node_modules/hardhat/src/internal/core/runtime-environment.ts:308:14)
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.`

Hi, I created a PR to solve this issue @PatrickAlphaC 🙂

Hi,
I suppose that latest Pinata version is not stable yet (in my case "@pinata/sdk": "2.1.0"). I've created PinataCloud/Pinata-SDK#140 in their repository.
I would suggest to use temporary "@pinata/sdk": "^1.1.23" as in the video, instead of fixing uploadToPinata.js

Hi,
This issue is resolve passing the param options, in the PR´s commit you can see it:

for (const fileIndex in files) {
        const readableStreamForFile = fs.createReadStream(
            `${fullImagesPath}/${files[fileIndex]}`
        )
        const indexForNaming = readableStreamForFile.path.lastIndexOf("/")

        const options = {
            pinataMetadata: {
                name: readableStreamForFile.path.slice(indexForNaming + 1),
            },
        }
        try {
            await pinata
                .pinFileToIPFS(readableStreamForFile, options)
                .then((result) => {
                    responses.push(result)
                })
                .catch((err) => {
                    console.log(err)
                })
        } catch (error) {
            console.log(error)
        }
    }

In this case, I give to it the name of the dog + .png .

Agree, technically your fix works, but from my point of view it won't be the right way to pass options attribute into pinFileToIPFS function while it marked optional in the documention and in the declaration.

https://github.com/PinataCloud/Pinata-SDK/blob/master/README.md#pinFileToIPFS-anchor

I understand your point of view. If you check its code, the parameter options, is not optional, because if you don't pass it, it throws the error 'filename was not provide, make sure to provide options.pinataMetadata.name'.

export default function pinFileToIPFS(
    config: PinataConfig,
    readStream: any,
    options?: PinataPinOptions
): Promise<PinataPinResponse> {
    return new Promise((resolve, reject) => {
        const data = new NodeFormData();

        if (
            !(
                options?.pinataMetadata?.name &&
                typeof options.pinataMetadata.name === 'string' &&
                options.pinataMetadata.name.length > 0
            )
        ) {
            throw Error(
                'filename was not provide, make sure to provide options.pinataMetadata.name'
            );
        }

        data.append('file', readStream, {
            filename: options.pinataMetadata.name
        });

        if (
            !(
                readStream instanceof stream.Readable ||
                readStream instanceof NodeFormData
            )
        ) {
            reject(
                new Error('readStream is not a readable stream or form data')
            );
        }

        resolve(uploadToIPFS(config, data, options));
    });
}

In my opinion it's more a documentation bug than a code bug. Because if you don't pass the options with the name it would be like a file with undefined name.

I am not able to upload the images on pinata.
Fun Fact - not facing any error and while deploying its showing Done in secs.
Can anyone help with same issue?