This one is a bit involved, so it probably makes sense starting with 1, seeing how it goes (implementing across the tree, i.e. here, api & apps) and then taking it further.
Basically, atm any value actually refers to the base type. For this example, let's take BlockNumber
. In effect, it is type BlockNumber = BN
, the idea is -
// probably may not be needed to pass generic, probably just Uint8Array (sliceables)
interface ParamEncoder <S> {
encode (): S;
toJSON (): any;
}
interface ParamDecoder <C, O> {
decode (input: S): C;
fromJSON (input: any): C;
}
interface BaseParam <T> { // or BasePrimitive, or BaseType, or...
value: T;
}
class BlockNumber implements ParamEncoder<Uint8Array>, BaseParam<BN> {
value: BN;
encode (): Uint8Array {
// ... convert to sliceable
}
toJSON (): any {
// yes, we have utils for this, just a sample
return '0x' + this.value.toString(16);
}
static decode (input: Uint8Array): BlockNumber {
return new BlockNumber(...);
}
static fromJSON (input: any): BlockNumber {
return new BlockNumber(...);
}
}