lockup.js 26 KB

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