-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwallet.js
154 lines (121 loc) · 4.23 KB
/
wallet.js
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
const WB = require('kryptokrona-wallet-backend-js')
const fs = require('fs')
const WALLET_NAME = 'faucet'
const WALLET_PASSWORD = 'faucet123'
const NODE = 'localhost'
const PORT = 11898
const AMOUNT_TO_SEND = 500000
const daemon = new WB.Daemon(NODE, PORT)
let wallet
const logIntoWallet = async () => {
const [wallet, error] = await WB.WalletBackend.openWalletFromFile(daemon, `${WALLET_NAME}.wallet`, WALLET_PASSWORD);
if (error) {
console.log('Failed to open wallet: ' + error.toString());
}
return wallet
}
const startWallet = async () => {
//Start sync process
wallet = await logIntoWallet()
await wallet.start();
wallet.enableAutoOptimization(false)
const [walletBlockCount, localDaemonBlockCount, networkBlockCount] = wallet.getSyncStatus();
if (walletBlockCount === 0) {
await wallet.reset(networkBlockCount - 100)
}
wallet.on('heightchange', async (walletBlockCount, localDaemonBlockCount, networkBlockCount) => {
console.log('SYNC: ' + walletBlockCount, 'local: ' + localDaemonBlockCount, 'network: ' + networkBlockCount)
console.log('BALANCE: ' + await wallet.getBalance())
console.log('SAVING WALLET')
const saved = wallet.saveWalletToFile(`${WALLET_NAME}.wallet`, WALLET_PASSWORD)
if (!saved) {
console.log('Failed to save wallet!');
}
})
}
const initWallet = async () => {
try {
//Creates a wallet if we don't have one
if (!(fs.existsSync('./faucet.wallet'))) {
console.log('Creating wallet')
const wallet = await WB.WalletBackend.createWallet(daemon);
console.log('Saving wallet')
const saved = wallet.saveWalletToFile(`${WALLET_NAME}.wallet`, WALLET_PASSWORD)
if (!saved) {
console.log('Failed to save wallet!');
}
}
//Creates a .json file to save claimers in
if (!(fs.existsSync('./claimed.json'))) {
fs.writeFile('claimed.json', '[]', function (err) {
if (err) throw err;
console.log('File is created successfully.');
});
}
//Start wallet
await startWallet()
} catch (err) {
console.error(err)
}
}
const getWalletInfo = async () => {
let balance = await wallet.getBalance()
let address = wallet.getPrimaryAddress()
return {balance, address}
}
const sendTransaction = async (address) => {
try {
await optimizeMessages()
return await wallet.sendTransactionAdvanced(
[[address, AMOUNT_TO_SEND]],
3,
{fixedFee: 10000, isFixedFee: true},
);
} catch (err) {
console.log('Error', err);
}
}
const optimizeMessages = async nbrOfTxs => {
console.log('optimize');
try {
const [walletHeight, localHeight, networkHeight] = wallet.getSyncStatus();
let inputs = await wallet.subWallets.getSpendableTransactionInputs(wallet.subWallets.getAddresses(), networkHeight);
if (inputs.length > 8) {
console.log('enough inputs');
return;
}
let subWallets = wallet.subWallets.subWallets
subWallets.forEach((value, name) => {
let txs = value.unconfirmedIncomingAmounts.length;
if (txs > 0) {
console.log('Already have incoming inputs, aborting..');
}
})
let payments = [];
let i = 0;
/* User payment */
while (i < nbrOfTxs - 1 && i < 10) {
payments.push([
wallet.subWallets.getAddresses()[0],
500000
]);
i += 1;
}
let result = await wallet.sendTransactionAdvanced(
payments, // destinations,
3, // mixin
{fixedFee: 10000, isFixedFee: true}, // fee
undefined, //paymentID
undefined, // subWalletsToTakeFrom
undefined, // changeAddress
true, // relayToNetwork
false, // sendAll
undefined
);
console.log('optimize completed');
return result;
} catch (err) {
console.log('error optimizer', err);
}
}
module.exports = {initWallet, sendTransaction, getWalletInfo}