@redstone-finance/protocol
Advanced tools
Comparing version 0.4.0 to 0.5.0-alpha.0
@@ -6,3 +6,4 @@ import { BytesLike } from "ethers/lib/utils"; | ||
export declare const convertStringToBytes32: (str: string) => Uint8Array; | ||
export declare const convertNumberToBytes: (value: NumberLike, decimals: number, byteSize: number, roundFractionalComponentIfExceedsDecimals?: boolean) => Uint8Array; | ||
export declare const convertNumberToBytes: (value: NumberLike, decimals: number, byteSize: number) => Uint8Array; | ||
export declare const convertNumberToString: (value: NumberLike, decimals: number) => string; | ||
export declare const convertIntegerNumberToBytes: (value: NumberLike, byteSize: number) => Uint8Array; | ||
@@ -9,0 +10,0 @@ export declare const convertBytesToNumber: (bytes: Uint8Array) => number; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.useDefaultIfUndefined = exports.hexlifyWithout0xPrefix = exports.convertBytesToNumber = exports.convertIntegerNumberToBytes = exports.convertNumberToBytes = exports.convertStringToBytes32 = exports.assert = void 0; | ||
exports.useDefaultIfUndefined = exports.hexlifyWithout0xPrefix = exports.convertBytesToNumber = exports.convertIntegerNumberToBytes = exports.convertNumberToString = exports.convertNumberToBytes = exports.convertStringToBytes32 = exports.assert = void 0; | ||
const ethers_1 = require("ethers"); | ||
@@ -25,11 +25,4 @@ const utils_1 = require("ethers/lib/utils"); | ||
exports.convertStringToBytes32 = convertStringToBytes32; | ||
const convertNumberToBytes = (value, decimals, byteSize, roundFractionalComponentIfExceedsDecimals = true) => { | ||
let stringifiedNumber = roundFractionalComponentIfExceedsDecimals | ||
? Number(value).toFixed(decimals) | ||
: String(value); | ||
if (stringifiedNumber.includes("e")) { | ||
stringifiedNumber = Number(stringifiedNumber).toLocaleString("fullwide", { | ||
useGrouping: false, | ||
}); | ||
} | ||
const convertNumberToBytes = (value, decimals, byteSize) => { | ||
const stringifiedNumber = (0, exports.convertNumberToString)(value, decimals); | ||
const bigNumberValue = (0, utils_1.parseUnits)(stringifiedNumber, decimals); | ||
@@ -48,2 +41,12 @@ const bytesValue = (0, utils_1.arrayify)(bigNumberValue.toHexString()); | ||
exports.convertNumberToBytes = convertNumberToBytes; | ||
const convertNumberToString = (value, decimals) => { | ||
const stringifiedNumber = Number(value).toFixed(decimals); | ||
if (!stringifiedNumber.includes("e")) { | ||
return stringifiedNumber; | ||
} | ||
return Number(stringifiedNumber).toLocaleString("fullwide", { | ||
useGrouping: false, | ||
}); | ||
}; | ||
exports.convertNumberToString = convertNumberToString; | ||
const convertIntegerNumberToBytes = (value, byteSize) => { | ||
@@ -50,0 +53,0 @@ (0, exports.assert)(Number.isInteger(Number(value)), "convertIntegerNumberToBytes expects integer as input"); |
@@ -7,2 +7,3 @@ import { Serializable } from "../common/Serializable"; | ||
timestampMilliseconds: number; | ||
dataFeedId?: string; | ||
} | ||
@@ -12,3 +13,4 @@ export declare class DataPackage extends Serializable { | ||
readonly timestampMilliseconds: number; | ||
constructor(dataPoints: DataPoint[], timestampMilliseconds: number); | ||
readonly dataFeedId?: string | undefined; | ||
constructor(dataPoints: DataPoint[], timestampMilliseconds: number, dataFeedId?: string | undefined); | ||
getEachDataPointByteSize(): number; | ||
@@ -15,0 +17,0 @@ toBytes(): Uint8Array; |
@@ -13,6 +13,8 @@ "use strict"; | ||
timestampMilliseconds; | ||
constructor(dataPoints, timestampMilliseconds) { | ||
dataFeedId; | ||
constructor(dataPoints, timestampMilliseconds, dataFeedId) { | ||
super(); | ||
this.dataPoints = dataPoints; | ||
this.timestampMilliseconds = timestampMilliseconds; | ||
this.dataFeedId = dataFeedId; | ||
if (dataPoints.length === 0) { | ||
@@ -41,2 +43,3 @@ throw new Error("Can not create a data package with no data points"); | ||
timestampMilliseconds: this.timestampMilliseconds, | ||
dataFeedId: this.dataFeedId, | ||
}; | ||
@@ -46,3 +49,3 @@ } | ||
const dataPoints = plainObject.dataPoints.map(data_point_deserializer_1.deserializeDataPointFromObj); | ||
return new DataPackage(dataPoints, plainObject.timestampMilliseconds); | ||
return new DataPackage(dataPoints, plainObject.timestampMilliseconds, plainObject.dataFeedId); | ||
} | ||
@@ -49,0 +52,0 @@ getSignableHash() { |
@@ -0,4 +1,4 @@ | ||
import { Signature } from "ethers"; | ||
import { DataPackage } from "./DataPackage"; | ||
import { SignedDataPackagePlainObj } from "./SignedDataPackage"; | ||
import { Signature } from "ethers"; | ||
export interface SignedDataPackageLike { | ||
@@ -5,0 +5,0 @@ signature: Signature; |
@@ -11,6 +11,6 @@ export * from "./data-package/DataPackage"; | ||
export { Serializable } from "./common/Serializable"; | ||
export * as utils from "./common/utils"; | ||
export * as consts from "./common/redstone-constants"; | ||
export * from "./common/types"; | ||
export * as utils from "./common/utils"; | ||
export * from "./on-demand"; | ||
//# sourceMappingURL=index.d.ts.map |
@@ -29,3 +29,3 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.consts = exports.utils = exports.Serializable = void 0; | ||
exports.utils = exports.consts = exports.Serializable = void 0; | ||
__exportStar(require("./data-package/DataPackage"), exports); | ||
@@ -42,6 +42,6 @@ __exportStar(require("./data-package/SignedDataPackage"), exports); | ||
Object.defineProperty(exports, "Serializable", { enumerable: true, get: function () { return Serializable_1.Serializable; } }); | ||
exports.utils = __importStar(require("./common/utils")); | ||
exports.consts = __importStar(require("./common/redstone-constants")); | ||
__exportStar(require("./common/types"), exports); | ||
exports.utils = __importStar(require("./common/utils")); | ||
__exportStar(require("./on-demand"), exports); | ||
//# sourceMappingURL=index.js.map |
@@ -5,4 +5,4 @@ "use strict"; | ||
const utils_1 = require("ethers/lib/utils"); | ||
const Serializable_1 = require("../common/Serializable"); | ||
const redstone_constants_1 = require("../common/redstone-constants"); | ||
const Serializable_1 = require("../common/Serializable"); | ||
const utils_2 = require("../common/utils"); | ||
@@ -9,0 +9,0 @@ const RedstonePayloadParser_1 = require("./RedstonePayloadParser"); |
{ | ||
"name": "@redstone-finance/protocol", | ||
"prettier": "redstone-prettier-config", | ||
"version": "0.4.0", | ||
"version": "0.5.0-alpha.0", | ||
"main": "dist/src/index.js", | ||
@@ -20,3 +19,3 @@ "types": "dist/src/index.d.ts", | ||
}, | ||
"gitHead": "f50680734dc6adbb74c27d66a6bce6e222fb5edd" | ||
"gitHead": "479667bb0b4e8215667fe77dab5dc3df3975c6ad" | ||
} |
import { BigNumber } from "ethers"; | ||
import { | ||
BytesLike, | ||
arrayify, | ||
BytesLike, | ||
formatBytes32String, | ||
hexlify, | ||
isHexString, | ||
keccak256, | ||
@@ -11,3 +12,2 @@ parseUnits, | ||
zeroPad, | ||
isHexString, | ||
} from "ethers/lib/utils"; | ||
@@ -41,17 +41,5 @@ | ||
decimals: number, | ||
byteSize: number, | ||
roundFractionalComponentIfExceedsDecimals: boolean = true | ||
byteSize: number | ||
): Uint8Array => { | ||
let stringifiedNumber = roundFractionalComponentIfExceedsDecimals | ||
? Number(value).toFixed(decimals) | ||
: String(value); | ||
// js for numbers >1e20 uses scientific notation, | ||
// which is not supported by BigNumber.js | ||
if (stringifiedNumber.includes("e")) { | ||
stringifiedNumber = Number(stringifiedNumber).toLocaleString("fullwide", { | ||
useGrouping: false, | ||
}); | ||
} | ||
const stringifiedNumber = convertNumberToString(value, decimals); | ||
const bigNumberValue = parseUnits(stringifiedNumber, decimals); | ||
@@ -72,2 +60,18 @@ const bytesValue = arrayify(bigNumberValue.toHexString()); | ||
export const convertNumberToString = ( | ||
value: NumberLike, | ||
decimals: number | ||
): string => { | ||
const stringifiedNumber = Number(value).toFixed(decimals); | ||
if (!stringifiedNumber.includes("e")) { | ||
return stringifiedNumber; | ||
} | ||
// js for numbers >1e20 uses scientific notation, | ||
// which is not supported by BigNumber.js | ||
return Number(stringifiedNumber).toLocaleString("fullwide", { | ||
useGrouping: false, | ||
}); | ||
}; | ||
export const convertIntegerNumberToBytes = ( | ||
@@ -74,0 +78,0 @@ value: NumberLike, |
@@ -9,4 +9,4 @@ import { | ||
import { | ||
DATA_POINT_VALUE_BYTE_SIZE_BS, | ||
DATA_POINTS_COUNT_BS, | ||
DATA_POINT_VALUE_BYTE_SIZE_BS, | ||
TIMESTAMP_BS, | ||
@@ -23,2 +23,3 @@ } from "../common/redstone-constants"; | ||
timestampMilliseconds: number; | ||
dataFeedId?: string; | ||
} | ||
@@ -29,3 +30,4 @@ | ||
public readonly dataPoints: DataPoint[], | ||
public readonly timestampMilliseconds: number | ||
public readonly timestampMilliseconds: number, | ||
public readonly dataFeedId?: string | ||
) { | ||
@@ -66,2 +68,3 @@ super(); | ||
timestampMilliseconds: this.timestampMilliseconds, | ||
dataFeedId: this.dataFeedId, | ||
}; | ||
@@ -72,3 +75,7 @@ } | ||
const dataPoints = plainObject.dataPoints.map(deserializeDataPointFromObj); | ||
return new DataPackage(dataPoints, plainObject.timestampMilliseconds); | ||
return new DataPackage( | ||
dataPoints, | ||
plainObject.timestampMilliseconds, | ||
plainObject.dataFeedId | ||
); | ||
} | ||
@@ -75,0 +82,0 @@ |
@@ -0,1 +1,2 @@ | ||
import { Signature } from "ethers"; | ||
import { | ||
@@ -10,3 +11,2 @@ arrayify, | ||
import { SignedDataPackagePlainObj } from "./SignedDataPackage"; | ||
import { Signature } from "ethers"; | ||
@@ -13,0 +13,0 @@ export interface SignedDataPackageLike { |
@@ -12,6 +12,6 @@ import { Signature } from "ethers"; | ||
import { | ||
SignedDataPackageLike, | ||
deserializeSignedPackage, | ||
recoverSignerAddress, | ||
recoverSignerPublicKey, | ||
SignedDataPackageLike, | ||
} from "./signed-package-deserializing"; | ||
@@ -18,0 +18,0 @@ |
import { base64, concat } from "ethers/lib/utils"; | ||
import { Serializable } from "../common/Serializable"; | ||
import { convertStringToBytes32, ConvertibleToBytes32 } from "../common/utils"; | ||
import { ConvertibleToBytes32, convertStringToBytes32 } from "../common/utils"; | ||
import { INumericDataPoint } from "./NumericDataPoint"; | ||
@@ -5,0 +5,0 @@ |
@@ -0,3 +1,3 @@ | ||
import { toUtf8Bytes } from "ethers/lib/utils"; | ||
import { ConvertibleToBytes32 } from "../common/utils"; | ||
import { toUtf8Bytes } from "ethers/lib/utils"; | ||
import { DataPoint } from "./DataPoint"; | ||
@@ -4,0 +4,0 @@ |
@@ -16,6 +16,6 @@ export * from "./data-package/DataPackage"; | ||
export * as utils from "./common/utils"; | ||
export * as consts from "./common/redstone-constants"; | ||
export * from "./common/types"; | ||
export * as utils from "./common/utils"; | ||
export * from "./on-demand"; |
import { concat, toUtf8Bytes } from "ethers/lib/utils"; | ||
import { Serializable } from "../common/Serializable"; | ||
import { | ||
@@ -7,3 +8,2 @@ DATA_PACKAGES_COUNT_BS, | ||
} from "../common/redstone-constants"; | ||
import { Serializable } from "../common/Serializable"; | ||
import { convertIntegerNumberToBytes } from "../common/utils"; | ||
@@ -10,0 +10,0 @@ import { SignedDataPackage } from "../data-package/SignedDataPackage"; |
import { Signer, Wallet } from "ethers"; | ||
import { | ||
SigningKey, | ||
computeAddress, | ||
@@ -7,3 +8,2 @@ joinSignature, | ||
recoverPublicKey, | ||
SigningKey, | ||
toUtf8Bytes, | ||
@@ -10,0 +10,0 @@ verifyMessage, |
@@ -81,12 +81,2 @@ import { hexlify } from "ethers/lib/utils"; | ||
test("Should round fractional component if it exceeds decimal", () => { | ||
expect(() => convertNumberToBytes(42.123456789, 8, 32, false)).toThrow( | ||
"fractional component exceeds decimals" | ||
); | ||
expect(hexlify(convertNumberToBytes(42.123456789, 8, 32))).toBe( | ||
"0x00000000000000000000000000000000000000000000000000000000fb134b4f" | ||
); | ||
}); | ||
test("Should works for big numbers (>1e21)", () => { | ||
@@ -93,0 +83,0 @@ expect(hexlify(convertIntegerNumberToBytes(1e21, 20))).toBe( |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
231546
2123