Example #1
0
func GetAll(prefix string) map[string]string {
	kvMap := map[string]string{}
	rows, err := db.Query(
		`SELECT key_, value
         FROM kvstore WHERE key_ like ?`,
		prefix+"%",
	)
	if err != nil {
		panic(err)
	}
	for rows.Next() {
		var kvRow KeyValue
		err := rows.Scan(&kvRow)
		if err != nil {
			panic(err)
		}
		kvMap[kvRow.Key[len(prefix):]] = kvRow.Value
	}
	return kvMap
}
Example #2
0
func computeAssets() {
	Info("Computing Assets...")

	// TODO: filter results by blockheight.
	coinAddressBalances := map[string]map[string]*AddressBalance{}
	rows, err := db.Query(
		`SELECT p.coin, p.address, p.amount, p.mpk_id, a.chain_path, a.chain_idx FROM payment AS p
         INNER JOIN address AS a ON p.address=a.address
         WHERE p.spent=0 AND p.orphaned=0`)
	if err != nil {
		panic(err)
	}
	for rows.Next() {
		var coin, address string
		var amount uint64
		var mpkId int64
		var chainPath string
		var chainIdx int32
		err := rows.Scan(&coin, &address, &amount, &mpkId, &chainPath, &chainIdx)
		if err != nil {
			panic(err)
		}
		if coinAddressBalances[coin] == nil {
			coinAddressBalances[coin] = map[string]*AddressBalance{}
		}
		if coinAddressBalances[coin][address] == nil {
			coinAddressBalances[coin][address] = &AddressBalance{
				Address:   address,
				Amount:    amount,
				ChainPath: fmt.Sprintf("%v/%v", chainPath, chainIdx),
				MPKId:     mpkId,
			}
		} else {
			coinAddressBalances[coin][address].Amount += amount
		}
	}

	for coin, addressBalances := range coinAddressBalances {
		// Group addresses by MPK.
		var mpkBalances = map[int64][]*AddressBalance{}
		var sum uint64
		for _, addrBalance := range addressBalances {
			sum += addrBalance.Amount
			mpkBalances[addrBalance.MPKId] = append(mpkBalances[addrBalance.MPKId], addrBalance)
			//fmt.Printf("[%v] %v\t%v\t%v\n", coin, address, addrBalance.ChainPath, addrBalance.Amount)
		}
		fmt.Printf("Total %v:\t%v\n", coin, sum)
		// Write to assets.json
		var mpkAssets = []interface{}{}
		for mpkId, addrBalances := range mpkBalances {
			mpk := bitcoin.LoadMPK(mpkId)
			mpkAsset := map[string]interface{}{
				"public_key": mpk.PubKey,
				"chain":      mpk.Chain,
				"assets":     addrBalances,
			}
			mpkAssets = append(mpkAssets, mpkAsset)
		}
		dirPath := os.Getenv("HOME") + "/.ftnox.com/solvency/" + coin
		filePath := dirPath + "/assets.json"
		writeToFile(filePath, mpkAssets)
	}
}