Skip to content

Commit eb4e5d3

Browse files
committed
updated simple example
1 parent eb1a7c4 commit eb4e5d3

File tree

6 files changed

+1056
-888
lines changed

6 files changed

+1056
-888
lines changed

examples/simple/README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Simple Example
2+
3+
In this example, we simply put and get a value from HollowDB.
4+
5+
```sh
6+
yarn install
7+
```
8+
9+
Run the example:
10+
11+
```sh
12+
node index.js
13+
```
14+
15+
You can also use a bundler (Irys) with, if you have funded your wallet:
16+
17+
```sh
18+
USE_IRYS=true node index.js
19+
```

examples/simple/example.js

Lines changed: 0 additions & 44 deletions
This file was deleted.

examples/simple/exampleBundlr.js

Lines changed: 0 additions & 74 deletions
This file was deleted.

examples/simple/index.js

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
const {SDK} = require('hollowdb');
2+
const {WarpFactory} = require('warp-contracts');
3+
const {computeKey} = require('hollowdb-prover');
4+
const {readFileSync} = require('fs');
5+
const Irys = require('@irys/sdk');
6+
7+
async function uploadToIrys(jwk, payload) {
8+
const irys = new Irys({
9+
url: 'https://node1.irys.xyz',
10+
token: 'arweave',
11+
key: jwk,
12+
});
13+
14+
try {
15+
const receipt = await irys.upload(Buffer.from(JSON.stringify(payload)));
16+
console.log(`Data uploaded ==> https://gateway.irys.xyz/${receipt.id}`);
17+
return receipt.id;
18+
} catch (e) {
19+
console.log('Error uploading data ', e);
20+
throw e;
21+
}
22+
}
23+
24+
async function main() {
25+
const USE_IRYS = process.env.USE_IRYS === 'true' ? true : false;
26+
27+
const wallet = JSON.parse(readFileSync('./config/wallet.json', 'utf-8'));
28+
const warp = WarpFactory.forMainnet();
29+
30+
// example contract
31+
const contractTxId = 'bNsZhQ1UaZpAk-x-Zm7pq_EC15m-sLqsRTbki-LpW_M';
32+
33+
// create a hollowdb instance
34+
const db = new SDK(wallet, contractTxId, warp);
35+
36+
// create a secret, this can be a signature/number or anything you want
37+
const secret = BigInt(123);
38+
39+
// prepare your key, as a hash of your secret
40+
const key = computeKey(secret);
41+
42+
// value can be anything you want to store, but it must not exceed 2kb
43+
// if you need to store more than 2kb, you can use bundlr to store the data on arweave and store the txid in hollowdb
44+
const payload = {
45+
name: 'John Doe',
46+
age: 21,
47+
address: '123 Main St',
48+
};
49+
50+
// put the key and payload into hollowdb
51+
let result;
52+
if (USE_IRYS) {
53+
const txid = await uploadToIrys(wallet, payload);
54+
await db.put(key, txid);
55+
56+
const getTxId = await db.get(key);
57+
result = await fetch(`https://arweave.net/${getTxId}`);
58+
} else {
59+
await db.put(key, payload);
60+
result = await db.get(key);
61+
}
62+
console.log(JSON.parse(result));
63+
}
64+
65+
main();

examples/simple/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
"author": "FirstBatch Team <dev@firstbatch.xyz>",
44
"private": true,
55
"dependencies": {
6-
"@bundlr-network/client": "^0.11.0",
7-
"hollowdb": "latest",
6+
"@irys/sdk": "^0.1.1",
7+
"hollowdb": "^1.3.5",
88
"hollowdb-prover": "latest"
99
}
1010
}

0 commit comments

Comments
 (0)