-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
473e284
commit 8ee4554
Showing
12 changed files
with
176 additions
and
90 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import BigNumber from "bignumber.js"; | ||
import { BigIntType, BigIntValue, ManagedDecimalSignedType, ManagedDecimalSignedValue, U32Value } from "../typesystem"; | ||
import { BinaryCodec } from "./binary"; | ||
import { bufferToBigInt } from "./utils"; | ||
import { SizeOfU32 } from "./constants"; | ||
|
||
export class ManagedDecimalSignedCodec { | ||
private readonly binaryCodec: BinaryCodec; | ||
|
||
constructor(binaryCodec: BinaryCodec) { | ||
this.binaryCodec = binaryCodec; | ||
} | ||
|
||
decodeNested(buffer: Buffer, type: ManagedDecimalSignedType): [ManagedDecimalSignedValue, number] { | ||
const length = buffer.readUInt32BE(0); | ||
const payload = buffer.slice(0, length); | ||
|
||
const result = this.decodeTopLevel(payload, type); | ||
return [result, length]; | ||
} | ||
|
||
decodeTopLevel(buffer: Buffer, type: ManagedDecimalSignedType): ManagedDecimalSignedValue { | ||
if (buffer.length === 0) { | ||
return new ManagedDecimalSignedValue(new BigNumber(0), 0); | ||
} | ||
|
||
if (type.isVariable()) { | ||
const bigUintSize = buffer.length - SizeOfU32; | ||
|
||
const [value] = this.binaryCodec.decodeNested(buffer.slice(0, bigUintSize), new BigIntType()); | ||
const scale = buffer.readUInt32BE(bigUintSize); | ||
|
||
return new ManagedDecimalSignedValue(value.valueOf(), scale); | ||
} | ||
|
||
const value = bufferToBigInt(buffer); | ||
const metadata = type.getMetadata(); | ||
const scale = metadata !== "usize" ? parseInt(metadata.toString()) : 0; | ||
return new ManagedDecimalSignedValue(value.shiftedBy(-scale), scale); | ||
} | ||
|
||
encodeNested(value: ManagedDecimalSignedValue): Buffer { | ||
let buffers: Buffer[] = []; | ||
if (value.isVariable()) { | ||
buffers.push(Buffer.from(this.binaryCodec.encodeNested(new BigIntValue(new BigNumber("23.000000000"))))); | ||
buffers.push(Buffer.from(this.binaryCodec.encodeNested(new U32Value(value.getScale())))); | ||
} else { | ||
buffers.push(Buffer.from(this.binaryCodec.encodeTopLevel(new BigIntValue(23.0)))); | ||
} | ||
return Buffer.concat(buffers); | ||
} | ||
|
||
encodeTopLevel(value: ManagedDecimalSignedValue): Buffer { | ||
return this.encodeNested(value); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import BigNumber from "bignumber.js"; | ||
import { Type, TypedValue } from "./types"; | ||
|
||
export class ManagedDecimalSignedType extends Type { | ||
static ClassName = "ManagedDecimalSignedType"; | ||
|
||
constructor(metadata: number | "usize") { | ||
super("ManagedDecimalSigned", undefined, undefined, metadata); | ||
} | ||
|
||
getClassName(): string { | ||
return ManagedDecimalSignedType.ClassName; | ||
} | ||
|
||
getMetadata(): number | "usize" { | ||
return this.metadata; | ||
} | ||
|
||
isVariable(): boolean { | ||
return this.metadata == "usize"; | ||
} | ||
} | ||
|
||
export class ManagedDecimalSignedValue extends TypedValue { | ||
static ClassName = "ManagedDecimalSignedValue"; | ||
private readonly value: BigNumber; | ||
private readonly scale: number; | ||
private readonly variable: boolean; | ||
|
||
constructor(value: BigNumber.Value, scale: number, isVariable: boolean = false) { | ||
super(new ManagedDecimalSignedType(isVariable ? "usize" : scale)); | ||
this.value = new BigNumber(value); | ||
this.scale = scale; | ||
this.variable = isVariable; | ||
} | ||
|
||
getClassName(): string { | ||
return ManagedDecimalSignedValue.ClassName; | ||
} | ||
|
||
getPrecision(): number { | ||
return this.value.toFixed(this.scale).replace(".", "").length; | ||
} | ||
|
||
getScale(): number { | ||
return this.scale; | ||
} | ||
|
||
/** | ||
* Returns whether two objects have the same value. | ||
*/ | ||
equals(other: ManagedDecimalSignedValue): boolean { | ||
if (this.getPrecision() != other.getPrecision()) { | ||
return false; | ||
} | ||
|
||
return new BigNumber(this.value).eq(other.value); | ||
} | ||
|
||
valueOf(): BigNumber { | ||
return this.value; | ||
} | ||
|
||
toString(): string { | ||
return this.value.toFixed(this.scale); | ||
} | ||
|
||
isVariable(): boolean { | ||
return this.variable; | ||
} | ||
} |
Oops, something went wrong.