/
cashburner.go
147 lines (130 loc) · 3.43 KB
/
cashburner.go
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
package main
import (
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"log"
"math"
"net/http"
"net/url"
"github.com/btcsuite/btcd/chaincfg"
"gitlab.com/instak_backend/op_return/conf"
"gitlab.com/instak_backend/op_return/lib/cashburner"
"gitlab.com/instak_backend/op_return/lib/utxorq"
)
func main() {
defer func() {
println("\n\n---------------- done ----------------\n\n")
}()
println("\n")
// configure blockchain networks
networks := map[string]chaincfg.Params{
"PGT": conf.KomodoParams,
}
for _, v := range networks {
chaincfg.Register(&v)
}
// configure arguments
argAmount := flag.Float64("val", 0, "transaction amount")
argKey := flag.String("key", "", "wallet privkey")
argDest := flag.String("dest", "", "destination address")
argSource := flag.String("src", "", "source address")
argMessage := flag.String("msg", "", "transaction data message")
flag.Parse()
if *argAmount <= float64(0) {
println("invalid transaction amount value")
return
}
if *argKey == "" {
println("invalid privkey value")
return
}
if *argSource == "" {
println("invalid source address value")
return
}
// if *argDest == "" {
// println("invalid destination address value")
// return
// }
if *argMessage == "" {
println("invalid message data value")
return
}
// data
println("amount: ", *argAmount)
println("message: ", *argMessage)
println("source address: ", *argSource)
println("source privkey: ", *argKey)
println("\n")
// fetch utxos
source := "https://electrum.pungo.network/api/listunspent?ip=electrum1.komodo.build&port=10002&address=" + *argSource
res, err := http.Get(source)
if err != nil {
panic(err.Error())
}
body, err := ioutil.ReadAll(res.Body)
if err != nil {
panic(err.Error())
}
var utxos = new(utxorq.UTXOResponse)
err = json.Unmarshal(body, &utxos)
if err != nil {
panic(err.Error())
}
// check available utxos
if len(utxos.Result) == 0 {
println("no available utxos")
return
}
println("utxos: ")
for k, v := range utxos.Result {
println(k, v.Hash, v.Value)
}
println("\n")
// amount to transfer in satoshis, transaction fee is zero for pungo token
amount := uint64(*argAmount * math.Pow10(int(8)))
txFee := int64(0)
// select utxo
isUTXOSelect := false
utxoSelect := make([]*utxorq.UTXOValue, 0)
for _, v := range utxos.Result {
if uint64(v.Value+txFee) >= amount {
utxoSelect = append(utxoSelect, &v)
isUTXOSelect = true
break
}
}
if !isUTXOSelect {
println("not enougth value in available single utxos to complete transaction\n")
return
}
// build OP_RETURN transaction
transaction, err := cashburner.Build(networks["PGT"], *argKey, *argDest, *argSource, int(amount), int(txFee), utxoSelect, *argMessage)
if err != nil {
println("error building transaction: ", err.Error())
return
}
println("transaction: ", transaction)
println("\n")
// push transaction
formData := url.Values{
"ip": {"electrum1.komodo.build"},
"port": {"10002"},
"rawtx": {transaction},
}
resp, err := http.PostForm("https://electrum.pungo.network/api/pushtx", formData)
if err != nil {
log.Fatalln("error sending transaction: ", err)
}
var result map[string]interface{}
json.NewDecoder(resp.Body).Decode(&result)
if result["msg"].(string) == "success" {
explorer := "https://pgt.komodo.build/tx/"
fmt.Println("\ntransaction hash:", result["result"].(string))
fmt.Println("transaction explorer:", explorer+result["result"].(string))
} else {
fmt.Println(result)
}
}