forked from Open-Transactions/Open-Transactions
-
Notifications
You must be signed in to change notification settings - Fork 48
/
ot_commands.ot
12121 lines (10130 loc) · 510 KB
/
ot_commands.ot
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
// NEW RETURN CODE CONVENTION!
// OT API already returns: -1 for error, 0 for "didn't need to do anything" and 1 for success (or larger.)
// All of the below functions follow that convention (they just return the same values.)
//
// But for the OT command line tool, we need to return 0 for success, and non-zero codes for errors.
// This is because it's a UNIX convention and will be expected by anyone writing bash scripts.
//
// However, I don't want to physically change all the below functions, so I'm going to change the
// opentxs script itself, so that it translates the return value. That way I only have to change it
// in one spot, and we have access to use these scripts both ways.
//
// THE POINT? If you notice these scripts returning 1, which is then interpreted as 0 by bash scripts,
// it's because the script that CALLS these scripts, is doing that translation (for the above described
// reasons.)
//
// ------------------------------------------
def main_revoke_credential()
{
OT_API_Output(0, "\nThis command not coded yet. We need to use the OT_API_RevokeSubcredential API call here.\n\n")
-1
}
// ------------------------------------------
def main_new_credential()
{
OT_API_Output(0, "\nThis command not coded yet. A Nym is created with credentials already,\nbut to add MORE credentials to an existing Nym, I need to use the OT_API_AddSubcredential API call.\n\n")
-1
}
// Also TODO:
// Update the "cancel" command, for outgoing cash, to give you a choice to deposit the cash instead
// of discarding it.
// details_deposit_purse(strServerID, strMyAcctID, strToNymID, strInstrument, "") // strIndices is left blank in this case
// -----------------------------------------------
// BASKETS
def main_show_basket()
{
var strUsage = "Usage: opentxs showbasket\nOPTIONAL: --args \"index BASKET_INDEX\"\n\nNOTE: If you leave off the index, then it lists all the basket currencies.\nBut if an index is provided, this command will zoom in and show the details\nfor that specific basket currency.\n\n"
OT_API_Output(0, strUsage)
// ---------------------------------------------------
details_show_basket()
}
def details_show_basket()
{
var bOnFirstIteration = true
var nAssetCount = OT_API_GetAssetTypeCount()
if (VerifyIntVal(nAssetCount) && (nAssetCount > 0))
{
var strIndex = ""
if (VerifyExists("Args", false))
{
// OPTION: --args "key value key value"
// EXAMPLE: --args "indices 4,6,9"
//
var strTempIndex = OT_CLI_GetValueByKey(Args, "index")
// ------------------------------------
if (VerifyStringVal(strTempIndex))
{
strIndex = strTempIndex
}
} // args
// -----------------------------------
// Zoom in on a specific basket currency.
//
if (VerifyStringVal(strIndex))
{
var nIndex = strIndex.to_int()
if (!VerifyIntVal(nIndex))
{
return -1
}
else if ( (nIndex < 0) || (nIndex >= nAssetCount) )
{
OT_API_Output(0, "Error: index out of bounds.\n")
return -1
}
else
{
var strAssetID = OT_API_GetAssetType_ID(int32_t(nIndex))
if (!VerifyStringVal(strAssetID))
{
OT_API_Output(0, "ERROR: Failed getting asset type ID at index: "+nIndex.to_string()+"\n")
}
else
{
var strAssetName = OT_API_GetAssetType_Name(strAssetID)
if (!VerifyStringVal(strAssetName))
{
strAssetName = ""
}
// ----------------------------------
if (!OT_API_IsBasketCurrency(strAssetID))
{
OT_API_Output(0, "Failure: not a basket currency: "+strAssetID+" : "+strAssetName+"\n")
}
else
{
// DISPLAY ALL THE DETAILS ABOUT THIS BASKET CURRENCY:
//
print("Name: " + strAssetName)
print("ID: " + strAssetID)
var nMemberCount = OT_API_Basket_GetMemberCount(strAssetID)
if (!VerifyIntVal(nMemberCount))
{
OT_API_Output(0, "ERROR: expected int32_t return value from OT_API_Basket_GetMemberCount(strAssetID)\n")
}
else if (nMemberCount <= 0)
{
OT_API_Output(0, "Strange: this basket has "+nMemberCount.to_string()+" sub-currencies. (Expected 1 or more.)\n")
}
else
{
var nBasketMinTransAmt = OT_API_Basket_GetMinimumTransferAmount(strAssetID)
if (!VerifyIntVal(nBasketMinTransAmt))
{
OT_API_Output(0, "Strange: expected minimum transfer amount for basket, but got bad value instead.\n")
}
else
{
print("Minimum transfer amount for basket: " + nBasketMinTransAmt.to_string())
print("Number of sub-currencies in the basket: " + nMemberCount.to_string())
var bFirstMember = true
for (var nMemberIndex = 0; nMemberIndex < nMemberCount; ++nMemberIndex)
{
var strMemberType = OT_API_Basket_GetMemberType(strAssetID, int32_t(nMemberIndex))
var strMemberName = VerifyStringVal(strMemberType) ? OT_API_GetAssetType_Name(strMemberType) : ""
// ---------------------------------------------
var nMinTransAmt = OT_API_Basket_GetMemberMinimumTransferAmount(strAssetID, int32_t(nMemberIndex))
if (bFirstMember)
{
bFirstMember = false
OT_API_Output(0, " Index : Min Transfer Amount : Member currency \n---------------------------------------\n")
}
print(" "+nMemberIndex.to_string()+" : " + (VerifyIntVal(nMinTransAmt) ? nMinTransAmt.to_string() : "ERROR_AMT") + " : " + strMemberType + " : " + (VerifyStringVal(strMemberName) ? strMemberName : "ERROR_NAME") )
OT_API_Output(0, "---------------------------------------\n")
}
}
}
}
}
}
return 1
}
// -----------------------------------
// List ALL the basket currencies.
//
for (var nAssetIndex = 0; nAssetIndex < nAssetCount; ++nAssetIndex)
{
var strAssetID = OT_API_GetAssetType_ID(int32_t(nAssetIndex))
if (VerifyStringVal(strAssetID) && OT_API_IsBasketCurrency(strAssetID))
{
if (bOnFirstIteration)
{
bOnFirstIteration = false
OT_API_Output(0, "Index | Basket currencies:\n------------------------------------\n")
}
var strAssetName = OT_API_GetAssetType_Name(strAssetID)
if (VerifyStringVal(strAssetName))
{ print(nAssetIndex.to_string() + ": " + strAssetID+" : "+strAssetName) }
else
{ print(nAssetIndex.to_string() + ": " + strAssetID) }
}
} // for
if (nAssetCount > 0) { OT_API_Output(0, "\n") }
}
-1
}
def main_new_basket()
{
if (VerifyExists("Server") && VerifyExists("MyNym"))
{
return details_new_basket(Server, MyNym)
}
-1
}
def details_new_basket(strServer, strNym)
{
var nBasketCount = 2
OT_API_Output(0, "How many different asset types will compose this new basket currency? [2]: ")
var strBasketCount = OT_CLI_ReadLine()
if (VerifyStringVal(strBasketCount))
{
nBasketCount = strBasketCount.to_int()
if (nBasketCount < 2)
{
OT_API_Output(0, "Sorry, but a basket currency must be composed of at least 2 sub-currencies.\n")
return -1
}
}
// --------------------------------------------------
var lMinimumTransAmount = int64_t(100)
OT_API_Output(0, "\nIf your basket has a minimum transfer amount of 100, you might have 2 or 3 sub-currencies,\nwith the first being a minimum of 2 gold, the second being a minimum of 50 dollars, and the\nthird being a minimum of 30 silver.\nIn this example, 100 units of the basket currency is transferrable in or out of the\nbasket currency, in return for 2 gold, 50 dollars, and 30 silver.\n\n")
OT_API_Output(0, "What is the minimum transfer amount for the basket currency itself? [100]: ")
var strMinAmount = OT_CLI_ReadLine()
if (VerifyStringVal(strMinAmount))
{
lMinimumTransAmount = int64_t(strMinAmount.to_int())
if (lMinimumTransAmount < 1)
{
OT_API_Output(0, "Sorry, but this needs to be a non-zero value. Minimum is 1.\n")
return -1
}
}
// --------------------------------------------------
var strBasket = OT_API_GenerateBasketCreation(strNym, lMinimumTransAmount)
if (!VerifyStringVal(strBasket))
{
OT_API_Output(0, "Error while generating initial basket object.\n")
return -1
}
// --------------------------------------------------
for (var ibasket = 0; ibasket < nBasketCount; ++ibasket)
{
main_stat_assets()
OT_API_Output(0, "\nThis basket currency has "+nBasketCount.to_string()+" subcurrencies.\n")
OT_API_Output(0, "So far you have defined "+ibasket.to_string()+" of them.\n")
OT_API_Output(0, "Please PASTE the asset type ID for a subcurrency of this basket: ")
var strSubcurrencyID = OT_CLI_ReadLine()
if (!VerifyStringVal(strSubcurrencyID))
{
return -1
}
// ----------------------------------------
var strSubcurrencyContract = OT_API_GetAssetType_Contract(strSubcurrencyID)
if (!VerifyStringVal(strSubcurrencyContract))
{
OT_API_Output(0, "Sorry, but "+strSubcurrencyID+" is apparently not a currency contract in your wallet.\n")
ibasket -= 1
continue
}
// ----------------------------------------
OT_API_Output(0, "Enter minimum transfer amount for that asset type [100]: ")
lMinimumTransAmount = int64_t(100)
strMinAmount = OT_CLI_ReadLine()
if (VerifyStringVal(strMinAmount))
{
lMinimumTransAmount = OT_API_StringToAmount(strSubcurrencyID, strMinAmount)
if (lMinimumTransAmount < 1)
{
OT_API_Output(0, "Sorry, but this needs to be a non-zero value. Minimum is 1.\n")
ibasket -= 1
continue
}
}
// ------------------------------------------
var strTempBasket = OT_API_AddBasketCreationItem(strNym, strBasket, strSubcurrencyID, lMinimumTransAmount)
if (!VerifyStringVal(strTempBasket))
{
OT_API_Output(0, "Error: OT_API_AddBasketCreationItem returned NULL. (Failure.)\n")
return -1
}
else
{
strBasket = strTempBasket
}
}
// ------------------------------------------
OT_API_Output(0, "Here's the basket we're issuing:\n\n"+strBasket+"\n")
// --------------------------
var strResponse = OT_ME_issue_basket_currency(strServer, strNym, strBasket)
var nStatus = VerifyMessageSuccess(strResponse)
// NOTICE: No need here to deal with retries, timeouts, request number,
// syncing transaction number, download / process nymbox, etc! It's all
// handled at a lower level! Instead, simply check for success or failure:
//
// -1 is error,
// 0 is reply received: failure
// 1 is reply received: success
//
switch(nStatus)
{
case (1)
{
OT_API_Output(0, "\n\n SUCCESS in issue_basket_currency! Server response:\n\n")
print(strResponse) // stdout
var strNewID = OT_API_Message_GetNewAssetTypeID(strResponse)
var bGotNewID = VerifyStringVal(strNewID)
var bRetrieved = false
var strEnding = "."
if (bGotNewID)
{
var strRetrieved = OT_ME_retrieve_contract(strServer, strNym, strNewID)
strEnding = ": "+strNewID
if (1 == VerifyMessageSuccess(strRetrieved))
{
bRetrieved = true
}
}
OT_API_Output(0, "Server response: SUCCESS in issue_basket_currency!\n")
OT_API_Output(0, (bRetrieved ? "Success" : "Failed") + " retrieving new basket contract"+strEnding+"\n")
break
}
case (0)
{
OT_API_Output(0, "\n\n FAILURE in issue_basket_currency! Server response:\n\n")
print(strResponse) // stdout
OT_API_Output(0, " FAILURE in issue_basket_currency!\n")
break
}
default
{
OT_API_Output(0, "\n\nError in issue_basket_currency! nStatus is: "+nStatus.to_string()+"\n")
if (VerifyStringVal(strResponse))
{
OT_API_Output(0, "Server response:\n\n")
print(strResponse) // stdout
OT_API_Output(0, "\nError in issue_basket_currency! nStatus is: "+nStatus.to_string()+"\n")
}
break
}
}
OT_API_Output(0, "\n") //stderr
return (0 == nStatus) ? (-1) : nStatus
}
// ------------------------------------------
def details_exchange_basket(strServer, strNym, strAcct, strBasketType)
{
// NOTE: details_exchange_basket ASSUMES that strAcct has a server of strServer and
// a NymID of strNym and an asset type of strBasketType. (These are already verified
// in main_exchange_basket.)
//
var nMemberCount = OT_API_Basket_GetMemberCount(strBasketType)
if (!VerifyIntVal(nMemberCount) || (nMemberCount < 2))
{
OT_API_Output(0, "Strange, the chosen basket asset type apparently has no sub-currencies. (Failure.)\n")
return (-1)
}
// --------------------------------------------------
var lMinimumTransAmount = OT_API_Basket_GetMinimumTransferAmount(strBasketType)
if (!VerifyIntVal(lMinimumTransAmount))
{
OT_API_Output(0, "Strange, the chosen basket asset type apparently has no minimum transfer amount. (Failure.)\n")
return (-1)
}
// --------------------------------------------------
var bExchangingIn = true
OT_API_Output(0, "Are you exchanging IN or OUT of the basket? [IN]: ")
var strInOrOut = OT_CLI_ReadLine()
if (!VerifyStringVal(strInOrOut))
{
strInOrOut = "in"
}
// --------------------
var lBalance = OT_API_GetAccountWallet_Balance(strAcct)
if (!VerifyIntVal(lBalance))
{
OT_API_Output(0, "Strange: unable to retrieve balance for basket account: "+strAcct+"\n")
return (-1)
}
// --------------------
if ((strInOrOut == "out") || (strInOrOut == "OUT"))
{
OT_API_Output(0, "Exchanging OUT of the basket currency...\n\n")
bExchangingIn = false
if (lBalance < lMinimumTransAmount)
{
OT_API_Output(0, "Sorry, but the minimum transfer amount for this basket currency is "+lMinimumTransAmount.to_string()+"\nand the chosen account's balance is only "+lBalance.to_string()+". (Failure.)\n")
return (-1)
}
}
else
{
OT_API_Output(0, "Exchanging IN to the basket currency...\n\n")
}
// -----------------------------
var nTransferMultiple = 1
OT_API_Output(0, "This basket currency has a minimum transfer amount of "+lMinimumTransAmount.to_string()+".\n")
OT_API_Output(0, "Now you must choose the Transfer Multiple for the exchange. For example:\n")
for (var i = 1; i < 5; ++i)
{
var lUnits = i * lMinimumTransAmount
OT_API_Output(0, "A transfer multiple of "+i.to_string()+" will exchange "+lUnits.to_string()+" units of the basket currency.\n")
}
OT_API_Output(0, "Etc.\n")
if (!bExchangingIn) { OT_API_Output(0, "Keep in mind, the balance in the basket account is: "+lBalance.to_string()+"\n\n") }
OT_API_Output(0, "Please choose a transfer multiple [1]: ")
var strTransferMultiple = OT_CLI_ReadLine()
if (VerifyStringVal(strTransferMultiple))
{
var nTempMultiple = strTransferMultiple.to_int()
if (VerifyIntVal(nTempMultiple) && (nTempMultiple > 1)) { nTransferMultiple = nTempMultiple }
}
// -----------------------------
var lAttemptedTransfer = (nTransferMultiple * lMinimumTransAmount)
if (!bExchangingIn && (lAttemptedTransfer > lBalance))
{
OT_API_Output(0, "A transfer multiple of "+nTransferMultiple.to_string()+" will exchange "+lAttemptedTransfer.to_string()+" units out of the basket currency.\nUnfortunately, you only have a "+lBalance.to_string()+" balance in your basket account. (Failed.)\n")
return (-1)
}
// -----------------------------
if (!OT_ME_insure_enough_nums(20, strServer, strNym))
{
return -1
}
// ------------------------------
var strBasket = OT_API_GenerateBasketExchange(strServer, strNym, strBasketType, strAcct, int32_t(nTransferMultiple))
if (!VerifyStringVal(strBasket))
{
OT_API_Output(0, "Failed generating basket exchange request.\n")
return (-1)
}
// ------------------------------
// Below this point, after any failure, call this before returning:
//
// OT_API_Msg_HarvestTransactionNumbers(strBasket, strNym, false, false, false, false, false)
//
// NOTE: Only do this up until the message is sent. Once it's sent, the harvesting is already
// handled internally. (Only need to harvest in the event that some failure occurs in the middle of
// constructing the basket exchange request, for any time BEFORE actually trying to send it.)
// ------------------------------
// SUB-CURRENCIES!
for (var nMember = 0; nMember < nMemberCount; ++nMember)
{
var strMemberType = OT_API_Basket_GetMemberType(strBasketType, int32_t(nMember))
if (!VerifyStringVal(strMemberType))
{
OT_API_Output(0, "Error retrieving member type from index "+nMember.to_string()+" of basket currency: "+strBasketType+"\n")
OT_API_Msg_HarvestTransactionNumbers(strBasket, strNym, false, false, false, false, false)
return (-1)
}
// --------------------------------------------
var strMemberTypeName = OT_API_GetAssetType_Name(strMemberType)
if (!VerifyStringVal(strMemberTypeName))
{
strMemberTypeName = ""
}
// --------------------------------------------
var lMemberAmount = OT_API_Basket_GetMemberMinimumTransferAmount(strBasketType, nMember)
if (!VerifyIntVal(lMemberAmount))
{
OT_API_Output(0, "Error retrieving minimum transfer amount from index "+nMember.to_string()+" ("+strMemberType+") on basket currency: "+strBasketType+"\n")
OT_API_Msg_HarvestTransactionNumbers(strBasket, strNym, false, false, false, false, false)
return (-1)
}
// --------------------------------------------
lAttemptedTransfer = (nTransferMultiple * lMemberAmount)
// This will only display accounts registered on strServer of asset type strMemberType.
//
stat_basket_accounts(strServer, strNym, true, strMemberType)
OT_API_Output(0, "There are "+(nMemberCount-nMember).to_string()+" accounts remaining to be selected.\n\n")
OT_API_Output(0, "Currently we need to select an account with the asset type:\n"+strMemberType+" ("+strMemberTypeName+")\n")
OT_API_Output(0, "Above are all the accounts in the wallet, for the relevant server and nym, of that asset type.\n")
if (bExchangingIn) { OT_API_Output(0, "\nKeep in mind, with a transfer multiple of "+nTransferMultiple.to_string()+" and a minimum transfer amount of "+lMemberAmount.to_string()+"\n(for this sub-currency), you must therefore select an account with a minimum\nbalance of: "+lAttemptedTransfer.to_string()+"\n") }
OT_API_Output(0, "\nPlease PASTE an account ID from the above list: ")
var strSubAccount = OT_CLI_ReadLine()
if (!VerifyStringVal(strSubAccount))
{
OT_API_Msg_HarvestTransactionNumbers(strBasket, strNym, false, false, false, false, false)
return (-1)
}
// -------------------------
var strSubAssetID = OT_API_GetAccountWallet_AssetTypeID(strSubAccount)
if (!VerifyStringVal(strSubAssetID))
{
OT_API_Output(0, "Error retrieving asset type ID from pasted account: "+strSubAccount+"\n")
OT_API_Msg_HarvestTransactionNumbers(strBasket, strNym, false, false, false, false, false)
return (-1)
}
// -------------------------
if (strMemberType != strSubAssetID)
{
OT_API_Output(0, "Failure: The selected account has the wrong asset type: "+strSubAssetID+"\n")
OT_API_Msg_HarvestTransactionNumbers(strBasket, strNym, false, false, false, false, false)
return (-1)
}
// --------------------------------------------
lBalance = OT_API_GetAccountWallet_Balance(strSubAccount)
if (!VerifyIntVal(lBalance))
{
OT_API_Output(0, "Strange, error while retrieving balance for sub-account: "+strSubAccount+"\n")
OT_API_Msg_HarvestTransactionNumbers(strBasket, strNym, false, false, false, false, false)
return (-1)
}
// --------------------------------------------
if (bExchangingIn && (lAttemptedTransfer > lBalance))
{
OT_API_Output(0, "\nWith a minimum transfer amount of "+lMemberAmount.to_string()+" for this sub-currency, a transfer multiple of "+nTransferMultiple.to_string()+" will exchange "+lAttemptedTransfer.to_string()+" units out of the basket currency.\n\nUnfortunately, you only have a "+lBalance.to_string()+" balance in your selected account. (Failure.)\n")
OT_API_Msg_HarvestTransactionNumbers(strBasket, strNym, false, false, false, false, false)
return (-1)
}
// -----------------------------
var strTempBasket = OT_API_AddBasketExchangeItem(strServer, strNym, strBasket, strSubAssetID, strSubAccount)
if (!VerifyStringVal(strTempBasket))
{
OT_API_Output(0, "Failed while adding this sub-account to the exchange request!\n")
OT_API_Msg_HarvestTransactionNumbers(strBasket, strNym, false, false, false, false, false)
return (-1)
}
// -----------------------------
strBasket = strTempBasket
OT_API_Output(0, "\n\n")
} // for
// -------------------------------------------------------------------------
// Send the exchange transaction...
//
var strResponse = OT_ME_exchange_basket_currency(strServer, strNym, strBasketType, strBasket, strAcct, bExchangingIn)
var strAttempt = "exchange_basket"
// ***************************************************************
// Interpret the server reply...
var nInterpretReply = InterpretTransactionMsgReply(strServer, strNym, strAcct, strAttempt, strResponse)
if (1 == nInterpretReply)
{
// Download all the intermediary files (account balance, inbox, outbox, etc)
// since they have probably changed from this operation.
//
var bRetrieved = OT_ME_retrieve_account(strServer, strNym, strAcct, true) //bForceDownload defaults to false.
OT_API_Output(0, "Server response ("+strAttempt+"): SUCCESS exchanging basket!\n")
OT_API_Output(0, (bRetrieved ? "Success" : "Failed") + " retrieving intermediary files for account.\n")
}
// ***************************************************************
//
// Return status to caller.
//
return nInterpretReply
}
def main_exchange_basket()
{
var strUsage = "Usage: opentxs exchange --myacct BASKET_ACCT_ID\n\nThis command exchanges in or out of a basket currency.\nYou must already have an asset account which has a basket currency as its asset type.\nYou must also have accounts for all the subcurrencies in that basket.\n\n"
OT_API_Output(0, strUsage)
// ---------------------------------------------------------------
if (VerifyExists("MyAcct"))
{
var strAcctType = OT_API_GetAccountWallet_AssetTypeID(MyAcct)
var strServer = OT_API_GetAccountWallet_ServerID(MyAcct)
var strNym = OT_API_GetAccountWallet_NymID(MyAcct)
if (!VerifyStringVal(strAcctType))
{
OT_API_Output(0, "Error while trying to retrieve asset type for account: "+MyAcct+"\n")
}
else if (!VerifyStringVal(strServer))
{
OT_API_Output(0, "Error while trying to retrieve server ID for account: "+MyAcct+"\n")
}
else if (!VerifyStringVal(strNym))
{
OT_API_Output(0, "Error while trying to retrieve Nym ID for account: "+MyAcct+"\n")
}
// ------------------------------------------------------------------
else if (VerifyExists("MyPurse", false) && (MyPurse != strAcctType))
{
OT_API_Output(0, "MyAcct has a different asset type than the one specified at the command line.\nPlease re-try, and add: --mypurse "+strAcctType+"\n")
}
else if (VerifyExists("Server", false) && (Server != strServer))
{
OT_API_Output(0, "MyAcct has a different server ID than the one specified at the command line.\nPlease re-try, and add: --server "+strServer+"\n")
}
else if (VerifyExists("MyNym", false) && (MyNym != strNym))
{
OT_API_Output(0, "MyAcct has a different owner Nym ID than the one specified at the command line.\nPlease re-try, and add: --mynym "+strNym+"\n")
}
// ------------------------------------------------------------------
else if (!OT_API_IsBasketCurrency(strAcctType))
{
OT_API_Output(0, "\nMyAcct is NOT a basket currency! Its asset type is: "+strAcctType+"\nHere is a list of the basket currencies in your wallet:\n")
details_show_basket()
OT_API_Output(0, "\nHere is a list of the accounts whose asset type IS a basket currency:\n")
stat_basket_accounts("", "", false, "")
OT_API_Output(0, "\nMyAcct is not a basket currency!\nPlease use --myacct to specify an account whose asset type IS a basket currency.\n")
}
// ------------------------------------------------------------------
else
{
return details_exchange_basket(strServer, strNym, MyAcct, strAcctType)
}
}
else
{
OT_API_Output(0, "You must provide an account ID, and that account must have a basket\ncurrency for its asset type. Use --myacct and choose from these basket accounts:\n")
stat_basket_accounts("", "", false, "")
}
-1
}
// Used by exchange_basket for displaying certain types of accounts.
//
// if strBasketType doesn't exist, it will ONLY show accounts that are basket currencies.
// if strBasketType exists, and bFilter is TRUE, it will ONLY show accounts of that type.
// if strBasketType exists, and bFilter is FALSE, it will only show accounts that are NOT of that type.
//
// Also: if strServer exists, the accounts are filtered by that server.
// Also: if strNym exists, the accounts are filtered by that Nym.
//
def stat_basket_accounts(strServer, strNym, bFilter, strBasketType)
{
print("------------------------------------------------------------------")
print(" ** ACCOUNTS :\n")
var nAccountCount = OT_API_GetAccountCount()
for (var i = 0; i < nAccountCount; ++i)
{
var strID = OT_API_GetAccountWallet_ID(i)
var strAssetID = OT_API_GetAccountWallet_AssetTypeID(strID)
var strAcctServerID = OT_API_GetAccountWallet_ServerID(strID)
var strAcctNymID = OT_API_GetAccountWallet_NymID(strID)
if (!VerifyStringVal(strServer) || (VerifyStringVal(strServer) && (strServer == strAcctServerID)))
{
if (!VerifyStringVal(strNym) || (VerifyStringVal(strNym) && (strNym == strAcctNymID)))
{
if ((!VerifyStringVal(strBasketType) && OT_API_IsBasketCurrency(strAssetID)) || // Only show basket accounts.
( VerifyStringVal(strBasketType) && bFilter && (strBasketType == strAssetID)) || // Only show accounts of type strBasketType
( VerifyStringVal(strBasketType) && !bFilter && (strBasketType != strAssetID))) // Only show accounts NOT of type strBasketType
{
if ((i > 0) && (i != (nAccountCount))) { print("-------------------------------------") }
var strStatAcct = OT_ME_stat_asset_account(strID)
var bSuccess = VerifyStringVal(strStatAcct)
if (bSuccess)
{
print(strStatAcct)
}
else
{
print("Error trying to stat an account: "+strID)
}
// stderr
print("")
}
}
}
}
print("------------------------------------------------------------------")
1
}
// ALSO TODO: Modified cancelCronItem so you can pass an instrument.
// So even for non-cron items, you should still be able to cancel them.
// (aka cancel the transaction numbers on it.) This is for when you change
// your mind on an outgoing instrument, and you want it cancelled before the
// recipient can process it.
// Outgoing cheque, outgoing payment plan, outgoing smart contract, etc.
//
// AND WHAT ABOUT incoming cheques? I can already discard it, but that leaves
// the sender with it still sitting in his outpayment box, until it expires.
// I'd rather notify him. He should get a failure/rejection notice so that his
// client can also harvest whatever numbers it needs to harvest.
// -------------------------------------------------
// TODO: details_cancel_outgoing and details_discard_incoming both need to be updated
// so that they involve a server message. This way the other parties can be notified of
// the cancellation / discarding.
//
def details_discard_incoming(strServer, strMyNym, strIndices)
{
if (!VerifyStringVal(strIndices)) { return -1 }
// ---------------------------------------------
var nSuccess = 1
// ---------------------------------------------
var strInbox = OT_API_LoadPaymentInbox(strServer, strMyNym) // Returns NULL, or an inbox.
if (!VerifyStringVal(strInbox))
{
OT_API_Output(0, "\n\n details_discard_incoming: OT_API_LoadPaymentInbox failed.\n\n")
return (-1)
}
// ---------------------------------------------
var nInboxCount = OT_API_Ledger_GetCount(strServer, strMyNym, strMyNym, strInbox)
if (!VerifyIntVal(nInboxCount))
{
OT_API_Output(0, "details_discard_incoming: Unable to retrieve size of payments inbox ledger. (Failure.)\n")
return (-1)
}
// ---------------------------------------------
if (nInboxCount > 0)
{
// ----------------------------------------------
var nIndicesCount = VerifyStringVal(strIndices) ? OT_API_NumList_Count(strIndices) : 0
// ------------------------------------------------------------------------
// Either we loop through all the instruments and accept them all, or
// we loop through all the instruments and accept the specified indices.
//
// (But either way, we loop through all the instruments.)
//
for (var nInboxIndex = (nInboxCount-1); nInboxIndex >= 0; --nInboxIndex) // Loop from back to front, so if any are removed, the indices remain accurate subsequently.
{
var bContinue = false
// - If "all" was passed, we process the item.
// - If indices are specified, but the current index is not on
// that list, then continue...
//
if (!("all" == strIndices) && ((nIndicesCount > 0) && !OT_API_NumList_VerifyQuery(strIndices, nInboxIndex.to_string())))
{
// continue // apparently not supported by the language.
bContinue = true
}
// --------------------
// If it IS "all" OR, if there are indices and the current index was found in them.
//
if (!bContinue)
{
// removes payment instrument (from payments in or out box)
var bRecorded = OT_API_RecordPayment(strServer, strMyNym,
true, // true == payments inbox. false == payments outbox.
int32_t(nInboxIndex), false) //bSaveCopy = false, since we're discarding it.
var strRecorded = bRecorded ? "Success" : "Failure"
OT_API_Output(0, "\n" + strRecorded + " discarding instrument from payments inbox at index: " + nInboxIndex.to_string() + ".\n\n NOTE: Now the sender has to leave it in his outbox until it expires or he cancels it on his end.\n")
if (!bRecorded) { nSuccess = (-1) }
} //if (!bContinue)
// -----------------------
} // for
// --------------------
} // if (nCount > 0)
else { return 0 } // 1 means we successfully did something. 0 means the payments inbox was empty.
// -----------------------------
return nSuccess
}
def main_discard_incoming()
{
var strUsage = "Usage: opentxs discard --mynym NYM_ID --server SERVER_ID --args \"index|indices INDICES_GO_HERE\"\n\nThis command discards an incoming instrument from the payments inbox.\n(Usually used for discarding an invoice, for when you don't want to pay it.)\nYou can also use 'all' for the index, for it to process ALL instruments.\n\n"
OT_API_Output(0, strUsage)
// --------------------------------------------------------------------
if (VerifyExists("MyNym") && VerifyExists("Server"))
{
var strIndex = ""
var strIndices = ""
var strFinalIndex = ""
var bUseStdin = false
if (VerifyExists("Args", false))
{
// OPTION: --args "key value key value"
// EXAMPLE: --args "indices 4,6,9"
//
strIndex = OT_CLI_GetValueByKey(Args, "index")
strIndices = OT_CLI_GetValueByKey(Args, "indices") // OTNumList will be used for this value.
// ------------------------------------
if (VerifyStringVal(strIndex))
{
strFinalIndex = strIndex
}
else if (VerifyStringVal(strIndices))
{
strFinalIndex = strIndices
}
else
{
bUseStdin = true
}
} else { bUseStdin = true }
// ---------------------------------------------
if (bUseStdin)
{
main_show_payments_inbox()
OT_API_Output(0, "\nPlease enter the index (in your payments inbox)\nof the instrument you are discarding: ")
strIndex = OT_CLI_ReadLine()
// ---------------------------------------------
if (VerifyStringVal(strIndex))
{
strFinalIndex = strIndex
}
else { return -1 }
}
// ---------------------------------------------
return details_discard_incoming(Server, MyNym, strFinalIndex)
}
// --------------------------------------------------------------------
-1
}
/*
std::string GetNym_OutpaymentsContentsByIndex(const std::string & NYM_ID, const int32_t & nIndex); // returns the message itself
std::string GetNym_OutpaymentsRecipientIDByIndex(const std::string & NYM_ID, const int32_t & nIndex); // returns the NymID of the recipient.
std::string GetNym_OutpaymentsServerIDByIndex(const std::string & NYM_ID, const int32_t & nIndex); // returns the ServerID where the message came from.
bool Nym_RemoveOutpaymentsByIndex(const std::string & NYM_ID, const int32_t & nIndex); // (1 or 0.)
bool Nym_VerifyOutpaymentsByIndex(const std::string & NYM_ID, const int32_t & nIndex); // true if signature verifies. (Sender Nym MUST be in my wallet for this to work.)
const char * OT_API_Instrmnt_GetAmount(const char * THE_INSTRUMENT);
const char * OT_API_Instrmnt_GetTransNum(const char * THE_INSTRUMENT);
const char * OT_API_Instrmnt_GetValidFrom(const char * THE_INSTRUMENT);
const char * OT_API_Instrmnt_GetValidTo(const char * THE_INSTRUMENT);
const char * OT_API_Instrmnt_GetMemo(const char * THE_INSTRUMENT);
const char * OT_API_Instrmnt_GetType(const char * THE_INSTRUMENT);
const char * OT_API_Instrmnt_GetServerID(const char * THE_INSTRUMENT);
const char * OT_API_Instrmnt_GetAssetID(const char * THE_INSTRUMENT);
const char * OT_API_Instrmnt_GetSenderUserID(const char * THE_INSTRUMENT);
const char * OT_API_Instrmnt_GetSenderAcctID(const char * THE_INSTRUMENT);
const char * OT_API_Instrmnt_GetRecipientUserID(const char * THE_INSTRUMENT);
const char * OT_API_Instrmnt_GetRecipientAcctID(const char * THE_INSTRUMENT);
*/
def details_cancel_outgoing(strMyNym, strMyAcct, strIndices)
{
var nIndicesCount = OT_API_NumList_Count(strIndices)
var nCount = OT_API_GetNym_OutpaymentsCount(strMyNym)
var nSuccess = 1
// OT_API_Output(0, "\n\nNOTE: You can't just discard a sent cheque from your outpayment box. Why not?\nJust because you remove your record of the outgoing cheque, doesn't mean you didn't already send it. (The recipient still received it, and still has it, whether you remove it from your outbox or not.) If you really want to cancel the cheque, then you need to do it in such a way that it will fail-as-cancelled when the recipient tries to deposit it. Otherwise, he would get that money whether you kept your own outgoing record or not. Therefore TODO: SOME server message must be performed here, which actually cancels the transaction number itself that appears on the cheque. This is the only way to insure that the cheque can't be used by the recipient (and even this will only work if you beat him to the punch -- otherwise if he deposits it before you cancel it, then it's already too late and he has the money.) THIS is why RecordPayment, regarding outpayments, only works on expired instruments -- because if it's not expired, you don't just want to remove your record of it. You want to cancel the transaction number itself -- and that requires server communication.\n\n")
if (!VerifyIntVal(nCount))
{
OT_API_Output(0, "Error retrieving outpayments count for Nym: "+strMyNym+"\n")
return -1
}
// ------------------------------
if (nCount == 0)
{
OT_API_Output(0, "(Outpayment box is empty.)\n")
nSuccess = 0
}
else
{
for (var nIndex = (nCount-1); nIndex >= 0; --nIndex)
{
// var bRemoved = OT_API_Nym_RemoveOutpaymentsByIndex(strMyNym, nIndex); // (1 or 0.)
// var strRecorded = bRecorded ? "Success" : "Failure"
// OT_API_Output(0, strRecorded + " discarding instrument from outpayment box at index: " + nIndex.to_string() + ".\n")
// if (!bRemoved)
// { nSuccess = -1 }
var bContinue = false
// - If "all" was passed, we process the item.
// - If indices are specified, but the current index is not on
// that list, then continue...
//
if (!("all" == strIndices) && (VerifyIntVal(nIndicesCount) && (nIndicesCount > 0) && !OT_API_NumList_VerifyQuery(strIndices, nIndex.to_string())))
{
// continue // apparently not supported by the language.
bContinue = true
}
// --------------------
var strServer = ""
// If it IS "all," OR, if there are indices and the current index was found in them.
//
if (!bContinue)
{
strServer = OT_API_GetNym_OutpaymentsServerIDByIndex(strMyNym, nIndex)
if (!VerifyStringVal(strServer))
{
OT_API_Output(0, "ERROR: Failed retrieving server ID from outpayment at index: "+nIndex.to_string()+"\n")
}
// ---------------
else
{
var strPaymentContents = OT_API_GetNym_OutpaymentsContentsByIndex(strMyNym, nIndex)
// ------------------------------------------------------
if (VerifyStringVal(strPaymentContents))
{
// var strPaymentAssetID = OT_API_Instrmnt_GetAssetID (strPaymentContents)
var strPaymentType = OT_API_Instrmnt_GetType (strPaymentContents)
// Done: Put the code here where we message the server to cancel all relevant transaction
// numbers for the instrument. If it's a cheque, there's only one number. But if it's a smart
// contract, there could be many numbers. Seems like best thing is to just activate it, but
// have a "rejected" flag which causes the activation to fail. (That way, all the other parties
// will be properly notified, which the server already does.) We don't even need to remove it
// from the outpayment box, because the failure notification from the server will already cause
// the OTClient to remove it from the outpayment box.
//
// Ah-ha! ANY outgoing payment plan or smart contract is necessarily incomplete: it's outgoing
// because it was sent to the next party so he could sign it, too, and probably activate it.
// Presumably he has not done so yet (if I am 'beating him to the punch' by cancelling it before
// he can activate it) and therefore the plan or smart contract still is missing at least one
// signer, so it is GUARANTEED to fail verification if I try to activate it myself. (Good.)
//
// This means I can just take whatever instrument appears outgoing, and try to activate it myself.
// It will definitely fail activation, and then the failure notice will already be sent from that,
// to all the parties, and they can harvest back their numbers automatically as necessary.
//
// The one problem is, though this works for payment plans and smart contracts, it will not work
// for cheques. The cheque is made out to someone else, and he is the one normally who needs to
// deposit it. Plus, I can't deposit a cheque into the same account it's drawn on.
//
// UPDATE: Now when a cheque is deposited into the same account it was drawn on, that will be
// interpreted by the server as a request to CANCEL the cheque.
//
// ------------------------------------------------------------------------
switch(strPaymentType)
{
case ("SMARTCONTRACT")
{
// Just take the smart contract from the outpayment box, and try to
// activate it. It WILL fail, and then the failure message will be
// propagated to all the other parties to the contract. (Which will
// result in its automatic removal from the outpayment box.)
//
if (!VerifyStringVal(strMyAcct))
{