You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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(...);
}
}
The benefits we get -
consistent interface for all values
encoding/decoding is not done in different places across the codebase
instead of import encoder from '@polkadot/primitives/bn/encoder'; enoder(bn) we just do bn.encode()
BlockNumber here is actually an interesting one -
It may encode differently depending on chain (sizes, Nonce & Balance are currently good examples of this)
It shares the same type with others, only differring in length, i.e. Balance & Nonce are also with the same logic.
With the above in mind, it probably makes a bit more sense doing Nonce or Balance first (the latter has further-reaching UI impacts, so Nonce conevrs all the bases, but has a lesser footprint)
The text was updated successfully, but these errors were encountered:
This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue if you think you have a related problem or query.
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 istype BlockNumber = BN
, the idea is -The benefits we get -
import encoder from '@polkadot/primitives/bn/encoder'; enoder(bn)
we just dobn.encode()
BlockNumber here is actually an interesting one -
The text was updated successfully, but these errors were encountered: