{"size": 6200, "ext": "js", "max_stars_repo_path": "packages/run-protocol/scripts/init-core.js", "max_stars_repo_name": "gibson042/agoric-sdk", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "packages/run-protocol/scripts/init-core.js", "max_issues_repo_name": "gibson042/agoric-sdk", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "packages/run-protocol/scripts/init-core.js", "max_forks_repo_name": "gibson042/agoric-sdk", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 28.1818181818, "max_line_length": 91, "alphanum_fraction": 0.6262903226}
/* global process */
// @ts-check
import { makeHelpers } from '@agoric/deploy-script-support';

import {
  getManifestForRunProtocol,
  getManifestForEconCommittee,
  getManifestForMain,
} from '../src/proposals/core-proposal.js';
import { makeInstallCache } from '../src/proposals/utils.js';

/** @type {Record<string, Record<string, [string, string]>>} */
const installKeyGroups = {
  econCommittee: {
    contractGovernor: [
      '@agoric/governance/src/contractGovernor.js',
      '../../governance/bundles/bundle-contractGovernor.js',
    ],
    committee: [
      '@agoric/governance/src/committee.js',
      '../../governance/bundles/bundle-committee.js',
    ],
    binaryVoteCounter: [
      '@agoric/governance/src/binaryVoteCounter.js',
      '../../governance/bundles/bundle-binaryVoteCounter.js',
    ],
  },
  runStake: {
    runStake: ['../src/runStake/runStake.js', '../bundles/bundle-runStake.js'],
  },
  main: {
    amm: [
      '../src/vpool-xyk-amm/multipoolMarketMaker.js',
      '../bundles/bundle-amm.js',
    ],
    interchainPool: [
      '../src/interchainPool.js',
      '../bundles/bundle-interchainPool.js',
    ],
    vaultFactory: [
      '../src/vaultFactory/vaultFactory.js',
      '../bundles/bundle-vaultFactory.js',
    ],
    feeDistributor: [
      '../src/feeDistributor.js',
      '../bundles/bundle-feeDistributor.js',
    ],
    liquidateMinimum: [
      '../src/vaultFactory/liquidateMinimum.js',
      '../bundles/bundle-liquidateMinimum.js',
    ],
    liquidate: [
      '../src/vaultFactory/liquidateIncrementally.js',
      '../bundles/bundle-liquidateIncrementally.js',
    ],
    reserve: ['../src/reserve/assetReserve.js', '../bundles/bundle-reserve.js'],
  },
};

const { entries, fromEntries } = Object;

/** @type { <K extends string, T, U>(obj: Record<K, T>, f: (t: T) => U) => Record<K, U>} */
const mapValues = (obj, f) =>
  // @ts-expect-error entries() loses the K type
  harden(fromEntries(entries(obj).map(([p, v]) => [p, f(v)])));

/**
 *
 * @param {object} opts
 * @param {(i: I) => R} opts.publishRef
 * @param {(m: string, b: string, opts?: any) => I} opts.install
 * @param {<T>(f: T) => T} [opts.wrapInstall]
 *
 * @template I
 * @template R
 */
export const committeeProposalBuilder = async ({
  publishRef,
  install: install0,
  wrapInstall,
}) => {
  const { ROLE = 'chain' } = process.env;

  const install = wrapInstall ? wrapInstall(install0) : install0;

  /** @param { Record<string, [string, string]> } group */
  const publishGroup = group =>
    mapValues(group, ([mod, bundle]) =>
      publishRef(install(mod, bundle, { persist: true })),
    );
  return harden({
    sourceSpec: '../src/proposals/core-proposal.js',
    getManifestCall: [
      getManifestForEconCommittee.name,
      {
        ROLE,
        installKeys: {
          ...publishGroup(installKeyGroups.econCommittee),
        },
      },
    ],
  });
};

/**
 *
 * @param {object} opts
 * @param {(i: I) => R} opts.publishRef
 * @param {(m: string, b: string, opts?: any) => I} opts.install
 * @param {<T>(f: T) => T} [opts.wrapInstall]
 *
 * @template I
 * @template R
 */
export const mainProposalBuilder = async ({
  publishRef,
  install: install0,
  wrapInstall,
}) => {
  const { ROLE = 'chain', VAULT_FACTORY_CONTROLLER_ADDR } = process.env;

  const install = wrapInstall ? wrapInstall(install0) : install0;

  const persist = true;
  /** @param { Record<string, [string, string]> } group */
  const publishGroup = group =>
    mapValues(group, ([mod, bundle]) =>
      publishRef(install(mod, bundle, { persist })),
    );
  return harden({
    sourceSpec: '../src/proposals/core-proposal.js',
    getManifestCall: [
      getManifestForMain.name,
      {
        ROLE,
        vaultFactoryControllerAddress: VAULT_FACTORY_CONTROLLER_ADDR,
        installKeys: {
          ...publishGroup(installKeyGroups.main),
          ...publishGroup(installKeyGroups.runStake),
        },
      },
    ],
  });
};

// Build proposal for sim-chain etc.
export const defaultProposalBuilder = async (
  { publishRef, install },
  options = {},
  { env = process.env } = {},
) => {
  /** @param {string|undefined} s */
  const optBigInt = s => s && BigInt(s);
  const {
    ROLE = env.ROLE || 'chain',
    vaultFactoryControllerAddress = env.VAULT_FACTORY_CONTROLLER_ADDR,
    minInitialPoolLiquidity = env.MIN_INITIAL_POOL_LIQUIDITY,
    anchorOptions: {
      anchorDenom = env.ANCHOR_DENOM,
      anchorDecimalPlaces = '6',
      anchorKeyword = 'AUSD',
      anchorProposedName = anchorKeyword,
    } = {},
    econCommitteeOptions: {
      committeeSize: econCommitteeSize = env.ECON_COMMITTEE_SIZE || '3',
    } = {},
  } = options;

  /** @param { Record<string, [string, string]> } group */
  const publishGroup = group =>
    mapValues(group, ([mod, bundle]) => publishRef(install(mod, bundle)));

  const anchorOptions = anchorDenom && {
    denom: anchorDenom,
    decimalPlaces: parseInt(anchorDecimalPlaces, 10),
    keyword: anchorKeyword,
    proposedName: anchorProposedName,
  };

  const econCommitteeOptions = {
    committeeSize: parseInt(econCommitteeSize, 10),
  };

  return harden({
    sourceSpec: '../src/proposals/core-proposal.js',
    getManifestCall: [
      getManifestForRunProtocol.name,
      {
        ROLE,
        vaultFactoryControllerAddress,
        minInitialPoolLiquidity: optBigInt(minInitialPoolLiquidity),
        anchorOptions,
        econCommitteeOptions,
        installKeys: {
          ...publishGroup(installKeyGroups.econCommittee),
          ...publishGroup(installKeyGroups.runStake),
          ...publishGroup(installKeyGroups.main),
        },
      },
    ],
  });
};

export default async (homeP, endowments) => {
  const { writeCoreProposal } = await makeHelpers(homeP, endowments);

  const tool = await makeInstallCache(homeP, {
    loadBundle: spec => import(spec),
  });
  await Promise.all([
    writeCoreProposal('gov-econ-committee', opts =>
      committeeProposalBuilder({ ...opts, wrapInstall: tool.wrapInstall }),
    ),
    writeCoreProposal('gov-amm-vaults-etc', opts =>
      mainProposalBuilder({ ...opts, wrapInstall: tool.wrapInstall }),
    ),
  ]);
  await tool.saveCache();
};
