Skip to content

Commit

Permalink
updated simple example
Browse files Browse the repository at this point in the history
  • Loading branch information
erhant committed Jan 3, 2024
1 parent eb1a7c4 commit eb4e5d3
Show file tree
Hide file tree
Showing 6 changed files with 1,056 additions and 888 deletions.
19 changes: 19 additions & 0 deletions examples/simple/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Simple Example

In this example, we simply put and get a value from HollowDB.

```sh
yarn install
```

Run the example:

```sh
node index.js
```

You can also use a bundler (Irys) with, if you have funded your wallet:

```sh
USE_IRYS=true node index.js
```
44 changes: 0 additions & 44 deletions examples/simple/example.js

This file was deleted.

74 changes: 0 additions & 74 deletions examples/simple/exampleBundlr.js

This file was deleted.

65 changes: 65 additions & 0 deletions examples/simple/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
const {SDK} = require('hollowdb');
const {WarpFactory} = require('warp-contracts');
const {computeKey} = require('hollowdb-prover');
const {readFileSync} = require('fs');
const Irys = require('@irys/sdk');

async function uploadToIrys(jwk, payload) {
const irys = new Irys({
url: 'https://node1.irys.xyz',
token: 'arweave',
key: jwk,
});

try {
const receipt = await irys.upload(Buffer.from(JSON.stringify(payload)));
console.log(`Data uploaded ==> https://gateway.irys.xyz/${receipt.id}`);
return receipt.id;
} catch (e) {
console.log('Error uploading data ', e);
throw e;
}
}

async function main() {
const USE_IRYS = process.env.USE_IRYS === 'true' ? true : false;

const wallet = JSON.parse(readFileSync('./config/wallet.json', 'utf-8'));
const warp = WarpFactory.forMainnet();

// example contract
const contractTxId = 'bNsZhQ1UaZpAk-x-Zm7pq_EC15m-sLqsRTbki-LpW_M';

// create a hollowdb instance
const db = new SDK(wallet, contractTxId, warp);

// create a secret, this can be a signature/number or anything you want
const secret = BigInt(123);

// prepare your key, as a hash of your secret
const key = computeKey(secret);

// value can be anything you want to store, but it must not exceed 2kb
// if you need to store more than 2kb, you can use bundlr to store the data on arweave and store the txid in hollowdb
const payload = {
name: 'John Doe',
age: 21,
address: '123 Main St',
};

// put the key and payload into hollowdb
let result;
if (USE_IRYS) {
const txid = await uploadToIrys(wallet, payload);
await db.put(key, txid);

const getTxId = await db.get(key);
result = await fetch(`https://arweave.net/${getTxId}`);
} else {
await db.put(key, payload);
result = await db.get(key);
}
console.log(JSON.parse(result));
}

main();
4 changes: 2 additions & 2 deletions examples/simple/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
"author": "FirstBatch Team <[email protected]>",
"private": true,
"dependencies": {
"@bundlr-network/client": "^0.11.0",
"hollowdb": "latest",
"@irys/sdk": "^0.1.1",
"hollowdb": "^1.3.5",
"hollowdb-prover": "latest"
}
}
Loading

0 comments on commit eb4e5d3

Please sign in to comment.