Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: dont modify keys when accessing state #147

Merged
merged 1 commit into from
Feb 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions examples/state/client.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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]) } })
Expand All @@ -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 () => {
Expand Down
8 changes: 4 additions & 4 deletions examples/state/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2039,19 +2039,19 @@ export class StateAppClient {
/**
* Get the current value of the local_bytes1 key in local state
*/
localBytes1: async (): Promise<BinaryState> => { return new BinaryStateValue((await this.appClient.state.local(encodedAddress).getValue("localBytes1")) as Uint8Array | undefined) },
localBytes1: async (): Promise<BinaryState> => { 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<BinaryState> => { return new BinaryStateValue((await this.appClient.state.local(encodedAddress).getValue("localBytes2")) as Uint8Array | undefined) },
localBytes2: async (): Promise<BinaryState> => { 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<bigint | undefined> => { return (await this.appClient.state.local(encodedAddress).getValue("localInt1")) as bigint | undefined },
localInt1: async (): Promise<bigint | undefined> => { 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<bigint | undefined> => { return (await this.appClient.state.local(encodedAddress).getValue("localInt2")) as bigint | undefined },
localInt2: async (): Promise<bigint | undefined> => { return (await this.appClient.state.local(encodedAddress).getValue("local_int2")) as bigint | undefined },
}
},
}
Expand Down
24 changes: 12 additions & 12 deletions examples/voting/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -912,55 +912,55 @@ export class VotingRoundAppClient {
/**
* Get the current value of the close_time key in global state
*/
closeTime: async (): Promise<bigint | undefined> => { return (await this.appClient.state.global.getValue("closeTime")) as bigint | undefined },
closeTime: async (): Promise<bigint | undefined> => { 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<bigint | undefined> => { return (await this.appClient.state.global.getValue("endTime")) as bigint | undefined },
endTime: async (): Promise<bigint | undefined> => { 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<bigint | undefined> => { return (await this.appClient.state.global.getValue("isBootstrapped")) as bigint | undefined },
isBootstrapped: async (): Promise<bigint | undefined> => { 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<BinaryState> => { return new BinaryStateValue((await this.appClient.state.global.getValue("metadataIpfsCid")) as Uint8Array | undefined) },
metadataIpfsCid: async (): Promise<BinaryState> => { 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<bigint | undefined> => { return (await this.appClient.state.global.getValue("nftAssetId")) as bigint | undefined },
nftAssetId: async (): Promise<bigint | undefined> => { 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<BinaryState> => { return new BinaryStateValue((await this.appClient.state.global.getValue("nftImageUrl")) as Uint8Array | undefined) },
nftImageUrl: async (): Promise<BinaryState> => { 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<BinaryState> => { return new BinaryStateValue((await this.appClient.state.global.getValue("optionCounts")) as Uint8Array | undefined) },
optionCounts: async (): Promise<BinaryState> => { 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
*/
quorum: async (): Promise<bigint | undefined> => { return (await this.appClient.state.global.getValue("quorum")) as bigint | undefined },
/**
* Get the current value of the snapshot_public_key key in global state
*/
snapshotPublicKey: async (): Promise<BinaryState> => { return new BinaryStateValue((await this.appClient.state.global.getValue("snapshotPublicKey")) as Uint8Array | undefined) },
snapshotPublicKey: async (): Promise<BinaryState> => { 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<bigint | undefined> => { return (await this.appClient.state.global.getValue("startTime")) as bigint | undefined },
startTime: async (): Promise<bigint | undefined> => { 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<bigint | undefined> => { return (await this.appClient.state.global.getValue("totalOptions")) as bigint | undefined },
totalOptions: async (): Promise<bigint | undefined> => { 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<BinaryState> => { return new BinaryStateValue((await this.appClient.state.global.getValue("voteId")) as Uint8Array | undefined) },
voteId: async (): Promise<BinaryState> => { 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<bigint | undefined> => { return (await this.appClient.state.global.getValue("voterCount")) as bigint | undefined },
voterCount: async (): Promise<bigint | undefined> => { return (await this.appClient.state.global.getValue("voter_count")) as bigint | undefined },
},
}

Expand Down
2 changes: 1 addition & 1 deletion src/client/app-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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])) {
Expand Down