lockup.js 26 KB

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