forked from qubic/qubic-cli
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathqpool.cpp
More file actions
2045 lines (1874 loc) · 73.7 KB
/
qpool.cpp
File metadata and controls
2045 lines (1874 loc) · 73.7 KB
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
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "qpool.h"
#include "keyUtils.h"
#include "structs.h"
#include "logger.h"
#include "nodeUtils.h"
#include "K12AndKeyUtil.h"
#include "connection.h"
#include "walletUtils.h"
#include <vector>
#include <string>
#include <sstream>
#include <fstream>
#include <cstring>
#include "stdio.h"
#define QPOOL_ADDRESS "GAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAQGNM"
#define QPOOL_CREATE_FN 1
#define QPOOL_GET_LIST_OF_POOL_FN 1
#define QPOOL_ISSUE_ASSET 2
#define QPOOL_ENABLE_TOKEN 3
#define QPOOL_SWAP 4
#define QPOOL_BIGNUMBERTO_STRING 10
#define QPOOL_BIGSTRINGTO_NUMBER 11
#define QPOOL_BIGPLUS 12
#define QPOOL_BIGMINUS 13
#define QPOOL_BIGMULTI 14
#define QPOOL_BIGDIV 15
#define QPOOL_BIGMODULUS 16
#define QPOOL_BIGOREQUALCOMPARSIN 17
#define QPOOL_SMALLOREQUALCOMPARISON 18
#define QPOOL_BIGCOMPARISON 19
#define QPOOL_SMALLCOMPARISON 20
#define QPOOL_DEPOSIT_EXPENSIVE_TOKEN 21
#define QPOOL_WITHDRAW_EXPENSIVE_TOKEN 22
#define FEE_CREATE_POOL 100000000LL
#define TOKEN_TRANSER_FEE 1000LL // Amount of qus
#define QPOOL_EXPENSIVE_TOKEN_DEPOSIT_FEE 10000LL
#define QPOOL_EXPENSIVE_TOKEN_WITHDRAW_FEE 10000LL
constexpr int QPOOL_CONTRACT_ID = 7;
enum qPoolFunctionId{
GetNumberOfEnableToken = 1,
GetEnableToken = 2,
PoolList = 3,
GetValueOfToken = 4,
};
struct CreateLiquidityPool_input {
uint64_t NameOfLPToken; // Name of LP token
uint64_t swapFee; // Swap fee in a Pool
uint64_t initialAmountOfQWALLET;
uint64_t initialAmountOfQU;
uint64_t initialAmount1;
uint64_t initialAmount2;
uint64_t initialAmount3;
uint64_t initialAmount4;
uint32_t initialAmountForMicrotoken1;
uint32_t initialAmountForMicrotoken2;
uint32_t initialAmountForMicrotoken3;
uint32_t initialAmountForMicrotoken4;
uint16_t IndexOfToken1;
uint16_t IndexOfToken2;
uint16_t IndexOfToken3;
uint16_t IndexOfToken4;
uint8_t NumberOfToken; // Number(maximum 5) of token in a pool
uint8_t WeightOfQWALLET;
uint8_t Weight1;
uint8_t Weight2;
uint8_t Weight3;
uint8_t Weight4;
bool TypeOfToken1; // General token or Expensive token
bool TypeOfToken2;
bool TypeOfToken3;
bool TypeOfToken4;
};
struct CreateLiquidityPool_output {
uint64_t poolAddress;; // created pool address
};
struct IssueAsset_input {
uint64_t name;
int64_t numberOfUnits;
uint64_t unitOfMeasurement;
char numberOfDecimalPlaces;
};
struct IssueAsset_output
{
uint64_t issuedNumberOfShares;
};
struct PoolList_input {
uint32_t NumberOfPool;
};
struct EnableToken_input
{
uint8_t issuer[32];
uint64_t assetName;
uint16_t contractIndex;
bool TypeOfToken; // Expensive token or general token
};
struct qpoolenableToken_output
{
uint16_t TokenID;
};
struct GetNumberOfEnableToken_input
{
};
struct GetNumberOfEnableToken_output
{
uint16_t NumberOfEnabledGeneralToken;
uint16_t NumberOfEnabledExpensiveToken;
};
struct GetEnableToken_input
{
uint32_t NumberOfToken;
bool TypeOfToken; //General or Expensive token
};
struct GetEnableToken_output
{
uint64_t assetName;
uint16_t contractIndex;
uint8_t issuer[32];
};
struct Swap_input {
uint64_t AmountOfToken1;
uint16_t IndexOfToken1;
uint16_t IndexOfToken2;
uint16_t Poolnum;
bool TypeOfToken1;
bool TypeOfToken2;
};
struct Swap_output {
uint64_t AmountOfToken2;
};
struct PoolList_output {
uint64_t NameOfLPToken; // Name of LP token
uint64_t swapFee; // Swap fee in a Pool
uint64_t liquidityOfQWALLET;
uint64_t liquidityOfQU;
uint64_t liquidity1;
uint64_t liquidity2;
uint64_t liquidity3;
uint64_t liquidity4;
uint64_t totalAmountOfQPT;
uint32_t liquidityForMicrotoken1;
uint32_t liquidityForMicrotoken2;
uint32_t liquidityForMicrotoken3;
uint32_t liquidityForMicrotoken4;
uint16_t IndexOfToken1;
uint16_t IndexOfToken2;
uint16_t IndexOfToken3;
uint16_t IndexOfToken4;
uint8_t NumberOfToken; // Number(maximum 5) of token in a pool
uint8_t WeightOfQWALLET;
uint8_t Weight1;
uint8_t Weight2;
uint8_t Weight3;
uint8_t Weight4;
bool TypeOfToken1;
bool TypeOfToken2;
bool TypeOfToken3;
bool TypeOfToken4;
};
struct GetValueOfToken_input {
uint16_t IndexOfToken;
uint16_t Poolnum;
};
struct GetValueOfToken_output {
uint64_t ValueOfToken;
};
struct BIGNumberToString_input {
int64_t a;
};
struct BIGNumberToString_output {
uint8_t len;
uint8_t result[128];
};
struct GetBIGStatus_input {
};
struct GetBIGStatus_output {
int64_t BIGTest;
uint8_t BIGStringNumberLen;
uint8_t BIGStringNumber[128];
bool BIGTestComparisonResult;
};
struct BIGStringToNumber_input {
uint8_t len;
uint8_t a[128];
};
struct BIGStringToNumber_output {
int64_t result;
};
struct BIGPlus_input {
uint8_t alen;
uint8_t blen;
uint8_t a[128];
uint8_t b[128];
};
struct BIGPlus_output {
uint8_t resultlen;
uint8_t result[128];
};
struct BIGMinus_input {
uint8_t alen;
uint8_t blen;
uint8_t a[128];
uint8_t b[128];
};
struct BIGMinus_output {
uint8_t resultlen;
uint8_t result[128];
};
struct BIGMultiply_input {
uint8_t alen;
uint8_t blen;
uint8_t a[128];
uint8_t b[128];
};
struct BIGMultiply_output {
uint8_t resultlen;
uint8_t result[128];
};
struct BIGDiv_input {
uint8_t alen;
uint8_t blen;
uint8_t a[128];
uint8_t b[128];
};
struct BIGDiv_output {
uint8_t resultlen;
uint8_t result[128];
};
struct BIGModulus_input {
uint8_t alen;
uint8_t blen;
uint8_t a[128];
uint8_t b[128];
};
struct BIGModulus_output {
uint8_t resultlen;
uint8_t result[128];
};
struct BIGBigOrEqualComparison_input {
uint8_t alen;
uint8_t blen;
uint8_t a[128];
uint8_t b[128];
};
struct BIGBigOrEqualComparison_output {
bool result;
};
struct BIGSmallOrEqualComparison_input {
uint8_t alen;
uint8_t blen;
uint8_t a[128];
uint8_t b[128];
};
struct BIGSmallOrEqualComparison_output {
bool result;
};
struct BIGBigComparison_input {
uint8_t alen;
uint8_t blen;
uint8_t a[128];
uint8_t b[128];
};
struct BIGBigComparison_output {
bool result;
};
struct BIGSmallComparison_input {
uint8_t alen;
uint8_t blen;
uint8_t a[128];
uint8_t b[128];
};
struct BIGSmallComparison_output {
bool result;
};
struct DepositExpensivetoken_input
{
uint64_t AmountOfToken;
uint16_t IndexOfToken;
};
struct DepositExpensivetoken_output
{
};
struct WithdrawExpensivetoken_input
{
uint64_t AmountOfToken;
uint16_t IndexOfToken;
};
struct WithdrawExpensivetoken_output
{
};
struct GetAmountOfExpensivetokenUserDeposited_input
{
uint8_t user[32];
uint32_t IndexOfToken;
};
struct GetAmountOfExpensivetokenUserDeposited_output
{
uint64_t AmountOfExpensiveTokenUserProvided; // Amount of Expensive Token User provides in the Qpool
uint64_t TokenNameOfExpensiveTokenUserProvided; // Token name of Expensive Token User provides in the Qpool
uint32_t AmountOfMicrotokenOfUser; // Amount of MicrotokenUser has in the Qpool. less than 1M
};
void QpoolCreate(const char* nodeIp, int nodePort,
const char* seed,
char* nameOfLP,
int64_t number_of_token,
int64_t amountOfQwallet,
int8_t weightOfQwallet,
int64_t amountOfQu,
int16_t* index_of_token,
int64_t* amount_of_token,
int8_t* weight_of_token,
int32_t* amount_of_microtoken,
bool* type_of_token,
int64_t swap_fee,
uint32_t offsetScheduledTick)
{
char nameOfLPS1[8][8] = {0};
auto qc = make_qc(nodeIp, nodePort);
memcpy(nameOfLPS1, nameOfLP, strlen(nameOfLP));
printf("\n1\n");
uint8_t privateKey[32] = {0};
uint8_t sourcePublicKey[32] = {0};
uint8_t destPublicKey[32] = {0};
uint8_t subSeed[32] = {0};
uint8_t digest[32] = {0};
uint8_t signature[64] = {0};
char txHash[128] = {0};
getSubseedFromSeed((uint8_t*)seed, subSeed);
getPrivateKeyFromSubSeed(subSeed, privateKey);
getPublicKeyFromPrivateKey(privateKey, sourcePublicKey);
getPublicKeyFromIdentity(QPOOL_ADDRESS, destPublicKey);
((uint64_t*)destPublicKey)[0] = QPOOL_CONTRACT_ID;
((uint64_t*)destPublicKey)[1] = 0;
((uint64_t*)destPublicKey)[2] = 0;
((uint64_t*)destPublicKey)[3] = 0;
printf("2\n");
struct {
RequestResponseHeader header;
Transaction transaction;
CreateLiquidityPool_input ia;
uint8_t sig[SIGNATURE_SIZE];
} packet;
printf("3\n");
memcpy(packet.transaction.sourcePublicKey, sourcePublicKey, 32);
memcpy(packet.transaction.destinationPublicKey, destPublicKey, 32);
packet.transaction.amount = amountOfQu + TOKEN_TRANSER_FEE * (number_of_token - 1) + FEE_CREATE_POOL;
printf("4 %llu\n", packet.transaction.amount);
uint32_t scheduledTick = 0;
if (offsetScheduledTick < 50000){
uint32_t currentTick = getTickNumberFromNode(qc);
scheduledTick = currentTick + offsetScheduledTick;
} else {
scheduledTick = offsetScheduledTick;
}
printf("5\n");
packet.transaction.tick = scheduledTick;
packet.transaction.inputType = QPOOL_CREATE_FN;
packet.transaction.inputSize = sizeof(CreateLiquidityPool_input);
printf("6\n");
// fill the input
memcpy(&packet.ia.NameOfLPToken, nameOfLPS1, 8);
packet.ia.NumberOfToken = number_of_token;
printf("\n%u\n", packet.ia.NumberOfToken);
packet.ia.initialAmountOfQWALLET = amountOfQwallet;
packet.ia.WeightOfQWALLET = weightOfQwallet;
packet.ia.initialAmountOfQU = amountOfQu;
if(number_of_token > 2) {
packet.ia.IndexOfToken1 = index_of_token[0];
packet.ia.initialAmount1 = amount_of_token[0];
packet.ia.Weight1 = weight_of_token[0];
packet.ia.initialAmountForMicrotoken1 = amount_of_microtoken[0];
packet.ia.TypeOfToken1 = type_of_token[0];
}
if(number_of_token > 3) {
packet.ia.IndexOfToken2 = index_of_token[1];
packet.ia.initialAmount2 = amount_of_token[1];
packet.ia.Weight2 = weight_of_token[1];
packet.ia.initialAmountForMicrotoken1 = amount_of_microtoken[1];
packet.ia.TypeOfToken1 = type_of_token[1];
}
if(number_of_token > 4) {
packet.ia.IndexOfToken3 = index_of_token[2];
packet.ia.initialAmount3 = amount_of_token[2];
packet.ia.Weight3 = weight_of_token[2];
packet.ia.initialAmountForMicrotoken1 = amount_of_microtoken[2];
packet.ia.TypeOfToken1 = type_of_token[2];
}
if(number_of_token > 5) {
packet.ia.IndexOfToken4 = index_of_token[3];
packet.ia.initialAmount4 = amount_of_token[3];
packet.ia.Weight4 = weight_of_token[3];
packet.ia.initialAmountForMicrotoken1 = amount_of_microtoken[3];
packet.ia.TypeOfToken1 = type_of_token[3];
}
packet.ia.swapFee = swap_fee;
printf("%lu\n", packet.ia.swapFee);
printf("%lu %u %u %u %u\n", packet.ia.initialAmount1, packet.ia.IndexOfToken1, packet.ia.Weight1, packet.ia.initialAmountForMicrotoken1, packet.ia.TypeOfToken1);
printf("%lu %u %u %u %u\n", packet.ia.initialAmount2, packet.ia.IndexOfToken2, packet.ia.Weight2, packet.ia.initialAmountForMicrotoken2, packet.ia.TypeOfToken2);
printf("%lu %u %u %u %u\n", packet.ia.initialAmount3, packet.ia.IndexOfToken3, packet.ia.Weight3, packet.ia.initialAmountForMicrotoken3, packet.ia.TypeOfToken3);
printf("%lu %u %u %u %u\n", packet.ia.initialAmount4, packet.ia.IndexOfToken4, packet.ia.Weight4, packet.ia.initialAmountForMicrotoken4, packet.ia.TypeOfToken4);
// sign the packet
KangarooTwelve((unsigned char*)&packet.transaction,
sizeof(Transaction) + sizeof(CreateLiquidityPool_input),
digest,
32);
sign(subSeed, sourcePublicKey, digest, signature);
memcpy(packet.sig, signature, SIGNATURE_SIZE);
// set header
packet.header.setSize(sizeof(packet.header)+sizeof(Transaction)+sizeof(CreateLiquidityPool_input)+ SIGNATURE_SIZE);
packet.header.zeroDejavu();
packet.header.setType(BROADCAST_TRANSACTION);
qc->sendData((uint8_t *) &packet, packet.header.size());
KangarooTwelve((unsigned char*)&packet.transaction,
sizeof(Transaction)+sizeof(CreateLiquidityPool_input)+ SIGNATURE_SIZE,
digest,
32); // recompute digest for txhash
getTxHashFromDigest(digest, txHash);
LOG("Transaction has been sent!\n");
printReceipt(packet.transaction, txHash, reinterpret_cast<const uint8_t *>(&packet.ia));
LOG("run ./qubic-cli [...] -checktxontick %u %s\n", scheduledTick, txHash);
LOG("to check your tx confirmation status\n");
}
void qpoolIssueAsset(const char* nodeIp, int nodePort,
const char* seed,
const char* assetName,
const char* unitOfMeasurement,
int64_t numberOfUnits,
char numberOfDecimalPlaces,
uint32_t scheduledTickOffset)
{
auto qc = make_qc(nodeIp, nodePort);
char assetNameS1[8] = {0};
char UoMS1[8] = {0};
memcpy(assetNameS1, assetName, strlen(assetName));
for (int i = 0; i < 7; i++) UoMS1[i] = unitOfMeasurement[i] - 48;
uint8_t privateKey[32] = {0};
uint8_t sourcePublicKey[32] = {0};
uint8_t destPublicKey[32] = {0};
uint8_t subSeed[32] = {0};
uint8_t digest[32] = {0};
uint8_t signature[64] = {0};
char txHash[128] = {0};
getSubseedFromSeed((uint8_t*)seed, subSeed);
getPrivateKeyFromSubSeed(subSeed, privateKey);
getPublicKeyFromPrivateKey(privateKey, sourcePublicKey);
getPublicKeyFromIdentity(QPOOL_ADDRESS, destPublicKey);
((uint64_t*)destPublicKey)[0] = QPOOL_CONTRACT_ID;
((uint64_t*)destPublicKey)[1] = 0;
((uint64_t*)destPublicKey)[2] = 0;
((uint64_t*)destPublicKey)[3] = 0;
struct {
RequestResponseHeader header;
Transaction transaction;
IssueAsset_input ia;
uint8_t sig[SIGNATURE_SIZE];
} packet;
memcpy(packet.transaction.sourcePublicKey, sourcePublicKey, 32);
memcpy(packet.transaction.destinationPublicKey, destPublicKey, 32);
packet.transaction.amount = 1000000000;
uint32_t scheduledTick = 0;
if (scheduledTickOffset < 50000){
uint32_t currentTick = getTickNumberFromNode(qc);
scheduledTick = currentTick + scheduledTickOffset;
} else {
scheduledTick = scheduledTickOffset;
}
packet.transaction.tick = scheduledTick;
packet.transaction.inputType = QPOOL_ISSUE_ASSET;
packet.transaction.inputSize = sizeof(IssueAsset_input);
// fill the input
memcpy(&packet.ia.name, assetNameS1, 8);
memcpy(&packet.ia.unitOfMeasurement, UoMS1, 8);
packet.ia.numberOfUnits = numberOfUnits;
packet.ia.numberOfDecimalPlaces = numberOfDecimalPlaces;
// sign the packet
KangarooTwelve((unsigned char*)&packet.transaction,
sizeof(Transaction) + sizeof(IssueAsset_input),
digest,
32);
sign(subSeed, sourcePublicKey, digest, signature);
memcpy(packet.sig, signature, SIGNATURE_SIZE);
// set header
packet.header.setSize(sizeof(packet.header)+sizeof(Transaction)+sizeof(IssueAsset_input)+ SIGNATURE_SIZE);
packet.header.zeroDejavu();
packet.header.setType(BROADCAST_TRANSACTION);
qc->sendData((uint8_t *) &packet, packet.header.size());
KangarooTwelve((unsigned char*)&packet.transaction,
sizeof(Transaction)+sizeof(IssueAsset_input)+ SIGNATURE_SIZE,
digest,
32); // recompute digest for txhash
getTxHashFromDigest(digest, txHash);
LOG("Transaction has been sent!\n");
printReceipt(packet.transaction, txHash, reinterpret_cast<const uint8_t *>(&packet.ia));
LOG("run ./qubic-cli [...] -checktxontick %u %s\n", scheduledTick, txHash);
LOG("to check your tx confirmation status\n");
}
void getpool(QCPtr qc, uint32_t numberOfPool, PoolList_output& result)
{
struct {
RequestResponseHeader header;
RequestContractFunction rcf;
PoolList_input input;
} packet;
packet.header.setSize(sizeof(packet));
packet.header.randomizeDejavu();
packet.header.setType(RequestContractFunction::type());
packet.rcf.inputSize = sizeof(PoolList_input);
packet.rcf.inputType = qPoolFunctionId::PoolList;
packet.rcf.contractIndex = QPOOL_CONTRACT_ID;
packet.input.NumberOfPool = numberOfPool;
qc->sendData((uint8_t *) &packet, packet.header.size());
std::vector<uint8_t> buffer;
qc->receiveDataAll(buffer);
uint8_t* data = buffer.data();
int recvByte = buffer.size();
int ptr = 0;
while (ptr < recvByte)
{
auto header = (RequestResponseHeader*)(data+ptr);
if (header->type() == RespondContractFunction::type()){
auto oup = (PoolList_output*)(data + ptr + sizeof(RequestResponseHeader));
result = *oup;
}
ptr+= header->size();
}
}
void qpoolgetInfor(const char* nodeIp, int nodePort,
const char* seed,
uint32_t number_of_pool,
uint32_t scheduledTickOffset
) {
auto qc = make_qc(nodeIp, nodePort);
PoolList_output pool;
memset(&pool, 0, sizeof(PoolList_output));
getpool(qc, number_of_pool, pool);
LOG("number of token: %llu\n", pool.NameOfLPToken);
LOG("number of token: %u\n", pool.NumberOfToken);
char issueroftoken[128] = {0};
LOG("liquidity of QWALLET: %llu\n", pool.liquidityOfQWALLET);
LOG("Weight of QWALLET: %u\n", pool.WeightOfQWALLET);
LOG("liquidity of QU: %llu\n", pool.liquidityOfQU);
if(pool.NumberOfToken > 2) {
LOG("Address of token1: %u ", pool.IndexOfToken1);
LOG("Weight of token1: %u ", pool.Weight1);
LOG("liquidity of token1: %llu\n", pool.liquidity1);
LOG("liquidity of Microtoken for token1: %u\n", pool.liquidityForMicrotoken1);
LOG("type of token1: %u\n", pool.TypeOfToken1);
}
if(pool.NumberOfToken > 3) {
LOG("Address of token2: %u ", pool.IndexOfToken2);
LOG("Weight of token2: %u ", pool.Weight2);
LOG("liquidity of token2: %llu\n", pool.liquidity2);
LOG("liquidity of Microtoken for token2: %u\n", pool.liquidityForMicrotoken2);
LOG("type of token2: %u\n", pool.TypeOfToken2);
}
if(pool.NumberOfToken > 4) {
LOG("Address of token3: %u ", pool.IndexOfToken3);
LOG("Weight of token3: %u ", pool.Weight3);
LOG("liquidity of token3: %llu\n", pool.liquidity3);
LOG("liquidity of Microtoken for token3: %u\n", pool.liquidityForMicrotoken3);
LOG("type of token3: %u\n", pool.TypeOfToken3);
}
if(pool.NumberOfToken > 5) {
LOG("Address of token4: %u ", pool.IndexOfToken4);
LOG("Weight of token4: %u ", pool.Weight4);
LOG("liquidity of token4: %llu\n", pool.liquidity4);
LOG("liquidity of Microtoken for token4: %u\n", pool.liquidityForMicrotoken4);
LOG("type of token4: %u\n", pool.TypeOfToken4);
}
LOG("Swap fee in pool%u: %llu\n",number_of_pool+1, pool.swapFee);
LOG("Total Amount Of QPT in pool%u: %llu\n", number_of_pool+1 ,pool.totalAmountOfQPT);
}
void qpoolenableToken(const char* nodeIp, int nodePort,
const char* seed,
char* assetname,
char* issuerOfAsset,
uint16_t contractIndex,
bool typeoftoken,
uint32_t scheduledTickOffset)
{
auto qc = make_qc(nodeIp, nodePort);
char assetNameS1[8] = {0};
memcpy(assetNameS1, assetname, strlen(assetname));
uint8_t privateKey[32] = {0};
uint8_t sourcePublicKey[32] = {0};
uint8_t destPublicKey[32] = {0};
uint8_t subSeed[32] = {0};
uint8_t digest[32] = {0};
uint8_t signature[64] = {0};
uint8_t TokenIssuerPublicKey[32] = {0};
char txHash[128] = {0};
if (strlen(issuerOfAsset) != 60){
LOG("WARNING: Stop supporting hex format, please use qubic format 60-char length addresses\n");
exit(0);
}
getPublicKeyFromIdentity(issuerOfAsset, TokenIssuerPublicKey);
getSubseedFromSeed((uint8_t*)seed, subSeed);
getPrivateKeyFromSubSeed(subSeed, privateKey);
getPublicKeyFromPrivateKey(privateKey, sourcePublicKey);
getPublicKeyFromIdentity(QPOOL_ADDRESS, destPublicKey);
((uint64_t*)destPublicKey)[0] = QPOOL_CONTRACT_ID;
((uint64_t*)destPublicKey)[1] = 0;
((uint64_t*)destPublicKey)[2] = 0;
((uint64_t*)destPublicKey)[3] = 0;
struct {
RequestResponseHeader header;
Transaction transaction;
EnableToken_input ia;
uint8_t sig[SIGNATURE_SIZE];
} packet;
memcpy(packet.transaction.sourcePublicKey, sourcePublicKey, 32);
memcpy(packet.transaction.destinationPublicKey, destPublicKey, 32);
packet.transaction.amount = 100000000;
uint32_t scheduledTick = 0;
if (scheduledTickOffset < 50000){
uint32_t currentTick = getTickNumberFromNode(qc);
scheduledTick = currentTick + scheduledTickOffset;
} else {
scheduledTick = scheduledTickOffset;
}
packet.transaction.tick = scheduledTick;
packet.transaction.inputType = QPOOL_ENABLE_TOKEN;
packet.transaction.inputSize = sizeof(EnableToken_input);
// fill the input
memcpy(&packet.ia.assetName, assetNameS1, 8);
memcpy(packet.ia.issuer , TokenIssuerPublicKey, 32);
packet.ia.contractIndex = contractIndex;
packet.ia.TypeOfToken = typeoftoken;
for(int16_t i = 0 ; i < 32 ; i++) printf("%u ", packet.ia.issuer[i]);
printf("\n%llu\n", packet.ia.assetName);
printf("%u\n", packet.ia.contractIndex);
printf("%u ", packet.ia.TypeOfToken);
// sign the packet
KangarooTwelve((unsigned char*)&packet.transaction,
sizeof(Transaction) + sizeof(EnableToken_input),
digest,
32);
sign(subSeed, sourcePublicKey, digest, signature);
memcpy(packet.sig, signature, SIGNATURE_SIZE);
// set header
packet.header.setSize(sizeof(packet.header)+sizeof(Transaction)+sizeof(EnableToken_input)+ SIGNATURE_SIZE);
packet.header.zeroDejavu();
packet.header.setType(BROADCAST_TRANSACTION);
qc->sendData((uint8_t *) &packet, packet.header.size());
KangarooTwelve((unsigned char*)&packet.transaction,
sizeof(Transaction)+sizeof(EnableToken_input)+ SIGNATURE_SIZE,
digest,
32); // recompute digest for txhash
getTxHashFromDigest(digest, txHash);
LOG("Transaction has been sent!\n");
printReceipt(packet.transaction, txHash, reinterpret_cast<const uint8_t *>(&packet.ia));
LOG("run ./qubic-cli [...] -checktxontick %u %s\n", scheduledTick, txHash);
LOG("to check your tx confirmation status\n");
}
void qpoolgetnumberofenableToken(const char* nodeIp, int nodePort,
const char* seed,
uint32_t scheduledTickOffset
) {
auto qc = make_qc(nodeIp, nodePort);
struct {
RequestResponseHeader header;
RequestContractFunction rcf;
} packet;
packet.header.setSize(sizeof(packet));
packet.header.randomizeDejavu();
packet.header.setType(RequestContractFunction::type());
packet.rcf.inputSize = 0;
packet.rcf.inputType = qPoolFunctionId::GetNumberOfEnableToken;
packet.rcf.contractIndex = QPOOL_CONTRACT_ID;
qc->sendData((uint8_t *) &packet, packet.header.size());
std::vector<uint8_t> buffer;
qc->receiveDataAll(buffer);
uint8_t* data = buffer.data();
int recvByte = buffer.size();
int ptr = 0;
GetNumberOfEnableToken_output result;
while (ptr < recvByte)
{
auto header = (RequestResponseHeader*)(data+ptr);
if (header->type() == RespondContractFunction::type()){
auto oup = (GetNumberOfEnableToken_output*)(data + ptr + sizeof(RequestResponseHeader));
result = *oup;
}
ptr+= header->size();
}
LOG("Number of enabled expensive token in pool: %u\n", result.NumberOfEnabledExpensiveToken);
LOG("Number of enabled general token in pool: %u\n", result.NumberOfEnabledGeneralToken);
}
void qpoolgetenableToken(const char* nodeIp, int nodePort,
const char* seed,
uint32_t tokenID,
bool typeoftoken,
uint32_t scheduledTickOffset)
{
auto qc = make_qc(nodeIp, nodePort);
struct {
RequestResponseHeader header;
RequestContractFunction rcf;
GetEnableToken_input input;
} packet;
packet.header.setSize(sizeof(packet));
packet.header.randomizeDejavu();
packet.header.setType(RequestContractFunction::type());
packet.rcf.inputSize = sizeof(GetEnableToken_input);
packet.rcf.inputType = qPoolFunctionId::GetEnableToken;
packet.rcf.contractIndex = QPOOL_CONTRACT_ID;
packet.input.NumberOfToken = tokenID;
packet.input.TypeOfToken = typeoftoken;
qc->sendData((uint8_t *) &packet, packet.header.size());
std::vector<uint8_t> buffer;
qc->receiveDataAll(buffer);
uint8_t* data = buffer.data();
int recvByte = buffer.size();
int ptr = 0;
GetEnableToken_output result;
while (ptr < recvByte)
{
auto header = (RequestResponseHeader*)(data+ptr);
if (header->type() == RespondContractFunction::type()){
auto oup = (GetEnableToken_output*)(data + ptr + sizeof(RequestResponseHeader));
result = *oup;
}
ptr+= header->size();
}
LOG("Name of enable token: %llu\n", result.assetName);
LOG("Index of contract issued token: %u\n", result.contractIndex);
for(int i = 0 ; i < 32; i++) LOG("%u ", result.issuer[i]);
}
GetValueOfToken_output QpoolGetValueOfTokenByQu(const char* nodeIp, int nodePort,
uint16_t indexOfToken,
uint16_t Poolnum
)
{
auto qc = make_qc(nodeIp, nodePort);
struct {
RequestResponseHeader header;
RequestContractFunction rcf;
GetValueOfToken_input input;
} packet;
packet.header.setSize(sizeof(packet));
packet.header.randomizeDejavu();
packet.header.setType(RequestContractFunction::type());
packet.rcf.inputSize = sizeof(GetValueOfToken_input);
packet.rcf.inputType = qPoolFunctionId::GetValueOfToken;
packet.rcf.contractIndex = QPOOL_CONTRACT_ID;
packet.input.IndexOfToken = indexOfToken;
packet.input.Poolnum = Poolnum;
qc->sendData((uint8_t *) &packet, packet.header.size());
std::vector<uint8_t> buffer;
qc->receiveDataAll(buffer);
uint8_t* data = buffer.data();
int recvByte = buffer.size();
int ptr = 0;
GetValueOfToken_output result;
while (ptr < recvByte)
{
auto header = (RequestResponseHeader*)(data+ptr);
if (header->type() == RespondContractFunction::type()){
auto oup = (GetValueOfToken_output*)(data + ptr + sizeof(RequestResponseHeader));
result = *oup;
}
ptr+= header->size();
}
return result;
}
void qpoolswap(char* nodeIp, int nodePort,
const char* seed,
uint64_t Amountoftoken1,
uint16_t indexOfToken1,
uint16_t indexOfToken2,
uint16_t NumberOfPool,
bool typeoftoken1,
bool typeoftoken2,
uint32_t scheduledTickOffset)
{
auto qc = make_qc(nodeIp, nodePort);
uint8_t privateKey[32] = {0};
uint8_t sourcePublicKey[32] = {0};
uint8_t destPublicKey[32] = {0};
uint8_t subSeed[32] = {0};
uint8_t digest[32] = {0};
uint8_t signature[64] = {0};
uint8_t TokenIssuerPublicKey[32] = {0};
char txHash[128] = {0};
getSubseedFromSeed((uint8_t*)seed, subSeed);
getPrivateKeyFromSubSeed(subSeed, privateKey);
getPublicKeyFromPrivateKey(privateKey, sourcePublicKey);
getPublicKeyFromIdentity(QPOOL_ADDRESS, destPublicKey);
((uint64_t*)destPublicKey)[0] = QPOOL_CONTRACT_ID;
((uint64_t*)destPublicKey)[1] = 0;
((uint64_t*)destPublicKey)[2] = 0;
((uint64_t*)destPublicKey)[3] = 0;
struct {
RequestResponseHeader header;
Transaction transaction;
Swap_input ia;
uint8_t sig[SIGNATURE_SIZE];
} packet;
memcpy(packet.transaction.sourcePublicKey, sourcePublicKey, 32);
memcpy(packet.transaction.destinationPublicKey, destPublicKey, 32);
GetValueOfToken_output Token1 = QpoolGetValueOfTokenByQu(nodeIp, nodePort, indexOfToken1, NumberOfPool);
packet.transaction.amount = Token1.ValueOfToken * Amountoftoken1 / 70; // Swap fee is 1% but should provide enough amount. it will be refunded the rest amount excluding 1% for swap fee
uint32_t scheduledTick = 0;
if (scheduledTickOffset < 50000){
uint32_t currentTick = getTickNumberFromNode(qc);
scheduledTick = currentTick + scheduledTickOffset;
} else {
scheduledTick = scheduledTickOffset;
}
packet.transaction.tick = scheduledTick;
packet.transaction.inputType = QPOOL_SWAP;
packet.transaction.inputSize = sizeof(Swap_input);
// fill the input
packet.ia.AmountOfToken1 = Amountoftoken1;
packet.ia.IndexOfToken1 = indexOfToken1;
packet.ia.IndexOfToken2 = indexOfToken2;
packet.ia.Poolnum = NumberOfPool;
packet.ia.TypeOfToken1 = typeoftoken1;
packet.ia.TypeOfToken2 = typeoftoken2;
// sign the packet
KangarooTwelve((unsigned char*)&packet.transaction,
sizeof(Transaction) + sizeof(Swap_input),
digest,
32);
sign(subSeed, sourcePublicKey, digest, signature);
memcpy(packet.sig, signature, SIGNATURE_SIZE);
// set header
packet.header.setSize(sizeof(packet.header)+sizeof(Transaction)+sizeof(Swap_input)+ SIGNATURE_SIZE);
packet.header.zeroDejavu();
packet.header.setType(BROADCAST_TRANSACTION);
qc->sendData((uint8_t *) &packet, packet.header.size());
KangarooTwelve((unsigned char*)&packet.transaction,
sizeof(Transaction)+sizeof(Swap_input)+ SIGNATURE_SIZE,
digest,
32); // recompute digest for txhash
getTxHashFromDigest(digest, txHash);
LOG("Transaction has been sent!\n");
printReceipt(packet.transaction, txHash, reinterpret_cast<const uint8_t *>(&packet.ia));
LOG("run ./qubic-cli [...] -checktxontick %u %s\n", scheduledTick, txHash);
LOG("to check your tx confirmation status\n");
}
void qpooldepositexpensivetoken(char* nodeIp, int nodePort,
const char* seed,
uint64_t Amountoftoken,
uint16_t indexOfToken,
uint32_t scheduledTickOffset)
{
auto qc = make_qc(nodeIp, nodePort);
uint8_t privateKey[32] = {0};
uint8_t sourcePublicKey[32] = {0};
uint8_t destPublicKey[32] = {0};
uint8_t subSeed[32] = {0};
uint8_t digest[32] = {0};
uint8_t signature[64] = {0};
uint8_t TokenIssuerPublicKey[32] = {0};
char txHash[128] = {0};
getSubseedFromSeed((uint8_t*)seed, subSeed);
getPrivateKeyFromSubSeed(subSeed, privateKey);
getPublicKeyFromPrivateKey(privateKey, sourcePublicKey);
getPublicKeyFromIdentity(QPOOL_ADDRESS, destPublicKey);
((uint64_t*)destPublicKey)[0] = QPOOL_CONTRACT_ID;
((uint64_t*)destPublicKey)[1] = 0;
((uint64_t*)destPublicKey)[2] = 0;
((uint64_t*)destPublicKey)[3] = 0;
struct {
RequestResponseHeader header;
Transaction transaction;
DepositExpensivetoken_input ia;
uint8_t sig[SIGNATURE_SIZE];
} packet;
memcpy(packet.transaction.sourcePublicKey, sourcePublicKey, 32);
memcpy(packet.transaction.destinationPublicKey, destPublicKey, 32);
packet.transaction.amount = QPOOL_EXPENSIVE_TOKEN_DEPOSIT_FEE;
uint32_t scheduledTick = 0;
if (scheduledTickOffset < 50000){
uint32_t currentTick = getTickNumberFromNode(qc);
scheduledTick = currentTick + scheduledTickOffset;
} else {
scheduledTick = scheduledTickOffset;
}
packet.transaction.tick = scheduledTick;
packet.transaction.inputType = QPOOL_DEPOSIT_EXPENSIVE_TOKEN;
packet.transaction.inputSize = sizeof(DepositExpensivetoken_input);
// fill the input
packet.ia.AmountOfToken = Amountoftoken;
packet.ia.IndexOfToken = indexOfToken;
// sign the packet
KangarooTwelve((unsigned char*)&packet.transaction,
sizeof(Transaction) + sizeof(DepositExpensivetoken_input),
digest,
32);
sign(subSeed, sourcePublicKey, digest, signature);
memcpy(packet.sig, signature, SIGNATURE_SIZE);
// set header