Skip to content

Commit 50a038f

Browse files
committed
fix: convert AVMBytes template parameters from base64 to Uint8Array
1 parent f4e5d27 commit 50a038f

File tree

9 files changed

+50
-22
lines changed

9 files changed

+50
-22
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: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,13 @@ export const avmTypeToFormItem = (formFieldHelper: FormFieldHelper<any>, type: A
144144
field: `${path}`,
145145
})
146146
}
147+
if (type === 'AVMBytes') {
148+
return formFieldHelper.textField({
149+
label: 'Value',
150+
field: `${path}`,
151+
helpText: 'A base64 encoded bytes value',
152+
})
153+
}
147154
return formFieldHelper.textField({
148155
label: 'Value',
149156
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/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: 4 additions & 4 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'
@@ -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,7 @@ 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(state.context.templateParams[index] as UnknownTypeTemplateParam & AVMTypeTemplateParam & ABITypeTemplateParam)
205205
: 'defaultValue' in field
206206
? field.defaultValue
207207
: {

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)