Skip to content

Commit

Permalink
Merge branch 'main' into ns-upgrade-polkadot
Browse files Browse the repository at this point in the history
  • Loading branch information
saboonikhil committed Aug 25, 2022
2 parents 38c2308 + 43ae5c5 commit 8fdb087
Show file tree
Hide file tree
Showing 7 changed files with 221 additions and 139 deletions.
2 changes: 1 addition & 1 deletion lerna.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"packages/type-defs",
"packages/types"
],
"version": "0.7.1",
"version": "0.7.4",
"npmClient": "yarn",
"useWorkspaces": true,
"scripts": {
Expand Down
4 changes: 2 additions & 2 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@zeitgeistpm/cli",
"version": "0.7.1",
"version": "0.7.4",
"description": "Commandline tool for the Zeitgeist prediction market.",
"main": "./dist/index.js",
"types": "./dist/index.d.ts",
Expand All @@ -15,7 +15,7 @@
"author": "",
"license": "ISC",
"dependencies": {
"@zeitgeistpm/sdk": "^0.7.1",
"@zeitgeistpm/sdk": "^0.7.4",
"commander": "^7.2.0"
},
"devDependencies": {
Expand Down
2 changes: 1 addition & 1 deletion packages/sdk/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@zeitgeistpm/sdk",
"version": "0.7.1",
"version": "0.7.4",
"description": "Zeitgeist network javascript library.",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
29 changes: 25 additions & 4 deletions packages/sdk/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { GraphQLClient, request, gql } from "graphql-request";

import ErrorTable from "./errorTable";
import Models from "./models";
import { initApi } from "./util";
import { lazyInitApi } from "./util/polkadot";

export * as models from "./models";
export * as types from "./types";
Expand All @@ -13,6 +13,7 @@ type InitOptions = {
logEndpointInitTime?: boolean;
graphQlEndpoint?: string;
ipfsClientUrl?: string;
initialConnectionTries?: number;
};

export const pingGqlEndpoint = async (endpoint: string) => {
Expand Down Expand Up @@ -60,13 +61,33 @@ export default class SDK {
opts: InitOptions = {
logEndpointInitTime: true,
ipfsClientUrl: "https://ipfs.zeitgeist.pm",
initialConnectionTries: 5,
}
): Promise<SDK> {
try {
const start = Date.now();
const api = await SDK.promiseWithTimeout(
10000,
initApi(endpoint),

const api = await SDK.promiseWithTimeout<ApiPromise>(
60 * 1000,
new Promise(async (resolve, reject) => {
const [provider, create] = lazyInitApi(endpoint);
let finished = false;
let connectionTries = 0;
provider.on("error", () => {
if (
!finished &&
connectionTries++ > (opts.initialConnectionTries || 5)
) {
finished = true;
provider.disconnect();
reject("Could not connect to the node");
}
});
provider.on("connected", () => {
finished = true;
resolve(create());
});
}),
"Timed out while connecting to the zeitgeist node. Check your node address."
);

Expand Down
34 changes: 24 additions & 10 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 Expand Up @@ -1032,10 +1050,6 @@ export default class Models {
const orderByQuery =
orderBy === "newest" ? `marketId_${orderingStr}` : `end_${orderingStr}`;

const timestamp = parseInt(
(await this.api.query.timestamp.now()).toString()
);

const variables = {
statuses,
tags,
Expand All @@ -1045,7 +1059,7 @@ export default class Models {
orderByQuery,
creator,
oracle,
minPoolId: liquidityOnly ? 0 : undefined,
minPoolId: liquidityOnly ? 0 : null,
marketIds,
assets,
};
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
Loading

0 comments on commit 8fdb087

Please sign in to comment.