-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathfunctions-wallet
More file actions
158 lines (132 loc) · 4.43 KB
/
functions-wallet
File metadata and controls
158 lines (132 loc) · 4.43 KB
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
155
156
157
158
#!/bin/bash
# wallet functions file for nntools
#
# @author webworker01
#
sendRaw()
{
local rawtx=$1
local acname=${2^^}
local setnlocktime=$3
if [[ ! -z $acname ]] && [[ $acname != "KMD" ]]; then
coin=${acname}
asset=" -ac_name=${acname}"
else
coin="KMD"
asset=""
fi
if [[ ! -z ${setnlocktime} ]] && [[ ${setnlocktime} == "1" ]]; then
nlocktime=$(printf "%08x" $(date +%s) | dd conv=swab 2> /dev/null | rev)
else
nlocktime="00000000"
fi
sapling=$(isSapling $coin)
if [[ ! -z $rawtx ]]; then
if (( sapling > 0 )); then
txtail="000000000000000000000000000000"
hex="${rawtx:0:${#rawtx}-38}${nlocktime}${txtail}"
else
hex="${rawtx:0:${#rawtx}-8}${nlocktime}"
fi
signedtx=$($komodocli $asset signrawtransaction $hex | jq -r '.hex')
if [[ ! -z $signedtx ]]; then
rawtxid=$($komodocli $asset sendrawtransaction $signedtx)
else
rawtxid="failed no signed tx"
fi
echo "${rawtxid}"
else
echo "failed"
fi
}
calcFee()
{
local vins=$1
local vouts=$2
local scripttypein=$3
local scripttypeout=$4
local feeperkb=$5
if [[ $scripttypein == "p2pk" ]]; then
local vinmultiplier=114
else
local vinmultiplier=150
fi
if [[ $scripttypeout == "p2pk" ]]; then
local voutmultiplier=44
else
local voutmultiplier=35
fi
local size=$(bc -l <<< "${vins} * ${vinmultiplier} + ${vouts} * ${voutmultiplier} + 30")
local fee=$( printf "%.8f" $(bc -l <<< "${size} / 1000 * ${feeperkb}") )
# log "calcfee" "size: ${size} fee: ${fee}" "" "file"
echo "${fee}"
}
#
# Send up to 400 UTXOs at a time to $nn_address
# @param acname Specify the coin name
#
consolidateUTXOs ()
{
local acname=${1^^}
local exclude_amount=${2}
number_regex='^[0-9]+([.][0-9]+)?$'
if [[ ${acname} != "KMD" ]]; then
coin=${acname}
asset=" -ac_name=${acname}"
feepkb=$sctxfeeperkb
else
coin="KMD"
asset=""
feepkb=$txfeeperkb
fi
if [[ -z $exclude_amount ]]; then
filter_amount=0
exclude_amount=0
#check amount is a number
elif [[ ! -z $exclude_amount ]] && ! [[ $exclude_amount =~ $number_regex ]] ; then
log "consolidate" "${acname} - exclude_amount must be a number, got ${exclude_amount}" "red" "file"
exit 0
else
filter_amount=1
exclude_amount=$exclude_amount
fi
unspent=$($komodocli $asset listunspent)
if (( ${filter_amount} > 0 )); then
consolidateutxo=$(jq --arg checkaddr $nn_address --arg exclude_amount $exclude_amount '[.[] | select (.amount!=($exclude_amount|tonumber) and .address==$checkaddr and .spendable==true)] | sort_by(-.amount)[0:399]' <<< $unspent)
else
consolidateutxo=$(jq --arg checkaddr $nn_address '[.[] | select (.address==$checkaddr and .spendable==true)] | sort_by(-.amount)[0:399]' <<< $unspent)
fi
consolidatethese=$(jq -r '[.[] | {"txid":.txid, "vout":.vout}] | tostring' <<< $consolidateutxo)
consolidatetheselength=$(jq -r '. | length' <<< $consolidateutxo)
consolidateamount=$(jq -r '[.[].amount] | add' <<< $consolidateutxo)
txfee=$(calcFee ${consolidatetheselength} 1 "p2pkh" "p2pkh" $feepkb)
if [[ "$consolidateamount" != "null" ]]; then
consolidateamountfixed=$( printf "%.8f" $(bc -l <<< "(${consolidateamount}-${txfee})") )
if (( $(echo "$consolidateamountfixed > 0" | bc -l) )); then
rawtxresult=$($komodocli $asset createrawtransaction "${consolidatethese}" '''{ "'$nn_address'": '$consolidateamountfixed' }''')
rawtxid=$(sendRaw ${rawtxresult} ${coin} 1)
log "consolidate" "${coin} - Sent $consolidateamount to $nn_address TXID: $rawtxid" "green" "file"
echo $rawtxid
fi
fi
}
waitForConfirm()
{
local acname=${1^^}
local txid=${2}
if [[ ${acname} != "KMD" ]]; then
asset=" -ac_name=${acname}"
# txfeeperkb=$sctxfeeperkb
else
asset=""
# txfeeperkb=${txfeeperkb}
fi
i=0
confirmations=0
while [ "$confirmations" -eq "0" ]; do
sleep 10
confirmations=$(${komodocli} ${asset} gettransaction ${txid} | jq .confirmations)
i=$((i+1))
log "waitForConfirm" "[${acname}] - waiting for confirmation ${confirmations} (${i}) - ${txid}" "red" "echo"
done
}