Skip to content
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
5 changes: 5 additions & 0 deletions .changeset/dry-files-clap.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@graphprotocol/grc-20": minor
---

return object with cid and editId from Ipfs.publishEdit
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ We've abstracted the IPFS publishing and binary encoding into a single API.
```ts
import { Ipfs } from '@graphprotocol/grc-20';

const cid = await Ipfs.publishEdit({
const { cid } = await Ipfs.publishEdit({
name: 'Edit name',
ops: ops,
author: '0x000000000000000000000000000000000000',
Expand Down
21 changes: 16 additions & 5 deletions src/ipfs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import { Micro } from 'effect';
import { gzipSync } from 'fflate';
import { imageSize } from 'image-size';

import { EditProposal } from '../proto.js';
import { Edit, EditProposal } from '../proto.js';
import { Id } from './id.js';
import type { Op } from './types.js';

class IpfsUploadError extends Error {
Expand All @@ -22,24 +23,31 @@ type PublishEditProposalParams = {
author: string;
};

type PublishEditResult = {
// IPFS CID representing the edit prefixed with `ipfs://`
cid: string;
// The ID of the edit
editId: Id;
};

/**
* Generates correct protobuf encoding for an Edit and uploads it to IPFS.
*
* @example
* ```ts
* import { IPFS } from '@graphprotocol/grc-20';
*
* const cid = await IPFS.publishEdit({
* const { cid, editId } = await IPFS.publishEdit({
* name: 'Edit name',
* ops: ops,
* author: '0x000000000000000000000000000000000000',
* });
* ```
*
* @param args arguments for publishing an edit to IPFS {@link PublishEditProposalParams}
* @returns IPFS CID representing the edit prefixed with `ipfs://`
* @returns - {@link PublishEditResult}
*/
export async function publishEdit(args: PublishEditProposalParams): Promise<string> {
export async function publishEdit(args: PublishEditProposalParams): Promise<PublishEditResult> {
const { name, ops, author } = args;

const edit = EditProposal.encode({ name, ops, author });
Expand All @@ -48,7 +56,10 @@ export async function publishEdit(args: PublishEditProposalParams): Promise<stri
const formData = new FormData();
formData.append('file', blob);

return await Micro.runPromise(uploadBinary(formData));
const cid = await Micro.runPromise(uploadBinary(formData));
const result = Edit.fromBinary(edit);

return { cid, editId: Id(result.id) };
}

type PublishImageParams =
Expand Down
Loading