devnet_deploy.ts 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. // npx pretty-quick
  2. const nearAPI = require("near-api-js");
  3. const BN = require("bn.js");
  4. const fs = require("fs");
  5. const fetch = require("node-fetch");
  6. import { NodeHttpTransport } from "@improbable-eng/grpc-web-node-http-transport";
  7. const { parseSeedPhrase, generateSeedPhrase } = require("near-seed-phrase");
  8. function getConfig(env: any) {
  9. switch (env) {
  10. case "sandbox":
  11. case "local":
  12. return {
  13. networkId: "sandbox",
  14. nodeUrl: "http://localhost:3030",
  15. masterAccount: "test.near",
  16. pythAccount: "pyth.test.near",
  17. tokenAccount: "token.test.near",
  18. nftAccount: "nft.test.near",
  19. testAccount: "test.test.near",
  20. };
  21. case "testnet":
  22. return {
  23. networkId: "testnet",
  24. nodeUrl: "https://rpc.testnet.near.org",
  25. masterAccount: "pyth.testnet",
  26. pythAccount: "pyth.pyth.testnet",
  27. tokenAccount: "token.pyth.testnet",
  28. nftAccount: "nft.pyth.testnet",
  29. testAccount: "test.pyth.testnet",
  30. };
  31. }
  32. return {};
  33. }
  34. async function initNear() {
  35. let e = process.env.NEAR_ENV || "sandbox";
  36. let config = getConfig(e);
  37. // Retrieve the validator key directly in the Tilt environment
  38. const response = await fetch("http://localhost:3031/validator_key.json");
  39. const keyFile = await response.json();
  40. const masterKey = nearAPI.utils.KeyPair.fromString(
  41. keyFile.secret_key || keyFile.private_key
  42. );
  43. let keyStore = new nearAPI.keyStores.InMemoryKeyStore();
  44. keyStore.setKey(config.networkId, config.masterAccount, masterKey);
  45. let near = await nearAPI.connect({
  46. keyStore,
  47. networkId: config.networkId,
  48. nodeUrl: config.nodeUrl,
  49. });
  50. let masterAccount = new nearAPI.Account(
  51. near.connection,
  52. config.masterAccount
  53. );
  54. console.log(
  55. "Finish init NEAR masterAccount: " +
  56. JSON.stringify(await masterAccount.getAccountBalance())
  57. );
  58. const deposit = parseSeedPhrase(
  59. "weather opinion slam purpose access artefact word orbit matter rice poem badge"
  60. );
  61. const response = await masterAccount.createAccount(
  62. "devnet.test.near",
  63. response.publicKey,
  64. new BN(10).pow(new BN(27))
  65. );
  66. console.log("Key: devnet.test.near funded");
  67. const pythContract = await fs.readFileSync("./pyth.wasm");
  68. keyStore.setKey(config.networkId, config.pythAccount, masterKey);
  69. console.log("Deploying Core/Pyth contract: " + config.pythAccount);
  70. pythAccount = await masterAccount.createAndDeployContract(
  71. config.pythAccount,
  72. masterKey.getPublicKey(),
  73. pythContract,
  74. new BN("20000000000000000000000000")
  75. );
  76. }
  77. initNear();