stake.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. const anchor = require("@project-serum/anchor");
  2. const serumCmn = require("@project-serum/common");
  3. const TokenInstructions = require("@project-serum/serum").TokenInstructions;
  4. const utils = require("../../deps/stake/tests/utils");
  5. const lockup = anchor.workspace.Lockup;
  6. const registry = anchor.workspace.Registry;
  7. const provider = anchor.Provider.env();
  8. let lockupAddress = null;
  9. let mint = null;
  10. let god = null;
  11. let registrarAccount = null;
  12. let registrarSigner = null;
  13. let nonce = null;
  14. let poolMint = null;
  15. const registrar = new anchor.web3.Account();
  16. const rewardQ = new anchor.web3.Account();
  17. const withdrawalTimelock = new anchor.BN(4);
  18. const stakeRate = new anchor.BN(2);
  19. const rewardQLen = 170;
  20. let member = null;
  21. let memberAccount = null;
  22. let memberSigner = null;
  23. let balances = null;
  24. let balancesLocked = null;
  25. const WHITELIST_SIZE = 10;
  26. async function setupStakePool(mint, god) {
  27. // Registry genesis.
  28. const [
  29. _registrarSigner,
  30. _nonce,
  31. ] = await anchor.web3.PublicKey.findProgramAddress(
  32. [registrar.publicKey.toBuffer()],
  33. registry.programId
  34. );
  35. registrarSigner = _registrarSigner;
  36. nonce = _nonce;
  37. poolMint = await serumCmn.createMint(provider, registrarSigner);
  38. try {
  39. // Init registry.
  40. await registry.state.rpc.new({
  41. accounts: { lockupProgram: lockup.programId },
  42. });
  43. // Init lockup.
  44. await lockup.state.rpc.new({
  45. accounts: {
  46. authority: provider.wallet.publicKey,
  47. },
  48. });
  49. } catch (err) {
  50. // Skip errors for convenience when developing locally,
  51. // since the state constructors can only be called once.
  52. }
  53. // Initialize stake pool.
  54. await registry.rpc.initialize(
  55. mint,
  56. provider.wallet.publicKey,
  57. nonce,
  58. withdrawalTimelock,
  59. stakeRate,
  60. rewardQLen,
  61. {
  62. accounts: {
  63. registrar: registrar.publicKey,
  64. poolMint,
  65. rewardEventQ: rewardQ.publicKey,
  66. rent: anchor.web3.SYSVAR_RENT_PUBKEY,
  67. },
  68. signers: [registrar, rewardQ],
  69. instructions: [
  70. await registry.account.registrar.createInstruction(registrar),
  71. await registry.account.rewardQueue.createInstruction(rewardQ, 8250),
  72. ],
  73. }
  74. );
  75. registrarAccount = await registry.account.registrar.fetch(
  76. registrar.publicKey
  77. );
  78. console.log("Registrar", registrar.publicKey.toString());
  79. console.log("Wallet", registry.provider.wallet.publicKey.toString());
  80. // Create account for staker.
  81. const seed = anchor.utils.sha256
  82. .hash(`${registrar.publicKey.toString()}:Member`)
  83. .slice(0, 32);
  84. member = await anchor.web3.PublicKey.createWithSeed(
  85. registry.provider.wallet.publicKey,
  86. seed,
  87. registry.programId
  88. );
  89. const [
  90. _memberSigner,
  91. nonce2,
  92. ] = await anchor.web3.PublicKey.findProgramAddress(
  93. [registrar.publicKey.toBuffer(), member.toBuffer()],
  94. registry.programId
  95. );
  96. memberSigner = _memberSigner;
  97. const [mainTx, _balances] = await utils.createBalanceSandbox(
  98. provider,
  99. registrarAccount,
  100. memberSigner
  101. );
  102. const [lockedTx, _balancesLocked] = await utils.createBalanceSandbox(
  103. provider,
  104. registrarAccount,
  105. memberSigner
  106. );
  107. balances = _balances;
  108. balancesLocked = _balancesLocked;
  109. const tx = registry.transaction.createMember(nonce2, {
  110. accounts: {
  111. registrar: registrar.publicKey,
  112. member: member,
  113. beneficiary: provider.wallet.publicKey,
  114. memberSigner,
  115. balances,
  116. balancesLocked,
  117. tokenProgram: TokenInstructions.TOKEN_PROGRAM_ID,
  118. rent: anchor.web3.SYSVAR_RENT_PUBKEY,
  119. },
  120. instructions: [
  121. anchor.web3.SystemProgram.createAccountWithSeed({
  122. fromPubkey: registry.provider.wallet.publicKey,
  123. newAccountPubkey: member,
  124. basePubkey: registry.provider.wallet.publicKey,
  125. seed,
  126. lamports: await registry.provider.connection.getMinimumBalanceForRentExemption(
  127. registry.account.member.size
  128. ),
  129. space: registry.account.member.size,
  130. programId: registry.programId,
  131. }),
  132. ],
  133. });
  134. const signers = [provider.wallet.payer];
  135. const allTxs = [mainTx, lockedTx, { tx, signers }];
  136. await provider.sendAll(allTxs);
  137. memberAccount = await registry.account.member.fetch(member);
  138. // Deposit into stake program.
  139. const depositAmount = new anchor.BN(120);
  140. await registry.rpc.deposit(depositAmount, {
  141. accounts: {
  142. depositor: god,
  143. depositorAuthority: provider.wallet.publicKey,
  144. tokenProgram: TokenInstructions.TOKEN_PROGRAM_ID,
  145. vault: memberAccount.balances.vault,
  146. beneficiary: provider.wallet.publicKey,
  147. member: member,
  148. },
  149. });
  150. // Stake.
  151. const stakeAmount = new anchor.BN(10);
  152. await registry.rpc.stake(stakeAmount, false, {
  153. accounts: {
  154. // Stake instance.
  155. registrar: registrar.publicKey,
  156. rewardEventQ: rewardQ.publicKey,
  157. poolMint,
  158. // Member.
  159. member: member,
  160. beneficiary: provider.wallet.publicKey,
  161. balances,
  162. balancesLocked,
  163. // Program signers.
  164. memberSigner,
  165. registrarSigner,
  166. // Misc.
  167. clock: anchor.web3.SYSVAR_CLOCK_PUBKEY,
  168. tokenProgram: TokenInstructions.TOKEN_PROGRAM_ID,
  169. },
  170. });
  171. }
  172. module.exports = {
  173. setupStakePool,
  174. };