-
Notifications
You must be signed in to change notification settings - Fork 0
/
SOLution.ts
719 lines (636 loc) · 18.8 KB
/
SOLution.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
import * as anchor from "@project-serum/anchor";
import { Program, BN } from "@project-serum/anchor";
import { assert } from "chai";
import { LAMPORTS_PER_SOL } from "@solana/web3.js";
import * as bs58 from "bs58";
import { SoLution } from "../target/types/so_lution";
const RENT_MAX_APPROX = 15000000;
const BASE_FEE_LAMPORTS = LAMPORTS_PER_SOL / 1000;
describe("SOLution", () => {
// Configure the client to use the local cluster.
anchor.setProvider(anchor.Provider.env());
const program = anchor.workspace.SoLution as Program<SoLution>;
// Function that returns balance of public key
const getBalance = async (publicKey) => {
return await program.provider.connection.getBalance(publicKey);
};
// Function that wraps the logic of asking questions
const askQuestion = async (
author,
topic,
content,
amount = new BN(LAMPORTS_PER_SOL)
) => {
const question = anchor.web3.Keypair.generate();
const signers =
author == program.provider.wallet ? [question] : [question, author];
await program.rpc.askQuestion(topic, content, amount, {
accounts: {
question: question.publicKey,
author: author.publicKey,
systemProgram: anchor.web3.SystemProgram.programId,
},
signers: signers,
});
return question;
};
// Function that wraps the logic of submitting answers
const submitAnswer = async (
author,
targetQuestion,
targetAuthor,
content
) => {
const answer = anchor.web3.Keypair.generate();
const signers =
author == program.provider.wallet ? [answer] : [answer, author];
await program.rpc.submitAnswer(targetQuestion, targetAuthor, content, {
accounts: {
answer: answer.publicKey,
author: author.publicKey,
systemProgram: anchor.web3.SystemProgram.programId,
},
signers: signers,
});
return answer;
};
describe("# Create and Fetch Questions", () => {
it("can ask a question", async () => {
const testTopic = "cns";
const testContent =
"Can anyone explain what is homomorphic encryption?";
let author = program.provider.wallet;
let before_balance = await getBalance(author.publicKey);
let amount = new BN(LAMPORTS_PER_SOL);
const question = await askQuestion(
author,
testTopic,
testContent,
amount
);
const questionAccount = await program.account.question.fetch(
question.publicKey
);
let after_balance = await getBalance(author.publicKey);
assert.closeTo(
before_balance - after_balance,
amount.toNumber(),
RENT_MAX_APPROX
);
assert.equal(
questionAccount.author.toBase58(),
program.provider.wallet.publicKey.toBase58()
);
assert.equal(questionAccount.topic, testTopic);
assert.equal(questionAccount.content, testContent);
assert.ok(questionAccount.timestamp);
});
it("can ask a question without a topic", async () => {
const testTopic = "";
const testContent =
"Is pallier encryption some kind of homomorphic encryption?";
const question = await askQuestion(
program.provider.wallet,
testTopic,
testContent
);
const questionAccount = await program.account.question.fetch(
question.publicKey
);
assert.equal(
questionAccount.author.toBase58(),
program.provider.wallet.publicKey.toBase58()
);
assert.equal(questionAccount.topic, testTopic);
assert.equal(questionAccount.content, testContent);
assert.ok(questionAccount.timestamp);
});
it("can ask a question as a different author", async () => {
const testTopic = "cns";
const testContent =
"Why does impermanent loss happen in AMMs like Uniswap? (Oops i thought this was defi)";
const otherUser = anchor.web3.Keypair.generate();
const signature = await program.provider.connection.requestAirdrop(
otherUser.publicKey,
10000000000
);
await program.provider.connection.confirmTransaction(signature);
const question = await askQuestion(
otherUser,
testTopic,
testContent
);
const questionAccount = await program.account.question.fetch(
question.publicKey
);
assert.equal(
questionAccount.author.toBase58(),
otherUser.publicKey.toBase58()
);
assert.equal(questionAccount.topic, testTopic);
assert.equal(questionAccount.content, testContent);
assert.ok(questionAccount.timestamp);
});
it("cannot provide a topic with more than 50 characters", async () => {
try {
const topicWith51Chars = "x".repeat(51);
const testContent = "Hummus, am I right?";
const question = await askQuestion(
program.provider.wallet,
topicWith51Chars,
testContent
);
} catch (error) {
assert.equal(
error.msg,
"The provided topic should be 50 characters long maximum."
);
return;
}
assert.fail(
"The instruction should have failed with a 51-character topic."
);
});
it("cannot provide a content with more than 280 characters", async () => {
try {
const contentWith281Chars = "x".repeat(281);
const testTopic = "veganism";
const question = await askQuestion(
program.provider.wallet,
testTopic,
contentWith281Chars
);
} catch (error) {
assert.equal(
error.msg,
"The provided content should be 280 characters long maximum."
);
return;
}
assert.fail(
"The instruction should have failed with a 281-character content."
);
});
it("can fetch all questions", async () => {
const questionAccounts = await program.account.question.all();
assert.equal(questionAccounts.length, 3);
});
});
describe("# Create and Fetch Answers", () => {
it("can submit an answer", async () => {
const testContent =
"Homomorphic encryption satisfies the following simple formula: D(E(x1) * E(x2)) = x1 + x2";
const questionAccounts = await program.account.question.all();
const answer = await submitAnswer(
program.provider.wallet,
questionAccounts[0].publicKey,
questionAccounts[0].account.author,
testContent
);
const answerAccount = await program.account.answer.fetch(
answer.publicKey
);
assert.equal(
answerAccount.author.toBase58(),
program.provider.wallet.publicKey.toBase58()
);
assert.equal(
answerAccount.targetQuestion.toBase58(),
questionAccounts[0].publicKey.toBase58()
);
assert.equal(answerAccount.content, testContent);
assert.ok(answerAccount.timestamp);
});
it("can submit an answer as a different author", async () => {
const testContent = "i dont think this is a relevant question";
const otherUser = anchor.web3.Keypair.generate();
const signature = await program.provider.connection.requestAirdrop(
otherUser.publicKey,
10000000000
);
await program.provider.connection.confirmTransaction(signature);
const questionAccounts = await program.account.question.all();
const answer = await submitAnswer(
otherUser,
questionAccounts[1].publicKey,
questionAccounts[1].account.author,
testContent
);
const answerAccount = await program.account.answer.fetch(
answer.publicKey
);
assert.equal(
answerAccount.author.toBase58(),
otherUser.publicKey.toBase58()
);
assert.equal(
answerAccount.targetQuestion.toBase58(),
questionAccounts[1].publicKey.toBase58()
);
assert.equal(answerAccount.content, testContent);
assert.ok(answerAccount.timestamp);
});
it("can fetch all answers", async () => {
const answerAccounts = await program.account.answer.all();
assert.equal(answerAccounts.length, 2);
});
});
describe("# Filtering", () => {
it("can filter questions and answers by author", async () => {
const authorPublicKey = program.provider.wallet.publicKey;
const questionAccounts = await program.account.question.all([
{
memcmp: {
offset: 8, // Discriminator
bytes: authorPublicKey.toBase58(),
},
},
]);
const answerAccounts = await program.account.answer.all([
{
memcmp: {
offset: 8, // Discriminator
bytes: authorPublicKey.toBase58(),
},
},
]);
assert.equal(questionAccounts.length, 2);
assert.equal(answerAccounts.length, 1);
});
it("can filter answers by target_question", async () => {
const questionAccounts = await program.account.question.all();
const targetQuestionPublicKey = questionAccounts[0].publicKey;
const answerAccounts = await program.account.answer.all([
{
memcmp: {
offset: 8 + 32 + 8 + 1, // Discriminator + Author + timestamp + isSolution
bytes: targetQuestionPublicKey.toBase58(),
},
},
]);
assert.equal(answerAccounts.length, 1);
});
it("can filter questions by topic", async () => {
const testTopic = "cns";
const questionAccounts = await program.account.question.all([
{
memcmp: {
offset:
8 + // Discriminator.
32 + // Author public key.
8 + // Timestamp.
1 + // has_solution
32 + // Solution public key
8 + // amount
4, // Topic string prefix.
bytes: bs58.encode(Buffer.from(testTopic)),
},
},
]);
assert.equal(questionAccounts.length, 2);
assert.ok(
questionAccounts.every((questionAccount) => {
return questionAccount.account.topic === testTopic;
})
);
});
});
describe("# Update and Delete", () => {
it("can update a question and its answer", async () => {
const topic = "babydoge";
const content = "to the moon or not?";
const author = program.provider.wallet;
// Create question and an answer under the question
const question = await askQuestion(author, topic, content);
const questionAccount = await program.account.question.fetch(
question.publicKey
);
assert.equal(questionAccount.topic, topic);
assert.equal(questionAccount.content, content);
const answer = await submitAnswer(
author,
question.publicKey,
questionAccount.author,
content
);
const answerAccount = await program.account.answer.fetch(
answer.publicKey
);
assert.equal(answerAccount.content, content);
const new_topic = "shiba inu";
const new_content = "to mars <3";
// Update question
await program.rpc.updateQuestion(new_topic, new_content, {
accounts: {
question: question.publicKey,
author: author.publicKey,
},
});
const updatedQuestionAccount = await program.account.question.fetch(
question.publicKey
);
assert.equal(updatedQuestionAccount.topic, new_topic);
assert.equal(updatedQuestionAccount.content, new_content);
// Update Answer
await program.rpc.updateAnswer(new_content, {
accounts: {
answer: answer.publicKey,
author: author.publicKey,
},
});
const updatedAnswerAccount = await program.account.answer.fetch(
answer.publicKey
);
assert.equal(updatedAnswerAccount.content, new_content);
});
it("cannot update someone else's question or answer", async () => {
const topic = "fantom";
const content = "Fantom is wonderful";
const author = program.provider.wallet;
const question = await askQuestion(author, topic, content);
const answer = await submitAnswer(
author,
question.publicKey,
author.publicKey,
content
);
try {
await program.rpc.updateQuestion(
"solana",
"solana as in on Solana beach",
{
accounts: {
question: question.publicKey,
author: anchor.web3.Keypair.generate().publicKey,
},
}
);
assert.fail("We were able to update someone else's question.");
} catch (error) {
const questionAccount = await program.account.question.fetch(
question.publicKey
);
assert.equal(questionAccount.topic, topic);
assert.equal(questionAccount.content, content);
}
try {
await program.rpc.updateAnswer("solana as in on Solana beach", {
accounts: {
answer: answer.publicKey,
author: anchor.web3.Keypair.generate().publicKey,
},
});
assert.fail("We were able to update someone else's answer");
} catch (error) {
const answerAccount = await program.account.answer.fetch(
answer.publicKey
);
assert.equal(answerAccount.content, content);
}
});
it("can delete an answer", async () => {
const author = program.provider.wallet;
const question = await askQuestion(
author,
"babydoge",
"babydoge dodo do do do do"
);
const answer = await submitAnswer(
author,
question.publicKey,
author.publicKey,
"babydoge dodo do do do do"
);
await program.rpc.deleteAnswer({
accounts: {
answer: answer.publicKey,
authority: author.publicKey,
receiver: author.publicKey,
},
});
// Ensure that fetcing the question and answer account returns NULL
const answerAccount = await program.account.answer.fetchNullable(
answer.publicKey
);
assert.ok(answerAccount === null);
});
it("can delete an answer as the owner of the question", async () => {
const author = program.provider.wallet;
const question = await askQuestion(
author,
"babydoge",
"babydoge dodo do do do do"
);
// Create answer as another person
const otherUser = anchor.web3.Keypair.generate();
const signature = await program.provider.connection.requestAirdrop(
otherUser.publicKey,
10000000000
);
await program.provider.connection.confirmTransaction(signature);
const answer = await submitAnswer(
otherUser,
question.publicKey,
author.publicKey,
"babydoge dodo do do do do"
);
await program.rpc.deleteAnswer({
accounts: {
answer: answer.publicKey,
authority: author.publicKey,
receiver: otherUser.publicKey,
},
});
const answerAccount = await program.account.answer.fetchNullable(
answer.publicKey
);
assert.ok(answerAccount === null);
});
it("cannot delete delete someone else's answer", async () => {
const topic = "topic";
const content = "content";
const author = program.provider.wallet;
const question = await askQuestion(author, topic, content);
const answer = await submitAnswer(
author,
question.publicKey,
author.publicKey,
content
);
try {
const otherUser = anchor.web3.Keypair.generate();
await program.rpc.deleteAnswer({
accounts: {
answer: answer.publicKey,
authority: otherUser.publicKey,
receiver: author.publicKey,
},
});
assert.fail("We were able to delete someone else's answer");
} catch (error) {
const answerAccount = await program.account.answer.fetch(
answer.publicKey
);
assert.equal(answerAccount.content, content);
}
});
it("cannot update someone else's question or answer", async () => {
const topic = "fantom";
const content = "Fantom is wonderful";
const author = program.provider.wallet;
const question = await askQuestion(author, topic, content);
const answer = await submitAnswer(
author,
question.publicKey,
author.publicKey,
content
);
try {
await program.rpc.selectSolution({
accounts: {
question: question.publicKey,
answer: answer.publicKey,
author: anchor.web3.Keypair.generate().publicKey,
},
});
assert.fail("We were able to update someone else's question.");
} catch (error) {
return;
}
});
});
describe("# Utilities", () => {
it("can select solution as asker and redeem rewards as solver", async () => {
const topic = "babydoge";
const content = "to the moon or not?";
const author = program.provider.wallet;
// Create question and an answer under the question
const question = await askQuestion(author, topic, content);
const questionAccount = await program.account.question.fetch(
question.publicKey
);
assert.equal(questionAccount.topic, topic);
assert.equal(questionAccount.content, content);
const answer1 = await submitAnswer(
author,
question.publicKey,
questionAccount.author,
"answer 1"
);
const answerAccount1 = await program.account.answer.fetch(
answer1.publicKey
);
assert.equal(answerAccount1.content, "answer 1");
const answer2 = await submitAnswer(
author,
question.publicKey,
questionAccount.author,
"answer 2"
);
const answerAccount2 = await program.account.answer.fetch(
answer2.publicKey
);
assert.equal(answerAccount2.content, "answer 2");
const questionAmount = (
await program.account.question.fetch(question.publicKey)
).amount;
// select solution
await program.rpc.selectSolution({
accounts: {
question: question.publicKey,
answer: answer2.publicKey,
author: author.publicKey,
},
});
const QuestionAccount = await program.account.question.fetch(
question.publicKey
);
// check if selected answer is equivalent
assert.equal(
QuestionAccount.solution.toBase58(),
answer2.publicKey.toBase58()
);
const reward = (
await program.account.answer.fetch(answer2.publicKey)
).amount;
// Check if balance in question account is correct
assert.equal(
reward.toNumber(),
questionAmount.toNumber() - BASE_FEE_LAMPORTS
);
const authorPreBalance = await getBalance(author.publicKey);
// Redeem reward as answer author
await program.rpc.redeemReward({
accounts: {
answer: answer2.publicKey,
author: author.publicKey,
},
});
const authorBalance = await getBalance(author.publicKey);
assert.closeTo(
authorBalance - authorPreBalance,
reward.toNumber(),
RENT_MAX_APPROX
);
// Check that we can filter by hasSolution
const hasSolutionQuestionAccounts =
await program.account.question.all([
{
memcmp: {
offset:
8 + // Discriminator.
32 + // Author public key.
8, // Timestamp.
bytes: bs58.encode(new BN(1, "le").toArray()),
},
},
]);
assert.equal(hasSolutionQuestionAccounts.length, 1);
// Check that we can filter by isSolution
const isSolutionAnswerAccounts = await program.account.question.all(
[
{
memcmp: {
offset:
8 + // Discriminator.
32 + // Author public key.
8, // Timestamp.
bytes: bs58.encode(new BN(1, "le").toArray()),
},
},
]
);
assert.equal(isSolutionAnswerAccounts.length, 1);
});
it("can submit 10 sample questions", async () => {
const testTopic = "Test topic";
for (let i = 0; i < 10; ++i) {
let testContent = `this is the test content ${i}`;
let otherUser = anchor.web3.Keypair.generate();
let signature =
await program.provider.connection.requestAirdrop(
otherUser.publicKey,
10000000000
);
await program.provider.connection.confirmTransaction(signature);
await askQuestion(otherUser, testTopic, testContent);
}
const questionAccounts = await program.account.question.all([
{
memcmp: {
offset:
8 + // Discriminator.
32 + // Author public key.
8 + // Timestamp.
1 + // hasSolution
32 + // Solution public key
8 + // Amount
4, // Topic string prefix.
bytes: bs58.encode(Buffer.from(testTopic)),
},
},
]);
assert.equal(questionAccounts.length, 10);
});
});
});