-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.ts
956 lines (774 loc) · 38.5 KB
/
server.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
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
import express, { Request, Response } from 'express';
import cors from 'cors';
import { ethers } from "ethers";
import dotenv from "dotenv";
import mongoose from 'mongoose';
import Submission from './models/Submission'; // Import the Submission model
import UserVote from './models/UserVote'; // Import the UserVote model
import { keccak256, toUtf8Bytes } from 'ethers';
import { NonceManager } from 'ethers';
import LiveMarket from './models/LiveMarket'; // Import the LiveMarket model;
dotenv.config(); // Ensure this is called to load .env variables
const app = express();
const PORT = 3001;
app.use(cors());
app.use(express.json());
mongoose.connect(process.env.MONGO_URI!, {})
.then(() => { console.log('Connected to MongoDB'); })
.catch(err => { console.error('Failed to connect to MongoDB', err); });
interface Submission {
_id: string;
title: string;
question: string;
outcomes: string[];
source: string;
endTime: string;
votes: number;
}
interface LiveMarket extends Submission {
trading: {
yes: number;
no: number;
};
marketAddress: string; // Add this line
questionId: string;
isResolved: boolean; // Add this line
isRedeemed: boolean; // Add this line
}
let liveMarket: LiveMarket | null = null;
// Ethers.js setup
const provider = new ethers.JsonRpcProvider(`https://base-sepolia.g.alchemy.com/v2/${process.env.ALCHEMY_API_KEY}`);
const wallet = new ethers.Wallet(process.env.PRIVATE_KEY!, provider);
const managedSigner = new NonceManager(wallet); // Wrap the wallet with NonceManager
// get contract abis
const ERC20Artifact = require('./artifacts/@openzeppelin/contracts/token/ERC20/IERC20.sol/IERC20.json');
const ConditionalTokensArtifact = require('./artifacts/contracts/ConditionalTokens.sol/ConditionalTokens.json');
const FixedProductMarketMakerFactoryArtifact = require('./artifacts/contracts/FixedProductMarketMakerFactory.sol/FixedProductMarketMakerFactory.json');
const VotePowerArtifact = require('./artifacts/contracts/votepower.sol/VotePower.json'); // Import the JSON file
const FixedProductMarketMakerArtifact = require('./artifacts/contracts/FixedProductMarketMaker.sol/FixedProductMarketMaker.json');
const CTHelpersArtifact = require('./artifacts/contracts/dependencies/CTHelpers.sol/CTHelpers.json');
const collateralToken = new ethers.Contract(process.env.TOKEN_CONTRACT_ADDRESS!, ERC20Artifact.abi, managedSigner);
const factory = new ethers.Contract(
process.env.FIXED_PRODUCT_MARKET_MAKER_FACTORY_ADDRESS!,
FixedProductMarketMakerFactoryArtifact.abi,
managedSigner
);
const conditionalTokens = new ethers.Contract(
process.env.CONDITIONAL_TOKENS_CONTRACT_ADDRESS!,
ConditionalTokensArtifact.abi,
managedSigner
);
const votePowerContract = new ethers.Contract(
process.env.VOTE_POWER_CONTRACT_ADDRESS!,
VotePowerArtifact.abi,
managedSigner
);
const CTHelpers = new ethers.Contract(
process.env.CT_HELPERS_CONTRACT_ADDRESS!,
CTHelpersArtifact.abi,
managedSigner
);
// Initialize live market from the database
const initializeLiveMarket = async () => {
try {
const savedLiveMarket = await LiveMarket.findOne();
if (savedLiveMarket) {
liveMarket = savedLiveMarket.toObject();
console.log('Live market initialized:', liveMarket);
} else {
console.log('No live market found in database.');
}
} catch (error) {
console.error('Error initializing live market:', error);
}
};
const isSubmissionPeriod = (): boolean => {
const now = new Date();
const day = now.getUTCDay();
const hour = now.getUTCHours();
return day >= 0 && day <= 6 && hour >= 0 && hour <= 23;
};
const getVotePower = async (account: string): Promise<number> => {
try {
if (!ethers.isAddress(account)) {
throw new Error("Invalid Ethereum address");
}
console.log(`Getting vote power for account: ${account}`);
const votePower = await votePowerContract.getVotePower(account);
console.log(`Vote power for account ${account}: ${votePower.toString()}`);
return votePower.toString();
} catch (error) {
console.error("Error in getVotePower:", error);
throw error;
}
};
const resetVotes = async () => {
try {
await UserVote.updateMany({}, { hasVoted: false });
console.log("User votes have been reset.");
} catch (err) {
console.error("Error resetting user votes:", err);
}
};
// const findReturnAmount = async (fixedProductMarketMaker: any, outcomeIndex: number, outcomeTokensToSell: string): Promise<string> => {
// let lowerBound = 0n;
// let upperBound = BigInt("1000000000000000000"); // Start with a high upper bound in units of ether (1 ether)
// let mid: bigint;
// let calculatedOutcomeTokens: bigint;
// while (lowerBound < upperBound) {
// mid = (lowerBound + upperBound) / 2n;
// console.log('current mid:', mid);
// try {
// calculatedOutcomeTokens = BigInt((await fixedProductMarketMaker.calcSellAmount(mid, outcomeIndex)).toString());
// } catch (error) {
// console.error('Error in calcSellAmount:', error);
// upperBound = mid; // Reduce upper bound if error occurs
// continue;
// }
// const outcomeTokensParsedBigInt = ethers.parseUnits(outcomeTokensToSell, 18);
// // console.log(calculatedOutcomeTokens,outcomeTokensParsedBigInt)
// if (calculatedOutcomeTokens === outcomeTokensParsedBigInt) {
// return ethers.formatUnits(mid, 18);
// }
// if (calculatedOutcomeTokens < outcomeTokensParsedBigInt) {
// lowerBound = mid + 1n;
// } else {
// upperBound = mid;
// }
// }
// return ethers.formatUnits(lowerBound, 18);
// };
app.get('/api/submissions', async (req: Request, res: Response) => {
if (!isSubmissionPeriod()) {
return res.status(403).json({ message: 'Submission period is closed' });
}
try {
const submissions = await Submission.find();
res.json(submissions);
} catch (err) {
res.status(500).json({ message: 'Error fetching submissions', error: err });
}
});
app.post('/api/submissions', async (req: Request, res: Response) => {
if (!isSubmissionPeriod()) {
return res.status(403).json({ message: 'Submission period is closed' });
}
const { title, question, outcomes, source, endTime } = req.body;
const newSubmission = new Submission({
title,
question,
outcomes,
source,
endTime,
votes: 0
});
try {
const savedSubmission = await newSubmission.save();
res.status(201).json(savedSubmission);
} catch (err) {
res.status(500).json({ message: 'Error saving submission', error: err });
}
});
app.post('/api/vote', async (req: Request, res: Response) => {
if (!isSubmissionPeriod()) {
return res.status(403).json({ message: 'Voting period is closed' });
}
const { submissionId, walletAddress } = req.body;
console.log(`Vote request received for submissionId: ${submissionId}, walletAddress: ${walletAddress}`);
try {
const submission = await Submission.findById(submissionId);
if (!submission) {
return res.status(404).json({ message: "Submission not found" });
}
const userVote = await UserVote.findOne({ walletAddress });
if (userVote && userVote.hasVoted) {
return res.status(403).json({ message: 'You have already used your vote power for this period.' });
}
const votePower = await getVotePower(walletAddress);
submission.votes += Number(votePower);
await submission.save();
if (userVote) {
userVote.hasVoted = true;
await userVote.save();
} else {
const newUserVote = new UserVote({ walletAddress, hasVoted: true });
await newUserVote.save();
}
res.json({ message: "Vote recorded", submission });
} catch (error) {
res.status(500).json({ message: "Error fetching vote power", error });
}
});
app.get('/api/vote-power/:walletAddress', async (req: Request, res: Response) => {
const { walletAddress } = req.params;
try {
const votePower = await getVotePower(walletAddress);
res.json({ votePower });
} catch (error) {
console.error("Error fetching vote power:", error);
res.status(500).json({ message: "Error fetching vote power", error });
}
});
app.get('/api/winner', async (req: Request, res: Response) => {
try {
const submissions = await Submission.find();
if (submissions.length === 0) {
return res.status(404).json({ message: "No submissions available." });
}
const winner = submissions.reduce((a, b) => (a.votes > b.votes ? a : b));
res.json(winner);
} catch (err) {
res.status(500).json({ message: 'Error fetching winner', error: err });
}
});
app.post('/api/set-live-market', async (req: Request, res: Response) => {
try {
const submissions = await Submission.find();
if (submissions.length === 0) {
return res.status(404).json({ message: "No submissions available to set as a live market." });
}
const winningSubmission = submissions.reduce((prev, current) => (prev.votes > current.votes ? prev : current));
const uniqueString = `${(winningSubmission._id as string).toString()}-${Date.now()}`;
const questionId = ethers.keccak256(ethers.toUtf8Bytes(uniqueString));
const oracle = wallet.address;
const outcomeSlotCount = 2;
const newLiveMarket: LiveMarket = {
...winningSubmission.toObject(),
trading: {
yes: 0,
no: 0
},
marketAddress: '',
questionId: questionId,
isResolved: false, // Initialize as false
isRedeemed: false // Initialize as false
};
try {
const prepareConditionTx = await conditionalTokens.prepareCondition(oracle, questionId, outcomeSlotCount, {
gasLimit: 500000
});
await prepareConditionTx.wait();
} catch (error) {
return res.status(500).json({ message: 'Error preparing condition', error });
}
const conditionId = await conditionalTokens.getConditionId(oracle, questionId, outcomeSlotCount);
const outcomeSlotCountFromCondition = Number(await conditionalTokens.getOutcomeSlotCount(conditionId));
console.log('Condition ID:', conditionId);
console.log('Outcome Slot Count:', outcomeSlotCountFromCondition);
console.log('other outcomeslot', outcomeSlotCount);
console.log('Conditional Tokens Address:', conditionalTokens.target);
console.log('Collateral Token Address:', collateralToken.target);
const fee = ethers.parseUnits("0.0001", 18); // Example fee update
const createMarketTx = await factory.createFixedProductMarketMaker(
conditionalTokens.target,
collateralToken.target,
[conditionId],
fee
);
const createMarketReceipt = await createMarketTx.wait();
const iface = new ethers.Interface(FixedProductMarketMakerFactoryArtifact.abi);
const parsedLogs = createMarketReceipt.logs
.map((log: ethers.Log) => {
try {
return iface.parseLog(log);
} catch (e) {
return null;
}
})
.filter((log: ethers.LogDescription | null): log is ethers.LogDescription => log !== null && log.name === 'FixedProductMarketMakerCreation');
if (parsedLogs.length === 0) {
return res.status(500).json({ message: 'FixedProductMarketMakerCreation event not found' });
}
const fixedProductMarketMakerAddress = parsedLogs[0].args.fixedProductMarketMaker;
console.log('FixedProductMarketMaker Address:', fixedProductMarketMakerAddress);
newLiveMarket.marketAddress = fixedProductMarketMakerAddress;
await LiveMarket.deleteMany();
const savedLiveMarket = new LiveMarket(newLiveMarket);
await savedLiveMarket.save();
liveMarket = savedLiveMarket.toObject();
await Submission.deleteMany();
await resetVotes();
res.status(201).json(savedLiveMarket);
} catch (err) {
console.error('Error setting live market:', err);
res.status(500).json({ message: 'Error setting live market', error: err });
}
});
// Endpoint to get the current live market
app.get('/api/live-market', async (req: Request, res: Response) => {
try {
const liveMarket = await LiveMarket.findOne();
if (liveMarket) {
res.json(liveMarket);
} else {
res.status(404).json({ message: "No live market set" });
}
} catch (error) {
console.error('Error fetching live market:', error);
res.status(500).json({ message: 'Error fetching live market', error });
}
});
app.post('/api/add-liquidity', async (req: Request, res: Response) => {
const { amount } = req.body;
const liveMarket = await LiveMarket.findOne();
if (!liveMarket || liveMarket.isResolved) {
return res.status(400).json({ message: 'Market is resolved. Cannot add liquidity.' });
}
if (!liveMarket || !liveMarket.marketAddress) {
return res.status(400).json({ message: 'No live market available' });
}
try {
const amountParsed = ethers.parseUnits(amount, 18);
const approveTx = await collateralToken.approve(liveMarket.marketAddress, amountParsed);
await approveTx.wait();
console.log("Collateral tokens approved for adding liquidity:", approveTx.hash);
const fixedProductMarketMaker = new ethers.Contract(liveMarket.marketAddress, FixedProductMarketMakerArtifact.abi, managedSigner);
const addLiquidityTx = await fixedProductMarketMaker.addFunding(amountParsed, []);
await addLiquidityTx.wait();
console.log("Liquidity added:", addLiquidityTx.hash);
res.status(200).json({ message: 'Liquidity added', txHash: addLiquidityTx.hash });
} catch (error) {
console.error('Error adding liquidity:', error);
res.status(500).json({ message: 'Error adding liquidity', error });
}
});
app.post('/api/remove-liquidity', async (req: Request, res: Response) => {
const { amount } = req.body;
const liveMarket = await LiveMarket.findOne();
if (!liveMarket || !liveMarket.marketAddress) {
return res.status(400).json({ message: 'No live market available' });
}
try {
const amountParsed = ethers.parseUnits(amount, 18);
const fixedProductMarketMaker = new ethers.Contract(liveMarket.marketAddress, FixedProductMarketMakerArtifact.abi, managedSigner);
// Fetch outcome token balances
const poolBalances: ethers.BigNumberish[] = await fixedProductMarketMaker.getPoolBalances();
console.log("Pool Balances:", poolBalances);
// Check if there is any liquidity in the market
const totalLiquidity = poolBalances.reduce((acc: bigint, balance: ethers.BigNumberish) => acc + BigInt(balance.toString()), BigInt(0));
if (totalLiquidity === BigInt(0)) {
return res.status(400).json({ message: 'No liquidity in the market. Cannot remove liquidity.' });
}
const removeLiquidityTx = await fixedProductMarketMaker.removeFunding(amountParsed);
await removeLiquidityTx.wait();
console.log("Liquidity removed:", removeLiquidityTx.hash);
res.status(200).json({ message: 'Liquidity removed', txHash: removeLiquidityTx.hash });
} catch (error) {
console.error('Error removing liquidity:', error);
res.status(500).json({ message: 'Error removing liquidity', error });
}
});
app.post('/api/buy-outcome', async (req: Request, res: Response) => {
const { outcomeIndex, amount, minOutcomeTokensToBuy } = req.body;
const liveMarket = await LiveMarket.findOne();
if (!liveMarket || liveMarket.isResolved) {
return res.status(400).json({ message: 'Market is resolved. Cannot buy outcome shares.' });
}
if (!liveMarket || !liveMarket.marketAddress) {
return res.status(400).json({ message: 'No live market available' });
}
try {
const amountParsed = ethers.parseUnits(amount, 18);
const fixedProductMarketMaker = new ethers.Contract(liveMarket.marketAddress, FixedProductMarketMakerArtifact.abi, managedSigner);
// Fetch outcome token balances
const poolBalances: ethers.BigNumberish[] = await fixedProductMarketMaker.getPoolBalances();
console.log("Pool Balances:", poolBalances);
// Check if there is any liquidity in the market
const totalLiquidity = poolBalances.reduce((acc: bigint, balance: ethers.BigNumberish) => acc + BigInt(balance.toString()), BigInt(0));
if (totalLiquidity === BigInt(0)) {
return res.status(400).json({ message: 'No liquidity in the market. Cannot buy outcome shares.' });
}
const approveTx = await collateralToken.approve(liveMarket.marketAddress, amountParsed);
await approveTx.wait();
console.log("Collateral tokens approved for buying outcome:", approveTx.hash);
const buyOutcomeTx = await fixedProductMarketMaker.buy(amountParsed, outcomeIndex, minOutcomeTokensToBuy);
await buyOutcomeTx.wait();
console.log("Outcome shares bought:", buyOutcomeTx.hash);
res.status(200).json({ message: 'Outcome shares bought', txHash: buyOutcomeTx.hash });
} catch (error) {
console.error('Error buying outcome shares:', error);
res.status(500).json({ message: 'Error buying outcome shares', error });
}
});
app.post('/api/calc-sell-amount', async (req: Request, res: Response) => {
const { outcomeIndex, amount } = req.body;
const liveMarket = await LiveMarket.findOne();
if (!liveMarket || liveMarket.isRedeemed) {
return res.status(400).json({ message: 'Market is either not live or already redeemed. Cannot sell outcome shares.' });
}
if (!liveMarket || !liveMarket.marketAddress) {
return res.status(400).json({ message: 'No live market available' });
}
try {
const amountParsed = ethers.parseUnits(amount, 18);
const fixedProductMarketMaker = new ethers.Contract(liveMarket.marketAddress, FixedProductMarketMakerArtifact.abi, managedSigner);
// Fetch user's balance for the specific outcome token
const positionId = await fixedProductMarketMaker.positionIds(0);
const userBalance = await conditionalTokens.balanceOf(wallet.address, positionId);
console.log('userOutcometokens to sell', userBalance)
if (BigInt(userBalance) === BigInt(0)) {
return res.status(400).json({ message: 'You do not have any tokens to sell.', });
}
console.log('outcome',outcomeIndex)
console.log('final lowerbound proper', amountParsed)
const maxOutcomeTokensToSell = await fixedProductMarketMaker.calcSellAmount(amountParsed, outcomeIndex);
console.log('Max Outcome Tokens to Sell:', maxOutcomeTokensToSell.toString());
res.status(200).json({ maxOutcomeTokensToSell: maxOutcomeTokensToSell.toString() });
} catch (error) {
console.error('Error calculating sell amount:', error);
if (error instanceof Error) {
console.error('Error Message:', error.message);
const errorAny = error as any; // Explicitly cast to any for dynamic properties
if (errorAny.code) {
console.error('Error Code:', errorAny.code);
}
if (errorAny.transaction) {
console.error('Transaction Data:', errorAny.transaction);
}
if (errorAny.receipt) {
console.error('Transaction Receipt:', errorAny.receipt);
}
}
res.status(500).json({ message: 'Error calculating sell amount', error });
}
});
// app.post('/api/sell-outcome-true', async (req: Request, res: Response) => {
// const { outcomeIndex, outcomeTokensToSell } = req.body;
// const amount = '0.1'; // Hardcoded amount for now
// const liveMarket = await LiveMarket.findOne();
// if (!liveMarket || liveMarket.isRedeemed) {
// return res.status(400).json({ message: 'Market is either not live or already redeemed. Cannot sell outcome shares.' });
// }
// if (!liveMarket || !liveMarket.marketAddress) {
// return res.status(400).json({ message: 'No live market available' });
// }
// try {
// const fixedProductMarketMaker = new ethers.Contract(liveMarket.marketAddress, FixedProductMarketMakerArtifact.abi, managedSigner);
// // Fetch user's balance for the specific outcome token
// const positionId = await fixedProductMarketMaker.positionIds(0);
// const userBalance = await conditionalTokens.balanceOf(wallet.address, positionId);
// const outcomeTokensParsedBigInt = ethers.parseUnits(outcomeTokensToSell, 18);
// // console.log('userOutcometokens to sell', userBalance.toString());
// // console.log('outcomeTokensParsedBigInt', outcomeTokensParsedBigInt.toString());
// if (userBalance < outcomeTokensToSell) {
// return res.status(400).json({ message: 'You do not have enough tokens to sell.' });
// }
// // Calculate return amount using binary search
// const returnAmountString = await findReturnAmount(fixedProductMarketMaker, outcomeIndex, outcomeTokensToSell);
// const returnAmountParsed = ethers.parseUnits(returnAmountString, 18); // Convert back to BigInt for the contract call
// const amountParsed = ethers.parseUnits(amount, 18);
// // console.log('return amount to be returned:', returnAmountParsed.toString());
// // console.log('amount to be returned:', returnAmountParsed.toString());
// const approveERC1155Tx = await conditionalTokens.setApprovalForAll(liveMarket.marketAddress, true);
// await approveERC1155Tx.wait();
// console.log("ERC1155 tokens approved for FixedProductMçarketMaker:", approveERC1155Tx.hash);
// console.log('true',returnAmountParsed, outcomeIndex, outcomeTokensToSell)
// console.log('nottrue',amountParsed, outcomeIndex, outcomeTokensToSell)
// const sellOutcomeTx = await fixedProductMarketMaker.sell(returnAmountParsed, outcomeIndex, outcomeTokensToSell);
// await sellOutcomeTx.wait();
// console.log("Outcome shares sold:", sellOutcomeTx.hash);
// res.status(200).json({ message: 'Outcome shares sold', txHash: sellOutcomeTx.hash });
// } catch (error) {
// console.error('Error selling outcome shares:', error);
// if (error instanceof Error) {
// console.error('Error Message:', error.message);
// const errorAny = error as any; // Explicitly cast to any for dynamic properties
// if (errorAny.code) {
// console.error('Error Code:', errorAny.code);
// }
// if (errorAny.transaction) {
// console.error('Transaction Data:', errorAny.transaction);
// }
// if (errorAny.receipt) {
// console.error('Transaction Receipt:', errorAny.receipt);
// }
// }
// res.status(500).json({ message: 'Error selling outcome shares', error });
// }
// });
app.post('/api/sell-outcome', async (req: Request, res: Response) => {
const { outcomeIndex, amount, maxOutcomeTokensToSell } = req.body;
const liveMarket = await LiveMarket.findOne();
if (!liveMarket || liveMarket.isRedeemed) {
return res.status(400).json({ message: 'Market is either not live or already redeemed. Cannot sell outcome shares.'});
}
if (!liveMarket || !liveMarket.marketAddress) {
return res.status(400).json({ message: 'No live market available' });
}
try {
const amountParsed = ethers.parseUnits(amount, 18);
const fixedProductMarketMaker = new ethers.Contract(liveMarket.marketAddress, FixedProductMarketMakerArtifact.abi, managedSigner);
// Fetch user's balance for the specific outcome token
const positionId = await fixedProductMarketMaker.positionIds(0);
const userBalance = await conditionalTokens.balanceOf(wallet.address, positionId);
// console.log('userOutcometokens to sell', userBalance)
if (BigInt(userBalance) === BigInt(0)) {
return res.status(400).json({ message: 'You do not have any tokens to sell.' });
}
const approveERC1155Tx = await conditionalTokens.setApprovalForAll(liveMarket.marketAddress, true);
await approveERC1155Tx.wait();
console.log("ERC1155 tokens approved for FixedProductMarketMaker:", approveERC1155Tx.hash);
console.log('true',amountParsed, outcomeIndex, maxOutcomeTokensToSell)
const sellOutcomeTx = await fixedProductMarketMaker.sell(amountParsed, outcomeIndex, maxOutcomeTokensToSell);
await sellOutcomeTx.wait();
console.log("Outcome shares sold:", sellOutcomeTx.hash);
res.status(200).json({ message: 'Outcome shares sold', txHash: sellOutcomeTx.hash });
} catch (error) {
console.error('Error selling outcome shares:', error);
if (error instanceof Error) {
console.error('Error Message:', error.message);
const errorAny = error as any; // Explicitly cast to any for dynamic properties
if (errorAny.code) {
console.error('Error Code:', errorAny.code);
}
if (errorAny.transaction) {
console.error('Transaction Data:', errorAny.transaction);
}
if (errorAny.receipt) {
console.error('Transaction Receipt:', errorAny.receipt);
}
}
res.status(500).json({ message: 'Error selling outcome shares', error });
}
});
app.post('/api/resolve-condition', async (req: Request, res: Response) => {
const { payoutNumerators } = req.body;
if (!liveMarket || !liveMarket.marketAddress || !liveMarket.questionId) {
return res.status(400).json({ message: 'No live market available or question ID is missing' });
}
const fixedProductMarketMaker = new ethers.Contract(liveMarket.marketAddress, FixedProductMarketMakerArtifact.abi, managedSigner);
try {
// Check if the condition has already been resolved
const conditionId = await conditionalTokens.getConditionId(wallet.address, liveMarket.questionId, 2);
const payoutDenominator = await conditionalTokens.payoutDenominator(conditionId);
if (payoutDenominator.toString() !== '0') {
return res.status(400).json({ message: 'Condition has already been resolved' });
}
// Check tokens
const poolBalancesBefore = await fixedProductMarketMaker.getPoolBalances();
console.log('Market Pool Balances Before resolution:', poolBalancesBefore);
const collateralBalanceBefore = await collateralToken.balanceOf(liveMarket.marketAddress);
console.log('Market Collateral Balance Before resolution:', ethers.formatUnits(collateralBalanceBefore, 18));
const userCollateral = await collateralToken.balanceOf(wallet.address);
console.log("User ERC-20 Balance Before resolution:", ethers.formatUnits(userCollateral, 18));
const positionId = await fixedProductMarketMaker.positionIds(0);
const userOutcomeTokens = await conditionalTokens.balanceOf(wallet.address, positionId);
console.log("User Balance of ERC1155 outcome tokens Before resolution:", ethers.formatUnits(userOutcomeTokens, 18));
const preLiquidityBalance = await fixedProductMarketMaker.balanceOf(wallet.address);
console.log("User's liquidity token balance post resolution:", ethers.formatUnits(preLiquidityBalance, 18));
const preLiquidityInMarket = await fixedProductMarketMaker.balanceOf(wallet.address);
console.log("Market's liquidity token balance pre resolution:", ethers.formatUnits(preLiquidityInMarket, 18));
const resolveTx = await conditionalTokens.reportPayouts(liveMarket.questionId, payoutNumerators);
await resolveTx.wait();
console.log("Condition resolved:", resolveTx.hash);
// Update the live market to set isResolved to true
const updatedLiveMarket = await LiveMarket.findOneAndUpdate(
{ _id: liveMarket._id },
{ $set: { isResolved: true } },
{ new: true }
);
liveMarket = updatedLiveMarket?.toObject() ?? null;
//remove
if (!liveMarket || !liveMarket.marketAddress || !liveMarket.questionId) {
return res.status(400).json({ message: 'No live market available or question ID is missing' });
}
// Check tokens
const poolBalancesAfter = await fixedProductMarketMaker.getPoolBalances();
console.log('Market Pool Balances after resolution:', poolBalancesAfter);
const collateralBalanceAfter = await collateralToken.balanceOf(liveMarket.marketAddress);
console.log('Market Collateral Balance after resolution::', ethers.formatUnits(collateralBalanceAfter, 18));
const userCollateralAfter = await collateralToken.balanceOf(wallet.address);
console.log("User ERC-20 Balance after resolution::", ethers.formatUnits(userCollateralAfter, 18));
const positionIdAfter = await fixedProductMarketMaker.positionIds(0);
const userOutcomeTokensAfter = await conditionalTokens.balanceOf(wallet.address, positionIdAfter);
console.log("User Balance of ERC1155 outcome tokens after resolution::", ethers.formatUnits(userOutcomeTokensAfter, 18));
const postLiquidityBalance = await fixedProductMarketMaker.balanceOf(wallet.address);
console.log("User's liquidity token balance post resolution:", ethers.formatUnits(postLiquidityBalance, 18));
const postLiquidityInMarket = await fixedProductMarketMaker.balanceOf(wallet.address);
console.log("Market's liquidity token balance post resolution:", ethers.formatUnits(postLiquidityInMarket, 18));
res.status(200).json({ message: 'Condition resolved', txHash: resolveTx.hash });
} catch (error) {
console.error('Error resolving condition:', error);
res.status(500).json({ message: 'Error resolving condition', error });
}
});
app.post('/api/redeem-positions', async (req: Request, res: Response) => {
const { indexSets } = req.body;
if (!liveMarket || !liveMarket.marketAddress || !liveMarket.questionId) {
return res.status(400).json({ message: 'No live market available or question ID is missing' });
}
try {
const conditionId = await conditionalTokens.getConditionId(wallet.address, liveMarket.questionId, 2);
console.log('Condition ID for redemption:', conditionId);
const payoutDenominator = await conditionalTokens.payoutDenominator(conditionId);
if (payoutDenominator.toString() === '0') {
return res.status(400).json({ message: 'Condition has not been resolved yet' });
}
console.log('Payout Denominator:', payoutDenominator.toString());
const fixedProductMarketMaker = new ethers.Contract(liveMarket.marketAddress, FixedProductMarketMakerArtifact.abi, managedSigner);
// Check tokens
const poolBalancesBefore = await fixedProductMarketMaker.getPoolBalances();
console.log('Market Pool Balances Before Redemption:', poolBalancesBefore);
const collateralBalanceBefore = await collateralToken.balanceOf(liveMarket.marketAddress);
console.log('Market Collateral Balance Before Redemption:', ethers.formatUnits(collateralBalanceBefore, 18));
const userCollateral = await collateralToken.balanceOf(wallet.address);
console.log("User ERC-20 Balance Before Redemption:", ethers.formatUnits(userCollateral, 18));
const positionId = await fixedProductMarketMaker.positionIds(0);
const userOutcomeTokens = await conditionalTokens.balanceOf(wallet.address, positionId);
console.log("User Balance of ERC1155 outcome tokens Before Redemption:", ethers.formatUnits(userOutcomeTokens, 18));
const preLiquidityBalance = await fixedProductMarketMaker.balanceOf(wallet.address);
console.log("User's liquidity token balance post resolution:", ethers.formatUnits(preLiquidityBalance, 18));
const preLiquidityInMarket = await fixedProductMarketMaker.balanceOf(wallet.address);
console.log("Market's liquidity token balance pre resolution:", ethers.formatUnits(preLiquidityInMarket, 18));
const redeemTx = await conditionalTokens.redeemPositions(
collateralToken.target,
ethers.ZeroHash,
conditionId,
indexSets
);
await redeemTx.wait();
console.log("Positions redeemed:", redeemTx.hash);
// Update the live market to set isRedeemed to true
const updatedLiveMarket = await LiveMarket.findOneAndUpdate(
{ _id: liveMarket._id },
{ $set: { isRedeemed: true } },
{ new: true }
);
liveMarket = updatedLiveMarket?.toObject() ?? null;
console.log('Updated live market after redemption:', liveMarket);
//remove
if (!liveMarket || !liveMarket.marketAddress || !liveMarket.questionId) {
return res.status(400).json({ message: 'No live market available or question ID is missing' });
}
// Check tokens
const poolBalancesAfter = await fixedProductMarketMaker.getPoolBalances();
console.log('Market Pool Balances after Redemption:', poolBalancesAfter);
const collateralBalanceAfter = await collateralToken.balanceOf(liveMarket.marketAddress);
console.log('Market Collateral Balance after Redemption:', ethers.formatUnits(collateralBalanceAfter, 18));
const userCollateralAfter = await collateralToken.balanceOf(wallet.address);
console.log("User ERC-20 Balance after Redemption:", ethers.formatUnits(userCollateralAfter, 18));
const positionIdAfter = await fixedProductMarketMaker.positionIds(0);
const userOutcomeTokensAfter = await conditionalTokens.balanceOf(wallet.address, positionIdAfter);
console.log("User Balance of ERC1155 outcome tokens after Redemption:", ethers.formatUnits(userOutcomeTokensAfter, 18));
const postLiquidityBalance = await fixedProductMarketMaker.balanceOf(wallet.address);
console.log("User's liquidity token balance post resolution:", ethers.formatUnits(postLiquidityBalance, 18));
const postLiquidityInMarket = await fixedProductMarketMaker.balanceOf(wallet.address);
console.log("Market's liquidity token balance post resolution:", ethers.formatUnits(postLiquidityInMarket, 18));
res.status(200).json({ message: 'Positions redeemed', txHash: redeemTx.hash });
} catch (error) {
console.error('Error redeeming positions:', error);
if (error instanceof Error) {
console.error('Error Message:', error.message);
const errorAny = error as any;
if (errorAny.code) {
console.error('Error Code:', errorAny.code);
}
if (errorAny.transaction) {
console.error('Transaction Data:', errorAny.transaction);
}
if (errorAny.receipt) {
console.error('Transaction Receipt:', errorAny.receipt);
}
}
res.status(500).json({ message: 'Error redeeming positions', error });
}
});
app.get('/api/get-liquidity-and-prices', async (req: Request, res: Response) => {
if (!liveMarket || !liveMarket.marketAddress) {
return res.status(400).json({ message: 'No live market available' });
}
try {
const fixedProductMarketMaker = new ethers.Contract(liveMarket.marketAddress, FixedProductMarketMakerArtifact.abi, managedSigner);
// Fetch outcome token balances
const poolBalances: ethers.BigNumberish[] = await fixedProductMarketMaker.getPoolBalances();
console.log("Pool Balances:", poolBalances);
// Convert balances to BigInt
const poolBalancesBigInt = poolBalances.map((balance: ethers.BigNumberish) => BigInt(balance.toString()));
// Fetch collateral token balance
const collateralBalance = await collateralToken.balanceOf(liveMarket.marketAddress);
console.log("Collateral Token Balance:", ethers.formatUnits(collateralBalance, 18));
// Check for zero liquidity
const totalBalanceBigInt = poolBalancesBigInt.reduce((acc: bigint, balance: bigint) => acc + balance, BigInt(0));
if (totalBalanceBigInt === BigInt(0)) {
return res.status(200).json({
poolBalances: poolBalances.map((balance: ethers.BigNumberish) => balance.toString()),
collateralBalance: ethers.formatUnits(collateralBalance, 18), // Format to a readable number
prices: [], // No prices available due to zero liquidity
message: "No liquidity in the market."
});
}
// Calculate outcome token prices
const prices = poolBalancesBigInt.map((balance: bigint) => (balance * 10n ** 18n) / totalBalanceBigInt);
res.status(200).json({
poolBalances: poolBalances.map((balance: any) => balance.toString()),
collateralBalance: ethers.formatUnits(collateralBalance, 18), // Format to a readable number
prices: prices.map((price) => ethers.formatUnits(price.toString(), 18)) // Format prices to readable numbers
});
} catch (error) {
console.error('Error fetching current liquidity:', error);
res.status(500).json({ message: 'Error fetching current liquidity', error });
}
});
app.get('/api/fees', async (req: Request, res: Response) => {
const { account } = req.query;
if (!account || typeof account !== 'string') {
return res.status(400).json({ message: 'Account address is required and must be a string' });
}
if (!liveMarket || !liveMarket.marketAddress) {
return res.status(400).json({ message: 'No live market available' });
}
try {
const fixedProductMarketMaker = new ethers.Contract(liveMarket.marketAddress, FixedProductMarketMakerArtifact.abi, managedSigner);
const collectedFees = await fixedProductMarketMaker.collectedFees();
console.log("Collected Fees:", ethers.formatUnits(collectedFees, 18));
if (BigInt(collectedFees) === BigInt(0)) {
return res.status(200).json({ message: 'No collected fees', collectedFees: '0', withdrawableFees: '0' });
}
const withdrawableFees = await fixedProductMarketMaker.feesWithdrawableBy(account);
console.log("Withdrawable Fees for user:", ethers.formatUnits(withdrawableFees, 18));
if (BigInt(withdrawableFees) === BigInt(0)) {
return res.status(200).json({ message: 'No fees available for withdrawal', collectedFees: '0', withdrawableFees: '0' });
}
res.status(200).json({
collectedFees: ethers.formatUnits(collectedFees, 18),
withdrawableFees: ethers.formatUnits(withdrawableFees, 18),
});
} catch (error) {
console.error('Error reading fees:', error);
res.status(500).json({ message: 'Error reading fees', error });
}
});
app.post('/api/withdraw-fees', async (req: Request, res: Response) => {
const { account } = req.body;
if (!account || typeof account !== 'string') {
return res.status(400).json({ message: 'Account address is required and must be a string' });
}
if (!liveMarket || !liveMarket.marketAddress) {
return res.status(400).json({ message: 'No live market available' });
}
try {
const fixedProductMarketMaker = new ethers.Contract(liveMarket.marketAddress, FixedProductMarketMakerArtifact.abi, managedSigner);
// check fees available for collection
const collectedFees = await fixedProductMarketMaker.collectedFees();
console.log("Collected Fees:", ethers.formatUnits(collectedFees, 18));
if (BigInt(collectedFees) === BigInt(0)) {
return res.status(200).json({ message: 'No collected fees or fees withdrawn', collectedFees: '0', withdrawableFees: '0' });
}
const withdrawableFees = await fixedProductMarketMaker.feesWithdrawableBy(account);
console.log("Withdrawable Fees for user:", ethers.formatUnits(withdrawableFees, 18));
if (BigInt(withdrawableFees) === BigInt(0)) {
return res.status(200).json({ message: 'No fees available for withdrawal', collectedFees: '0', withdrawableFees: '0' });
}
const withdrawFeesTx = await fixedProductMarketMaker.withdrawFees(account);
await withdrawFeesTx.wait();
console.log("Fees withdrawn:", withdrawFeesTx.hash);
res.status(200).json({ message: 'Fees withdrawn', txHash: withdrawFeesTx.hash });
} catch (error) {
console.error('Error withdrawing fees:', error);
res.status(500).json({ message: 'Error withdrawing fees', error });
}
});
app.listen(PORT, async () => {
await initializeLiveMarket();
console.log(`Server running on port ${PORT}`);
});