From 748abea30fb057604fc73dc86f470f3944527698 Mon Sep 17 00:00:00 2001 From: "Davide Gessa (dakk)" Date: Thu, 9 May 2024 12:46:01 +0200 Subject: [PATCH] add two contract view for getting job stats and verifier number --- dqpu-contract/README.md | 6 ++++++ dqpu-contract/sandbox-ts/main.ava.ts | 4 ++++ dqpu-contract/src/contract.ts | 20 ++++++++++++++++++++ 3 files changed, 30 insertions(+) diff --git a/dqpu-contract/README.md b/dqpu-contract/README.md index 9ab1ca7..6d7f58f 100644 --- a/dqpu-contract/README.md +++ b/dqpu-contract/README.md @@ -20,6 +20,12 @@ near create-account --useFaucet near deploy build/release/dqpu.wasm ``` +Now initialize it: + +```bash +near call '{"owner": ""}' --accountId +``` + ### 3. Retrieve data from view ```bash diff --git a/dqpu-contract/sandbox-ts/main.ava.ts b/dqpu-contract/sandbox-ts/main.ava.ts index 108fb8c..6bfce8b 100644 --- a/dqpu-contract/sandbox-ts/main.ava.ts +++ b/dqpu-contract/sandbox-ts/main.ava.ts @@ -66,6 +66,8 @@ test('add a verifier and check the status', async (t) => { let am_i: boolean = await contract.view('is_a_verifier', { account: alice.accountId }); t.is(am_i, false); + t.is(await contract.view('get_number_of_verifiers', {}), 1); + await owner.call(contract, 'add_verifier', { account: alice.accountId }); am_i = await contract.view('is_a_verifier', { account: alice.accountId }); @@ -73,6 +75,8 @@ test('add a verifier and check the status', async (t) => { am_i = await contract.view('is_a_verifier', { account: owner.accountId }); t.is(am_i, true); + + t.is(await contract.view('get_number_of_verifiers', {}), 2); }); test('add a job and mark as invalid', async (t) => { diff --git a/dqpu-contract/src/contract.ts b/dqpu-contract/src/contract.ts index 25f5fcb..42df4cb 100644 --- a/dqpu-contract/src/contract.ts +++ b/dqpu-contract/src/contract.ts @@ -192,6 +192,26 @@ class DQPU { return this.jobs.length; } + @view({}) + get_jobs_stats({ from_index = 0, limit = 1000 }: { from_index: number, limit: number }) { + const st = {}; + + for (const id of this.jobs.keys({ start: from_index, limit })) { + const j: Job = this.jobs.get(id); + if (! (j.status in st)) + st[j.status] = 0; + + st[j.status] += 1 + } + + return st; + } + + @view({}) + get_number_of_verifiers(): number { + return this.verifiers.length; + } + @view({}) get_handled_amount(): bigint { return this.money_handled;