@tradelayerprotocol/tradelayer 1.9.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (249) hide show
  1. package/.claude/settings.local.json +13 -0
  2. package/.claude/skills/tl-algo/SKILL.md +255 -0
  3. package/.gitattributes +2 -0
  4. package/.github/workflows/publish.yaml +26 -0
  5. package/4mm.js +163 -0
  6. package/LICENSE +21 -0
  7. package/NPMSwapRefactor.zip +0 -0
  8. package/README.md +217 -0
  9. package/address.sh +26 -0
  10. package/algoAPI.js +581 -0
  11. package/analyzepsbt.js +92 -0
  12. package/apiEx.js +99 -0
  13. package/bb_hyperscalper.js +290 -0
  14. package/bbo_demo.js +111 -0
  15. package/buyer.js +622 -0
  16. package/client.js +50 -0
  17. package/createTxTest.js +26 -0
  18. package/createWallet.js +75 -0
  19. package/daytrader.js +531 -0
  20. package/decodeTest.js +69 -0
  21. package/fundingManager.js +144 -0
  22. package/index.js +4 -0
  23. package/listener.js +27 -0
  24. package/litecoreTxBuilder.js +1128 -0
  25. package/mmEx.js +356 -0
  26. package/networks.js +51 -0
  27. package/orderbook.js +200 -0
  28. package/package.json +34 -0
  29. package/perTradeQueue.js +36 -0
  30. package/projectsTLNPMTLNPM/package-lock.json +162 -0
  31. package/projectsTLNPMTLNPM/package.json +5 -0
  32. package/quick.js +32 -0
  33. package/quickFut.js +37 -0
  34. package/quickSell.js +37 -0
  35. package/relayerClient.js +117 -0
  36. package/run4mm.js +80 -0
  37. package/run_bbo_tracker.js +241 -0
  38. package/seller.js +443 -0
  39. package/session.js +45 -0
  40. package/setup-lin-ltc.sh +139 -0
  41. package/setup-lin.sh +203 -0
  42. package/setup-win-ltc.bat +108 -0
  43. package/setup-win.bat +167 -0
  44. package/spam_screamer_futures.js +222 -0
  45. package/tradelayer.js/.gitattributes +2 -0
  46. package/tradelayer.js/README.md +2 -0
  47. package/tradelayer.js/oldTests/activationTest.js +6 -0
  48. package/tradelayer.js/oldTests/base58.test.js +23 -0
  49. package/tradelayer.js/oldTests/base64Decode.test.js +16 -0
  50. package/tradelayer.js/oldTests/blocksRefactor.js +140 -0
  51. package/tradelayer.js/oldTests/checkVestBalance.js +25 -0
  52. package/tradelayer.js/oldTests/consensusHashProto.js +151 -0
  53. package/tradelayer.js/oldTests/contractOrderbook.js +243 -0
  54. package/tradelayer.js/oldTests/createPayload.js +0 -0
  55. package/tradelayer.js/oldTests/createTestnetAddr.js +43 -0
  56. package/tradelayer.js/oldTests/decode.js +205 -0
  57. package/tradelayer.js/oldTests/decodeTest.js +50 -0
  58. package/tradelayer.js/oldTests/displayTallyMap.js +19 -0
  59. package/tradelayer.js/oldTests/encodeDecode.js +340 -0
  60. package/tradelayer.js/oldTests/expressTest.js +29 -0
  61. package/tradelayer.js/oldTests/extractBlocksVanilla.js +214 -0
  62. package/tradelayer.js/oldTests/extractBlocksVanillaa.js +179 -0
  63. package/tradelayer.js/oldTests/extractPubkeyTest.js +60 -0
  64. package/tradelayer.js/oldTests/fillInputCacheProto.js +111 -0
  65. package/tradelayer.js/oldTests/getRawTxTest.js +22 -0
  66. package/tradelayer.js/oldTests/indexTest.js +26 -0
  67. package/tradelayer.js/oldTests/initTokensTest.js +32 -0
  68. package/tradelayer.js/oldTests/interfaceChild.js +129 -0
  69. package/tradelayer.js/oldTests/listenerChild.js +112 -0
  70. package/tradelayer.js/oldTests/opdecode.js +26 -0
  71. package/tradelayer.js/oldTests/options.js +79 -0
  72. package/tradelayer.js/oldTests/optxtest.js +116 -0
  73. package/tradelayer.js/oldTests/optxtest1.js +64 -0
  74. package/tradelayer.js/oldTests/oracle.test.js +32 -0
  75. package/tradelayer.js/oldTests/orderbook.test.js +36 -0
  76. package/tradelayer.js/oldTests/parsing.js +93 -0
  77. package/tradelayer.js/oldTests/payload.js +13 -0
  78. package/tradelayer.js/oldTests/persistenceUnitTest.js +23 -0
  79. package/tradelayer.js/oldTests/property.test.js +53 -0
  80. package/tradelayer.js/oldTests/propertyLevel.js +75 -0
  81. package/tradelayer.js/oldTests/propertyTest.js +32 -0
  82. package/tradelayer.js/oldTests/queryAddressTest.js +17 -0
  83. package/tradelayer.js/oldTests/salter.js +14 -0
  84. package/tradelayer.js/oldTests/tally.js +81 -0
  85. package/tradelayer.js/oldTests/tally.test.js +48 -0
  86. package/tradelayer.js/oldTests/tally2.js +124 -0
  87. package/tradelayer.js/oldTests/tally3.js +142 -0
  88. package/tradelayer.js/oldTests/tallyDiag.js +38 -0
  89. package/tradelayer.js/oldTests/testGetRaw.js +40 -0
  90. package/tradelayer.js/oldTests/testHexConvert.js +47 -0
  91. package/tradelayer.js/oldTests/testNewEncoding.js +96 -0
  92. package/tradelayer.js/oldTests/testNewEncoding2.js +113 -0
  93. package/tradelayer.js/oldTests/testNewEncoding3 +112 -0
  94. package/tradelayer.js/oldTests/testNewEncoding3.js +168 -0
  95. package/tradelayer.js/oldTests/testOPReturn.js +102 -0
  96. package/tradelayer.js/oldTests/testPayload.js +23 -0
  97. package/tradelayer.js/oldTests/testRaw.js +50 -0
  98. package/tradelayer.js/oldTests/testSendTooMuch.js +20 -0
  99. package/tradelayer.js/oldTests/testTxBuild +28 -0
  100. package/tradelayer.js/oldTests/testTxBuild.js +42 -0
  101. package/tradelayer.js/oldTests/tokenOrderbook.js +243 -0
  102. package/tradelayer.js/oldTests/txUtilsA.js +515 -0
  103. package/tradelayer.js/oldTests/validityUnitTest.js +53 -0
  104. package/tradelayer.js/oldTests/vaults.js +72 -0
  105. package/tradelayer.js/oldTests/volumeIndex.js +117 -0
  106. package/tradelayer.js/oldTests/volumeIndex2.js +88 -0
  107. package/tradelayer.js/output_base64.txt +1 -0
  108. package/tradelayer.js/package-lock.json +9967 -0
  109. package/tradelayer.js/package.json +61 -0
  110. package/tradelayer.js/server/index.js +88 -0
  111. package/tradelayer.js/server/litecoind.exe +0 -0
  112. package/tradelayer.js/src/activation.js +303 -0
  113. package/tradelayer.js/src/adjuster.js +77 -0
  114. package/tradelayer.js/src/amm.js +400 -0
  115. package/tradelayer.js/src/base256.js +55 -0
  116. package/tradelayer.js/src/base94.js +79 -0
  117. package/tradelayer.js/src/channels.js +1163 -0
  118. package/tradelayer.js/src/clearing.js +3109 -0
  119. package/tradelayer.js/src/clearlist.js +364 -0
  120. package/tradelayer.js/src/client.js +295 -0
  121. package/tradelayer.js/src/consensus.js +613 -0
  122. package/tradelayer.js/src/contractRegistry.js +964 -0
  123. package/tradelayer.js/src/db.js +89 -0
  124. package/tradelayer.js/src/init.js +24 -0
  125. package/tradelayer.js/src/insurance.js +347 -0
  126. package/tradelayer.js/src/interface.js +218 -0
  127. package/tradelayer.js/src/interfaceExpress.js +178 -0
  128. package/tradelayer.js/src/iou.js +509 -0
  129. package/tradelayer.js/src/listener.js +226 -0
  130. package/tradelayer.js/src/logic.js +1702 -0
  131. package/tradelayer.js/src/main.js +927 -0
  132. package/tradelayer.js/src/marginMap.js +2165 -0
  133. package/tradelayer.js/src/options.js +126 -0
  134. package/tradelayer.js/src/oracle.js +394 -0
  135. package/tradelayer.js/src/orderbook.js +4123 -0
  136. package/tradelayer.js/src/persistence.js +554 -0
  137. package/tradelayer.js/src/property.js +411 -0
  138. package/tradelayer.js/src/reOrg.js +41 -0
  139. package/tradelayer.js/src/scaling.js +145 -0
  140. package/tradelayer.js/src/tally.js +1275 -0
  141. package/tradelayer.js/src/tradeHistoryManager.js +552 -0
  142. package/tradelayer.js/src/txDecoder.js +584 -0
  143. package/tradelayer.js/src/txEncoder.js +610 -0
  144. package/tradelayer.js/src/txIndex.js +502 -0
  145. package/tradelayer.js/src/txUtils.js +1392 -0
  146. package/tradelayer.js/src/types.js +429 -0
  147. package/tradelayer.js/src/validity.js +3077 -0
  148. package/tradelayer.js/src/vaults.js +430 -0
  149. package/tradelayer.js/src/vesting.js +491 -0
  150. package/tradelayer.js/src/volumeIndex.js +618 -0
  151. package/tradelayer.js/src/walletInterface.js +220 -0
  152. package/tradelayer.js/src/walletListener.js +665 -0
  153. package/tradelayer.js/tests/256decode.js +82 -0
  154. package/tradelayer.js/tests/UTXOracle.js +205 -0
  155. package/tradelayer.js/tests/base94test.js +23 -0
  156. package/tradelayer.js/tests/cancelTxTest.js +62 -0
  157. package/tradelayer.js/tests/contractInterfaceTest.js +48 -0
  158. package/tradelayer.js/tests/decimalTest.js +65 -0
  159. package/tradelayer.js/tests/decoderTest.js +100 -0
  160. package/tradelayer.js/tests/deltaCount.js +47 -0
  161. package/tradelayer.js/tests/deltaCount2.js +60 -0
  162. package/tradelayer.js/tests/interfaceTest.js +37 -0
  163. package/tradelayer.js/tests/mainTest.js +53 -0
  164. package/tradelayer.js/tests/makeActivationTest.js +24 -0
  165. package/tradelayer.js/tests/maxHeightTest.js +49 -0
  166. package/tradelayer.js/tests/reverseHash.js +72 -0
  167. package/tradelayer.js/tests/sensitiveConsoleOutput.txt +267 -0
  168. package/tradelayer.js/tests/tallyTest.js +40 -0
  169. package/tradelayer.js/tests/testBuybacks.js +46 -0
  170. package/tradelayer.js/tests/testCodeHash.js +49 -0
  171. package/tradelayer.js/tests/testConsensusHash.js +91 -0
  172. package/tradelayer.js/tests/testDecode.js +30 -0
  173. package/tradelayer.js/tests/testEncodingLengths.js +129 -0
  174. package/tradelayer.js/tests/testGetTx +32 -0
  175. package/tradelayer.js/tests/testGetTx.js +32 -0
  176. package/tradelayer.js/tests/testHexHash.js +32 -0
  177. package/tradelayer.js/tests/testIndexHash.js +35 -0
  178. package/tradelayer.js/tests/testInitContracts.js +38 -0
  179. package/tradelayer.js/tests/testMaxConsensus.js +12 -0
  180. package/tradelayer.js/tests/testMaxSynth.js +44 -0
  181. package/tradelayer.js/tests/testMint.js +21 -0
  182. package/tradelayer.js/tests/testNetwork.js +33 -0
  183. package/tradelayer.js/tests/testOrderbookLoad.js +62 -0
  184. package/tradelayer.js/tests/testRebates.js +32 -0
  185. package/tradelayer.js/tests/testRedeem.js +22 -0
  186. package/tradelayer.js/tests/testTokenTrade.js +39 -0
  187. package/tradelayer.js/tests/testTxBuild.js +42 -0
  188. package/tradelayer.js/tests/testUTXOTrade.js +27 -0
  189. package/tradelayer.js/tests/tokenTradeHistory.js +27 -0
  190. package/tradelayer.js/tests/tradeFutures.js +40 -0
  191. package/tradelayer.js/tests/tradeHistoryExample.js +35 -0
  192. package/tradelayer.js/tests/tradeHistoryLoad.js +15 -0
  193. package/tradelayer.js/tests/txScanTest.js +134 -0
  194. package/tradelayer.js/tests/validateTest.js +136 -0
  195. package/tradelayer.js/tests/vestingTest.js +37 -0
  196. package/tradelayer.js/utils/activateMainnet.js +59 -0
  197. package/tradelayer.js/utils/activateMainnetDoge.js +63 -0
  198. package/tradelayer.js/utils/autocompactdb.js +23 -0
  199. package/tradelayer.js/utils/base64toHex.js +32 -0
  200. package/tradelayer.js/utils/broadcastDoge.js +38 -0
  201. package/tradelayer.js/utils/calcRedeem.js +19 -0
  202. package/tradelayer.js/utils/checkNetwork.js +27 -0
  203. package/tradelayer.js/utils/createAddress.js +48 -0
  204. package/tradelayer.js/utils/createAttestation.js +133 -0
  205. package/tradelayer.js/utils/createContract.js +118 -0
  206. package/tradelayer.js/utils/createOracle.js +94 -0
  207. package/tradelayer.js/utils/createwallet.js +20 -0
  208. package/tradelayer.js/utils/crossFuturesTrades.js +57 -0
  209. package/tradelayer.js/utils/crossTokenTrades.js +62 -0
  210. package/tradelayer.js/utils/dumpPriv.js +29 -0
  211. package/tradelayer.js/utils/generateChannel.js +34 -0
  212. package/tradelayer.js/utils/getInfo.js +21 -0
  213. package/tradelayer.js/utils/hardWipe.js +20 -0
  214. package/tradelayer.js/utils/hexTo64.js +16 -0
  215. package/tradelayer.js/utils/importAddress.js +28 -0
  216. package/tradelayer.js/utils/importpriv.js +20 -0
  217. package/tradelayer.js/utils/issueOracleContract.js +67 -0
  218. package/tradelayer.js/utils/issueTokens.js +41 -0
  219. package/tradelayer.js/utils/listunspent.js +66 -0
  220. package/tradelayer.js/utils/litecoinClient.js +30 -0
  221. package/tradelayer.js/utils/loadwallet.js +20 -0
  222. package/tradelayer.js/utils/publishOracle.js +113 -0
  223. package/tradelayer.js/utils/sendActivation.js +21 -0
  224. package/tradelayer.js/utils/sendChannelContractTrade.js +34 -0
  225. package/tradelayer.js/utils/sendChannelTokenTrade.js +34 -0
  226. package/tradelayer.js/utils/sendCommit.js +24 -0
  227. package/tradelayer.js/utils/sendDoge.js +62 -0
  228. package/tradelayer.js/utils/sendDogeMain.js +67 -0
  229. package/tradelayer.js/utils/sendDogeTx.js +46 -0
  230. package/tradelayer.js/utils/sendLTC.js +63 -0
  231. package/tradelayer.js/utils/sendMainnet.js +62 -0
  232. package/tradelayer.js/utils/sendTransfer.js +19 -0
  233. package/tradelayer.js/utils/sendVestTest.js +88 -0
  234. package/tradelayer.js/utils/sendWithdrawal.js +26 -0
  235. package/tradelayer.js/utils/simpleStart.js +8 -0
  236. package/tradelayer.js/utils/startStop.js +27 -0
  237. package/tradelayer.js/utils/structuredTrades.js +136 -0
  238. package/tradelayer.js/utils/verifySignature.js +90 -0
  239. package/tradelayer.js/utils/verifyWitnessAndScriptPubkey.js +41 -0
  240. package/tradelayer.js/utils/walletCache.js +172 -0
  241. package/tradelayer.js/utils/walletContractInterface.js +48 -0
  242. package/tradelayer.js/utils/walletFetchTxs.js +66 -0
  243. package/tradelayer.js/utils/walletUtils.js +97 -0
  244. package/tradelayer.js/utils/wipeDB.js +55 -0
  245. package/tradelayer.js/utils/wipeDBNotTx.js +50 -0
  246. package/txEncoder.js +529 -0
  247. package/utility.js +28 -0
  248. package/verifymessage.js +38 -0
  249. package/ws-transport.js +311 -0
@@ -0,0 +1,81 @@
1
+ class TallyMap {
2
+ constructor() {
3
+ this.addresses = new Map();
4
+ const level = require('level');
5
+ }
6
+
7
+ updateBalance(address, propertyId, amount, available, reserved) {
8
+ if (!this.addresses.has(address)) {
9
+ this.addresses.set(address, {});
10
+ }
11
+ const addressObj = this.addresses.get(address);
12
+
13
+ if (!addressObj[propertyId]) {
14
+ addressObj[propertyId] = { amount: 0, available: 0, reserved: 0 };
15
+ }
16
+
17
+ addressObj[propertyId].amount += amount;
18
+ addressObj[propertyId].available += available;
19
+ addressObj[propertyId].reserved += reserved;
20
+ }
21
+
22
+ getAddressBalances(address) {
23
+ if (!this.addresses.has(address)) {
24
+ return [];
25
+ }
26
+
27
+ const addressObj = this.addresses.get(address);
28
+ const balances = [];
29
+
30
+ for (const propertyId in addressObj) {
31
+ if (Object.hasOwnProperty.call(addressObj, propertyId)) {
32
+ const balanceObj = addressObj[propertyId];
33
+ balances.push({
34
+ propertyId: propertyId,
35
+ amount: balanceObj.amount,
36
+ available: balanceObj.available,
37
+ reserved: balanceObj.reserved,
38
+ });
39
+ }
40
+ }
41
+ }
42
+ return balances;
43
+
44
+ totalTokens(propertyId) {
45
+ let total = 0;
46
+ for (const addressObj of this.addresses.values()) {
47
+ // Check if the addressObj has the propertyId
48
+ if (addressObj[propertyId]) {
49
+ total +=
50
+ addressObj[propertyId].available +
51
+ addressObj[propertyId].reserved; // Assuming these are the properties we want to sum
52
+ }
53
+ }
54
+ return total;
55
+ }
56
+
57
+ async save(blockHeight) {
58
+ const serializedData = JSON.stringify([...this.addresses]);
59
+ await this.db.put(`block-${blockHeight}`, serializedData);
60
+ }
61
+
62
+ async load(blockHeight) {
63
+ try {
64
+ const serializedData = await this.db.get(`block-${blockHeight}`);
65
+ const addressesArray = JSON.parse(serializedData);
66
+ this.addresses = new Map(addressesArray);
67
+ } catch (error) {
68
+ console.error('Error loading data:', error);
69
+ }
70
+ }
71
+ }
72
+
73
+ // Example usage
74
+ const tallyMap = new TallyMap('./path_to_db');
75
+
76
+ tallyMap.updateBalance('address1', 1, 100, 80, 20);
77
+ tallyMap.updateBalance('address1', 2, 200, 150, 50);
78
+ tallyMap.updateBalance('address2', 1, 50, 40, 10);
79
+
80
+ console.log('Address 1 Balances:', tallyMap.getAddressBalances('address1'));
81
+ console.log('Address 2 Balances:', tallyMap.getAddressBalances('address2'));
@@ -0,0 +1,48 @@
1
+ const Tally = require('../tally')
2
+
3
+ describe('=== TALLY TESTS ===', () => {
4
+ path = './dbTL'
5
+ tally = null
6
+
7
+ // beforeAll(async () => {
8
+ // //console.log('--beforeAll');
9
+ // })
10
+
11
+ beforeEach(async () => {
12
+ //console.log('--beforeEach');
13
+ tally = new Tally(path)
14
+ await tally.load()
15
+ });
16
+
17
+ test('PUT', () => {
18
+ tally.updateBalance('a1', 1, 100, 10, 0);
19
+ tally.updateBalance('a1', 2, 200, 20, 0);
20
+ })
21
+
22
+ test('BAL', () => {
23
+ let b1 = tally.getAddressBalances('a1').map(b => b.balance.amount).reduce((a, b) => a + b, 0)
24
+ expect(b1).toBe(300)
25
+ let b2 = tally.getAddressesWithBalanceForProperty(1).map(b => b.amount).reduce((a, b) => a + b, 0)
26
+ expect(b2).toBe(100)
27
+ })
28
+
29
+ test('SUM', () => {
30
+ let sum2 = tally.totalTokens(2)
31
+ expect(sum2).toBe(20)
32
+ let sum1 = tally.totalTokens(1)
33
+ expect(sum1).toBe(10)
34
+ })
35
+
36
+ afterEach(async () => {
37
+ //console.log('--afterEach');
38
+ await tally.save()
39
+ await tally.close()
40
+ })
41
+
42
+ afterAll(async () => {
43
+ //console.log('--afterAll');
44
+ tally = new Tally(path)
45
+ await tally.save()
46
+ await tally.close()
47
+ })
48
+ })
@@ -0,0 +1,124 @@
1
+ class TallyMap {
2
+ constructor(propertyId) {
3
+ this.propertyId = propertyId;
4
+ this.addresses = new Map();
5
+ this.index = TallyMap.getIndex(propertyId);
6
+ this.feeCache = 0;
7
+ TallyMap.registerInstance(this);
8
+ }
9
+
10
+ static getIndex(propertyId) {
11
+ if (!TallyMap.index) {
12
+ TallyMap.index = new Map();
13
+ }
14
+ if (!TallyMap.index.has(propertyId)) {
15
+ TallyMap.index.set(propertyId, new Map());
16
+ }
17
+ return TallyMap.index.get(propertyId);
18
+ }
19
+
20
+ updateAvailableBalance(address, amount) {
21
+ const addressObj = this.getOrCreateAddress(address);
22
+ addressObj.available += amount;
23
+ this.updateFeeCache(-amount);
24
+ }
25
+
26
+ updateOrderReservedBalance(address, amount) {
27
+ const addressObj = this.getOrCreateAddress(address);
28
+ addressObj.orderReserved += amount;
29
+ this.updateFeeCache(-amount);
30
+ }
31
+
32
+ updateCommittedBalance(address, amount) {
33
+ const addressObj = this.getOrCreateAddress(address);
34
+ addressObj.committed += amount;
35
+ this.updateFeeCache(-amount);
36
+ }
37
+
38
+ updateMarginBalance(address, amount) {
39
+ const addressObj = this.getOrCreateAddress(address);
40
+ addressObj.margin += amount;
41
+ this.updateFeeCache(-amount);
42
+ }
43
+
44
+ updateFeeCache(amount) {
45
+ this.feeCache += amount;
46
+ }
47
+
48
+ getOrCreateAddress(address) {
49
+ if (!this.addresses.has(address)) {
50
+ const newAddress = {
51
+ string: address,
52
+ available: 0,
53
+ orderReserved: 0,
54
+ committed: 0,
55
+ margin: 0,
56
+ propertyId: this.propertyId,
57
+ };
58
+ this.addresses.set(address, newAddress);
59
+ }
60
+ return this.addresses.get(address);
61
+ }
62
+
63
+ totalTokens(propertyId) {
64
+ let total = 0;
65
+ for (const addressObj of this.addresses.values()) {
66
+
67
+ total +=
68
+ addressObj.available +
69
+ addressObj.orderReserved +
70
+ addressObj.committed +
71
+ addressObj.margin;
72
+ }
73
+ return total;
74
+ }
75
+
76
+ static registerInstance(instance) {
77
+ // Maintain an array or object to store all the instances
78
+ // For example, using an array:
79
+ if (!TallyMap.instances) {
80
+ TallyMap.instances = [];
81
+ }
82
+ TallyMap.instances.push(instance);
83
+ }
84
+
85
+ static lookupInstancesByAddress(address) {
86
+ // Lookup all the instances that have a matching address
87
+ if (!TallyMap.instances) {
88
+ return [];
89
+ }
90
+ return TallyMap.instances.filter((instance) => instance.address === address);
91
+ }
92
+
93
+ static getAddressBalances(address) {
94
+ const balances = [];
95
+ for (const tallyMapIndex of TallyMap.index.values()) {
96
+ for (const tallyMap of tallyMapIndex.values()) {
97
+ if (tallyMap.addresses.has(address)) {
98
+ const addressObj = tallyMap.addresses.get(address);
99
+ balances.push({
100
+ propertyId: tallyMap.propertyId,
101
+ available: addressObj.available,
102
+ orderReserved: addressObj.orderReserved,
103
+ committed: addressObj.committed,
104
+ margin: addressObj.margin,
105
+ });
106
+ }
107
+ }
108
+ }
109
+ return balances;
110
+ }
111
+ }
112
+
113
+
114
+
115
+ // Example usage
116
+ const tallyMap1 = new TallyMap(1);
117
+ tallyMap1.updateAvailableBalance('address1', 100);
118
+ tallyMap1.updateOrderReservedBalance('address2', 50);
119
+
120
+ const tallyMap2 = new TallyMap(2);
121
+ tallyMap2.updateAvailableBalance('address1', 200);
122
+ tallyMap2.updateCommittedBalance('address2', 70);
123
+
124
+ console.log('Address Balances:', TallyMap.getAddressBalances('address1'));
@@ -0,0 +1,142 @@
1
+ var tallyMapDB = require('./db')
2
+
3
+ class TallyMap {
4
+ static instance;
5
+
6
+ constructor(path) {
7
+ if (!TallyMap.instance) {
8
+ this.addresses = new Map();
9
+ TallyMap.instance = this;
10
+ }
11
+ return TallyMap.instance;
12
+ }
13
+
14
+ updateBalance(address, propertyId, amount, available, reserved) {
15
+ if (!this.addresses.has(address)) {
16
+ this.addresses.set(address, {});
17
+ }
18
+ const addressObj = this.addresses.get(address);
19
+
20
+ if (!addressObj[propertyId]) {
21
+ addressObj[propertyId] = { amount: 0, available: 0, reserved: 0 };
22
+ }
23
+
24
+ addressObj[propertyId].amount += amount;
25
+ addressObj[propertyId].available += available;
26
+ addressObj[propertyId].reserved += reserved;
27
+ }
28
+
29
+ getAddressBalances(address) {
30
+ if (!this.addresses.has(address)) {
31
+ return [];
32
+ }
33
+
34
+ const addressObj = this.addresses.get(address);
35
+ const balances = [];
36
+
37
+ for (const propertyId in addressObj) {
38
+ if (Object.hasOwnProperty.call(addressObj, propertyId)) {
39
+ const balanceObj = addressObj[propertyId];
40
+ balances.push({
41
+ propertyId: propertyId,
42
+ amount: balanceObj.amount,
43
+ available: balanceObj.available,
44
+ reserved: balanceObj.reserved,
45
+ });
46
+ }
47
+ }
48
+ return balances;
49
+ }
50
+
51
+ totalTokens(propertyId) {
52
+ let total = 0;
53
+ for (const addressObj of this.addresses.values()) {
54
+ if (addressObj[propertyId]) {
55
+ total += addressObj[propertyId].available + addressObj[propertyId].reserved;
56
+ }
57
+ }
58
+ return total;
59
+ }
60
+
61
+ async save(blockHeight) {
62
+ const serializedData = JSON.stringify([...this.addresses]);
63
+ await this.db.put(`block-${blockHeight}`, serializedData);
64
+ }
65
+
66
+ async load(blockHeight) {
67
+ try {
68
+ const serializedData = await this.db.get(`block-${blockHeight}`);
69
+ const addressesArray = JSON.parse(serializedData);
70
+ this.addresses = new Map(addressesArray);
71
+ } catch (error) {
72
+ console.error('Error loading data:', error);
73
+ }
74
+ }
75
+
76
+ static getSingletonInstance() {
77
+ if (!TallyMap.instance) {
78
+ throw new Error("TallyMap instance has not been created yet");
79
+ }
80
+ return TallyMap.instance;
81
+ }
82
+
83
+ // Get the tally for a specific address and property
84
+ getTally(address, propertyId) {
85
+ const key = `${address}_${propertyId}`;
86
+ return this.tallyMap.get(key) || 0;
87
+ }
88
+
89
+ // Save the tally map to LevelDB
90
+ async saveTallyMap() {
91
+ const serializedMap = JSON.stringify(Array.from(this.tallyMap.entries()));
92
+ await this.dbInterface.storeData('tallyMap', serializedMap);
93
+ }
94
+
95
+ // Load the tally map from LevelDB
96
+ async loadTallyMap() {
97
+ const serializedMap = await this.dbInterface.getData('tallyMap');
98
+ if (serializedMap) {
99
+ this.tallyMap = new Map(JSON.parse(serializedMap));
100
+ }
101
+ }
102
+
103
+ getAddressBalances(address) {
104
+ const balances = [];
105
+ if (this.addresses.has(address)) {
106
+ const properties = this.addresses.get(address);
107
+ for (const [propertyId, balanceData] of Object.entries(properties)) {
108
+ balances.push({
109
+ propertyId: propertyId,
110
+ balance: balanceData
111
+ });
112
+ }
113
+ }
114
+ return balances;
115
+ }
116
+
117
+ /**
118
+ * Retrieves all addresses that have a balance for a given property.
119
+ * @param {number} propertyId - The property ID to check balances for.
120
+ * @return {Array} - An array of addresses that have a balance for the specified property.
121
+ */
122
+ getAddressesWithBalanceForProperty(propertyId) {
123
+ const addressesWithBalances = [];
124
+
125
+ for (const [address, balances] of this.addresses.entries()) {
126
+ if (balances[propertyId]) {
127
+ const balanceInfo = balances[propertyId];
128
+ if (balanceInfo.amount > 0 || balanceInfo.reserved > 0) {
129
+ addressesWithBalances.push({
130
+ address: address,
131
+ amount: balanceInfo.amount,
132
+ reserved: balanceInfo.reserved
133
+ });
134
+ }
135
+ }
136
+ }
137
+
138
+ return addressesWithBalances;
139
+ }
140
+ }
141
+
142
+ module.exports = TallyMap;
@@ -0,0 +1,38 @@
1
+ const TallyMap = require('./tally.js'); // Adjust the path
2
+
3
+ async function runUnitTest() {
4
+ console.log('Starting TallyMap Unit Test');
5
+
6
+ // Step 1: Initialize TallyMap
7
+ const blockHeight = 100; // Example block height
8
+ const tallyMap = await TallyMap.getInstance(blockHeight);
9
+
10
+ // Step 2: Update TallyMap
11
+ const address = 'test-address';
12
+ const propertyId = '1';
13
+ const amountChange = 100;
14
+ const availableChange = 50;
15
+ const reservedChange = 30;
16
+ const vestingChange = 20;
17
+
18
+ try {
19
+ await TallyMap.updateBalance(address, propertyId, amountChange, availableChange, reservedChange, vestingChange);
20
+ console.log('Balance updated successfully');
21
+ } catch (error) {
22
+ console.error('Error updating balance:', error);
23
+ }
24
+
25
+ // Step 3: Output Serialized TallyMap
26
+ const serializedTallyMap = JSON.stringify([...tallyMap.addresses]);
27
+ console.log('Serialized TallyMap:', serializedTallyMap);
28
+
29
+ // Extra Credit: Save to DB
30
+ try {
31
+ await tallyMap.saveToDB();
32
+ console.log('TallyMap saved to DB successfully');
33
+ } catch (error) {
34
+ console.error('Error saving TallyMap to DB:', error);
35
+ }
36
+ }
37
+
38
+ runUnitTest();
@@ -0,0 +1,40 @@
1
+ const Litecoin = require('litecoin'); // Replace with your actual library import
2
+ const util = require('util');
3
+
4
+ // Configure your Litecoin client
5
+ const client = new Litecoin.Client({
6
+ host: '127.0.0.1',
7
+ port: 18332,
8
+ user: 'user',
9
+ pass: 'pass',
10
+ timeout: 10000
11
+ });
12
+
13
+ // Promisify the getRawTransaction function
14
+ const getRawTransactionAsync = util.promisify(client.getRawTransaction.bind(client));
15
+
16
+ // List of txids to test
17
+ const txids = [
18
+ '5b0b0c97398dfcfc62c41f80c4f1c64d5220bd3a9e99d3a68a1ed514b93c6ab0',
19
+ 'e2c24ba7e7694cfdc3895e5690ab13b7a8dd8f061ae91fd6e58d896a6cf74737',
20
+ '3a9c8d7ddb4432f36d6d3d858354fa22acbee9e95bb512dc097ffb456c0d357a',
21
+ '19709105715a195f03cdb05cedb38b14f8d215418dc2ef416e083b40021e6fee'
22
+
23
+ // Add more txids here for testing
24
+ ];
25
+
26
+ // Function to test getRawTransaction for each txid
27
+ async function testGetRawTransaction() {
28
+ for (const txid of txids) {
29
+ try {
30
+ console.log(`Fetching transaction for txid: ${txid}`);
31
+ const transaction = await getRawTransactionAsync(txid, true);
32
+ console.log(`Transaction:`, transaction);
33
+ } catch (error) {
34
+ console.error(`Error fetching transaction for txid ${txid}:`, error);
35
+ }
36
+ }
37
+ }
38
+
39
+ // Run the test
40
+ testGetRawTransaction();
@@ -0,0 +1,47 @@
1
+ const BASE58_ALPHABET = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz';
2
+
3
+ function hexToBase58(hexStr) {
4
+ let num = BigInt('0x' + hexStr);
5
+ let base58 = '';
6
+ while (num > 0) {
7
+ const div = num / BigInt(BASE58_ALPHABET.length);
8
+ const mod = num % BigInt(BASE58_ALPHABET.length);
9
+ base58 = BASE58_ALPHABET[Number(mod)] + base58;
10
+ num = div;
11
+ }
12
+ // Add '1' for each leading '00' byte as per Base58 encoding
13
+ for (let i = 0; hexStr[i] === '0' && i < hexStr.length - 1; i += 2) {
14
+ base58 = '1' + base58;
15
+ }
16
+ return base58;
17
+ }
18
+
19
+ function base58ToHex(base58Str) {
20
+ let num = BigInt(0);
21
+ for (let char of base58Str) {
22
+ const charIndex = BASE58_ALPHABET.indexOf(char);
23
+ if (charIndex < 0) {
24
+ throw new Error(`Invalid character found: ${char}`);
25
+ }
26
+ num = num * BigInt(BASE58_ALPHABET.length) + BigInt(charIndex);
27
+ }
28
+ let hex = num.toString(16);
29
+ // Add '00' for each leading '1' as per Base58 encoding
30
+ for (let i = 0; base58Str[i] === '1' && i < base58Str.length - 1; i++) {
31
+ hex = '00' + hex;
32
+ }
33
+ // Ensure even length for the hex string
34
+ if (hex.length % 2) {
35
+ hex = '0' + hex;
36
+ }
37
+ return hex.toUpperCase();
38
+ }
39
+
40
+ // Example usage
41
+ const hexValue = 'd745ac8daf473ba7d7f9dc2d426beca18af97ba53f1f0e143761782c65c6cffe';
42
+ const base58Value = hexToBase58(hexValue);
43
+ const convertedBackHex = base58ToHex(base58Value);
44
+
45
+ console.log('Hex:', hexValue);
46
+ console.log('Base58:', base58Value);
47
+ console.log('Converted Back Hex:', convertedBackHex);
@@ -0,0 +1,96 @@
1
+ // Encode the transaction to base 256
2
+ function encodeOmniTransaction(type, params) {
3
+ const payload = [type];
4
+
5
+ switch (type) {
6
+ case 1:
7
+ payload.push(params.type);
8
+ payload.push(params.initialAmount);
9
+ payload.push(params.ticker);
10
+ payload.push(params.whitelists || 0);
11
+ break;
12
+ case 2:
13
+ payload.push(params.propertyId);
14
+ payload.push(params.amount);
15
+ break;
16
+ case 3:
17
+ payload.push(params.propertyId);
18
+ payload.push(params.amount);
19
+ payload.push(params.satsExpected);
20
+ break;
21
+ case 4:
22
+ payload.push(params.propertyId);
23
+ payload.push(params.amount);
24
+ break;
25
+ case 5:
26
+ payload.push(params.propertyId);
27
+ payload.push(params.propertyIdDesired);
28
+ payload.push(params.amountOffered);
29
+ payload.push(params.amountExpected);
30
+ break;
31
+ default:
32
+ throw new Error("Unknown transaction type");
33
+ }
34
+
35
+ const encodedPayload = payload.map(num => num.toString(16).padStart(2, '0')).join('');
36
+ return encodedPayload;
37
+ }
38
+
39
+ // Decode the base 256 payload to a transaction object
40
+ function decodeOmniTransaction(encodedPayload) {
41
+ const type = parseInt(encodedPayload.substr(0, 2), 16);
42
+ const params = {};
43
+
44
+ const payloadData = encodedPayload.substr(2);
45
+
46
+ switch (type) {
47
+ case 1:
48
+ params.type = parseInt(payloadData.substr(0, 2), 16);
49
+ params.initialAmount = parseInt(payloadData.substr(2, 8), 16);
50
+ params.ticker = payloadData.substr(10, 12);
51
+ params.whitelists = parseInt(payloadData.substr(22, 2), 16);
52
+ break;
53
+ case 2:
54
+ params.propertyId = parseInt(payloadData.substr(0, 8), 16);
55
+ params.amount = parseInt(payloadData.substr(8, 8), 16);
56
+ break;
57
+ case 3:
58
+ params.propertyId = parseInt(payloadData.substr(0, 8), 16);
59
+ params.amount = parseInt(payloadData.substr(8, 8), 16);
60
+ params.satsExpected = parseInt(payloadData.substr(16, 8), 16);
61
+ break;
62
+ case 4:
63
+ params.propertyId = parseInt(payloadData.substr(0, 8), 16);
64
+ params.amount = parseInt(payloadData.substr(8, 8), 16);
65
+ break;
66
+ case 5:
67
+ params.propertyId = parseInt(payloadData.substr(0, 8), 16);
68
+ params.propertyIdDesired = parseInt(payloadData.substr(8, 8), 16);
69
+ params.amountOffered = parseInt(payloadData.substr(16, 8), 16);
70
+ params.amountExpected = parseInt(payloadData.substr(24, 8), 16);
71
+ break;
72
+ default:
73
+ throw new Error("Unknown transaction type");
74
+ }
75
+
76
+ return { type, params };
77
+ }
78
+
79
+ // Logic test: Check if the encoded payload is <= 40 bytes
80
+ function isPayloadSizeValid(encodedPayload) {
81
+ return encodedPayload.length <= 40;
82
+ }
83
+
84
+ // Example Usage:
85
+ const tx = encodeOmniTransaction(1, {
86
+ type: 1,
87
+ initialAmount: 1000,
88
+ ticker: "TOKEN",
89
+ whitelists: 2,
90
+ });
91
+
92
+ console.log("Encoded Payload:", tx);
93
+ console.log("Is Payload Size Valid:", isPayloadSizeValid(tx));
94
+
95
+ const decodedTx = decodeOmniTransaction(tx);
96
+ console.log("Decoded Transaction:", decodedTx);