Skip to content

Commit

Permalink
Added pinata upload to IPFS
Browse files Browse the repository at this point in the history
  • Loading branch information
MIDAV0 committed Aug 29, 2023
1 parent 05a92d3 commit b376834
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 58 deletions.
126 changes: 81 additions & 45 deletions src/main/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,17 @@ import { autoUpdater } from 'electron-updater';
import log from 'electron-log';
import { ChildProcessWithoutNullStreams, spawn } from 'child_process';
import kill from 'tree-kill';
import Pinata from '@pinata/sdk';
import fs from 'fs';
import MenuBuilder from './menu';
import { resolveHtmlPath } from './util';
import { execPath } from './binaries';

const pinata = new Pinata(
'9db9fffc88c7e281b5b2',
'5c761fe42f8267832f70a1f00430d9f3d9006fc9624e5bce43a44b7edd79f624'
);

class AppUpdater {
constructor() {
log.transports.file.level = 'info';
Expand All @@ -29,57 +36,86 @@ class AppUpdater {
let mainWindow: BrowserWindow | null = null;
let main: ChildProcessWithoutNullStreams;

ipcMain.on('ipc', async (event, arg) => {
console.log(`arg: ${arg}`);
if (arg[0] === 'join') {
main = spawn(
execPath,
[
'--flock-token-address',
'0x95c5746A0E7c73b6Af16048d4e79dA294b8b7116',
'--task-address',
arg[1],
'--ipfs',
'/dns/ipfs.flock.io/tcp/80/http',
'--rpc',
'https://polygon-mumbai.g.alchemy.com/v2/2CfDEXaIoLouWbPS1Tw4v9tX5bkKruSa',
'--port',
'6222',
'--private-key',
arg[2],
'--dataset',
arg[3],
],
{}
ipcMain.handle('ipc', async (event, arg) => {
if (arg[0] === 'uploadToIPFS') {
const { IpfsHash: schemaHash } = await pinata.pinJSONToIPFS(
JSON.parse(arg[1])
);

main.stdout.on('data', (data) => {
console.log(`stdout: ${data}`);
event.reply('ipc', [arg[1], data.toString()]);
});
const fileName = arg[2].split('\\').pop();
const fileStream = fs.createReadStream(arg[2]);

const options = {
pinataMetadata: {
// @ts-ignore
name: fileName,
},
};
const { IpfsHash: fileHash } = await pinata.pinFileToIPFS(
fileStream,
options
);
return [schemaHash, fileHash];
}
return [];
});

main.stderr.on('data', (data) => {
console.error(`stderr: ${data}`);
if (
data.toString().includes('insufficient funds for gas * price + value')
) {
ipcMain.on('ipc', async (event, arg) => {
console.log(`arg: ${arg}`);
switch (arg[0]) {
case 'join':
main = spawn(
execPath,
[
'--flock-token-address',
'0x95c5746A0E7c73b6Af16048d4e79dA294b8b7116',
'--task-address',
arg[1],
'--ipfs',
'/dns/ipfs.flock.io/tcp/80/http',
'--rpc',
'https://polygon-mumbai.g.alchemy.com/v2/2CfDEXaIoLouWbPS1Tw4v9tX5bkKruSa',
'--port',
'6222',
'--private-key',
arg[2],
'--dataset',
arg[3],
],
{}
);

main.stdout.on('data', (data) => {
console.log(`stdout: ${data}`);
event.reply('ipc', [arg[1], data.toString()]);
});

main.stderr.on('data', (data) => {
console.error(`stderr: ${data}`);
if (
data.toString().includes('insufficient funds for gas * price + value')
) {
event.reply('ipc', [
arg[1],
'insufficient funds for gas * price + value',
]);
}
});

main.on('close', (code) => {
console.log(`child process exited with code ${code}`);
event.reply('ipc', [
arg[1],
'insufficient funds for gas * price + value',
`client exited with code ${code} (0 is success)`,
]);
}
});

main.on('close', (code) => {
console.log(`child process exited with code ${code}`);
event.reply('ipc', [
arg[1],
`client exited with code ${code} (0 is success)`,
]);
});
} else if (arg[0] === 'leave') {
// @ts-ignore
kill(main.pid);
});
break;
case 'leave':
// @ts-ignore
kill(main.pid);
break;
default:
break;
}
});

Expand Down
37 changes: 24 additions & 13 deletions src/renderer/components/CreateTask.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -432,21 +432,32 @@ export function CreateTask({

const handleCreate = async () => {
setIsProcessing(true);
const { path: schemaPath } = await ipfsClient.add(
JSON.stringify(value.schema, null, 4)
);

const { path: sampleDataPath } = await ipfsClient.add(value.sampleData[0]);
const res = await window.electron.ipcRenderer
.uploadToIPFS('ipc', [
'uploadToIPFS',
value.schema,
value.sampleData[0].path.toString(),
])
.then((result: string[]) => {
return result;
})
.catch((e: any) => {
console.error(e);
});

value.sampleData = sampleDataPath;
value.schema = schemaPath;
value.schema = res ? res[0] : '';
value.sampleData = res ? res[1] : '';

await writeAsyncApprove?.({
args: [
FLOCK_TASK_MANAGER_ADDRESS as `0x${string}`,
value.rewardPool * 10 ** 18,
],
});
if (res) {
await writeAsyncApprove?.({
args: [
FLOCK_TASK_MANAGER_ADDRESS as `0x${string}`,
value.rewardPool * 10 ** 18,
],
});
} else {
setIsProcessing(false);
}
};

useEffect(() => {
Expand Down

0 comments on commit b376834

Please sign in to comment.