lockup.js 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864
  1. const assert = require("assert");
  2. const anchor = require('@project-serum/anchor');
  3. const serumCmn = require("@project-serum/common");
  4. const TokenInstructions = require("@project-serum/serum").TokenInstructions;
  5. const utils = require("./utils");
  6. describe("Lockup and Registry", () => {
  7. const provider = anchor.Provider.local();
  8. // Configure the client to use the local cluster.
  9. anchor.setProvider(provider);
  10. const lockup = anchor.workspace.Lockup;
  11. const registry = anchor.workspace.Registry;
  12. let lockupAddress = null;
  13. let mint = null;
  14. let god = null;
  15. it("Sets up initial test state", async () => {
  16. const [_mint, _god] = await serumCmn.createMintAndVault(
  17. provider,
  18. new anchor.BN(1000000)
  19. );
  20. mint = _mint;
  21. god = _god;
  22. });
  23. it("Is initialized!", async () => {
  24. await lockup.state.rpc.new({
  25. accounts: {
  26. authority: provider.wallet.publicKey,
  27. },
  28. });
  29. lockupAddress = await lockup.state.address();
  30. const lockupAccount = await lockup.state();
  31. assert.ok(lockupAccount.authority.equals(provider.wallet.publicKey));
  32. assert.ok(lockupAccount.whitelist.length === 5);
  33. lockupAccount.whitelist.forEach((e) => {
  34. assert.ok(e.programId.equals(new anchor.web3.PublicKey()));
  35. });
  36. });
  37. it("Deletes the default whitelisted addresses", async () => {
  38. const defaultEntry = { programId: new anchor.web3.PublicKey() };
  39. await lockup.state.rpc.whitelistDelete(defaultEntry, {
  40. accounts: {
  41. authority: provider.wallet.publicKey,
  42. },
  43. });
  44. });
  45. it("Sets a new authority", async () => {
  46. const newAuthority = new anchor.web3.Account();
  47. await lockup.state.rpc.setAuthority(newAuthority.publicKey, {
  48. accounts: {
  49. authority: provider.wallet.publicKey,
  50. },
  51. });
  52. let lockupAccount = await lockup.state();
  53. assert.ok(lockupAccount.authority.equals(newAuthority.publicKey));
  54. await lockup.state.rpc.setAuthority(provider.wallet.publicKey, {
  55. accounts: {
  56. authority: newAuthority.publicKey,
  57. },
  58. signers: [newAuthority],
  59. });
  60. lockupAccount = await lockup.state();
  61. assert.ok(lockupAccount.authority.equals(provider.wallet.publicKey));
  62. });
  63. let e0 = null;
  64. let e1 = null;
  65. let e2 = null;
  66. let e3 = null;
  67. let e4 = null;
  68. it("Adds to the whitelist", async () => {
  69. const generateEntry = async () => {
  70. let programId = new anchor.web3.Account().publicKey;
  71. return {
  72. programId,
  73. };
  74. };
  75. e0 = await generateEntry();
  76. e1 = await generateEntry();
  77. e2 = await generateEntry();
  78. e3 = await generateEntry();
  79. e4 = await generateEntry();
  80. const e5 = await generateEntry();
  81. const accounts = {
  82. authority: provider.wallet.publicKey,
  83. };
  84. await lockup.state.rpc.whitelistAdd(e0, { accounts });
  85. let lockupAccount = await lockup.state();
  86. assert.ok(lockupAccount.whitelist.length === 1);
  87. assert.deepEqual(lockupAccount.whitelist, [e0]);
  88. await lockup.state.rpc.whitelistAdd(e1, { accounts });
  89. await lockup.state.rpc.whitelistAdd(e2, { accounts });
  90. await lockup.state.rpc.whitelistAdd(e3, { accounts });
  91. await lockup.state.rpc.whitelistAdd(e4, { accounts });
  92. lockupAccount = await lockup.state();
  93. assert.deepEqual(lockupAccount.whitelist, [e0, e1, e2, e3, e4]);
  94. await assert.rejects(
  95. async () => {
  96. await lockup.state.rpc.whitelistAdd(e5, { accounts });
  97. },
  98. (err) => {
  99. assert.equal(err.code, 108);
  100. assert.equal(err.msg, "Whitelist is full");
  101. return true;
  102. }
  103. );
  104. });
  105. it("Removes from the whitelist", async () => {
  106. await lockup.state.rpc.whitelistDelete(e0, {
  107. accounts: {
  108. authority: provider.wallet.publicKey,
  109. },
  110. });
  111. let lockupAccount = await lockup.state();
  112. assert.deepEqual(lockupAccount.whitelist, [e1, e2, e3, e4]);
  113. });
  114. const vesting = new anchor.web3.Account();
  115. let vestingAccount = null;
  116. let vestingSigner = null;
  117. it("Creates a vesting account", async () => {
  118. const beneficiary = provider.wallet.publicKey;
  119. const endTs = new anchor.BN(Date.now() / 1000 + 3);
  120. const periodCount = new anchor.BN(5);
  121. const depositAmount = new anchor.BN(100);
  122. const vault = new anchor.web3.Account();
  123. let [
  124. _vestingSigner,
  125. nonce,
  126. ] = await anchor.web3.PublicKey.findProgramAddress(
  127. [vesting.publicKey.toBuffer()],
  128. lockup.programId
  129. );
  130. vestingSigner = _vestingSigner;
  131. await lockup.rpc.createVesting(
  132. beneficiary,
  133. endTs,
  134. periodCount,
  135. depositAmount,
  136. nonce,
  137. {
  138. accounts: {
  139. vesting: vesting.publicKey,
  140. vault: vault.publicKey,
  141. depositor: god,
  142. depositorAuthority: provider.wallet.publicKey,
  143. tokenProgram: TokenInstructions.TOKEN_PROGRAM_ID,
  144. rent: anchor.web3.SYSVAR_RENT_PUBKEY,
  145. clock: anchor.web3.SYSVAR_CLOCK_PUBKEY,
  146. },
  147. signers: [vesting, vault],
  148. instructions: [
  149. await lockup.account.vesting.createInstruction(vesting),
  150. ...(await serumCmn.createTokenAccountInstrs(
  151. provider,
  152. vault.publicKey,
  153. mint,
  154. vestingSigner
  155. )),
  156. ],
  157. }
  158. );
  159. vestingAccount = await lockup.account.vesting(vesting.publicKey);
  160. assert.ok(vestingAccount.beneficiary.equals(provider.wallet.publicKey));
  161. assert.ok(vestingAccount.mint.equals(mint));
  162. assert.ok(vestingAccount.grantor.equals(provider.wallet.publicKey));
  163. assert.ok(vestingAccount.outstanding.eq(depositAmount));
  164. assert.ok(vestingAccount.startBalance.eq(depositAmount));
  165. assert.ok(vestingAccount.endTs.eq(endTs));
  166. assert.ok(vestingAccount.periodCount.eq(periodCount));
  167. assert.ok(vestingAccount.whitelistOwned.eq(new anchor.BN(0)));
  168. assert.equal(vestingAccount.nonce, nonce);
  169. assert.ok(endTs.gt(vestingAccount.startTs));
  170. });
  171. it("Fails to withdraw from a vesting account before vesting", async () => {
  172. await assert.rejects(
  173. async () => {
  174. await lockup.rpc.withdraw(new anchor.BN(100), {
  175. accounts: {
  176. vesting: vesting.publicKey,
  177. beneficiary: provider.wallet.publicKey,
  178. token: god,
  179. vault: vestingAccount.vault,
  180. vestingSigner: vestingSigner,
  181. tokenProgram: TokenInstructions.TOKEN_PROGRAM_ID,
  182. clock: anchor.web3.SYSVAR_CLOCK_PUBKEY,
  183. },
  184. });
  185. },
  186. (err) => {
  187. assert.equal(err.code, 107);
  188. assert.equal(err.msg, "Insufficient withdrawal balance.");
  189. return true;
  190. }
  191. );
  192. });
  193. it("Waits for a vesting period to pass", async () => {
  194. await serumCmn.sleep(5 * 1000);
  195. });
  196. it("Withdraws from the vesting account", async () => {
  197. const token = await serumCmn.createTokenAccount(
  198. provider,
  199. mint,
  200. provider.wallet.publicKey
  201. );
  202. await lockup.rpc.withdraw(new anchor.BN(100), {
  203. accounts: {
  204. vesting: vesting.publicKey,
  205. beneficiary: provider.wallet.publicKey,
  206. token,
  207. vault: vestingAccount.vault,
  208. vestingSigner,
  209. tokenProgram: TokenInstructions.TOKEN_PROGRAM_ID,
  210. clock: anchor.web3.SYSVAR_CLOCK_PUBKEY,
  211. },
  212. });
  213. vestingAccount = await lockup.account.vesting(vesting.publicKey);
  214. assert.ok(vestingAccount.outstanding.eq(new anchor.BN(0)));
  215. const vaultAccount = await serumCmn.getTokenAccount(
  216. provider,
  217. vestingAccount.vault
  218. );
  219. assert.ok(vaultAccount.amount.eq(new anchor.BN(0)));
  220. const tokenAccount = await serumCmn.getTokenAccount(provider, token);
  221. assert.ok(tokenAccount.amount.eq(new anchor.BN(100)));
  222. });
  223. const registrar = new anchor.web3.Account();
  224. const rewardQ = new anchor.web3.Account();
  225. const withdrawalTimelock = new anchor.BN(4);
  226. const stakeRate = new anchor.BN(2);
  227. const rewardQLen = 100;
  228. let registrarAccount = null;
  229. let registrarSigner = null;
  230. let nonce = null;
  231. let poolMint = null;
  232. it("Creates registry genesis", async () => {
  233. const [
  234. _registrarSigner,
  235. _nonce,
  236. ] = await anchor.web3.PublicKey.findProgramAddress(
  237. [registrar.publicKey.toBuffer()],
  238. registry.programId
  239. );
  240. registrarSigner = _registrarSigner;
  241. nonce = _nonce;
  242. poolMint = await serumCmn.createMint(provider, registrarSigner);
  243. });
  244. it("Initializes registry's global state", async () => {
  245. await registry.state.rpc.new({
  246. accounts: { lockupProgram: lockup.programId },
  247. });
  248. const state = await registry.state();
  249. assert.ok(state.lockupProgram.equals(lockup.programId));
  250. // Should not allow a second initializatoin.
  251. await assert.rejects(
  252. async () => {
  253. await registry.state.rpc.new(lockup.programId);
  254. },
  255. (err) => {
  256. return true;
  257. }
  258. );
  259. });
  260. it("Initializes the registrar", async () => {
  261. await registry.rpc.initialize(
  262. mint,
  263. provider.wallet.publicKey,
  264. nonce,
  265. withdrawalTimelock,
  266. stakeRate,
  267. rewardQLen,
  268. {
  269. accounts: {
  270. registrar: registrar.publicKey,
  271. poolMint,
  272. rewardEventQ: rewardQ.publicKey,
  273. rent: anchor.web3.SYSVAR_RENT_PUBKEY,
  274. },
  275. signers: [registrar, rewardQ],
  276. instructions: [
  277. await registry.account.registrar.createInstruction(registrar),
  278. await registry.account.rewardQueue.createInstruction(rewardQ, 8250),
  279. ],
  280. }
  281. );
  282. registrarAccount = await registry.account.registrar(registrar.publicKey);
  283. assert.ok(registrarAccount.authority.equals(provider.wallet.publicKey));
  284. assert.equal(registrarAccount.nonce, nonce);
  285. assert.ok(registrarAccount.mint.equals(mint));
  286. assert.ok(registrarAccount.poolMint.equals(poolMint));
  287. assert.ok(registrarAccount.stakeRate.eq(stakeRate));
  288. assert.ok(registrarAccount.rewardEventQ.equals(rewardQ.publicKey));
  289. assert.ok(registrarAccount.withdrawalTimelock.eq(withdrawalTimelock));
  290. });
  291. const member = new anchor.web3.Account();
  292. let memberAccount = null;
  293. let memberSigner = null;
  294. let balances = null;
  295. let balancesLocked = null;
  296. it("Creates a member", async () => {
  297. const [
  298. _memberSigner,
  299. nonce,
  300. ] = await anchor.web3.PublicKey.findProgramAddress(
  301. [registrar.publicKey.toBuffer(), member.publicKey.toBuffer()],
  302. registry.programId
  303. );
  304. memberSigner = _memberSigner;
  305. const [mainTx, _balances] = await utils.createBalanceSandbox(
  306. provider,
  307. registrarAccount,
  308. memberSigner
  309. );
  310. const [lockedTx, _balancesLocked] = await utils.createBalanceSandbox(
  311. provider,
  312. registrarAccount,
  313. memberSigner
  314. );
  315. balances = _balances;
  316. balancesLocked = _balancesLocked;
  317. const tx = registry.transaction.createMember(nonce, {
  318. accounts: {
  319. registrar: registrar.publicKey,
  320. member: member.publicKey,
  321. beneficiary: provider.wallet.publicKey,
  322. memberSigner,
  323. balances,
  324. balancesLocked,
  325. tokenProgram: TokenInstructions.TOKEN_PROGRAM_ID,
  326. rent: anchor.web3.SYSVAR_RENT_PUBKEY,
  327. },
  328. instructions: [await registry.account.member.createInstruction(member)],
  329. });
  330. const signers = [member, provider.wallet.payer];
  331. const allTxs = [mainTx, lockedTx, { tx, signers }];
  332. let txSigs = await provider.sendAll(allTxs);
  333. memberAccount = await registry.account.member(member.publicKey);
  334. assert.ok(memberAccount.registrar.equals(registrar.publicKey));
  335. assert.ok(memberAccount.beneficiary.equals(provider.wallet.publicKey));
  336. assert.ok(memberAccount.metadata.equals(new anchor.web3.PublicKey()));
  337. assert.equal(
  338. JSON.stringify(memberAccount.balances),
  339. JSON.stringify(balances)
  340. );
  341. assert.equal(
  342. JSON.stringify(memberAccount.balancesLocked),
  343. JSON.stringify(balancesLocked)
  344. );
  345. assert.ok(memberAccount.rewardsCursor === 0);
  346. assert.ok(memberAccount.lastStakeTs.eq(new anchor.BN(0)));
  347. });
  348. it("Deposits (unlocked) to a member", async () => {
  349. const depositAmount = new anchor.BN(120);
  350. await registry.rpc.deposit(depositAmount, {
  351. accounts: {
  352. depositor: god,
  353. depositorAuthority: provider.wallet.publicKey,
  354. tokenProgram: TokenInstructions.TOKEN_PROGRAM_ID,
  355. vault: memberAccount.balances.vault,
  356. beneficiary: provider.wallet.publicKey,
  357. member: member.publicKey,
  358. },
  359. });
  360. const memberVault = await serumCmn.getTokenAccount(
  361. provider,
  362. memberAccount.balances.vault
  363. );
  364. assert.ok(memberVault.amount.eq(depositAmount));
  365. });
  366. it("Stakes to a member (unlocked)", async () => {
  367. const stakeAmount = new anchor.BN(10);
  368. await registry.rpc.stake(stakeAmount, false, {
  369. accounts: {
  370. // Stake instance.
  371. registrar: registrar.publicKey,
  372. rewardEventQ: rewardQ.publicKey,
  373. poolMint,
  374. // Member.
  375. member: member.publicKey,
  376. beneficiary: provider.wallet.publicKey,
  377. balances,
  378. balancesLocked,
  379. // Program signers.
  380. memberSigner,
  381. registrarSigner,
  382. // Misc.
  383. clock: anchor.web3.SYSVAR_CLOCK_PUBKEY,
  384. tokenProgram: TokenInstructions.TOKEN_PROGRAM_ID,
  385. },
  386. });
  387. const vault = await serumCmn.getTokenAccount(
  388. provider,
  389. memberAccount.balances.vault
  390. );
  391. const vaultStake = await serumCmn.getTokenAccount(
  392. provider,
  393. memberAccount.balances.vaultStake
  394. );
  395. const spt = await serumCmn.getTokenAccount(
  396. provider,
  397. memberAccount.balances.spt
  398. );
  399. assert.ok(vault.amount.eq(new anchor.BN(100)));
  400. assert.ok(vaultStake.amount.eq(new anchor.BN(20)));
  401. assert.ok(spt.amount.eq(new anchor.BN(10)));
  402. });
  403. const unlockedVendor = new anchor.web3.Account();
  404. const unlockedVendorVault = new anchor.web3.Account();
  405. let unlockedVendorSigner = null;
  406. it("Drops an unlocked reward", async () => {
  407. const rewardKind = {
  408. unlocked: {},
  409. };
  410. const rewardAmount = new anchor.BN(200);
  411. const expiry = new anchor.BN(Date.now() / 1000 + 5);
  412. const [
  413. _vendorSigner,
  414. nonce,
  415. ] = await anchor.web3.PublicKey.findProgramAddress(
  416. [registrar.publicKey.toBuffer(), unlockedVendor.publicKey.toBuffer()],
  417. registry.programId
  418. );
  419. unlockedVendorSigner = _vendorSigner;
  420. await registry.rpc.dropReward(
  421. rewardKind,
  422. rewardAmount,
  423. expiry,
  424. provider.wallet.publicKey,
  425. nonce,
  426. {
  427. accounts: {
  428. registrar: registrar.publicKey,
  429. rewardEventQ: rewardQ.publicKey,
  430. poolMint,
  431. vendor: unlockedVendor.publicKey,
  432. vendorVault: unlockedVendorVault.publicKey,
  433. depositor: god,
  434. depositorAuthority: provider.wallet.publicKey,
  435. tokenProgram: TokenInstructions.TOKEN_PROGRAM_ID,
  436. clock: anchor.web3.SYSVAR_CLOCK_PUBKEY,
  437. rent: anchor.web3.SYSVAR_RENT_PUBKEY,
  438. },
  439. signers: [unlockedVendorVault, unlockedVendor],
  440. instructions: [
  441. ...(await serumCmn.createTokenAccountInstrs(
  442. provider,
  443. unlockedVendorVault.publicKey,
  444. mint,
  445. unlockedVendorSigner
  446. )),
  447. await registry.account.rewardVendor.createInstruction(unlockedVendor),
  448. ],
  449. }
  450. );
  451. const vendorAccount = await registry.account.rewardVendor(
  452. unlockedVendor.publicKey
  453. );
  454. assert.ok(vendorAccount.registrar.equals(registrar.publicKey));
  455. assert.ok(vendorAccount.vault.equals(unlockedVendorVault.publicKey));
  456. assert.ok(vendorAccount.nonce === nonce);
  457. assert.ok(vendorAccount.poolTokenSupply.eq(new anchor.BN(10)));
  458. assert.ok(vendorAccount.expiryTs.eq(expiry));
  459. assert.ok(vendorAccount.expiryReceiver.equals(provider.wallet.publicKey));
  460. assert.ok(vendorAccount.total.eq(rewardAmount));
  461. assert.ok(vendorAccount.expired === false);
  462. assert.ok(vendorAccount.rewardEventQCursor === 0);
  463. assert.deepEqual(vendorAccount.kind, rewardKind);
  464. const rewardQAccount = await registry.account.rewardQueue(
  465. rewardQ.publicKey
  466. );
  467. assert.ok(rewardQAccount.head === 1);
  468. assert.ok(rewardQAccount.tail === 0);
  469. const e = rewardQAccount.events[0];
  470. assert.ok(e.vendor.equals(unlockedVendor.publicKey));
  471. assert.equal(e.locked, false);
  472. });
  473. it("Collects an unlocked reward", async () => {
  474. const token = await serumCmn.createTokenAccount(
  475. provider,
  476. mint,
  477. provider.wallet.publicKey
  478. );
  479. await registry.rpc.claimReward({
  480. accounts: {
  481. to: token,
  482. cmn: {
  483. registrar: registrar.publicKey,
  484. member: member.publicKey,
  485. beneficiary: provider.wallet.publicKey,
  486. balances,
  487. balancesLocked,
  488. vendor: unlockedVendor.publicKey,
  489. vault: unlockedVendorVault.publicKey,
  490. vendorSigner: unlockedVendorSigner,
  491. tokenProgram: TokenInstructions.TOKEN_PROGRAM_ID,
  492. clock: anchor.web3.SYSVAR_CLOCK_PUBKEY,
  493. },
  494. },
  495. });
  496. let tokenAccount = await serumCmn.getTokenAccount(provider, token);
  497. assert.ok(tokenAccount.amount.eq(new anchor.BN(200)));
  498. const memberAccount = await registry.account.member(member.publicKey);
  499. assert.ok(memberAccount.rewardsCursor == 1);
  500. });
  501. const lockedVendor = new anchor.web3.Account();
  502. const lockedVendorVault = new anchor.web3.Account();
  503. let lockedVendorSigner = null;
  504. let lockedRewardAmount = null;
  505. let lockedRewardKind = null;
  506. it("Drops a locked reward", async () => {
  507. lockedRewardKind = {
  508. locked: {
  509. endTs: new anchor.BN(Date.now() / 1000 + 70),
  510. periodCount: new anchor.BN(10),
  511. },
  512. };
  513. lockedRewardAmount = new anchor.BN(200);
  514. const expiry = new anchor.BN(Date.now() / 1000 + 5);
  515. const [
  516. _vendorSigner,
  517. nonce,
  518. ] = await anchor.web3.PublicKey.findProgramAddress(
  519. [registrar.publicKey.toBuffer(), lockedVendor.publicKey.toBuffer()],
  520. registry.programId
  521. );
  522. lockedVendorSigner = _vendorSigner;
  523. await registry.rpc.dropReward(
  524. lockedRewardKind,
  525. lockedRewardAmount,
  526. expiry,
  527. provider.wallet.publicKey,
  528. nonce,
  529. {
  530. accounts: {
  531. registrar: registrar.publicKey,
  532. rewardEventQ: rewardQ.publicKey,
  533. poolMint,
  534. vendor: lockedVendor.publicKey,
  535. vendorVault: lockedVendorVault.publicKey,
  536. depositor: god,
  537. depositorAuthority: provider.wallet.publicKey,
  538. tokenProgram: TokenInstructions.TOKEN_PROGRAM_ID,
  539. clock: anchor.web3.SYSVAR_CLOCK_PUBKEY,
  540. rent: anchor.web3.SYSVAR_RENT_PUBKEY,
  541. },
  542. signers: [lockedVendorVault, lockedVendor],
  543. instructions: [
  544. ...(await serumCmn.createTokenAccountInstrs(
  545. provider,
  546. lockedVendorVault.publicKey,
  547. mint,
  548. lockedVendorSigner
  549. )),
  550. await registry.account.rewardVendor.createInstruction(lockedVendor),
  551. ],
  552. }
  553. );
  554. const vendorAccount = await registry.account.rewardVendor(
  555. lockedVendor.publicKey
  556. );
  557. assert.ok(vendorAccount.registrar.equals(registrar.publicKey));
  558. assert.ok(vendorAccount.vault.equals(lockedVendorVault.publicKey));
  559. assert.ok(vendorAccount.nonce === nonce);
  560. assert.ok(vendorAccount.poolTokenSupply.eq(new anchor.BN(10)));
  561. assert.ok(vendorAccount.expiryTs.eq(expiry));
  562. assert.ok(vendorAccount.expiryReceiver.equals(provider.wallet.publicKey));
  563. assert.ok(vendorAccount.total.eq(lockedRewardAmount));
  564. assert.ok(vendorAccount.expired === false);
  565. assert.ok(vendorAccount.rewardEventQCursor === 1);
  566. assert.equal(
  567. JSON.stringify(vendorAccount.kind),
  568. JSON.stringify(lockedRewardKind)
  569. );
  570. const rewardQAccount = await registry.account.rewardQueue(
  571. rewardQ.publicKey
  572. );
  573. assert.ok(rewardQAccount.head === 2);
  574. assert.ok(rewardQAccount.tail === 0);
  575. const e = rewardQAccount.events[1];
  576. assert.ok(e.vendor.equals(lockedVendor.publicKey));
  577. assert.ok(e.locked === true);
  578. });
  579. it("Collects a locked reward", async () => {
  580. const vendoredVesting = new anchor.web3.Account();
  581. const vendoredVestingVault = new anchor.web3.Account();
  582. let [
  583. vendoredVestingSigner,
  584. nonce,
  585. ] = await anchor.web3.PublicKey.findProgramAddress(
  586. [vendoredVesting.publicKey.toBuffer()],
  587. lockup.programId
  588. );
  589. const remainingAccounts = lockup.instruction.createVesting
  590. .accounts({
  591. vesting: vendoredVesting.publicKey,
  592. vault: vendoredVestingVault.publicKey,
  593. depositor: lockedVendorVault.publicKey,
  594. depositorAuthority: lockedVendorSigner,
  595. tokenProgram: TokenInstructions.TOKEN_PROGRAM_ID,
  596. rent: anchor.web3.SYSVAR_RENT_PUBKEY,
  597. clock: anchor.web3.SYSVAR_CLOCK_PUBKEY,
  598. })
  599. // Change the signer status on the vendor signer since it's signed by the program, not the
  600. // client.
  601. .map((meta) =>
  602. meta.pubkey === lockedVendorSigner ? { ...meta, isSigner: false } : meta
  603. );
  604. await registry.rpc.claimRewardLocked(nonce, {
  605. accounts: {
  606. registry: await registry.state.address(),
  607. lockupProgram: lockup.programId,
  608. cmn: {
  609. registrar: registrar.publicKey,
  610. member: member.publicKey,
  611. beneficiary: provider.wallet.publicKey,
  612. balances,
  613. balancesLocked,
  614. vendor: lockedVendor.publicKey,
  615. vault: lockedVendorVault.publicKey,
  616. vendorSigner: lockedVendorSigner,
  617. tokenProgram: TokenInstructions.TOKEN_PROGRAM_ID,
  618. clock: anchor.web3.SYSVAR_CLOCK_PUBKEY,
  619. },
  620. },
  621. remainingAccounts,
  622. signers: [vendoredVesting, vendoredVestingVault],
  623. instructions: [
  624. await lockup.account.vesting.createInstruction(vendoredVesting),
  625. ...(await serumCmn.createTokenAccountInstrs(
  626. provider,
  627. vendoredVestingVault.publicKey,
  628. mint,
  629. vendoredVestingSigner
  630. )),
  631. ],
  632. });
  633. const lockupAccount = await lockup.account.vesting(
  634. vendoredVesting.publicKey
  635. );
  636. assert.ok(lockupAccount.beneficiary.equals(provider.wallet.publicKey));
  637. assert.ok(lockupAccount.mint.equals(mint));
  638. assert.ok(lockupAccount.vault.equals(vendoredVestingVault.publicKey));
  639. assert.ok(lockupAccount.outstanding.eq(lockedRewardAmount));
  640. assert.ok(lockupAccount.startBalance.eq(lockedRewardAmount));
  641. assert.ok(lockupAccount.endTs.eq(lockedRewardKind.locked.endTs));
  642. assert.ok(
  643. lockupAccount.periodCount.eq(lockedRewardKind.locked.periodCount)
  644. );
  645. assert.ok(lockupAccount.whitelistOwned.eq(new anchor.BN(0)));
  646. });
  647. const pendingWithdrawal = new anchor.web3.Account();
  648. it("Unstakes (unlocked)", async () => {
  649. const unstakeAmount = new anchor.BN(10);
  650. await registry.rpc.startUnstake(unstakeAmount, false, {
  651. accounts: {
  652. registrar: registrar.publicKey,
  653. rewardEventQ: rewardQ.publicKey,
  654. poolMint,
  655. pendingWithdrawal: pendingWithdrawal.publicKey,
  656. member: member.publicKey,
  657. beneficiary: provider.wallet.publicKey,
  658. balances,
  659. balancesLocked,
  660. memberSigner,
  661. tokenProgram: TokenInstructions.TOKEN_PROGRAM_ID,
  662. clock: anchor.web3.SYSVAR_CLOCK_PUBKEY,
  663. rent: anchor.web3.SYSVAR_RENT_PUBKEY,
  664. },
  665. signers: [pendingWithdrawal],
  666. instructions: [
  667. await registry.account.pendingWithdrawal.createInstruction(
  668. pendingWithdrawal
  669. ),
  670. ],
  671. });
  672. const vaultPw = await serumCmn.getTokenAccount(
  673. provider,
  674. memberAccount.balances.vaultPw
  675. );
  676. const vaultStake = await serumCmn.getTokenAccount(
  677. provider,
  678. memberAccount.balances.vaultStake
  679. );
  680. const spt = await serumCmn.getTokenAccount(
  681. provider,
  682. memberAccount.balances.spt
  683. );
  684. assert.ok(vaultPw.amount.eq(new anchor.BN(20)));
  685. assert.ok(vaultStake.amount.eq(new anchor.BN(0)));
  686. assert.ok(spt.amount.eq(new anchor.BN(0)));
  687. });
  688. const tryEndUnstake = async () => {
  689. await registry.rpc.endUnstake({
  690. accounts: {
  691. registrar: registrar.publicKey,
  692. member: member.publicKey,
  693. beneficiary: provider.wallet.publicKey,
  694. pendingWithdrawal: pendingWithdrawal.publicKey,
  695. vault: balances.vault,
  696. vaultPw: balances.vaultPw,
  697. memberSigner,
  698. clock: anchor.web3.SYSVAR_CLOCK_PUBKEY,
  699. tokenProgram: TokenInstructions.TOKEN_PROGRAM_ID,
  700. },
  701. });
  702. };
  703. it("Fails to end unstaking before timelock", async () => {
  704. await assert.rejects(
  705. async () => {
  706. await tryEndUnstake();
  707. },
  708. (err) => {
  709. assert.equal(err.code, 109);
  710. assert.equal(err.msg, "The unstake timelock has not yet expired.");
  711. return true;
  712. }
  713. );
  714. });
  715. it("Waits for the unstake period to end", async () => {
  716. await serumCmn.sleep(5000);
  717. });
  718. it("Unstake finalizes (unlocked)", async () => {
  719. await tryEndUnstake();
  720. const vault = await serumCmn.getTokenAccount(
  721. provider,
  722. memberAccount.balances.vault
  723. );
  724. const vaultPw = await serumCmn.getTokenAccount(
  725. provider,
  726. memberAccount.balances.vaultPw
  727. );
  728. assert.ok(vault.amount.eq(new anchor.BN(120)));
  729. assert.ok(vaultPw.amount.eq(new anchor.BN(0)));
  730. });
  731. it("Withdraws deposits (unlocked)", async () => {
  732. const token = await serumCmn.createTokenAccount(
  733. provider,
  734. mint,
  735. provider.wallet.publicKey
  736. );
  737. const withdrawAmount = new anchor.BN(100);
  738. await registry.rpc.withdraw(withdrawAmount, {
  739. accounts: {
  740. registrar: registrar.publicKey,
  741. member: member.publicKey,
  742. beneficiary: provider.wallet.publicKey,
  743. vault: memberAccount.balances.vault,
  744. memberSigner,
  745. depositor: token,
  746. tokenProgram: TokenInstructions.TOKEN_PROGRAM_ID,
  747. },
  748. });
  749. const tokenAccount = await serumCmn.getTokenAccount(provider, token);
  750. assert.ok(tokenAccount.amount.eq(withdrawAmount));
  751. });
  752. });