Skip to content

Commit 015c852

Browse files
authored
Merge pull request #515 from algorandfoundation/fix/avmbytes-template-params
fix: convert AVMBytes template parameters from base64 to Uint8Array
2 parents 5c26f7e + 8a4b168 commit 015c852

File tree

10 files changed

+56
-24
lines changed

10 files changed

+56
-24
lines changed

src/features/abi-methods/mappers/avm-value.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import { AVMType } from '@algorandfoundation/algokit-utils/types/app-arc56'
22
import { base64ToUtf8, base64ToUtf8IfValid } from '@/utils/base64-to-utf8'
3-
import { AvmValue, DecodedAvmType, DecodedAvmValue } from '../models'
3+
import { AvmFormItemValue, DecodedAvmType, DecodedAvmValue } from '../models'
44
import algosdk from 'algosdk'
55
import { base64ToBytes } from '@/utils/base64-to-bytes'
66

7-
export const asAvmValue = (type: AVMType, base64Value: string): AvmValue => {
7+
export const asAvmValue = (type: AVMType, base64Value: string): AvmFormItemValue => {
88
if (type === 'AVMUint64') {
99
return algosdk.ABIType.from('uint64').decode(base64ToBytes(base64Value)) as bigint
1010
}

src/features/abi-methods/mappers/form-item-mappers.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ export const abiTypeToFormItem = (
7171
return formFieldHelper.addressField({
7272
label: 'Value',
7373
field: `${path}` as Path<any>,
74+
helpText: 'An Algorand address string',
7475
})
7576
}
7677
if (type instanceof algosdk.ABIArrayDynamicType) {
@@ -131,6 +132,7 @@ export const abiReferenceTypeToFormItem = (formFieldHelper: FormFieldHelper<any>
131132
return formFieldHelper.addressField({
132133
label: 'Value',
133134
field: `${path}` as Path<any>,
135+
helpText: 'An Algorand address string',
134136
})
135137
}
136138

@@ -144,6 +146,13 @@ export const avmTypeToFormItem = (formFieldHelper: FormFieldHelper<any>, type: A
144146
field: `${path}`,
145147
})
146148
}
149+
if (type === 'AVMBytes') {
150+
return formFieldHelper.textField({
151+
label: 'Value',
152+
field: `${path}`,
153+
helpText: 'A base64 encoded bytes value',
154+
})
155+
}
147156
return formFieldHelper.textField({
148157
label: 'Value',
149158
field: `${path}`,

src/features/abi-methods/mappers/form-item-value-mappers.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@ import algosdk from 'algosdk'
22
import { base64ToBytes } from '@/utils/base64-to-bytes'
33
import { AddressOrNfd } from '@/features/transaction-wizard/models'
44
import { bigIntToFixedPointDecimalString, fixedPointDecimalStringToBigInt } from '@/features/abi-methods/mappers/ufixed-mappers'
5-
import { DynamicArrayFormItemValue, AbiFormItemValue } from '../models'
5+
import { DynamicArrayFormItemValue, AbiFormItemValue, AvmValue, AvmFormItemValue } from '../models'
66
import { uint8ArrayToBase64 } from '@/utils/uint8-array-to-base64'
77
import { base64ToUtf8 } from '@/utils/base64-to-utf8'
88
import { asAddressOrNfd } from '@/features/transaction-wizard/mappers/as-address-or-nfd'
9+
import { AVMType } from '@algorandfoundation/algokit-utils/types/app-arc56'
910

1011
export const abiFormItemValueToABIValue = (type: algosdk.ABIArgumentType, value: AbiFormItemValue): algosdk.ABIValue => {
1112
if (type instanceof algosdk.ABIUfixedType) {
@@ -68,3 +69,17 @@ export const asAbiFormItemValue = (type: algosdk.ABIType, value: algosdk.ABIValu
6869

6970
throw new Error(`Unknown type ${type}`)
7071
}
72+
73+
export const avmFormItemValueToAVMValue = (type: AVMType, value: AvmFormItemValue): AvmValue => {
74+
if (type === 'AVMBytes') {
75+
return base64ToBytes(value as string)
76+
}
77+
return value
78+
}
79+
80+
export const asAvmFormItemValue = (type: AVMType, value: AvmValue): AvmFormItemValue => {
81+
if (type === 'AVMBytes') {
82+
return uint8ArrayToBase64(value as Uint8Array)
83+
}
84+
return value as AvmFormItemValue
85+
}

src/features/abi-methods/models/index.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,9 @@ export type DynamicArrayFormItemValue = {
9696

9797
export type AbiFormItemValue = string | boolean | bigint | number | AddressOrNfd | AbiFormItemValue[] | DynamicArrayFormItemValue[]
9898

99-
export type AvmValue = bigint | string
99+
export type AvmFormItemValue = string | bigint
100+
101+
export type AvmValue = Uint8Array | bigint | string
100102

101103
export enum DecodedAvmType {
102104
String = 'String',

src/features/app-interfaces/components/create/app-details.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ function FormInner({ helper }: FormInnerProps) {
5656
(appInterface) => appInterface.name.toLowerCase() === appInterfaceName.toLowerCase()
5757
)
5858
setValue('appInterfaceExists', appInterfaceExists)
59-
trigger()
59+
trigger('name')
6060
}
6161
}, [appInterfaceName, loadableAppInterfaces, setValue, trigger])
6262

src/features/app-interfaces/components/create/deploy-app.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ const getTealTemplateParams = (templateParams: ReturnType<typeof useCreateAppInt
5151
}
5252
} else if ('abiType' in templateParam) {
5353
acc[templateParam.name] = templateParam.abiType.encode(templateParam.value)
54-
} else {
54+
} else if ('avmType' in templateParam) {
5555
acc[templateParam.name] = templateParam.value
5656
}
5757
return acc

src/features/app-interfaces/components/create/deployment-details.tsx

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ import { asArc56AppSpec } from '@/features/applications/mappers'
2020
import { TealTemplateParamField, TealUnknownTypeTemplateParamFieldValue } from '../../models'
2121
import { asTealTemplateParamField } from '@/features/app-interfaces/mappers'
2222
import { getTemplateParamDefinition } from '../../utils/get-template-param-field-definition'
23-
import { ABITypeTemplateParam, TemplateParamType, UnknownTypeTemplateParam } from '../../data/types'
24-
import { AbiFormItemValue, AvmValue } from '@/features/abi-methods/models'
23+
import { ABITypeTemplateParam, AVMTypeTemplateParam, TemplateParamType, UnknownTypeTemplateParam } from '../../data/types'
24+
import { AbiFormItemValue, AvmFormItemValue } from '@/features/abi-methods/models'
2525

2626
export const UPDATABLE_TEMPLATE_VAR_NAME = 'UPDATABLE'
2727
export const DELETABLE_TEMPLATE_VAR_NAME = 'DELETABLE'
@@ -72,7 +72,7 @@ function FormInner({
7272
(appInterface) => appInterface.name.toLowerCase() === appInterfaceName.toLowerCase()
7373
)
7474
setValue('appInterfaceExists', appInterfaceExists)
75-
trigger()
75+
trigger('name')
7676
}
7777
}, [appInterfaceName, loadableAppInterfaces, setValue, trigger])
7878

@@ -170,7 +170,7 @@ export function DeploymentDetails({ machine }: Props) {
170170
(values: z.infer<typeof formSchema>) => {
171171
const templateParamValues = templateParamFields.map((f) => {
172172
const value = values[f.path as keyof z.infer<typeof formSchema>]
173-
return f.toTemplateParam(value as (TealUnknownTypeTemplateParamFieldValue & AvmValue) & AbiFormItemValue)
173+
return f.toTemplateParam(value as (TealUnknownTypeTemplateParamFieldValue & AvmFormItemValue) & AbiFormItemValue)
174174
})
175175

176176
send({
@@ -201,7 +201,9 @@ export function DeploymentDetails({ machine }: Props) {
201201
return {
202202
...acc,
203203
[field.path]: state.context.templateParams
204-
? field.fromTemplateParam(state.context.templateParams[index] as UnknownTypeTemplateParam & ABITypeTemplateParam)
204+
? field.fromTemplateParam(
205+
state.context.templateParams[index] as UnknownTypeTemplateParam & AVMTypeTemplateParam & ABITypeTemplateParam
206+
)
205207
: 'defaultValue' in field
206208
? field.defaultValue
207209
: {

src/features/app-interfaces/data/types/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { AvmValue } from '@/features/abi-methods/models'
22
import { AlgoAppSpec as Arc32AppSpec } from '@/features/app-interfaces/data/types/arc-32/application'
33
import { AbiContract as Arc4AppSpec } from '@/features/app-interfaces/data/types/arc-32/application'
44
import { ApplicationId } from '@/features/applications/data/types'
5-
import { Arc56Contract } from '@algorandfoundation/algokit-utils/types/app-arc56'
5+
import { Arc56Contract, AVMType } from '@algorandfoundation/algokit-utils/types/app-arc56'
66
import algosdk from 'algosdk'
77

88
export enum AppSpecStandard {
@@ -48,6 +48,7 @@ export type UnknownTypeTemplateParam = {
4848
}
4949
export type AVMTypeTemplateParam = {
5050
name: string
51+
avmType: AVMType
5152
value: AvmValue
5253
}
5354
export type ABITypeTemplateParam = {

src/features/app-interfaces/mappers/index.tsx

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ import {
2020
avmTypeToFormFieldSchema,
2121
avmTypeToFormItem,
2222
asAbiFormItemValue,
23+
avmFormItemValueToAVMValue,
24+
asAvmFormItemValue,
2325
} from '@/features/abi-methods/mappers'
2426
import { Arc56Contract, AVMType } from '@algorandfoundation/algokit-utils/types/app-arc56'
2527
import algosdk from 'algosdk'
@@ -32,7 +34,7 @@ import { zfd } from 'zod-form-data'
3234
import { TemplateParamForm } from '../components/create/template-param-form'
3335
import { Label } from '@/features/common/components/label'
3436
import { isAVMType } from '@/features/app-interfaces/utils/is-avm-type'
35-
import { AbiFormItemValue, AvmValue } from '@/features/abi-methods/models'
37+
import { AbiFormItemValue, AvmFormItemValue } from '@/features/abi-methods/models'
3638

3739
export const parseAsAppSpec = async (
3840
file: File,
@@ -115,7 +117,7 @@ export const asTealTemplateParamField = ({
115117
name: string
116118
type?: algosdk.ABIType | AVMType
117119
struct?: StructDefinition
118-
defaultValue?: AbiFormItemValue | AvmValue
120+
defaultValue?: AbiFormItemValue | AvmFormItemValue
119121
}): TealTemplateParamField => {
120122
if (!type) {
121123
return {
@@ -160,14 +162,15 @@ export const asTealTemplateParamField = ({
160162
</>
161163
)
162164
},
163-
toTemplateParam: (value: AvmValue) =>
165+
toTemplateParam: (value: AvmFormItemValue) =>
164166
({
165167
name: name,
166-
value: value,
168+
avmType: type,
169+
value: avmFormItemValueToAVMValue(type, value),
167170
}) satisfies AVMTypeTemplateParam,
168171
fromTemplateParam: (templateParam: AVMTypeTemplateParam) =>
169-
type === 'AVMUint64' ? BigInt(templateParam.value) : templateParam.value,
170-
defaultValue: defaultValue as AvmValue,
172+
asAvmFormItemValue(type, templateParam.value),
173+
defaultValue: defaultValue as AvmFormItemValue,
171174
}
172175
}
173176

src/features/app-interfaces/models/index.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import {
1111
AVMTypeTemplateParam,
1212
} from '@/features/app-interfaces/data/types'
1313
import { AVMType } from '@algorandfoundation/algokit-utils/types/app-arc56'
14-
import { AbiFormItemValue, AvmValue } from '@/features/abi-methods/models'
14+
import { AbiFormItemValue, AvmFormItemValue } from '@/features/abi-methods/models'
1515

1616
export type TealTemplateParamField =
1717
| {
@@ -28,9 +28,9 @@ export type TealTemplateParamField =
2828
type: AVMType
2929
fieldSchema: z.ZodTypeAny
3030
createField: (helper: FormFieldHelper<any>) => React.JSX.Element | undefined
31-
toTemplateParam: (value: AvmValue) => AVMTypeTemplateParam
32-
fromTemplateParam: (templateParam: AVMTypeTemplateParam) => AvmValue
33-
defaultValue?: AvmValue
31+
toTemplateParam: (value: AvmFormItemValue) => AVMTypeTemplateParam
32+
fromTemplateParam: (templateParam: AVMTypeTemplateParam) => AvmFormItemValue
33+
defaultValue?: AvmFormItemValue
3434
}
3535
| {
3636
name: string
@@ -48,8 +48,8 @@ export type TealTemplateParamDefinition = {
4848
name: string
4949
type?: algosdk.ABIType | AVMType
5050
struct?: StructDefinition
51-
defaultValue?: AbiFormItemValue | AvmValue
51+
defaultValue?: AbiFormItemValue | AvmFormItemValue
5252
}
5353

5454
export type TealUnknownTypeTemplateParamFieldValue = { type: TemplateParamType; value: string }
55-
export type TealTemplateParamFieldValue = AbiFormItemValue | TealUnknownTypeTemplateParamFieldValue | AvmValue
55+
export type TealTemplateParamFieldValue = AbiFormItemValue | TealUnknownTypeTemplateParamFieldValue | AvmFormItemValue

0 commit comments

Comments
 (0)