From 2fb3d3b47453802453f22fde5d5cdbffae4e4164 Mon Sep 17 00:00:00 2001 From: erhant Date: Mon, 25 Dec 2023 11:03:37 +0300 Subject: [PATCH 1/2] slightly better tests --- src/base/base.ts | 4 +--- src/base/sdk.ts | 9 ++++++-- src/contracts/states/hollowdb-set.state.json | 21 +++++++++++++++++++ tests/set.test.ts | 22 +++++++++++++++----- 4 files changed, 46 insertions(+), 10 deletions(-) create mode 100644 src/contracts/states/hollowdb-set.state.json diff --git a/src/base/base.ts b/src/base/base.ts index adb5455..4e4c4c8 100644 --- a/src/base/base.ts +++ b/src/base/base.ts @@ -6,12 +6,10 @@ import type {ContractInputGeneric, ContractMode, ContractState} from '../contrac export class Base { readonly contract: Contract>; readonly warp: Warp; - readonly contractTxId: string; readonly signer: ArWallet | CustomSignature; constructor(signer: ArWallet | CustomSignature, contractTxId: string, warp: Warp) { this.signer = signer; - this.contractTxId = contractTxId; this.warp = warp // required for proof verification .use(new SnarkjsExtension()) @@ -19,7 +17,7 @@ export class Base { .use(new EthersExtension()); this.contract = this.warp - .contract>(this.contractTxId) + .contract>(contractTxId) .setEvaluationOptions({ allowBigInt: true, // bigInt is required for circuits useKVStorage: true, diff --git a/src/base/sdk.ts b/src/base/sdk.ts index c1bc4b8..f31a3dd 100644 --- a/src/base/sdk.ts +++ b/src/base/sdk.ts @@ -40,6 +40,11 @@ export class SDK { return this.base.warp; } + /** Contract transaction id. */ + get contractTxId(): string { + return this.base.contract.txId(); + } + /** Signer. */ get signer(): ArWallet | CustomSignature { return this.base.signer; @@ -100,7 +105,7 @@ export class SDK { * @param options optional range * @returns a key-value `Map` */ - async getKVMap(options?: SortKeyCacheRangeOptions): Promise> { + async getKVMap(options?: SortKeyCacheRangeOptions): Promise> { return await this.base.safeReadInteraction>({ function: 'getKVMap', value: { @@ -114,7 +119,7 @@ export class SDK { * @param key the key of the value to be returned * @returns the value of the given key */ - async get(key: string): Promise { + async get(key: string): Promise { return await this.base.safeReadInteraction({ function: 'get', value: { diff --git a/src/contracts/states/hollowdb-set.state.json b/src/contracts/states/hollowdb-set.state.json new file mode 100644 index 0000000..9b0fdbb --- /dev/null +++ b/src/contracts/states/hollowdb-set.state.json @@ -0,0 +1,21 @@ +{ + "version": "0.0.0", + "owner": "", + "verificationKeys": { + "auth": null + }, + "isProofRequired": { + "auth": false + }, + "canEvolve": true, + "whitelists": { + "put": {}, + "update": {}, + "set": {} + }, + "isWhitelistRequired": { + "put": false, + "update": false, + "set": false + } +} diff --git a/tests/set.test.ts b/tests/set.test.ts index 3330da4..b5057cc 100644 --- a/tests/set.test.ts +++ b/tests/set.test.ts @@ -21,6 +21,8 @@ describe('set tests', () => { it('should allow putting a value', async () => { await owner.put(KEY, VALUE); + const value = await owner.get(KEY); + expect(value?.val).toBe(VALUE.val); }); it('should NOT allow putting a value again', async () => { @@ -28,19 +30,29 @@ describe('set tests', () => { }); it('should allow setting a value at an existing key', async () => { - await owner.set(KEY, VALUE); + await owner.set(KEY, NEXT_VALUE); + const value = await owner.get(KEY); + expect(value?.val).toBe(NEXT_VALUE.val); }); it('should allow setting a value at a new key', async () => { const newKV = createValues(); await owner.set(newKV.KEY, newKV.VALUE); + const value = await owner.get(newKV.KEY); + expect(value?.val).toBe(newKV.VALUE.val); }); it('should allow setting many values', async () => { const kvs = Array.from({length: 5}, () => createValues()); - await owner.setMany( - kvs.map(kv => kv.KEY), - kvs.map(kv => kv.VALUE) - ); + + const keys = kvs.map(kv => kv.KEY); + const values = kvs.map(kv => kv.VALUE); + await owner.setMany(keys, values); + + const results = await owner.getMany(keys); + expect(results.length).toBe(values.length); + for (let i = 0; i < results.length; i++) { + expect(results[i]?.val).toBe(values[i].val); + } }); }); From e12c47c1632b0a02f165876c4748d4171131ac7d Mon Sep 17 00:00:00 2001 From: erhant Date: Mon, 25 Dec 2023 11:54:34 +0300 Subject: [PATCH 2/2] doc fix --- src/hollowdb-set.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/hollowdb-set.ts b/src/hollowdb-set.ts index 037184c..b4be870 100644 --- a/src/hollowdb-set.ts +++ b/src/hollowdb-set.ts @@ -10,7 +10,7 @@ export class SetSDK extends BaseSDK extends BaseSDK