import { Binary } from 'polkadot-api';
import { mapObject } from 'polkadot-api/utils';
import { getParams, respond, errorResponse } from './rpc_utils.js';

const forklift_getStorageDiff = async (con, req, { chain }) => {
  const { hash, baseHash } = getParams(req, ["hash", "baseHash"]);
  const result = await chain.getStorageDiff(hash, baseHash);
  con.send(
    respond(
      req,
      mapObject(result, ({ value, prev }) => ({
        prev: prev ? Binary.toHex(prev) : null,
        value: value ? Binary.toHex(value) : null
      }))
    )
  );
};
const forklift_changeOptions = async (con, req, { changeOptions }) => {
  const { options } = getParams(req, ["options"]);
  changeOptions(options);
  con.send(respond(req, null));
};
const forklift_finalize = async (con, req, { chain }) => {
  const { hash } = getParams(req, ["hash"]);
  try {
    chain.changeFinalized(hash);
    con.send(respond(req, null));
  } catch (ex) {
    con.send(
      errorResponse(req, {
        code: -32603,
        message: "Block not readable"
      })
    );
  }
};

export { forklift_changeOptions, forklift_finalize, forklift_getStorageDiff };
//# sourceMappingURL=forklift.js.map
