Skip to content

Commit

Permalink
add two contract view for getting job stats and verifier number
Browse files Browse the repository at this point in the history
  • Loading branch information
dakk committed May 9, 2024
1 parent ab1775a commit 748abea
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 0 deletions.
6 changes: 6 additions & 0 deletions dqpu-contract/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ near create-account <your-account.testnet> --useFaucet
near deploy <your-account.testnet> build/release/dqpu.wasm
```

Now initialize it:

```bash
near call <contractId> <initMethod> '{"owner": ""}' --accountId <accountId>
```

### 3. Retrieve data from view

```bash
Expand Down
4 changes: 4 additions & 0 deletions dqpu-contract/sandbox-ts/main.ava.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,17 @@ 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 });
t.is(am_i, true);

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) => {
Expand Down
20 changes: 20 additions & 0 deletions dqpu-contract/src/contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 748abea

Please sign in to comment.