|
9 | 9 | import type { ABIStructValue, ABIValue } from './abi-value' |
10 | 10 | import { StructField } from './arc56-contract' |
11 | 11 | import { bigIntToBytes, bytesToBigInt } from './bigint' |
| 12 | +import { getStructValueFromTupleValue, getTupleValueFromStructValue } from './utils' |
12 | 13 |
|
13 | 14 | const STATIC_ARRAY_REGEX = /^([a-z\d[\](),]+)\[(0|[1-9][\d]*)]$/ |
14 | 15 | const UFIXED_REGEX = /^ufixed([1-9][\d]*)x([1-9][\d]*)$/ |
@@ -841,56 +842,14 @@ export class ABIStructType extends ABIType { |
841 | 842 | return tupleType.encode(value) |
842 | 843 | } |
843 | 844 |
|
844 | | - const tupleValue = this.getTupleValueFromStructValue(value) |
| 845 | + const tupleValue = getTupleValueFromStructValue(this, value) |
845 | 846 | return tupleType.encode(tupleValue) |
846 | 847 | } |
847 | 848 |
|
848 | 849 | decode(bytes: Uint8Array): ABIStructValue { |
849 | 850 | const tupleType = this.toABITupleType() |
850 | 851 | const tupleValue = tupleType.decode(bytes) |
851 | | - return this.getStructValueFromTupleValue(tupleValue) |
852 | | - } |
853 | | - |
854 | | - private getTupleValueFromStructValue(structValue: ABIStructValue): ABIValue[] { |
855 | | - const getTupleValueFromStructFields = (structFields: ABIStructField[], values: ABIValue[]): ABIValue[] => { |
856 | | - return structFields.map(({ type }, index) => { |
857 | | - // if type is an array of fields, treat as unnamed struct |
858 | | - if (Array.isArray(type)) { |
859 | | - const value = values[index] as ABIStructValue |
860 | | - return getTupleValueFromStructFields(type, Object.values(value)) |
861 | | - } |
862 | | - // if type is struct, treat as struct |
863 | | - if (type instanceof ABIStructType) { |
864 | | - const value = values[index] as ABIStructValue |
865 | | - return getTupleValueFromStructFields(type.structFields, Object.values(value)) |
866 | | - } |
867 | | - return values[index] |
868 | | - }) |
869 | | - } |
870 | | - |
871 | | - return getTupleValueFromStructFields(this.structFields, Object.values(structValue)) |
872 | | - } |
873 | | - |
874 | | - private getStructValueFromTupleValue(tupleValue: ABIValue[]): ABIStructValue { |
875 | | - const getStructFieldValues = (structFields: ABIStructField[], values: ABIValue[]): ABIStructValue => { |
876 | | - return Object.fromEntries( |
877 | | - structFields.map(({ name, type }, index) => { |
878 | | - // When the type is an array of fields, the value must be tuple |
879 | | - if (Array.isArray(type)) { |
880 | | - const value = values[index] as ABIValue[] |
881 | | - return [name, getStructFieldValues(type, value)] |
882 | | - } |
883 | | - // When the type is a struct, the value must be tuple |
884 | | - if (type instanceof ABIStructType) { |
885 | | - const value = values[index] as ABIValue[] |
886 | | - return [name, getStructFieldValues(type.structFields, value)] |
887 | | - } |
888 | | - return [name, values[index]] |
889 | | - }), |
890 | | - ) |
891 | | - } |
892 | | - |
893 | | - return getStructFieldValues(this.structFields, tupleValue) |
| 852 | + return getStructValueFromTupleValue(this, tupleValue) |
894 | 853 | } |
895 | 854 | } |
896 | 855 |
|
|
0 commit comments