build-contract.ts 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. import { readFileSync, writeFileSync } from "fs";
  2. import toml from "@ltd/j-toml";
  3. import { exec } from "child_process";
  4. import yargs from "yargs";
  5. import { hideBin } from "yargs/helpers";
  6. const argv = yargs(hideBin(process.argv))
  7. .usage("USAGE: npm run build-contract -- <command>")
  8. .option("cosmwasm", {
  9. type: "boolean",
  10. })
  11. .option("injective", {
  12. type: "boolean",
  13. })
  14. .option("osmosis", {
  15. type: "boolean",
  16. })
  17. .option("arm64", {
  18. type: "boolean",
  19. })
  20. .help()
  21. .alias("help", "h")
  22. .wrap(yargs.terminalWidth())
  23. .parseSync();
  24. // we need to update the toml file to have a feature on - default=[feature (passed as parameter)]
  25. // editing and writing the toml file before building the contract for other than cosmwasm
  26. function cargoPreSetup(contractTomlFilePath: string, feature: string) {
  27. const originalTomlContentStr = readFileSync(contractTomlFilePath, "utf-8");
  28. const parsedToml = toml.parse(originalTomlContentStr);
  29. // add default feature to the cargo.toml
  30. // @ts-ignore
  31. parsedToml.features.default = [feature];
  32. // @ts-ignore
  33. const updatedToml = toml.stringify(parsedToml, {
  34. // don't remove this or else stringify will return an array of strings
  35. // where each string represents a line
  36. // this lets it combine all of those line
  37. newline: "\n",
  38. newlineAround: "section",
  39. forceInlineArraySpacing: 0,
  40. });
  41. writeFileSync(contractTomlFilePath, updatedToml);
  42. // after contract compilation we need to reset the original content of the toml file
  43. return function cargoPostCleanup() {
  44. writeFileSync(contractTomlFilePath, originalTomlContentStr);
  45. };
  46. }
  47. function build() {
  48. const contractTomlFilePath = "../contracts/pyth/Cargo.toml";
  49. let cleanup = () => {};
  50. if (argv.cosmwasm !== true) {
  51. const feature =
  52. argv.osmosis === true
  53. ? "osmosis"
  54. : argv.injective === true
  55. ? "injective"
  56. : undefined;
  57. if (feature === undefined) {
  58. console.log(
  59. "Please provide one of the options: ['cosmwasm', 'injective', 'osmosis']"
  60. );
  61. return;
  62. }
  63. cleanup = cargoPreSetup(contractTomlFilePath, feature);
  64. }
  65. const dockerImage =
  66. argv.arm64 === true
  67. ? "cosmwasm/workspace-optimizer-arm64:0.12.11"
  68. : "cosmwasm/workspace-optimizer:0.12.11";
  69. const buildCommand = `
  70. docker run --rm -v "$(cd ..; pwd)":/code \
  71. -v "$(cd ../../../pythnet; pwd)":/pythnet \
  72. -v "$(cd ../../../wormhole_attester; pwd)":/wormhole_attester \
  73. --mount type=volume,source="$(basename "$(cd ..; pwd)")_cache",target=/code/target \
  74. --mount type=volume,source=registry_cache,target=/usr/local/cargo/registry \
  75. ${dockerImage}
  76. `;
  77. // build contract by running the command
  78. exec(buildCommand, (_error, stdout, stderr) => {
  79. console.log(stdout);
  80. console.log(stderr);
  81. cleanup();
  82. });
  83. }
  84. build();