Skip to content

Commit 0593b99

Browse files
authored
fix: dont modify keys when accessing state (#147)
1 parent a6deed5 commit 0593b99

File tree

4 files changed

+25
-17
lines changed

4 files changed

+25
-17
lines changed

examples/state/client.spec.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@ describe('state typed client', () => {
4848
expect(globalState.int2).toBe(2n)
4949
expect(globalState.bytes1?.asString()).toBe('asdf')
5050
expect(globalState.bytes2?.asByteArray()).toEqual(new Uint8Array([1, 2, 3, 4]))
51+
expect(await client.state.global.int1()).toBe(1n)
52+
expect(await client.state.global.int2()).toBe(2n)
53+
expect((await client.state.global.bytes1())?.asString()).toBe('asdf')
54+
expect((await client.state.global.bytes2())?.asByteArray()).toEqual(new Uint8Array([1, 2, 3, 4]))
5155

5256
await client.send.optIn.optIn()
5357
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', () => {
5862
expect(localState.localInt2).toBe(2n)
5963
expect(localState.localBytes1?.asString()).toBe('asdf')
6064
expect(localState.localBytes2?.asByteArray()).toEqual(new Uint8Array([1, 2, 3, 4]))
65+
expect(await client.state.local(testAccount.addr).localInt1()).toBe(1n)
66+
expect(await client.state.local(testAccount.addr).localInt2()).toBe(2n)
67+
expect((await client.state.local(testAccount.addr).localBytes1())?.asString()).toBe('asdf')
68+
expect((await client.state.local(testAccount.addr).localBytes2())?.asByteArray()).toEqual(new Uint8Array([1, 2, 3, 4]))
6169
})
6270

6371
test('Readonly methods do not consume algos', async () => {

examples/state/client.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2039,19 +2039,19 @@ export class StateAppClient {
20392039
/**
20402040
* Get the current value of the local_bytes1 key in local state
20412041
*/
2042-
localBytes1: async (): Promise<BinaryState> => { return new BinaryStateValue((await this.appClient.state.local(encodedAddress).getValue("localBytes1")) as Uint8Array | undefined) },
2042+
localBytes1: async (): Promise<BinaryState> => { return new BinaryStateValue((await this.appClient.state.local(encodedAddress).getValue("local_bytes1")) as Uint8Array | undefined) },
20432043
/**
20442044
* Get the current value of the local_bytes2 key in local state
20452045
*/
2046-
localBytes2: async (): Promise<BinaryState> => { return new BinaryStateValue((await this.appClient.state.local(encodedAddress).getValue("localBytes2")) as Uint8Array | undefined) },
2046+
localBytes2: async (): Promise<BinaryState> => { return new BinaryStateValue((await this.appClient.state.local(encodedAddress).getValue("local_bytes2")) as Uint8Array | undefined) },
20472047
/**
20482048
* Get the current value of the local_int1 key in local state
20492049
*/
2050-
localInt1: async (): Promise<bigint | undefined> => { return (await this.appClient.state.local(encodedAddress).getValue("localInt1")) as bigint | undefined },
2050+
localInt1: async (): Promise<bigint | undefined> => { return (await this.appClient.state.local(encodedAddress).getValue("local_int1")) as bigint | undefined },
20512051
/**
20522052
* Get the current value of the local_int2 key in local state
20532053
*/
2054-
localInt2: async (): Promise<bigint | undefined> => { return (await this.appClient.state.local(encodedAddress).getValue("localInt2")) as bigint | undefined },
2054+
localInt2: async (): Promise<bigint | undefined> => { return (await this.appClient.state.local(encodedAddress).getValue("local_int2")) as bigint | undefined },
20552055
}
20562056
},
20572057
}

examples/voting/client.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -912,55 +912,55 @@ export class VotingRoundAppClient {
912912
/**
913913
* Get the current value of the close_time key in global state
914914
*/
915-
closeTime: async (): Promise<bigint | undefined> => { return (await this.appClient.state.global.getValue("closeTime")) as bigint | undefined },
915+
closeTime: async (): Promise<bigint | undefined> => { return (await this.appClient.state.global.getValue("close_time")) as bigint | undefined },
916916
/**
917917
* Get the current value of the end_time key in global state
918918
*/
919-
endTime: async (): Promise<bigint | undefined> => { return (await this.appClient.state.global.getValue("endTime")) as bigint | undefined },
919+
endTime: async (): Promise<bigint | undefined> => { return (await this.appClient.state.global.getValue("end_time")) as bigint | undefined },
920920
/**
921921
* Get the current value of the is_bootstrapped key in global state
922922
*/
923-
isBootstrapped: async (): Promise<bigint | undefined> => { return (await this.appClient.state.global.getValue("isBootstrapped")) as bigint | undefined },
923+
isBootstrapped: async (): Promise<bigint | undefined> => { return (await this.appClient.state.global.getValue("is_bootstrapped")) as bigint | undefined },
924924
/**
925925
* Get the current value of the metadata_ipfs_cid key in global state
926926
*/
927-
metadataIpfsCid: async (): Promise<BinaryState> => { return new BinaryStateValue((await this.appClient.state.global.getValue("metadataIpfsCid")) as Uint8Array | undefined) },
927+
metadataIpfsCid: async (): Promise<BinaryState> => { return new BinaryStateValue((await this.appClient.state.global.getValue("metadata_ipfs_cid")) as Uint8Array | undefined) },
928928
/**
929929
* Get the current value of the nft_asset_id key in global state
930930
*/
931-
nftAssetId: async (): Promise<bigint | undefined> => { return (await this.appClient.state.global.getValue("nftAssetId")) as bigint | undefined },
931+
nftAssetId: async (): Promise<bigint | undefined> => { return (await this.appClient.state.global.getValue("nft_asset_id")) as bigint | undefined },
932932
/**
933933
* Get the current value of the nft_image_url key in global state
934934
*/
935-
nftImageUrl: async (): Promise<BinaryState> => { return new BinaryStateValue((await this.appClient.state.global.getValue("nftImageUrl")) as Uint8Array | undefined) },
935+
nftImageUrl: async (): Promise<BinaryState> => { return new BinaryStateValue((await this.appClient.state.global.getValue("nft_image_url")) as Uint8Array | undefined) },
936936
/**
937937
* Get the current value of the option_counts key in global state
938938
*/
939-
optionCounts: async (): Promise<BinaryState> => { return new BinaryStateValue((await this.appClient.state.global.getValue("optionCounts")) as Uint8Array | undefined) },
939+
optionCounts: async (): Promise<BinaryState> => { return new BinaryStateValue((await this.appClient.state.global.getValue("option_counts")) as Uint8Array | undefined) },
940940
/**
941941
* Get the current value of the quorum key in global state
942942
*/
943943
quorum: async (): Promise<bigint | undefined> => { return (await this.appClient.state.global.getValue("quorum")) as bigint | undefined },
944944
/**
945945
* Get the current value of the snapshot_public_key key in global state
946946
*/
947-
snapshotPublicKey: async (): Promise<BinaryState> => { return new BinaryStateValue((await this.appClient.state.global.getValue("snapshotPublicKey")) as Uint8Array | undefined) },
947+
snapshotPublicKey: async (): Promise<BinaryState> => { return new BinaryStateValue((await this.appClient.state.global.getValue("snapshot_public_key")) as Uint8Array | undefined) },
948948
/**
949949
* Get the current value of the start_time key in global state
950950
*/
951-
startTime: async (): Promise<bigint | undefined> => { return (await this.appClient.state.global.getValue("startTime")) as bigint | undefined },
951+
startTime: async (): Promise<bigint | undefined> => { return (await this.appClient.state.global.getValue("start_time")) as bigint | undefined },
952952
/**
953953
* Get the current value of the total_options key in global state
954954
*/
955-
totalOptions: async (): Promise<bigint | undefined> => { return (await this.appClient.state.global.getValue("totalOptions")) as bigint | undefined },
955+
totalOptions: async (): Promise<bigint | undefined> => { return (await this.appClient.state.global.getValue("total_options")) as bigint | undefined },
956956
/**
957957
* Get the current value of the vote_id key in global state
958958
*/
959-
voteId: async (): Promise<BinaryState> => { return new BinaryStateValue((await this.appClient.state.global.getValue("voteId")) as Uint8Array | undefined) },
959+
voteId: async (): Promise<BinaryState> => { return new BinaryStateValue((await this.appClient.state.global.getValue("vote_id")) as Uint8Array | undefined) },
960960
/**
961961
* Get the current value of the voter_count key in global state
962962
*/
963-
voterCount: async (): Promise<bigint | undefined> => { return (await this.appClient.state.global.getValue("voterCount")) as bigint | undefined },
963+
voterCount: async (): Promise<bigint | undefined> => { return (await this.appClient.state.global.getValue("voter_count")) as bigint | undefined },
964964
},
965965
}
966966

src/client/app-client.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -405,7 +405,7 @@ function* getStateMethods({ app, sanitizer }: GeneratorContext): DocumentParts {
405405
const name = sanitizer.makeSafePropertyIdentifier(n)
406406
const k = app.state.keys[storageType][n]
407407
yield* jsDoc(`Get the current value of the ${n} key in ${storageType} state`)
408-
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`} },`
408+
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`} },`
409409
}
410410

411411
for (const n of Object.keys(app.state.maps[storageType])) {

0 commit comments

Comments
 (0)