-
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.
Merge pull request #477 from multiversx/TOOL-223-add-managed-decimal-…
…support-from-rust Tool 223 add managed decimal support from rust
- Loading branch information
Showing
19 changed files
with
6,723 additions
and
32 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,21 @@ | ||
export class TypeFormula { | ||
name: string; | ||
metadata: any; | ||
typeParameters: TypeFormula[]; | ||
|
||
constructor(name: string, typeParameters: TypeFormula[]) { | ||
constructor(name: string, typeParameters: TypeFormula[], metadata?: any) { | ||
this.name = name; | ||
this.typeParameters = typeParameters; | ||
this.metadata = metadata; | ||
} | ||
|
||
toString(): string { | ||
if (this.typeParameters.length > 0) { | ||
const typeParameters = this.typeParameters.map((typeParameter) => typeParameter.toString()).join(", "); | ||
return `${this.name}<${typeParameters}>`; | ||
} else { | ||
return this.name; | ||
} | ||
const hasTypeParameters = this.typeParameters.length > 0; | ||
const typeParameters = hasTypeParameters | ||
? `<${this.typeParameters.map((tp) => tp.toString()).join(", ")}>` | ||
: ""; | ||
const baseName = `${this.name}${typeParameters}`; | ||
|
||
return this.metadata !== undefined ? `${baseName}*${this.metadata}*` : baseName; | ||
} | ||
} |
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 { BigUIntValue, ManagedDecimalType, ManagedDecimalValue, U32Value } from "../typesystem"; | ||
import { BinaryCodec } from "./binary"; | ||
import { bufferToBigInt } from "./utils"; | ||
import { SizeOfU32 } from "./constants"; | ||
|
||
export class ManagedDecimalCodec { | ||
private readonly binaryCodec: BinaryCodec; | ||
|
||
constructor(binaryCodec: BinaryCodec) { | ||
this.binaryCodec = binaryCodec; | ||
} | ||
|
||
decodeNested(buffer: Buffer, type: ManagedDecimalType): [ManagedDecimalValue, 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: ManagedDecimalType): ManagedDecimalValue { | ||
if (buffer.length === 0) { | ||
return new ManagedDecimalValue(new BigNumber(0), 0); | ||
} | ||
|
||
if (type.isVariable()) { | ||
const bigUintSize = buffer.length - SizeOfU32; | ||
|
||
const value = new BigNumber(buffer.slice(0, bigUintSize).toString("hex"), 16); | ||
const scale = buffer.readUInt32BE(bigUintSize); | ||
|
||
return new ManagedDecimalValue(value, scale); | ||
} | ||
|
||
const value = bufferToBigInt(buffer); | ||
const metadata = type.getMetadata(); | ||
const scale = typeof metadata === "number" ? metadata : 0; | ||
return new ManagedDecimalValue(value, scale); | ||
} | ||
|
||
encodeNested(value: ManagedDecimalValue): Buffer { | ||
let buffers: Buffer[] = []; | ||
if (value.isVariable()) { | ||
buffers.push(Buffer.from(this.binaryCodec.encodeNested(new BigUIntValue(value.valueOf())))); | ||
buffers.push(Buffer.from(this.binaryCodec.encodeNested(new U32Value(value.getScale())))); | ||
} else { | ||
buffers.push(Buffer.from(this.binaryCodec.encodeTopLevel(new BigUIntValue(value.valueOf())))); | ||
} | ||
return Buffer.concat(buffers); | ||
} | ||
|
||
encodeTopLevel(value: ManagedDecimalValue): 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
Oops, something went wrong.