Skip to content

Commit

Permalink
Merge pull request #37 from cryptoeconomicslab/fix-decode
Browse files Browse the repository at this point in the history
fix coder
  • Loading branch information
syuhei176 authored Jul 21, 2020
2 parents c03ab2f + bb03168 commit 0738b61
Showing 1 changed file with 41 additions and 23 deletions.
64 changes: 41 additions & 23 deletions packages/adaptor/src/coder/PolcadotCoder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,10 +124,11 @@ export function decodeFromPolcadotCodec(
definition: Codable,
data: any
): Codable {
if (definition instanceof Address) {
const name = definition.constructor.name
if (name === 'Address') {
const accountId = data as types.GenericAccountId
return new Address(accountId.toHex())
} else if (definition instanceof Bytes) {
} else if (name === 'Bytes') {
const arr = data as types.Vec<types.u8>
return Bytes.from(
Uint8Array.from(
Expand All @@ -136,44 +137,48 @@ export function decodeFromPolcadotCodec(
})
)
)
} else if (definition instanceof FixedBytes) {
} else if (name === 'FixedBytes') {
const arr = data as types.Vec<types.u8>
const bytesDef = definition as FixedBytes
return FixedBytes.from(
definition.size,
bytesDef.size,
Uint8Array.from(
arr.map(c => {
return c.toU8a()[0]
})
)
)
} else if (definition instanceof Integer) {
} else if (name === 'Integer') {
return Integer.from(Number(data))
} else if (definition instanceof BigNumber) {
} else if (name === 'BigNumber') {
return BigNumber.fromString(data)
} else if (definition instanceof List) {
} else if (name === 'List') {
const arr = data as any[]
const listDef = definition as List<any>
return List.from(
definition.getC().default(),
listDef.getC().default(),
arr.map(c =>
decodeFromPolcadotCodec(registry, definition.getC().default(), c)
decodeFromPolcadotCodec(registry, listDef.getC().default(), c)
)
)
} else if (definition instanceof Tuple) {
} else if (name === 'Tuple') {
const tuple = data as types.Tuple
const tupleDef = definition as Tuple
return Tuple.from(
tuple.map((c, index) => {
return decodeFromPolcadotCodec(registry, definition.data[index], c)
return decodeFromPolcadotCodec(registry, tupleDef.data[index], c)
})
)
} else if (definition instanceof Struct) {
} else if (name === 'Struct') {
const tuple = data as types.Tuple
const structDef = definition as Struct
return Struct.from(
tuple.map((c, index) => {
return {
key: definition.data[index].key,
key: structDef.data[index].key,
value: decodeFromPolcadotCodec(
registry,
definition.data[index].value,
structDef.data[index].value,
c
)
}
Expand All @@ -185,20 +190,33 @@ export function decodeFromPolcadotCodec(
}

function innerDecode(registry: TypeRegistry, definition: Codable, data: Bytes) {
if (definition instanceof Address) {
const name = definition.constructor.name
if (name === 'Address') {
return types.GenericAccountId.from(data.data)
} else if (definition instanceof Bytes) {
} else if (name === 'Bytes') {
return types.Vec.decodeVec(registry, types.u8, data.data)
} else if (definition instanceof FixedBytes) {
} else if (name === 'FixedBytes') {
return types.Vec.decodeVec(registry, types.u8, data.data)
} else if (definition instanceof Integer || definition instanceof BigNumber) {
} else if (name === 'Integer' || name === 'BigNumber') {
return new types.u128(registry, data.data)
} else if (definition instanceof List) {
return types.Vec.decodeVec(registry, getVecType(definition), data.data)
} else if (definition instanceof Tuple) {
return new types.Tuple(registry, getTupleType(definition), data.data)
} else if (name === 'List') {
return types.Vec.decodeVec(
registry,
getVecType(definition as List<any>),
data.data
)
} else if (name === 'Tuple') {
return new types.Tuple(
registry,
getTupleType(definition as Tuple),
data.data
)
} else if (definition instanceof Struct) {
return new types.Tuple(registry, getTupleType(definition), data.data)
return new types.Tuple(
registry,
getTupleType(definition as Struct),
data.data
)
} else {
throw new Error('method not implemented')
}
Expand Down

0 comments on commit 0738b61

Please sign in to comment.