All files / src index.ts

76.31% Statements 29/38
50% Branches 4/8
40% Functions 2/5
74.28% Lines 26/35

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 833x           3x           3x                                 3x         1x       1x         1x           1x       3x         1x   1x             3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x  
import {
  INumericDataPoint,
  RedstonePayload,
  SignedDataPackage,
  SignedDataPackagePlainObj,
} from "@redstone-finance/protocol";
import {
  DataPackagesRequestParams,
  DataPackagesResponse,
  requestDataPackages,
} from "./request-data-packages";
 
export const getDecimalsForDataFeedId = (
  dataPackages: SignedDataPackagePlainObj[]
) => {
  const firstDecimal = (dataPackages[0].dataPoints[0] as INumericDataPoint)
    .decimals;
  const areAllDecimalsEqual = dataPackages.every((dataPackage) =>
    dataPackage.dataPoints.every(
      (dataPoint) => (dataPoint as INumericDataPoint).decimals === firstDecimal
    )
  );
 
  Iif (!areAllDecimalsEqual) {
    throw new Error("Decimals from data points in data packages are not equal");
  }
  return firstDecimal;
};
 
export const convertDataPackagesResponse = (
  signedDataPackagesResponse: DataPackagesResponse,
  format = "hex",
  unsignedMetadataMsg?: string
) => {
  const signedDataPackages = Object.values(
    signedDataPackagesResponse
  ).flat() as SignedDataPackage[];
 
  const payload = new RedstonePayload(
    signedDataPackages,
    unsignedMetadataMsg ?? ""
  );
 
  switch (format) {
    case "json":
      return JSON.stringify(payload.toObj(), null, 2);
    case "bytes":
      return JSON.stringify(Array.from(payload.toBytes()));
    default:
      return payload.toBytesHexWithout0xPrefix();
  }
};
 
export const requestRedstonePayload = async (
  reqParams: DataPackagesRequestParams,
  format = "hex",
  unsignedMetadataMsg?: string
): Promise<string> => {
  const signedDataPackagesResponse = await requestDataPackages(reqParams);
 
  return convertDataPackagesResponse(
    signedDataPackagesResponse,
    format,
    unsignedMetadataMsg
  );
};
 
export * from "./contracts/ContractParamsProvider";
export * from "./contracts/ContractParamsProviderMock";
export * from "./contracts/IContractConnector";
export * from "./contracts/prices/IPriceFeedContractAdapter";
export * from "./contracts/prices/IPricesContractAdapter";
export * from "./contracts/prices/sample-run";
export * from "./data-feed-values";
export * from "./data-services-urls";
export * from "./DataPackageSubscriber";
export * from "./fetch-data-packages";
export * from "./oracle-registry";
export * from "./request-data-packages";
export * from "./simple-relayer/IPriceManagerContractAdapter";
export * from "./simple-relayer/IPriceRoundsFeedContractAdapter";
export * from "./simple-relayer/start-simple-relayer";