@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.
- package/.claude/settings.local.json +13 -0
- package/.claude/skills/tl-algo/SKILL.md +255 -0
- package/.gitattributes +2 -0
- package/.github/workflows/publish.yaml +26 -0
- package/4mm.js +163 -0
- package/LICENSE +21 -0
- package/NPMSwapRefactor.zip +0 -0
- package/README.md +217 -0
- package/address.sh +26 -0
- package/algoAPI.js +581 -0
- package/analyzepsbt.js +92 -0
- package/apiEx.js +99 -0
- package/bb_hyperscalper.js +290 -0
- package/bbo_demo.js +111 -0
- package/buyer.js +622 -0
- package/client.js +50 -0
- package/createTxTest.js +26 -0
- package/createWallet.js +75 -0
- package/daytrader.js +531 -0
- package/decodeTest.js +69 -0
- package/fundingManager.js +144 -0
- package/index.js +4 -0
- package/listener.js +27 -0
- package/litecoreTxBuilder.js +1128 -0
- package/mmEx.js +356 -0
- package/networks.js +51 -0
- package/orderbook.js +200 -0
- package/package.json +34 -0
- package/perTradeQueue.js +36 -0
- package/projectsTLNPMTLNPM/package-lock.json +162 -0
- package/projectsTLNPMTLNPM/package.json +5 -0
- package/quick.js +32 -0
- package/quickFut.js +37 -0
- package/quickSell.js +37 -0
- package/relayerClient.js +117 -0
- package/run4mm.js +80 -0
- package/run_bbo_tracker.js +241 -0
- package/seller.js +443 -0
- package/session.js +45 -0
- package/setup-lin-ltc.sh +139 -0
- package/setup-lin.sh +203 -0
- package/setup-win-ltc.bat +108 -0
- package/setup-win.bat +167 -0
- package/spam_screamer_futures.js +222 -0
- package/tradelayer.js/.gitattributes +2 -0
- package/tradelayer.js/README.md +2 -0
- package/tradelayer.js/oldTests/activationTest.js +6 -0
- package/tradelayer.js/oldTests/base58.test.js +23 -0
- package/tradelayer.js/oldTests/base64Decode.test.js +16 -0
- package/tradelayer.js/oldTests/blocksRefactor.js +140 -0
- package/tradelayer.js/oldTests/checkVestBalance.js +25 -0
- package/tradelayer.js/oldTests/consensusHashProto.js +151 -0
- package/tradelayer.js/oldTests/contractOrderbook.js +243 -0
- package/tradelayer.js/oldTests/createPayload.js +0 -0
- package/tradelayer.js/oldTests/createTestnetAddr.js +43 -0
- package/tradelayer.js/oldTests/decode.js +205 -0
- package/tradelayer.js/oldTests/decodeTest.js +50 -0
- package/tradelayer.js/oldTests/displayTallyMap.js +19 -0
- package/tradelayer.js/oldTests/encodeDecode.js +340 -0
- package/tradelayer.js/oldTests/expressTest.js +29 -0
- package/tradelayer.js/oldTests/extractBlocksVanilla.js +214 -0
- package/tradelayer.js/oldTests/extractBlocksVanillaa.js +179 -0
- package/tradelayer.js/oldTests/extractPubkeyTest.js +60 -0
- package/tradelayer.js/oldTests/fillInputCacheProto.js +111 -0
- package/tradelayer.js/oldTests/getRawTxTest.js +22 -0
- package/tradelayer.js/oldTests/indexTest.js +26 -0
- package/tradelayer.js/oldTests/initTokensTest.js +32 -0
- package/tradelayer.js/oldTests/interfaceChild.js +129 -0
- package/tradelayer.js/oldTests/listenerChild.js +112 -0
- package/tradelayer.js/oldTests/opdecode.js +26 -0
- package/tradelayer.js/oldTests/options.js +79 -0
- package/tradelayer.js/oldTests/optxtest.js +116 -0
- package/tradelayer.js/oldTests/optxtest1.js +64 -0
- package/tradelayer.js/oldTests/oracle.test.js +32 -0
- package/tradelayer.js/oldTests/orderbook.test.js +36 -0
- package/tradelayer.js/oldTests/parsing.js +93 -0
- package/tradelayer.js/oldTests/payload.js +13 -0
- package/tradelayer.js/oldTests/persistenceUnitTest.js +23 -0
- package/tradelayer.js/oldTests/property.test.js +53 -0
- package/tradelayer.js/oldTests/propertyLevel.js +75 -0
- package/tradelayer.js/oldTests/propertyTest.js +32 -0
- package/tradelayer.js/oldTests/queryAddressTest.js +17 -0
- package/tradelayer.js/oldTests/salter.js +14 -0
- package/tradelayer.js/oldTests/tally.js +81 -0
- package/tradelayer.js/oldTests/tally.test.js +48 -0
- package/tradelayer.js/oldTests/tally2.js +124 -0
- package/tradelayer.js/oldTests/tally3.js +142 -0
- package/tradelayer.js/oldTests/tallyDiag.js +38 -0
- package/tradelayer.js/oldTests/testGetRaw.js +40 -0
- package/tradelayer.js/oldTests/testHexConvert.js +47 -0
- package/tradelayer.js/oldTests/testNewEncoding.js +96 -0
- package/tradelayer.js/oldTests/testNewEncoding2.js +113 -0
- package/tradelayer.js/oldTests/testNewEncoding3 +112 -0
- package/tradelayer.js/oldTests/testNewEncoding3.js +168 -0
- package/tradelayer.js/oldTests/testOPReturn.js +102 -0
- package/tradelayer.js/oldTests/testPayload.js +23 -0
- package/tradelayer.js/oldTests/testRaw.js +50 -0
- package/tradelayer.js/oldTests/testSendTooMuch.js +20 -0
- package/tradelayer.js/oldTests/testTxBuild +28 -0
- package/tradelayer.js/oldTests/testTxBuild.js +42 -0
- package/tradelayer.js/oldTests/tokenOrderbook.js +243 -0
- package/tradelayer.js/oldTests/txUtilsA.js +515 -0
- package/tradelayer.js/oldTests/validityUnitTest.js +53 -0
- package/tradelayer.js/oldTests/vaults.js +72 -0
- package/tradelayer.js/oldTests/volumeIndex.js +117 -0
- package/tradelayer.js/oldTests/volumeIndex2.js +88 -0
- package/tradelayer.js/output_base64.txt +1 -0
- package/tradelayer.js/package-lock.json +9967 -0
- package/tradelayer.js/package.json +61 -0
- package/tradelayer.js/server/index.js +88 -0
- package/tradelayer.js/server/litecoind.exe +0 -0
- package/tradelayer.js/src/activation.js +303 -0
- package/tradelayer.js/src/adjuster.js +77 -0
- package/tradelayer.js/src/amm.js +400 -0
- package/tradelayer.js/src/base256.js +55 -0
- package/tradelayer.js/src/base94.js +79 -0
- package/tradelayer.js/src/channels.js +1163 -0
- package/tradelayer.js/src/clearing.js +3109 -0
- package/tradelayer.js/src/clearlist.js +364 -0
- package/tradelayer.js/src/client.js +295 -0
- package/tradelayer.js/src/consensus.js +613 -0
- package/tradelayer.js/src/contractRegistry.js +964 -0
- package/tradelayer.js/src/db.js +89 -0
- package/tradelayer.js/src/init.js +24 -0
- package/tradelayer.js/src/insurance.js +347 -0
- package/tradelayer.js/src/interface.js +218 -0
- package/tradelayer.js/src/interfaceExpress.js +178 -0
- package/tradelayer.js/src/iou.js +509 -0
- package/tradelayer.js/src/listener.js +226 -0
- package/tradelayer.js/src/logic.js +1702 -0
- package/tradelayer.js/src/main.js +927 -0
- package/tradelayer.js/src/marginMap.js +2165 -0
- package/tradelayer.js/src/options.js +126 -0
- package/tradelayer.js/src/oracle.js +394 -0
- package/tradelayer.js/src/orderbook.js +4123 -0
- package/tradelayer.js/src/persistence.js +554 -0
- package/tradelayer.js/src/property.js +411 -0
- package/tradelayer.js/src/reOrg.js +41 -0
- package/tradelayer.js/src/scaling.js +145 -0
- package/tradelayer.js/src/tally.js +1275 -0
- package/tradelayer.js/src/tradeHistoryManager.js +552 -0
- package/tradelayer.js/src/txDecoder.js +584 -0
- package/tradelayer.js/src/txEncoder.js +610 -0
- package/tradelayer.js/src/txIndex.js +502 -0
- package/tradelayer.js/src/txUtils.js +1392 -0
- package/tradelayer.js/src/types.js +429 -0
- package/tradelayer.js/src/validity.js +3077 -0
- package/tradelayer.js/src/vaults.js +430 -0
- package/tradelayer.js/src/vesting.js +491 -0
- package/tradelayer.js/src/volumeIndex.js +618 -0
- package/tradelayer.js/src/walletInterface.js +220 -0
- package/tradelayer.js/src/walletListener.js +665 -0
- package/tradelayer.js/tests/256decode.js +82 -0
- package/tradelayer.js/tests/UTXOracle.js +205 -0
- package/tradelayer.js/tests/base94test.js +23 -0
- package/tradelayer.js/tests/cancelTxTest.js +62 -0
- package/tradelayer.js/tests/contractInterfaceTest.js +48 -0
- package/tradelayer.js/tests/decimalTest.js +65 -0
- package/tradelayer.js/tests/decoderTest.js +100 -0
- package/tradelayer.js/tests/deltaCount.js +47 -0
- package/tradelayer.js/tests/deltaCount2.js +60 -0
- package/tradelayer.js/tests/interfaceTest.js +37 -0
- package/tradelayer.js/tests/mainTest.js +53 -0
- package/tradelayer.js/tests/makeActivationTest.js +24 -0
- package/tradelayer.js/tests/maxHeightTest.js +49 -0
- package/tradelayer.js/tests/reverseHash.js +72 -0
- package/tradelayer.js/tests/sensitiveConsoleOutput.txt +267 -0
- package/tradelayer.js/tests/tallyTest.js +40 -0
- package/tradelayer.js/tests/testBuybacks.js +46 -0
- package/tradelayer.js/tests/testCodeHash.js +49 -0
- package/tradelayer.js/tests/testConsensusHash.js +91 -0
- package/tradelayer.js/tests/testDecode.js +30 -0
- package/tradelayer.js/tests/testEncodingLengths.js +129 -0
- package/tradelayer.js/tests/testGetTx +32 -0
- package/tradelayer.js/tests/testGetTx.js +32 -0
- package/tradelayer.js/tests/testHexHash.js +32 -0
- package/tradelayer.js/tests/testIndexHash.js +35 -0
- package/tradelayer.js/tests/testInitContracts.js +38 -0
- package/tradelayer.js/tests/testMaxConsensus.js +12 -0
- package/tradelayer.js/tests/testMaxSynth.js +44 -0
- package/tradelayer.js/tests/testMint.js +21 -0
- package/tradelayer.js/tests/testNetwork.js +33 -0
- package/tradelayer.js/tests/testOrderbookLoad.js +62 -0
- package/tradelayer.js/tests/testRebates.js +32 -0
- package/tradelayer.js/tests/testRedeem.js +22 -0
- package/tradelayer.js/tests/testTokenTrade.js +39 -0
- package/tradelayer.js/tests/testTxBuild.js +42 -0
- package/tradelayer.js/tests/testUTXOTrade.js +27 -0
- package/tradelayer.js/tests/tokenTradeHistory.js +27 -0
- package/tradelayer.js/tests/tradeFutures.js +40 -0
- package/tradelayer.js/tests/tradeHistoryExample.js +35 -0
- package/tradelayer.js/tests/tradeHistoryLoad.js +15 -0
- package/tradelayer.js/tests/txScanTest.js +134 -0
- package/tradelayer.js/tests/validateTest.js +136 -0
- package/tradelayer.js/tests/vestingTest.js +37 -0
- package/tradelayer.js/utils/activateMainnet.js +59 -0
- package/tradelayer.js/utils/activateMainnetDoge.js +63 -0
- package/tradelayer.js/utils/autocompactdb.js +23 -0
- package/tradelayer.js/utils/base64toHex.js +32 -0
- package/tradelayer.js/utils/broadcastDoge.js +38 -0
- package/tradelayer.js/utils/calcRedeem.js +19 -0
- package/tradelayer.js/utils/checkNetwork.js +27 -0
- package/tradelayer.js/utils/createAddress.js +48 -0
- package/tradelayer.js/utils/createAttestation.js +133 -0
- package/tradelayer.js/utils/createContract.js +118 -0
- package/tradelayer.js/utils/createOracle.js +94 -0
- package/tradelayer.js/utils/createwallet.js +20 -0
- package/tradelayer.js/utils/crossFuturesTrades.js +57 -0
- package/tradelayer.js/utils/crossTokenTrades.js +62 -0
- package/tradelayer.js/utils/dumpPriv.js +29 -0
- package/tradelayer.js/utils/generateChannel.js +34 -0
- package/tradelayer.js/utils/getInfo.js +21 -0
- package/tradelayer.js/utils/hardWipe.js +20 -0
- package/tradelayer.js/utils/hexTo64.js +16 -0
- package/tradelayer.js/utils/importAddress.js +28 -0
- package/tradelayer.js/utils/importpriv.js +20 -0
- package/tradelayer.js/utils/issueOracleContract.js +67 -0
- package/tradelayer.js/utils/issueTokens.js +41 -0
- package/tradelayer.js/utils/listunspent.js +66 -0
- package/tradelayer.js/utils/litecoinClient.js +30 -0
- package/tradelayer.js/utils/loadwallet.js +20 -0
- package/tradelayer.js/utils/publishOracle.js +113 -0
- package/tradelayer.js/utils/sendActivation.js +21 -0
- package/tradelayer.js/utils/sendChannelContractTrade.js +34 -0
- package/tradelayer.js/utils/sendChannelTokenTrade.js +34 -0
- package/tradelayer.js/utils/sendCommit.js +24 -0
- package/tradelayer.js/utils/sendDoge.js +62 -0
- package/tradelayer.js/utils/sendDogeMain.js +67 -0
- package/tradelayer.js/utils/sendDogeTx.js +46 -0
- package/tradelayer.js/utils/sendLTC.js +63 -0
- package/tradelayer.js/utils/sendMainnet.js +62 -0
- package/tradelayer.js/utils/sendTransfer.js +19 -0
- package/tradelayer.js/utils/sendVestTest.js +88 -0
- package/tradelayer.js/utils/sendWithdrawal.js +26 -0
- package/tradelayer.js/utils/simpleStart.js +8 -0
- package/tradelayer.js/utils/startStop.js +27 -0
- package/tradelayer.js/utils/structuredTrades.js +136 -0
- package/tradelayer.js/utils/verifySignature.js +90 -0
- package/tradelayer.js/utils/verifyWitnessAndScriptPubkey.js +41 -0
- package/tradelayer.js/utils/walletCache.js +172 -0
- package/tradelayer.js/utils/walletContractInterface.js +48 -0
- package/tradelayer.js/utils/walletFetchTxs.js +66 -0
- package/tradelayer.js/utils/walletUtils.js +97 -0
- package/tradelayer.js/utils/wipeDB.js +55 -0
- package/tradelayer.js/utils/wipeDBNotTx.js +50 -0
- package/txEncoder.js +529 -0
- package/utility.js +28 -0
- package/verifymessage.js +38 -0
- package/ws-transport.js +311 -0
|
@@ -0,0 +1,491 @@
|
|
|
1
|
+
const InsuranceFund = require('./insurance.js');
|
|
2
|
+
const PropertyManager = require('./property.js'); // Assuming Property has the createToken method
|
|
3
|
+
const ClearList = require('./clearlist.js')
|
|
4
|
+
const ContractList = require('./contractRegistry.js')
|
|
5
|
+
const BigNumber = require('bignumber.js')
|
|
6
|
+
const ClientWrapper = require('./client.js')
|
|
7
|
+
//const math = require('mathjs');
|
|
8
|
+
|
|
9
|
+
class TradeLayerManager {
|
|
10
|
+
static instance = null;
|
|
11
|
+
|
|
12
|
+
constructor(adminAddress, chain, test) {
|
|
13
|
+
if (!TradeLayerManager.instance) {
|
|
14
|
+
this.adminAddress = adminAddress;
|
|
15
|
+
this.chain = chain; // Temporarily set
|
|
16
|
+
this.test = test
|
|
17
|
+
TradeLayerManager.instance = this;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
getChain(){
|
|
22
|
+
return this.chain
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
getTest(){
|
|
26
|
+
return this.test
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
async ensureChainInitialized() {
|
|
30
|
+
console.log("Checking for valid chain..."+this.chain+' '+this.test);
|
|
31
|
+
|
|
32
|
+
// Skip initialization if chain and test are already set
|
|
33
|
+
|
|
34
|
+
if (this.chain && this.test !== undefined) {
|
|
35
|
+
console.log(`Chain already initialized: ${this.chain}, Test: ${this.test}`);
|
|
36
|
+
this.setChainParams(this.chain);
|
|
37
|
+
return;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
const client = await ClientWrapper.getInstance();
|
|
41
|
+
let attempts = 0;
|
|
42
|
+
const maxAttempts = 10; // Maximum retries
|
|
43
|
+
const delay = 2000; // Delay between retries (ms)
|
|
44
|
+
|
|
45
|
+
while (attempts < maxAttempts) {
|
|
46
|
+
try {
|
|
47
|
+
const chainInfo = await client.getChain();
|
|
48
|
+
const testInfo = await client.getTest();
|
|
49
|
+
console.log('chain and test '+chainInfo+' '+testInfo)
|
|
50
|
+
if (chainInfo && testInfo!==undefined) {
|
|
51
|
+
this.chain = chainInfo;
|
|
52
|
+
this.test = testInfo;
|
|
53
|
+
console.log(`Chain initialized: ${this.chain}`);
|
|
54
|
+
this.setChainParams(this.chain);
|
|
55
|
+
return;
|
|
56
|
+
}
|
|
57
|
+
} catch (error) {
|
|
58
|
+
console.error(`Error fetching chain status (attempt ${attempts + 1}):`, error);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
attempts++;
|
|
62
|
+
console.log(`Retrying chain initialization... (${attempts}/${maxAttempts})`);
|
|
63
|
+
await new Promise((resolve) => setTimeout(resolve, delay));
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
// Fallback in case of failure
|
|
67
|
+
console.error("Failed to initialize chain. Using default chain.");
|
|
68
|
+
this.chain = "BTC"; // Default to BTC if initialization fails
|
|
69
|
+
this.setChainParams(this.chain);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
setChainParams(chain) {
|
|
73
|
+
// Access the chain via the client or environment variable
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
// Configure parameters based on the chain
|
|
77
|
+
if (this.chain === 'BTC') {
|
|
78
|
+
this.baseVolume = 100;
|
|
79
|
+
this.minRebate = 0.00000625;
|
|
80
|
+
this.maxRebate = 0.0001;
|
|
81
|
+
this.initialTokenAmount = 100000;
|
|
82
|
+
this.tickerSymbol = 'TL';
|
|
83
|
+
this.hedgeLeverage = 5;
|
|
84
|
+
this.expiryInterval = 12960
|
|
85
|
+
this.sponsorAddress = "bc1qhc2cj60auf67e0pa3dfd46cvg0fehchx56vw0f"
|
|
86
|
+
this.freePortion = 0.1
|
|
87
|
+
this.insurancePortion = 0.1
|
|
88
|
+
this.vestingPortion = 0.5
|
|
89
|
+
this.salePortion = 0
|
|
90
|
+
} else if (this.chain === 'DOGE') {
|
|
91
|
+
this.baseVolume = 2000000;
|
|
92
|
+
this.minRebate = 0.00000125;
|
|
93
|
+
this.maxRebate = 0.0005;
|
|
94
|
+
this.initialTokenAmount = 200000000;
|
|
95
|
+
this.tickerSymbol = 'TD';
|
|
96
|
+
this.hedgeLeverage = 10;
|
|
97
|
+
this.expiryInterval = 129600
|
|
98
|
+
this.sponsorAddress = "D8HA73pAhxK7eNXSUVhQrWpUkrszUDGs7Z"
|
|
99
|
+
this.freePortion = 0.25
|
|
100
|
+
this.insurancePortion = 0.25
|
|
101
|
+
this.vestingPortion = 0.5
|
|
102
|
+
this.salePortion = 0
|
|
103
|
+
} else { // Default to Litecoin (LTC)
|
|
104
|
+
this.baseVolume = 1000;
|
|
105
|
+
this.minRebate = 0.000003125;
|
|
106
|
+
this.maxRebate = 0.0001;
|
|
107
|
+
this.initialTokenAmount = 500000;
|
|
108
|
+
this.tickerSymbol = 'TLITE';
|
|
109
|
+
this.hedgeLeverage = 5;
|
|
110
|
+
this.expiryInterval= 51840
|
|
111
|
+
this.sponsorAddress = "MWip91xMhaEmDn5oUW5NDNbWSDyG5dSK9Q"
|
|
112
|
+
this.freePortion = 0.1
|
|
113
|
+
this.salePortion = 0.1
|
|
114
|
+
this.insurancePortion = 0.3
|
|
115
|
+
this.vestingPortion = 0.5
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
static async getInstance(adminAddress, chain){
|
|
120
|
+
if (!TradeLayerManager.instance) {
|
|
121
|
+
TradeLayerManager.instance = new TradeLayerManager(adminAddress,chain);
|
|
122
|
+
await TradeLayerManager.instance.ensureChainInitialized();
|
|
123
|
+
console.log('generating new TL manager')
|
|
124
|
+
}
|
|
125
|
+
console.log('returning TL Manager')
|
|
126
|
+
return TradeLayerManager.instance;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
async initializeTokens(block) {
|
|
130
|
+
const TallyMap = require('./tally.js');
|
|
131
|
+
const alreadyInitialized = await TallyMap.checkInitializationFlag();
|
|
132
|
+
console.log('initalizaing with admin '+this.adminAddress+' '+this.chain+' '+this.test)
|
|
133
|
+
if(this.adminAddress==undefined||this.adminAddress==null){
|
|
134
|
+
console.log('lag with admin assignment')
|
|
135
|
+
|
|
136
|
+
if (this.chain === 'BTC') {
|
|
137
|
+
this.adminAddress = this.test ? 'tb1q8f84erfegxhaylmvpfll9m5rgwymqy4akjnnvq' : 'bc1qktknrnx2jcchjht9anz0uy8ae02xryxq2vxeem,';
|
|
138
|
+
} else if (this.chain === 'DOGE') {
|
|
139
|
+
this.adminAddress = this.test ? 'nop27JQWbGr95ySHXZMzCg8XXxYzbCBZAW' : 'DLSfu9qvEggkeXAgCAwBBw5BVLvMCtkewz';
|
|
140
|
+
} else if (this.chain === 'LTC') {
|
|
141
|
+
this.adminAddress = this.test ? 'tltc1qa0kd2d39nmeph3hvcx8ytv65ztcywg5sazhtw8' : 'MTmoypkhRQoJ172ZqxcsVumPZfJ8KCrQCB';
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
if (!alreadyInitialized) {
|
|
146
|
+
var TLTokenId = 1;
|
|
147
|
+
const TLTotalAmount = this.initialTokenAmount;
|
|
148
|
+
const ticker = this.tickerSymbol;
|
|
149
|
+
const vestTicker = ticker+"VEST"
|
|
150
|
+
const incomeTicker = ticker+"I"
|
|
151
|
+
const incomeVestTicker = incomeTicker + "VEST"
|
|
152
|
+
var TLVESTTokenId = 2;
|
|
153
|
+
const TLVESTTotalAmount = new BigNumber(TLTotalAmount).times(this.vestingPortion).toNumber();
|
|
154
|
+
var amountToInsuranceFund = new BigNumber(TLTotalAmount).times(this.insurancePortion).toNumber();
|
|
155
|
+
const TLInitialLiquidity = new BigNumber(TLTotalAmount).times(this.salePortion).toNumber();
|
|
156
|
+
const freeTranche = new BigNumber(TLTotalAmount).times(this.freePortion).toNumber();
|
|
157
|
+
const TLIVESTinitialLiquidity = new BigNumber(TLTotalAmount).times(3).toNumber()
|
|
158
|
+
const TLITotalAmount = TLIVESTinitialLiquidity+1
|
|
159
|
+
const propertyManager = PropertyManager.getInstance()
|
|
160
|
+
TLTokenId = await propertyManager.createToken(ticker, TLTotalAmount, 'Fixed', 0);
|
|
161
|
+
TLVESTTokenId = await propertyManager.createToken(vestTicker, TLVESTTotalAmount, 'Vesting',0);
|
|
162
|
+
const TLIVESTToken = await propertyManager.createToken(incomeTicker, TLIVESTinitialLiquidity, 'Vesting', 0)
|
|
163
|
+
const TLI = await propertyManager.createToken(incomeVestTicker, TLITotalAmount, 'Native',0)
|
|
164
|
+
|
|
165
|
+
const NativeHedgeId = await TradeLayerManager.initializeContractSeries(block)
|
|
166
|
+
|
|
167
|
+
console.log('verifying that propertyid numbering is consistent with native contract id '+TLTokenId,TLVESTTokenId,NativeHedgeId)
|
|
168
|
+
var insuranceFund = new InsuranceFund(1,0,0.5,false)
|
|
169
|
+
// Distribute initial amount to insurance fund
|
|
170
|
+
insuranceFund.deposit(TLTokenId,amountToInsuranceFund,false)
|
|
171
|
+
|
|
172
|
+
await TallyMap.updateBalance(this.adminAddress, TLTokenId, TLInitialLiquidity, 0, 0, 0);
|
|
173
|
+
await TallyMap.updateBalance(this.adminAddress, TLVESTTokenId, TLVESTTotalAmount, 0, 0, TLVESTTotalAmount);
|
|
174
|
+
await TallyMap.updateBalance(this.adminAddress, TLIVESTToken, 1500000, 0,0, 1500000)
|
|
175
|
+
await TallyMap.updateBalance(this.adminAddress, TLI, 1,0,0,0)
|
|
176
|
+
await TallyMap.updateBalance(this.sponsorAddress, TLTokenId, freeTranche,0,0,0)
|
|
177
|
+
|
|
178
|
+
const balances = await TallyMap.getAddressBalances(this.adminAddress)
|
|
179
|
+
|
|
180
|
+
await TradeLayerManager.initializeClearlists();
|
|
181
|
+
|
|
182
|
+
// After initializing tokens, set the flag
|
|
183
|
+
await TallyMap.setInitializationFlag();
|
|
184
|
+
return balances
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
static async initializeContractSeries(block) {
|
|
189
|
+
const params = {
|
|
190
|
+
// Define contract properties such as margin requirements, expiry, etc.
|
|
191
|
+
// Example properties:
|
|
192
|
+
initialMargin: 0.2, // 20%
|
|
193
|
+
maintenanceMargin: 0.1, // 10%
|
|
194
|
+
expiry: 'perp', //need to assure that perp or 0 or null etc. codes to perpetual
|
|
195
|
+
onChainData: [[1, 0]], //LTC vs. TL, need to assure that the propertyid for TL init's to 1 and that 0 corresponds to LTC UTXO
|
|
196
|
+
expiryInterval: this.expiryInterval,
|
|
197
|
+
leverage: this.hedgeLeverage||5,
|
|
198
|
+
seriesLength: 6,
|
|
199
|
+
native:true,
|
|
200
|
+
inverse: true,
|
|
201
|
+
fee: false,
|
|
202
|
+
notionalPropertyId: 0,
|
|
203
|
+
notionalValue: 0.001,
|
|
204
|
+
collateralPropertyId: 1
|
|
205
|
+
};
|
|
206
|
+
|
|
207
|
+
// Create the contract series
|
|
208
|
+
return await ContractList.createContractSeries(this.adminAddress, params, block);
|
|
209
|
+
|
|
210
|
+
// Additional setup if required, such as initializing order books, setting initial market conditions, etc.
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
static async initializeClearlists() {
|
|
214
|
+
|
|
215
|
+
// Initialize issuer whitelist
|
|
216
|
+
const issuerClearlistId = ClearList.createClearlist(
|
|
217
|
+
this.adminAddress,
|
|
218
|
+
'Issuer Clearlist',
|
|
219
|
+
'',
|
|
220
|
+
'Oracles and Tokens included in Liquidity Reward',
|
|
221
|
+
this.sponsorAddress,
|
|
222
|
+
1
|
|
223
|
+
);
|
|
224
|
+
|
|
225
|
+
// Initialize market maker whitelist
|
|
226
|
+
const marketMakerClearlistId = ClearList.createClearlist(
|
|
227
|
+
this.adminAddress,
|
|
228
|
+
'Market Maker Clearlist',
|
|
229
|
+
'',
|
|
230
|
+
'Market Makers and active traders who do not wash trade.',
|
|
231
|
+
this.sponsorAddress,
|
|
232
|
+
2
|
|
233
|
+
);
|
|
234
|
+
|
|
235
|
+
const accreditedListId = ClearList.createClearlist(
|
|
236
|
+
this.adminAddress,
|
|
237
|
+
'Accredited Clearlist',
|
|
238
|
+
'',
|
|
239
|
+
'Exempt from restrictions on native tokens.',
|
|
240
|
+
this.sponsorAddress,
|
|
241
|
+
3
|
|
242
|
+
);
|
|
243
|
+
|
|
244
|
+
console.log(`Issuer clearlist created with ID: ${issuerClearlistId}`);
|
|
245
|
+
console.log(`Market maker clearlist created with ID: ${marketMakerClearlistId}`);
|
|
246
|
+
console.log(`Acc. clearlist created with ID: ${accreditedListId}`)
|
|
247
|
+
|
|
248
|
+
return
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
static async updateVesting(cumulativeVolumeLTC, currentBlockVolumeLTC, cumulativeVolumeGlobal, currentBlockVolumeGlobal) {
|
|
252
|
+
const propertyData1 = await PropertyManager.getPropertyData(2);
|
|
253
|
+
const propertyData2 = await PropertyManager.getPropertyData(3);
|
|
254
|
+
|
|
255
|
+
if (!propertyData1 || !propertyData2) {
|
|
256
|
+
return null;
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
let maxTokens2 = propertyData1.totalInCirculation;
|
|
260
|
+
let maxTokens3 = propertyData2.totalInCirculation;
|
|
261
|
+
|
|
262
|
+
// Constants for vesting ranges
|
|
263
|
+
const min1 = 1000;
|
|
264
|
+
const max1 = 1000000000; // 1B
|
|
265
|
+
const min2 = 10000000000; // 10B
|
|
266
|
+
const max2 = 1000000000000; // 1T
|
|
267
|
+
|
|
268
|
+
console.log(`Inside update vesting: ${cumulativeVolumeLTC}, ${currentBlockVolumeLTC}, ${cumulativeVolumeGlobal}, ${currentBlockVolumeGlobal}, ${maxTokens2}, ${maxTokens3}`);
|
|
269
|
+
|
|
270
|
+
// Calculate cumulative volumes
|
|
271
|
+
const newCumulativeVolumeLTC = cumulativeVolumeLTC + currentBlockVolumeLTC;
|
|
272
|
+
const newCumulativeVolumeGlobal = cumulativeVolumeGlobal + currentBlockVolumeGlobal;
|
|
273
|
+
|
|
274
|
+
// First Tranche Vesting (LTC Volume)
|
|
275
|
+
let vestingPercentage1 = 0;
|
|
276
|
+
if (newCumulativeVolumeLTC >= min1) {
|
|
277
|
+
vestingPercentage1 = Math.min(
|
|
278
|
+
(newCumulativeVolumeLTC - min1) / (max1 - min1),
|
|
279
|
+
1
|
|
280
|
+
);
|
|
281
|
+
}
|
|
282
|
+
const vestingAmount1 = vestingPercentage1 * maxTokens2;
|
|
283
|
+
|
|
284
|
+
// Second Tranche Vesting (Global Volume)
|
|
285
|
+
let vestingPercentage2 = 0;
|
|
286
|
+
if (newCumulativeVolumeGlobal >= min2) {
|
|
287
|
+
vestingPercentage2 = Math.min(
|
|
288
|
+
(newCumulativeVolumeGlobal - min2) / (max2 - min2),
|
|
289
|
+
1
|
|
290
|
+
);
|
|
291
|
+
}
|
|
292
|
+
const vestingAmount2 = vestingPercentage2 * maxTokens3;
|
|
293
|
+
|
|
294
|
+
console.log(`Ending vesting calc: ${vestingAmount1}, ${vestingAmount2}`);
|
|
295
|
+
return { two: vestingAmount1, three: vestingAmount2 };
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
static calculateTradeRebates(cumulativeVolume) {
|
|
299
|
+
const { baseVolume, minRebate, maxRebate } = TradeLayerManager.instance;
|
|
300
|
+
if (cumulativeVolume < baseVolume) {
|
|
301
|
+
return maxRebate;
|
|
302
|
+
}
|
|
303
|
+
const scale = Math.log(cumulativeVolume / baseVolume) / Math.log(100000000 / baseVolume);
|
|
304
|
+
const rebate = maxRebate - scale * (maxRebate - minRebate);
|
|
305
|
+
return Math.max(rebate, minRebate);
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
static performBuyback(feeCaches) {
|
|
309
|
+
feeCaches.forEach(cache => {
|
|
310
|
+
const orderbook = this.fetchOrderbookForToken(cache.tokenId);
|
|
311
|
+
const buybackAmount = this.calculateBuybackAmount(cache, orderbook);
|
|
312
|
+
// Execute buyback transaction
|
|
313
|
+
});
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
static fetchOrderbookForToken(tokenId) {
|
|
317
|
+
// Fetch the orderbook for the given token
|
|
318
|
+
// Implementation depends on your system's data sources
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
static calculateBuybackAmount(cache, orderbook) {
|
|
322
|
+
let availableFunds = cache.funds;
|
|
323
|
+
let totalBuybackAmount = 0;
|
|
324
|
+
|
|
325
|
+
for (const order of orderbook) {
|
|
326
|
+
const orderCost = order.price * order.amount;
|
|
327
|
+
if (availableFunds >= orderCost) {
|
|
328
|
+
totalBuybackAmount += order.amount;
|
|
329
|
+
availableFunds -= orderCost;
|
|
330
|
+
} else {
|
|
331
|
+
const partialAmount = availableFunds / order.price;
|
|
332
|
+
totalBuybackAmount += partialAmount;
|
|
333
|
+
availableFunds = 0;
|
|
334
|
+
break;
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
if (availableFunds <= 0) break;
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
return totalBuybackAmount;
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
/** ------------------------------
|
|
344
|
+
* TLVEST per-trade award model — static class methods
|
|
345
|
+
* Paste INSIDE your class body (e.g., class TradeLayerManager { ... })
|
|
346
|
+
* ------------------------------ */
|
|
347
|
+
|
|
348
|
+
static get EPS() { return 1e-18; }
|
|
349
|
+
|
|
350
|
+
/** Vesting fraction on a log curve from V0 → V* (in BTC printed volume) */
|
|
351
|
+
static vestFraction(cumV, V0, Vstar, alpha) {
|
|
352
|
+
if (cumV <= V0) return 0;
|
|
353
|
+
const num = Math.log(1 + (cumV - V0) / Math.max(alpha, this.EPS));
|
|
354
|
+
const den = Math.log(1 + (Vstar - V0) / Math.max(alpha, this.EPS));
|
|
355
|
+
const v = num / Math.max(den, this.EPS);
|
|
356
|
+
return Math.max(0, Math.min(1, v));
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
/** Geometric projection: sum of next 12 months given current monthly vol and CMGR */
|
|
360
|
+
static projectNextYearVolumeBTC(monthlyBTC, cmgr) {
|
|
361
|
+
if (cmgr <= this.EPS) return 12 * monthlyBTC;
|
|
362
|
+
const r = 1 + cmgr;
|
|
363
|
+
return monthlyBTC * ((Math.pow(r, 12) - 1) / cmgr);
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
/** Months needed to reach V* from cumV with monthlyBTC growing at cmgr */
|
|
367
|
+
static monthsToReach(cumV, Vstar, monthlyBTC, cmgr) {
|
|
368
|
+
const remaining = Math.max(0, Vstar - cumV);
|
|
369
|
+
if (remaining <= this.EPS) return 0;
|
|
370
|
+
if (monthlyBTC <= this.EPS) return Infinity;
|
|
371
|
+
if (cmgr <= this.EPS) return Math.ceil(remaining / monthlyBTC);
|
|
372
|
+
const r = 1 + cmgr;
|
|
373
|
+
// monthlyBTC * (r^n - 1) / (r - 1) >= remaining
|
|
374
|
+
const numerator = (remaining * (r - 1)) / monthlyBTC + 1;
|
|
375
|
+
if (numerator <= 1) return 0;
|
|
376
|
+
const n = Math.log(numerator) / Math.log(r);
|
|
377
|
+
return Math.ceil(n);
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
/** Annualize 12-mo CMGR to an annual growth rate */
|
|
381
|
+
static annualizeFromCMGR(cmgr) {
|
|
382
|
+
return Math.pow(1 + cmgr, 12) - 1;
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
/** Discount factor for t months at annual rate r (continuous comp) */
|
|
386
|
+
static discountFactorMonths(rAnnual, months) {
|
|
387
|
+
const tYears = months / 12;
|
|
388
|
+
return Math.exp(-rAnnual * tYears);
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
/** Optional: compute CMGR from a monthly series (array of numbers) */
|
|
392
|
+
static cmgrFromSeries(monthlySeries) {
|
|
393
|
+
if (!Array.isArray(monthlySeries) || monthlySeries.length < 2) return 0;
|
|
394
|
+
let sumLog = 0, pairs = 0;
|
|
395
|
+
for (let i = 1; i < monthlySeries.length; i++) {
|
|
396
|
+
const prev = Math.max(monthlySeries[i - 1], this.EPS);
|
|
397
|
+
const cur = Math.max(monthlySeries[i], this.EPS);
|
|
398
|
+
sumLog += Math.log(cur / prev);
|
|
399
|
+
pairs++;
|
|
400
|
+
}
|
|
401
|
+
return Math.exp(sumLog / Math.max(pairs, 1)) - 1; // monthly CMGR
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
/**
|
|
405
|
+
* Main: compute TLVEST tokens to award for a single trade.
|
|
406
|
+
* trade: { notionalBtc: number, vip?: boolean }
|
|
407
|
+
* params:
|
|
408
|
+
* - feeRate (e.g., 0.000005 for 0.5 bps)
|
|
409
|
+
* - baseRebateShare (e.g., 0.40)
|
|
410
|
+
* - vipRebateShare (e.g., 0.20) if trade.vip = true
|
|
411
|
+
* - dividendShareOfFees (e.g., 0.50)
|
|
412
|
+
* - discountRateAnnual (e.g., 0.08)
|
|
413
|
+
* - priceTLIinBTC
|
|
414
|
+
* - vestToTLIRatio
|
|
415
|
+
* - cumulativeVolumeBTC
|
|
416
|
+
* - monthlyVolumeBTC
|
|
417
|
+
* - cmgr12 (monthly CMGR, e.g., 0.05 = +5%/mo)
|
|
418
|
+
* - tlvestEffectiveSupply
|
|
419
|
+
* - vestV0BTC, vestVStarBTC, vestAlpha
|
|
420
|
+
*/
|
|
421
|
+
static computeTokensForTrade(trade, params) {
|
|
422
|
+
const p = Object.assign({
|
|
423
|
+
feeRate: 0.000005,
|
|
424
|
+
baseRebateShare: 0.40,
|
|
425
|
+
vipRebateShare: 0.20,
|
|
426
|
+
dividendShareOfFees: 0.50,
|
|
427
|
+
discountRateAnnual: 0.08,
|
|
428
|
+
priceTLIinBTC: 0.00001,
|
|
429
|
+
vestToTLIRatio: 1.0,
|
|
430
|
+
cumulativeVolumeBTC: 1000000,
|
|
431
|
+
monthlyVolumeBTC: 50000,
|
|
432
|
+
cmgr12: 0.05,
|
|
433
|
+
tlvestEffectiveSupply: 10000000,
|
|
434
|
+
vestV0BTC: 100000,
|
|
435
|
+
vestVStarBTC: 21000000,
|
|
436
|
+
vestAlpha: 500000
|
|
437
|
+
}, params || {});
|
|
438
|
+
|
|
439
|
+
// BTC budget for this trade’s award (fee-budgeted; safe against wash)
|
|
440
|
+
const feeBTC = p.feeRate * trade.notionalBtc;
|
|
441
|
+
const awardValueBTC = feeBTC * (p.baseRebateShare + (trade.vip ? p.vipRebateShare : 0));
|
|
442
|
+
|
|
443
|
+
// Growth & projection
|
|
444
|
+
const gAnnual = this.annualizeFromCMGR(p.cmgr12);
|
|
445
|
+
const r = Math.max(p.discountRateAnnual, 0.0001);
|
|
446
|
+
const gCapped = Math.min(gAnnual, r - 0.01); // keep (r - g) >= 1%
|
|
447
|
+
|
|
448
|
+
const projectedYearVolBTC = this.projectNextYearVolumeBTC(p.monthlyVolumeBTC, p.cmgr12);
|
|
449
|
+
|
|
450
|
+
// Dividend-discount PV per TLVEST (Gordon)
|
|
451
|
+
const feesPerYearBTC = p.feeRate * projectedYearVolBTC;
|
|
452
|
+
const dividendPoolBTC = p.dividendShareOfFees * feesPerYearBTC;
|
|
453
|
+
const perTokenDividendBTC = dividendPoolBTC / Math.max(p.tlvestEffectiveSupply, 1);
|
|
454
|
+
const pvDividendBTC = perTokenDividendBTC / Math.max(r - gCapped, 0.01);
|
|
455
|
+
|
|
456
|
+
// Conversion floor (TLI priced in BTC), discounted by vesting delay
|
|
457
|
+
const months = this.monthsToReach(p.cumulativeVolumeBTC, p.vestVStarBTC, p.monthlyVolumeBTC, p.cmgr12);
|
|
458
|
+
const vNow = this.vestFraction(p.cumulativeVolumeBTC, p.vestV0BTC, p.vestVStarBTC, p.vestAlpha);
|
|
459
|
+
const df = this.discountFactorMonths(r, Math.max(0, months));
|
|
460
|
+
const pvConversionBTC = p.priceTLIinBTC * p.vestToTLIRatio * (vNow + (1 - vNow) * df);
|
|
461
|
+
|
|
462
|
+
// Conservative issuance: use the higher PV and ensure nonzero
|
|
463
|
+
const pvTokenBTC = Math.max(pvDividendBTC, pvConversionBTC, this.EPS);
|
|
464
|
+
const tokens = awardValueBTC / pvTokenBTC;
|
|
465
|
+
|
|
466
|
+
return {
|
|
467
|
+
tokens,
|
|
468
|
+
awardValueBTC,
|
|
469
|
+
pvTokenBTC,
|
|
470
|
+
vestFractionNow: vNow,
|
|
471
|
+
monthsToFullVest: months,
|
|
472
|
+
components: {
|
|
473
|
+
pvDividendBTC,
|
|
474
|
+
pvConversionBTC,
|
|
475
|
+
gAnnual,
|
|
476
|
+
projectedYearVolumeBTC,
|
|
477
|
+
perTokenDividendBTC,
|
|
478
|
+
feeBTC
|
|
479
|
+
}
|
|
480
|
+
};
|
|
481
|
+
}
|
|
482
|
+
|
|
483
|
+
}
|
|
484
|
+
|
|
485
|
+
/** Example helper to compute monthly CMGR from your volume index series
|
|
486
|
+
* (Pass in your last 12 monthly printed_btc values)
|
|
487
|
+
*/
|
|
488
|
+
// const cmgr12 = cmgrFromSeries(last12MonthsPrintedBTC);
|
|
489
|
+
|
|
490
|
+
|
|
491
|
+
module.exports = TradeLayerManager;
|