Skip to content

Commit

Permalink
Merge pull request #25 from orochi-network/feature/zkdatabase_storage…
Browse files Browse the repository at this point in the history
…_example

Update ZK Database Storage example
  • Loading branch information
chiro-hiro authored Aug 2, 2023
2 parents 794e833 + 780ba14 commit be53b6b
Showing 1 changed file with 12 additions and 10 deletions.
22 changes: 12 additions & 10 deletions zkdb/src/examples/zk-database-storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,24 +25,26 @@ class Account extends Schema({
}

(async () => {
const zkDB = await ZKDatabaseStorage.getInstance(16, './data', false);
const zkDB = await ZKDatabaseStorage.getInstance(16);
await zkDB.use('test');

const findChiro = await zkDB.findOne('name', 'chiro');
const findFlash = await zkDB.findOne('name', 'flash');
console.log('Loaded Merkle root:',(await zkDB.getMerkleRoot()).toString());

const findChiro = await zkDB.findOne('accountName', 'chiro');
const findFlash = await zkDB.findOne('accountName', 'flash');

if (findChiro.isEmpty()) {
await zkDB.add(
new Account({
name: CircuitString.fromString('chiro'),
accountName: CircuitString.fromString('chiro'),
balance: UInt32.from(100),
})
);
} else {
const chiro = await findChiro.load(Account);
await findChiro.update(
new Account({
name: chiro.accountName,
accountName: chiro.accountName,
balance: chiro.balance.add(1),
})
);
Expand All @@ -51,23 +53,23 @@ class Account extends Schema({
if (findFlash.isEmpty()) {
await zkDB.add(
new Account({
name: CircuitString.fromString('flash'),
accountName: CircuitString.fromString('flash'),
balance: UInt32.from(50),
})
);
} else {
const flash = await findFlash.load(Account);
await findFlash.update(
new Account({
name: flash.accountName,
accountName: flash.accountName,
balance: flash.balance.add(1),
})
);
}

const index0 = await findChiro.load(Account);
console.log('Index 0:', index0.json());

const index1 = await findFlash.load(Account);
console.log('Index 1:', index1.json());
console.table( [index0.json(), index1.json()]);

console.log('New Merkle root:',(await zkDB.getMerkleRoot()).toString());
})();

0 comments on commit be53b6b

Please sign in to comment.