Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: use pinata upload #26

Merged
merged 1 commit into from
Feb 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ export * from './rpc/chainIds';
export * from './rpc/helpers';
export * from './rpc/clients';
export * from './ipfs/hash';
export * from './ipfs/upload';
export * from './ipfs/parseIpfs';
export * from './cache/json';
35 changes: 35 additions & 0 deletions src/ipfs/upload.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
export async function uploadToPinata(source: string) {
const PINATA_KEY = process.env.PINATA_KEY;
if (!PINATA_KEY) throw new Error('PINATA_KEY env must be set');
const PINATA_SECRET = process.env.PINATA_SECRET;
if (!PINATA_SECRET) throw new Error('PINATA_SECRET env must be set');
const data = new FormData();
data.append('file', new Blob([source]));
const res = await fetch('https://api.pinata.cloud/pinning/pinFileToIPFS', {
method: 'POST',
body: data,
headers: {
pinata_api_key: PINATA_KEY,
pinata_secret_api_key: PINATA_SECRET,
},
});

if (!res.ok) {
throw Error(await res.text());
}

const result = await res.json();

if (result.error) throw { message: result.error };
return result;
}

export async function uploadToTheGraph(source: string) {
const data = new FormData();
data.append('file', new Blob([source]));
const res = await fetch('https://api.thegraph.com/ipfs/api/v0/add', {
method: 'POST',
body: data,
});
return await res.json();
}
Loading