mirror of
https://github.com/aljazceru/ark.git
synced 2026-02-01 17:44:39 +01:00
* Rename arkd folder & drop cli * Rename ark cli folder & update docs * Update readme * Fix * scripts: add build-all * Add target to build cli for all platforms * Update build scripts --------- Co-authored-by: tiero <3596602+tiero@users.noreply.github.com>
58 lines
971 B
Go
58 lines
971 B
Go
package main
|
|
|
|
import (
|
|
"strings"
|
|
|
|
"github.com/vulpemventures/go-elements/transaction"
|
|
)
|
|
|
|
type Explorer interface {
|
|
GetTxHex(txid string) (string, error)
|
|
Broadcast(txHex string) (string, error)
|
|
}
|
|
|
|
type explorer struct {
|
|
cache map[string]string
|
|
}
|
|
|
|
func NewExplorer() Explorer {
|
|
return &explorer{
|
|
cache: make(map[string]string),
|
|
}
|
|
}
|
|
|
|
func (e *explorer) GetTxHex(txid string) (string, error) {
|
|
if hex, ok := e.cache[txid]; ok {
|
|
return hex, nil
|
|
}
|
|
|
|
txHex, err := getTxHex(txid)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
e.cache[txid] = txHex
|
|
|
|
return txHex, nil
|
|
}
|
|
|
|
func (e *explorer) Broadcast(txHex string) (string, error) {
|
|
tx, err := transaction.NewTxFromHex(txHex)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
txid := tx.TxHash().String()
|
|
e.cache[txid] = txHex
|
|
|
|
txid, err = broadcast(txHex)
|
|
if err != nil {
|
|
if strings.Contains(strings.ToLower(err.Error()), "transaction already in block chain") {
|
|
return txid, nil
|
|
}
|
|
|
|
return "", err
|
|
}
|
|
|
|
return txid, nil
|
|
}
|