deploy.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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("@project-serum/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() };
  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,
  80. stakeRate: 1000 * 10 ** 6,
  81. rewardQLen: 100,
  82. mint: "SRMuApVNdxXokk5GT7XD5cUUgXMBCoAz2LHeuAoKWRt",
  83. },
  84. msrm: {
  85. withdrawalTimelock: 45,
  86. stakeRate: 1,
  87. rewardQLen: 100,
  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,
  107. stakeRate: 2 * 10 ** 6,
  108. rewardQLen: 100,
  109. mint: token1Mint.toString(),
  110. },
  111. token2: {
  112. withdrawalTimelock: 45,
  113. stakeRate: 1,
  114. rewardQLen: 100,
  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 = new anchor.web3.Account();
  128. const rewardQ = new anchor.web3.Account();
  129. const withdrawalTimelock = new anchor.BN(_withdrawalTimelock);
  130. const stakeRate = new anchor.BN(_stakeRate);
  131. const [
  132. registrarSigner,
  133. nonce,
  134. ] = await anchor.web3.PublicKey.findProgramAddress(
  135. [registrar.publicKey.toBuffer()],
  136. registry.programId
  137. );
  138. const poolMint = await serumCmn.createMint(
  139. registry.provider,
  140. registrarSigner
  141. );
  142. await registry.rpc.initialize(
  143. mint,
  144. registry.provider.wallet.publicKey,
  145. nonce,
  146. withdrawalTimelock,
  147. stakeRate,
  148. rewardQLen,
  149. {
  150. accounts: {
  151. registrar: registrar.publicKey,
  152. poolMint,
  153. rewardEventQ: rewardQ.publicKey,
  154. rent: anchor.web3.SYSVAR_RENT_PUBKEY,
  155. },
  156. signers: [registrar, rewardQ],
  157. instructions: [
  158. await registry.account.registrar.createInstruction(registrar),
  159. await registry.account.rewardQueue.createInstruction(rewardQ, 8250),
  160. ],
  161. }
  162. );
  163. return registrar.publicKey;
  164. }