From 4d5b851b5ddd3d20c6552ff9e0f7df13bbcf0c8f Mon Sep 17 00:00:00 2001 From: Neil Campbell Date: Fri, 14 Feb 2025 21:45:48 +0800 Subject: [PATCH] fix: dont modify keys when accessing state --- examples/state/client.spec.ts | 8 ++++++++ examples/state/client.ts | 8 ++++---- examples/voting/client.ts | 24 ++++++++++++------------ src/client/app-client.ts | 2 +- 4 files changed, 25 insertions(+), 17 deletions(-) diff --git a/examples/state/client.spec.ts b/examples/state/client.spec.ts index 08101fb..094afa3 100644 --- a/examples/state/client.spec.ts +++ b/examples/state/client.spec.ts @@ -48,6 +48,10 @@ describe('state typed client', () => { expect(globalState.int2).toBe(2n) expect(globalState.bytes1?.asString()).toBe('asdf') expect(globalState.bytes2?.asByteArray()).toEqual(new Uint8Array([1, 2, 3, 4])) + expect(await client.state.global.int1()).toBe(1n) + expect(await client.state.global.int2()).toBe(2n) + expect((await client.state.global.bytes1())?.asString()).toBe('asdf') + expect((await client.state.global.bytes2())?.asByteArray()).toEqual(new Uint8Array([1, 2, 3, 4])) await client.send.optIn.optIn() await client.send.setLocal({ args: { int1: 1, int2: 2, bytes1: 'asdf', bytes2: new Uint8Array([1, 2, 3, 4]) } }) @@ -58,6 +62,10 @@ describe('state typed client', () => { expect(localState.localInt2).toBe(2n) expect(localState.localBytes1?.asString()).toBe('asdf') expect(localState.localBytes2?.asByteArray()).toEqual(new Uint8Array([1, 2, 3, 4])) + expect(await client.state.local(testAccount.addr).localInt1()).toBe(1n) + expect(await client.state.local(testAccount.addr).localInt2()).toBe(2n) + expect((await client.state.local(testAccount.addr).localBytes1())?.asString()).toBe('asdf') + expect((await client.state.local(testAccount.addr).localBytes2())?.asByteArray()).toEqual(new Uint8Array([1, 2, 3, 4])) }) test('Readonly methods do not consume algos', async () => { diff --git a/examples/state/client.ts b/examples/state/client.ts index d7144a2..d4b1be1 100644 --- a/examples/state/client.ts +++ b/examples/state/client.ts @@ -2039,19 +2039,19 @@ export class StateAppClient { /** * Get the current value of the local_bytes1 key in local state */ - localBytes1: async (): Promise => { return new BinaryStateValue((await this.appClient.state.local(encodedAddress).getValue("localBytes1")) as Uint8Array | undefined) }, + localBytes1: async (): Promise => { return new BinaryStateValue((await this.appClient.state.local(encodedAddress).getValue("local_bytes1")) as Uint8Array | undefined) }, /** * Get the current value of the local_bytes2 key in local state */ - localBytes2: async (): Promise => { return new BinaryStateValue((await this.appClient.state.local(encodedAddress).getValue("localBytes2")) as Uint8Array | undefined) }, + localBytes2: async (): Promise => { return new BinaryStateValue((await this.appClient.state.local(encodedAddress).getValue("local_bytes2")) as Uint8Array | undefined) }, /** * Get the current value of the local_int1 key in local state */ - localInt1: async (): Promise => { return (await this.appClient.state.local(encodedAddress).getValue("localInt1")) as bigint | undefined }, + localInt1: async (): Promise => { return (await this.appClient.state.local(encodedAddress).getValue("local_int1")) as bigint | undefined }, /** * Get the current value of the local_int2 key in local state */ - localInt2: async (): Promise => { return (await this.appClient.state.local(encodedAddress).getValue("localInt2")) as bigint | undefined }, + localInt2: async (): Promise => { return (await this.appClient.state.local(encodedAddress).getValue("local_int2")) as bigint | undefined }, } }, } diff --git a/examples/voting/client.ts b/examples/voting/client.ts index 0b34bd6..b5bdd42 100644 --- a/examples/voting/client.ts +++ b/examples/voting/client.ts @@ -912,31 +912,31 @@ export class VotingRoundAppClient { /** * Get the current value of the close_time key in global state */ - closeTime: async (): Promise => { return (await this.appClient.state.global.getValue("closeTime")) as bigint | undefined }, + closeTime: async (): Promise => { return (await this.appClient.state.global.getValue("close_time")) as bigint | undefined }, /** * Get the current value of the end_time key in global state */ - endTime: async (): Promise => { return (await this.appClient.state.global.getValue("endTime")) as bigint | undefined }, + endTime: async (): Promise => { return (await this.appClient.state.global.getValue("end_time")) as bigint | undefined }, /** * Get the current value of the is_bootstrapped key in global state */ - isBootstrapped: async (): Promise => { return (await this.appClient.state.global.getValue("isBootstrapped")) as bigint | undefined }, + isBootstrapped: async (): Promise => { return (await this.appClient.state.global.getValue("is_bootstrapped")) as bigint | undefined }, /** * Get the current value of the metadata_ipfs_cid key in global state */ - metadataIpfsCid: async (): Promise => { return new BinaryStateValue((await this.appClient.state.global.getValue("metadataIpfsCid")) as Uint8Array | undefined) }, + metadataIpfsCid: async (): Promise => { return new BinaryStateValue((await this.appClient.state.global.getValue("metadata_ipfs_cid")) as Uint8Array | undefined) }, /** * Get the current value of the nft_asset_id key in global state */ - nftAssetId: async (): Promise => { return (await this.appClient.state.global.getValue("nftAssetId")) as bigint | undefined }, + nftAssetId: async (): Promise => { return (await this.appClient.state.global.getValue("nft_asset_id")) as bigint | undefined }, /** * Get the current value of the nft_image_url key in global state */ - nftImageUrl: async (): Promise => { return new BinaryStateValue((await this.appClient.state.global.getValue("nftImageUrl")) as Uint8Array | undefined) }, + nftImageUrl: async (): Promise => { return new BinaryStateValue((await this.appClient.state.global.getValue("nft_image_url")) as Uint8Array | undefined) }, /** * Get the current value of the option_counts key in global state */ - optionCounts: async (): Promise => { return new BinaryStateValue((await this.appClient.state.global.getValue("optionCounts")) as Uint8Array | undefined) }, + optionCounts: async (): Promise => { return new BinaryStateValue((await this.appClient.state.global.getValue("option_counts")) as Uint8Array | undefined) }, /** * Get the current value of the quorum key in global state */ @@ -944,23 +944,23 @@ export class VotingRoundAppClient { /** * Get the current value of the snapshot_public_key key in global state */ - snapshotPublicKey: async (): Promise => { return new BinaryStateValue((await this.appClient.state.global.getValue("snapshotPublicKey")) as Uint8Array | undefined) }, + snapshotPublicKey: async (): Promise => { return new BinaryStateValue((await this.appClient.state.global.getValue("snapshot_public_key")) as Uint8Array | undefined) }, /** * Get the current value of the start_time key in global state */ - startTime: async (): Promise => { return (await this.appClient.state.global.getValue("startTime")) as bigint | undefined }, + startTime: async (): Promise => { return (await this.appClient.state.global.getValue("start_time")) as bigint | undefined }, /** * Get the current value of the total_options key in global state */ - totalOptions: async (): Promise => { return (await this.appClient.state.global.getValue("totalOptions")) as bigint | undefined }, + totalOptions: async (): Promise => { return (await this.appClient.state.global.getValue("total_options")) as bigint | undefined }, /** * Get the current value of the vote_id key in global state */ - voteId: async (): Promise => { return new BinaryStateValue((await this.appClient.state.global.getValue("voteId")) as Uint8Array | undefined) }, + voteId: async (): Promise => { return new BinaryStateValue((await this.appClient.state.global.getValue("vote_id")) as Uint8Array | undefined) }, /** * Get the current value of the voter_count key in global state */ - voterCount: async (): Promise => { return (await this.appClient.state.global.getValue("voterCount")) as bigint | undefined }, + voterCount: async (): Promise => { return (await this.appClient.state.global.getValue("voter_count")) as bigint | undefined }, }, } diff --git a/src/client/app-client.ts b/src/client/app-client.ts index 667b030..0413b10 100644 --- a/src/client/app-client.ts +++ b/src/client/app-client.ts @@ -405,7 +405,7 @@ function* getStateMethods({ app, sanitizer }: GeneratorContext): DocumentParts { const name = sanitizer.makeSafePropertyIdentifier(n) const k = app.state.keys[storageType][n] yield* jsDoc(`Get the current value of the ${n} key in ${storageType} state`) - yield `${name}: async (): Promise<${k.valueType === 'AVMBytes' ? 'BinaryState' : `${getEquivalentType(k.valueType, 'output', { app, sanitizer })} | undefined`}> => { return ${k.valueType === 'AVMBytes' ? 'new BinaryStateValue(' : ''}(await this.appClient.state.${storageType}${storageType === 'local' ? '(encodedAddress)' : ''}.getValue("${name}"))${k.valueType === 'AVMBytes' ? ' as Uint8Array | undefined)' : ` as ${getEquivalentType(k.valueType, 'output', { app, sanitizer })} | undefined`} },` + yield `${name}: async (): Promise<${k.valueType === 'AVMBytes' ? 'BinaryState' : `${getEquivalentType(k.valueType, 'output', { app, sanitizer })} | undefined`}> => { return ${k.valueType === 'AVMBytes' ? 'new BinaryStateValue(' : ''}(await this.appClient.state.${storageType}${storageType === 'local' ? '(encodedAddress)' : ''}.getValue("${sanitizer.makeSafeStringTypeLiteral(n)}"))${k.valueType === 'AVMBytes' ? ' as Uint8Array | undefined)' : ` as ${getEquivalentType(k.valueType, 'output', { app, sanitizer })} | undefined`} },` } for (const n of Object.keys(app.state.maps[storageType])) {