Coders and Decoders
Every data point in merkle tree is made with a key (LeafKey) and value (LeafValue). These keys and values aren't plain data, they are encoded.
You can encode/decode them using coders/decoders from the SDK:
import {LeafValueCoder, LeafKeyCoder} from `@umb-network/toolbox`;
const f = 1234.0000987;
const label = 'ETH-USD';
// encode data for leaf:
const leafData: Buffer = LeafValueCoder.encode(f, label);
console.log(leafData.toString('hex'));
// decode data
const originalValue = LeafValueCoder.decode(leafData.toString('hex'), label);
console.log(originalValue);
// encoder accepts Buffer or hex string
const encodedKey: Buffer = LeafKeyCoder.encode('ETH-USD');
console.log(encodedKey.toString('hex'));
const decodedKey: string = LeafKeyCoder.decode(encodedKey);
console.log(decodedKey);
Types
Currently we are supporting:
- floating point numbers: this is default type of any value, it is basically JS
number
- fixed data: any data in hex format
Types are determined by labels.
Floating points number
This is default type for any value. Floating point numbers are encoded to 18 decimal precision numbers - uint256
solidity type. This is compatible with blockchain standard of representing amounts.
Fixed data
- This type of data is saved as is. Leaves for this type start with the prefix
SDK.constants.FIXED_NUMBER_PREFIX
.
Updated about 3 years ago