-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelix.js
80 lines (65 loc) · 1.99 KB
/
helix.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
const composeAPI = require( '@helixnetwork/core').composeAPI;
const prepareForTangleWithLocalPow = require('@helixnetwork/pow').prepareForTangleWithLocalPow
let m = require('@helixnetwork/converter').asciiToTxHex
const { provider } = require('./conf-env.js')
const helix = composeAPI({
provider
})
// Depth or how far to go for tip selection entry point
const depth = 2
// Difficulty of Proof-of-Work required to attach transaction to tangle
// Minimum value on testnet is currently 2.
const minWeightMagnitude = 2
const getBalance = async (addrs) => {
let balances = await helix.getBalances([addrs], 100)
console.log(balances);
return balances;
}
const send = async function(seed, snd, rcv, val) {
let transfers = [{
address: rcv,
value: val, // 1Kh
tag: m('loot'), // optional tag in hexString
message: m("Enjoy free lunch")}
]
let inputs = [
{
address: snd,
security: 2,
}
];
try {
const txs = await helix.prepareTransfers(seed, transfers, {"security":2})
//console.log(`Tranasactinos: ${txs}`);
const bundle = await helix.sendTransactionStrings(txs, depth, minWeightMagnitude)
console.log(`Sent bundle: ${JSON.stringify(bundle)}`);
return {
bundle: bundle[0].bundle,
txs: bundle.map(b => b.hash),
values: bundle.map(b => b.value)
};
} catch (err) {
console.error(`Failed to send: ${err}`);
throw err;
}
}
const nodeInfo = async (url) => {
let provider = composeAPI({
provider: url
})
let info = await provider.getNodeInfo();
console.log(info);
return {
url,
latestSolidRoundIndex: info.latestSolidRoundIndex,
currentRoundIndex: info.currentRoundIndex,
time: info.time,
features: info.features
}
}
const getConfimationStatus = async (txHash) => {
return await helix.getInclusionStates([txHash]);
}
module.exports = {
send, getBalance, nodeInfo, getConfimationStatus
}