Skip to content

Commit

Permalink
Throw error on ipfs cluster pinning when creating market (#214)
Browse files Browse the repository at this point in the history
* throw error when pinning to cluster fails + type for pinning response

* added method to unpin from cluster

* unpin from cluster on transaction error

* log ipfs cleanup errors
  • Loading branch information
yornaath committed Aug 24, 2022
1 parent 6d22979 commit 768f448
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 10 deletions.
28 changes: 23 additions & 5 deletions packages/sdk/src/models/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,15 @@ export default class Models {
? callbackOrPaymentInfo
: undefined;

const ipfsCleanup = async () => {
try {
await this.ipfsClient.unpinCidFromCluster(cid.toString());
} catch (error) {
console.log("Ipfs cleanup error:");
console.log(error);
}
};

return new Promise(async (resolve) => {
const _callback = (
result: ISubmittableResult,
Expand Down Expand Up @@ -222,15 +231,24 @@ export default class Models {
const unsub = await tx.signAndSend(
signer.address,
{ signer: signer.signer },
(result) =>
(result) => {
if (result.dispatchError || result.internalError) {
setTimeout(ipfsCleanup);
}
callback
? callback(result, unsub)
: _callback(result, resolve, unsub)
: _callback(result, resolve, unsub);
}
);
} else {
const unsub = await tx.signAndSend(signer, (result) =>
callback ? callback(result, unsub) : _callback(result, resolve, unsub)
);
const unsub = await tx.signAndSend(signer, (result) => {
if (result.dispatchError || result.internalError) {
setTimeout(ipfsCleanup);
}
callback
? callback(result, unsub)
: _callback(result, resolve, unsub);
});
}
});
}
Expand Down
48 changes: 43 additions & 5 deletions packages/sdk/src/storage/ipfs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,26 @@ import { concat, toString } from "uint8arrays";
import ipfsClient from "ipfs-http-client";
import axios from "axios";

export type PinsPostResponse = {
replication_factor_min: number;
replication_factor_max: number;
name: string;
mode: string;
shard_size: number;
user_allocations: string;
expire_at: string;
metadata: string;
pin_update: string;
cid: string;
type: number;
allocations: Array<string>;
max_depth: number;
reference: string;
};

const username = "zeitgeist";
const password = "5ZpmQl*rWn%Z";

export default class IPFS {
private client: ReturnType<typeof ipfsClient>;
private pinToCluster: boolean;
Expand Down Expand Up @@ -47,14 +67,15 @@ export default class IPFS {
`\nFailed to publish data on cluster\n`
);
}
} catch (e) {
console.log(`Failed to publish data on cluster\n ${e}\n`);
} catch (error) {
console.log(`Failed to publish data on cluster\n ${error}\n`);
throw error;
}
}
return ipfsClientCid;
}

async pinCidToCluster(cid: string): Promise<any> {
async pinCidToCluster(cid: string): Promise<PinsPostResponse> {
const result = (
await axios({
headers: {
Expand All @@ -63,8 +84,25 @@ export default class IPFS {
method: `post`,
url: `https://ipfs-cluster.zeitgeist.pm/pins/${cid}?replication-min=2&replication-max=2`,
auth: {
username: `zeitgeist`,
password: `5ZpmQl*rWn%Z`,
username,
password,
},
})
).data;
return result;
}

async unpinCidFromCluster(cid: string): Promise<PinsPostResponse> {
const result = (
await axios({
headers: {
"Content-Type": "multipart/form-data",
},
method: `delete`,
url: `https://ipfs-cluster.zeitgeist.pm/pins/${cid}`,
auth: {
username,
password,
},
})
).data;
Expand Down

0 comments on commit 768f448

Please sign in to comment.