Skip to content

Instantly share code, notes, and snippets.

@jackcoble
Created January 25, 2020 14:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jackcoble/4cff7324b08cb2d66023d44f25240169 to your computer and use it in GitHub Desktop.
Save jackcoble/4cff7324b08cb2d66023d44f25240169 to your computer and use it in GitHub Desktop.
Amount of coins to a specific address within a TX, and determine most popular input address
package main
import(
"github.com/btcsuite/btcd/chaincfg/chainhash"
"log"
)
func main() {
hashes := []string{
"2ba68a3ad87c386e7b54c903e3bde25c043e0b8a6b18b59d361058689866e255",
}
var (
total float64
addressArray []string
count int
)
for _, hash := range hashes {
hashStr, _ := chainhash.NewHashFromStr(hash)
tx, err := app.RPCClient.GetRawTransactionVerbose(hashStr)
if err != nil {
log.Println(err.Error())
}
for _, i := range tx.Vout {
for _, address := range i.ScriptPubKey.Addresses {
// Check the output is to the correct address
if address == "mMXXXXXXXXXXXXXXXXXXXXXXXXXXZTGjM9" {
total += i.Value
continue
}
}
}
for _, txInput := range tx.Vin {
hash, _ := chainhash.NewHashFromStr(txInput.Txid)
inTx, err := app.RPCClient.GetRawTransactionVerbose(hash)
if err != nil {
log.Println(err.Error())
}
for _, i := range inTx.Vout {
for _, j := range i.ScriptPubKey.Addresses {
addressArray = append(addressArray, j)
}
}
count += 1
// Do maximum of 50 iterations to determine input address
if count == 50 {
break
}
}
}
sorted := Sorted(addressArray)
log.Println("Most frequent:", sorted)
log.Println("Original coins:", total)
}
// Determine the most popular address in the array
func Sorted(list []string) string {
counter := 0
str := list[0]
for _, i := range list {
freq := len(list)
if freq > counter {
counter = freq
str = i
}
}
return str
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment