Skip to content

Commit

Permalink
fix: use pinata upload
Browse files Browse the repository at this point in the history
  • Loading branch information
sakulstra committed Feb 27, 2024
1 parent 2c8881d commit a273486
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
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();
}

0 comments on commit a273486

Please sign in to comment.