-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
w3sper: Add an API to fetch stake information for the given account(s)
- Remove unnecessary `async` from `BookEntry#balance` - Add `BookEntry#stakeInfo()` method - Add `Bookkeeper#stakeInfo()` method - Add `Contracts#stakeContract` getter - Add private `StakeAmount` class in `AccountSyncer` - Add private `StakeInfo` class in `AccountSyncer` - Add `AccountSyncer#stakes` method - Change test Treasury to fetch and store stakes information - Add `tests/stake_test.js` Resolves #2942
- Loading branch information
Showing
5 changed files
with
283 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
// This Source Code Form is subject to the terms of the Mozilla Public | ||
// License, v. 2.0. If a copy of the MPL was not distributed with this | ||
// file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
// | ||
// Copyright (c) DUSK NETWORK. All rights reserved. | ||
|
||
import { | ||
Network, | ||
ProfileGenerator, | ||
Bookkeeper, | ||
AccountSyncer, | ||
} from "../src/mod.js"; | ||
|
||
import { test, assert, seeder, Treasury } from "./harness.js"; | ||
|
||
test.withLocalWasm = "release"; | ||
|
||
/** | ||
* Tests fetching the stake information using string representations | ||
* of accounts, verifying access without requiring instances of | ||
* ProfileGenerator, Treasury, or Bookkeeper. | ||
*/ | ||
test("stake info without profiles", async () => { | ||
const users = [ | ||
"oCqYsUMRqpRn2kSabH52Gt6FQCwH5JXj5MtRdYVtjMSJ73AFvdbPf98p3gz98fQwNy9ZBiDem6m9BivzURKFSKLYWP3N9JahSPZs9PnZ996P18rTGAjQTNFsxtbrKx79yWu", | ||
"ocXXBAafr7xFqQTpC1vfdSYdHMXerbPCED2apyUVpLjkuycsizDxwA6b9D7UW91kG58PFKqm9U9NmY9VSwufUFL5rVRSnFSYxbiKK658TF6XjHsHGBzavFJcxAzjjBRM4eF", | ||
]; | ||
|
||
const network = new Network("http://localhost:8080/"); | ||
const syncer = new AccountSyncer(network); | ||
|
||
const stakes = await syncer.stakes(users); | ||
|
||
assert.equal(stakes.length, 2); | ||
|
||
assert.equal(stakes[0].amount.value, 1_000_000_000_000n); | ||
assert.equal( | ||
stakes[0].amount.total, | ||
stakes[0].amount.value + stakes[0].amount.locked, | ||
); | ||
assert.equal(stakes[0].amount.locked, 0n); | ||
|
||
// No check for reward's value since it is not deterministic | ||
assert.equal(typeof stakes[0].reward, "bigint"); | ||
assert.equal(stakes[0].nonce, 0n); | ||
assert.equal(stakes[0].faults, 0); | ||
assert.equal(stakes[0].hardFaults, 0); | ||
|
||
// No stakes for the 2nd user | ||
assert.equal(stakes[1].amount, null); | ||
assert.equal(stakes[1].reward, 0n); | ||
assert.equal(stakes[1].nonce, 0n); | ||
assert.equal(stakes[1].faults, 0); | ||
assert.equal(stakes[1].hardFaults, 0); | ||
}); | ||
|
||
/** | ||
* Test fetching the stake information using profiles and treasury/bookkeeper | ||
* instances. | ||
* | ||
* Although this requires more code than using the syncer directly, it enables | ||
* the use of a decoupled cache to retrieve and store the stake information. | ||
*/ | ||
test("stake info with treasury", async () => { | ||
const profiles = new ProfileGenerator(seeder); | ||
const users = await Promise.all([profiles.default, profiles.next()]); | ||
|
||
const network = new Network("http://localhost:8080/"); | ||
const accounts = new AccountSyncer(network); | ||
|
||
const treasury = new Treasury(users); | ||
|
||
await treasury.update({ accounts }); | ||
|
||
const bookkeeper = new Bookkeeper(treasury); | ||
|
||
const stakes = await Promise.all([ | ||
bookkeeper.stakeInfo(users[0].account), | ||
bookkeeper.stakeInfo(users[1].account), | ||
bookkeeper.as(users[0]).stakeInfo(), | ||
]); | ||
|
||
assert.equal(stakes.length, 3); | ||
|
||
// Stake information for the default profile matches | ||
assert.equal(stakes[0], stakes[2]); | ||
|
||
assert.equal(stakes[0].amount.value, 1_000_000_000_000n); | ||
assert.equal( | ||
stakes[0].amount.total, | ||
stakes[0].amount.value + stakes[0].amount.locked, | ||
); | ||
assert.equal(stakes[0].amount.locked, 0n); | ||
|
||
// No check for reward's value since it is not deterministic | ||
assert.equal(typeof stakes[0].reward, "bigint"); | ||
assert.equal(stakes[0].nonce, 0n); | ||
assert.equal(stakes[0].faults, 0); | ||
assert.equal(stakes[0].hardFaults, 0); | ||
|
||
// No stakes for the 2nd user | ||
assert.equal(stakes[1].amount, null); | ||
assert.equal(stakes[1].reward, 0n); | ||
assert.equal(stakes[1].nonce, 0n); | ||
assert.equal(stakes[1].faults, 0); | ||
assert.equal(stakes[1].hardFaults, 0); | ||
}); |