Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add some js and fix Dockerfile #1

Merged
merged 5 commits into from
Sep 1, 2024
Merged
Changes from 1 commit
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
Next Next commit
add js scripts
marc-aurele-besner committed Sep 1, 2024
commit 823e88efbc05bf66de9c29da739eb2b7a64d3df5
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -35,5 +35,11 @@ fileonchain-*.tar
npm-debug.log
/assets/node_modules/

# Root node_modules
node_modules

# Ignore dist
dist

# Db hasura setup
/db/node_modules
17 changes: 14 additions & 3 deletions lib/fileonchain_web/live/file_live/form_component.ex
Original file line number Diff line number Diff line change
@@ -101,20 +101,31 @@ defmodule FileonchainWeb.FileLive.FormComponent do
chunks_results =
Enum.map(chunks, fn chunk ->
hash = Blake3.hash(chunk) |> Base.encode16(case: :lower)
Fileonchain.Chunks.create_chunk(%{hash: hash, cid: "dummy_cid", data: chunk})
case Fileonchain.Chunks.create_chunk(%{hash: hash, cid: "dummy_cid", data: chunk}) do
{:ok, _chunk} ->
# Send remark transaction
sender_seed = System.get_env("POLKADOT_SENDER_SEED") || "//Alice"
{tx_hash, exit_code} = System.cmd("node", ["dist/sendRemark.js", sender_seed, hash])
if exit_code == 0 do
{:ok, String.trim(tx_hash)}
else
{:error, "Failed to send remark: #{String.trim(tx_hash)}"}
end
error -> error
end
end)

if Enum.all?(chunks_results, fn {:ok, _} -> true; _ -> false end) do
notify_parent({:saved, file})

{:noreply,
socket
|> put_flash(:info, "File and Chunks created successfully")
|> put_flash(:info, "File and Chunks created successfully, remarks sent to Polkadot")
|> push_patch(to: socket.assigns.patch)}
else
{:noreply,
socket
|> put_flash(:error, "File created but failed to create some Chunks")
|> put_flash(:error, "File created but failed to create some Chunks or send remarks")
|> push_patch(to: socket.assigns.patch)}
end

27 changes: 27 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"name": "fileonchain",
"version": "1.0.0",
"description": "To start the server:",
"main": "index.js",
"directories": {
"lib": "lib",
"test": "test"
},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"build": "tsc",
"watch": "tsc -w"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"@autonomys/auto-consensus": "^0.4.0",
"@autonomys/auto-utils": "^0.4.0",
"@polkadot/api": "^10.9.1"
},
"devDependencies": {
"@types/node": "^20.3.1",
"typescript": "^5.1.3"
}
}
22 changes: 22 additions & 0 deletions src/sendRemark.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { connectToPolkadot, sendRemark } from './sendTransaction';

async function main(): Promise<void> {
const [senderSeed, hash] = process.argv.slice(2);

if (!senderSeed || !hash) {
console.error('Usage: node dist/sendRemark.js <senderSeed> <hash>');
process.exit(1);
}

try {
const api = await connectToPolkadot();
const txHash = await sendRemark(api, senderSeed, hash);
console.log(txHash);
process.exit(0);
} catch (error) {
console.error('Error:', error);
process.exit(1);
}
}

main();
17 changes: 17 additions & 0 deletions src/sendTransaction.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { ApiPromise, WsProvider, Keyring } from '@polkadot/api';

export async function connectToPolkadot(): Promise<ApiPromise> {
const wsProvider = new WsProvider('wss://rpc.polkadot.io');
const api = await ApiPromise.create({ provider: wsProvider });
return api;
}

export async function sendRemark(api: ApiPromise, senderSeed: string, hash: string): Promise<string> {
const keyring = new Keyring({ type: 'sr25519' });
const sender = keyring.addFromUri(senderSeed);

const remark = api.tx.system.remark(hash);
const txHash = await remark.signAndSend(sender);

return txHash.toHex();
}
14 changes: 14 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"compilerOptions": {
"target": "ES2020",
"module": "commonjs",
"strict": true,
"esModuleInterop": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true,
"outDir": "./dist",
"rootDir": "."
},
"include": ["src/**/*"],
"exclude": ["node_modules"]
}
859 changes: 859 additions & 0 deletions yarn.lock

Large diffs are not rendered by default.