-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathltcBasic.js
60 lines (50 loc) · 2.02 KB
/
ltcBasic.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
const _ = require('lodash')
const btcBasic = require('./btcBasic');
const litecore = require('bitcore-lib-ltc')
const BigNumber = require('bignumber.js');
const coinSelect = require('coinselect')
const axios = require('axios')
const config = require('./configLoad');
class ltcBasic extends btcBasic{
async sendLTC(address,amount){
const feePrice = await this.getfeeRate()
const feeRate = new BigNumber(feePrice).times(1e8).dividedBy(100).integerValue(BigNumber.ROUND_DOWN).toNumber()
const value = new BigNumber(amount).times(1e8).toNumber()
let targets = [{
address,
value
}]
// 扩展成50个output
// targets = Array(50).fill().map(() => targets).flat();
const utxos = await this.getUtxos()
const {inputs, outputs, fee} = coinSelect(utxos,targets,feeRate)
if (inputs && outputs){
//build tx
const filledOutputs = outputs.map(output => {
if (!output.address) {
output.address = inputs[0].address; // 在这里设置默认值
}
return output;
})
const network = litecore.Networks.testnet
const tx = new litecore.Transaction(network)
tx.from(inputs)
filledOutputs.forEach((e)=>tx.to(e.address,e.value))
//sign tx
const froms = this.config.KEYPAIR
let signer,priKey
for (let i =0;i<inputs.length;i++){
priKey = _.find(froms, function(o) { return o.ADDRESS == inputs[i].address})
signer = new litecore.PrivateKey(priKey.PRIKEY,network)
tx.sign(signer)
}
//send tx
const rawtx = tx.uncheckedSerialize()
const hash = await this.sendTx(rawtx)
console.log(`Transfer ${amount} LTC to ${address}. Transaction Hash: ${hash.txid}`);
} else {
throw new Error('not enough utxos!!!')
}
}
}
module.exports = ltcBasic