deploy.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. // deploy.js is a simple deploy script to initialize a program. This is run
  2. // immediately after a deploy.
  3. const serumCmn = require("@project-serum/common");
  4. const anchor = require("@coral-xyz/anchor");
  5. const PublicKey = anchor.web3.PublicKey;
  6. module.exports = async function (provider) {
  7. // Configure client to use the provider.
  8. anchor.setProvider(provider);
  9. // Setup genesis state.
  10. const registrarConfigs = await genesis(provider);
  11. // Program clients.
  12. const lockup = anchor.workspace.Lockup;
  13. const registry = anchor.workspace.Registry;
  14. // Registry state constructor.
  15. await registry.state.rpc.new({
  16. accounts: {
  17. lockupProgram: lockup.programId,
  18. },
  19. });
  20. // Lockup state constructor.
  21. await lockup.state.rpc.new({
  22. accounts: {
  23. authority: provider.wallet.publicKey,
  24. },
  25. });
  26. // Delete the default whitelist entries.
  27. const defaultEntry = { programId: new anchor.web3.PublicKey.default() };
  28. await lockup.state.rpc.whitelistDelete(defaultEntry, {
  29. accounts: {
  30. authority: provider.wallet.publicKey,
  31. },
  32. });
  33. // Whitelist the registry.
  34. await lockup.state.rpc.whitelistAdd(
  35. { programId: registry.programId },
  36. {
  37. accounts: {
  38. authority: provider.wallet.publicKey,
  39. },
  40. }
  41. );
  42. // Initialize all registrars.
  43. const cfgKeys = Object.keys(registrarConfigs);
  44. for (let k = 0; k < cfgKeys.length; k += 1) {
  45. let r = registrarConfigs[cfgKeys[k]];
  46. const registrar = await registrarInit(
  47. registry,
  48. r.withdrawalTimelock,
  49. r.stakeRate,
  50. r.rewardQLen,
  51. new anchor.web3.PublicKey(r.mint)
  52. );
  53. r["registrar"] = registrar.toString();
  54. }
  55. // Generate code for whitelisting on UIs.
  56. const code = generateCode(registry, lockup, registrarConfigs);
  57. console.log("Generated whitelisted UI addresses:", code);
  58. };
  59. function generateCode(registry, lockup, registrarConfigs) {
  60. const registrars = Object.keys(registrarConfigs)
  61. .map((cfg) => `${cfg}: new PublicKey('${registrarConfigs[cfg].registrar}')`)
  62. .join(",");
  63. const mints = Object.keys(registrarConfigs)
  64. .map((cfg) => `${cfg}: new PublicKey('${registrarConfigs[cfg].mint}')`)
  65. .join(",");
  66. return `{
  67. registryProgramId: new PublicKey('${registry.programId}'),
  68. lockupProgramId: new PublicKey('${lockup.programId}'),
  69. registrars: { ${registrars} },
  70. mints: { ${mints} },
  71. }`;
  72. }
  73. async function genesis(provider) {
  74. if (
  75. provider.connection._rpcEndpoint === "https://api.mainnet-beta.solana.com"
  76. ) {
  77. return {
  78. srm: {
  79. withdrawalTimelock: 60 * 60 * 24 * 7, // 1 week.
  80. stakeRate: 500 * 10 ** 6, // 500 SRM.
  81. rewardQLen: 150,
  82. mint: "SRMuApVNdxXokk5GT7XD5cUUgXMBCoAz2LHeuAoKWRt",
  83. },
  84. msrm: {
  85. withdrawalTimelock: 60 * 60 * 24 * 7, // 1 week.
  86. stakeRate: 1,
  87. rewardQLen: 150,
  88. mint: "MSRMcoVyrFxnSgo5uXwone5SKcGhT1KEJMFEkMEWf9L",
  89. },
  90. };
  91. } else {
  92. const [token1Mint, _god1] = await serumCmn.createMintAndVault(
  93. provider,
  94. new anchor.BN(10000000000000),
  95. undefined,
  96. 6
  97. );
  98. const [token2Mint, _god2] = await serumCmn.createMintAndVault(
  99. provider,
  100. new anchor.BN(10000000000),
  101. undefined,
  102. 0
  103. );
  104. return {
  105. token1: {
  106. withdrawalTimelock: 60 * 60 * 24 * 7,
  107. stakeRate: 1000 * 10 ** 6,
  108. rewardQLen: 150,
  109. mint: token1Mint.toString(),
  110. },
  111. token2: {
  112. withdrawalTimelock: 60 * 60 * 24 * 7,
  113. stakeRate: 1,
  114. rewardQLen: 150,
  115. mint: token2Mint.toString(),
  116. },
  117. };
  118. }
  119. }
  120. async function registrarInit(
  121. registry,
  122. _withdrawalTimelock,
  123. _stakeRate,
  124. rewardQLen,
  125. mint
  126. ) {
  127. const registrar = anchor.web3.Keypair.generate();
  128. const rewardQ = anchor.web3.Keypair.generate();
  129. const withdrawalTimelock = new anchor.BN(_withdrawalTimelock);
  130. const stakeRate = new anchor.BN(_stakeRate);
  131. const [registrarSigner, nonce] =
  132. await anchor.web3.PublicKey.findProgramAddress(
  133. [registrar.publicKey.toBuffer()],
  134. registry.programId
  135. );
  136. const poolMint = await serumCmn.createMint(
  137. registry.provider,
  138. registrarSigner
  139. );
  140. await registry.rpc.initialize(
  141. mint,
  142. registry.provider.wallet.publicKey,
  143. nonce,
  144. withdrawalTimelock,
  145. stakeRate,
  146. rewardQLen,
  147. {
  148. accounts: {
  149. registrar: registrar.publicKey,
  150. poolMint,
  151. rewardEventQ: rewardQ.publicKey,
  152. rent: anchor.web3.SYSVAR_RENT_PUBKEY,
  153. },
  154. signers: [registrar, rewardQ],
  155. instructions: [
  156. await registry.account.registrar.createInstruction(registrar),
  157. await registry.account.rewardQueue.createInstruction(rewardQ, 8250),
  158. ],
  159. }
  160. );
  161. return registrar.publicKey;
  162. }