示例#1
0
func FctBalance(adr string) (int64, error) {
	err := refresh()
	if err != nil {
		return 0, err
	}
	return Wallet.FactoidBalance(adr)
}
func GetAddressInformationFromFactom(address string) (*Address, error) {
	answer := new(Address)
	answer.Address = address

	invalid := 0 //to count how many times we got "invalid address"

	ecBalance, err := Wallet.ECBalance(address)
	fmt.Printf("ECBalance - %v, %v\n\n", ecBalance, err)
	if err != nil {
		if err.Error() != "Invalid EC Address" && !strings.Contains(err.Error(), "encoding/hex") {
			return nil, err
		}
		invalid++
	} else {
		answer.Balance = fmt.Sprintf("%d", ecBalance)
		answer.AddressType = "EC Address"
		if ecBalance > 0 {
			return answer, nil
		}
	}
	fctBalance, err := Wallet.FactoidBalance(address)
	fmt.Printf("FactoidBalance - %v, %v\n\n", fctBalance, err)
	if err != nil {
		if err.Error() != "Invalid Factoid Address" {
			return nil, err
		}
		invalid++
	} else {
		answer.AddressType = "Factoid Address"
		if fctBalance > 0 {
			answer.Balance = string(uint64(fctBalance))
			return answer, nil
		}
	}
	if invalid > 1 {
		//2 responses - it's not a valid address period
		return nil, fmt.Errorf("Invalid address")
	}
	if invalid == 0 {
		//no invalid responses - meaning it's a public key valid for both factoid and ec
		answer.AddressType = "Unknown Address"
	}

	return answer, nil
}
func GetAddresses() (*Addresses, error) {
	addresses := Wallet.GetAddresses()

	answer := new(Addresses)
	for _, we := range addresses {
		tmp := Address{}
		tmp.Type = we.GetType()
		if we.GetType() == "ec" {
			address, err := we.GetAddress()
			if err != nil {
				return nil, err
			}

			tmp.Address = factoid.ConvertECAddressToUserStr(address)
			tmp.Name = string(we.GetName())
			bal, err := Wallet.ECBalance(tmp.Address)
			if err != nil {
				return nil, err
			}
			tmp.Balance = float64(bal)
			answer.ECAddresses = append(answer.ECAddresses, tmp)
		} else {
			address, err := we.GetAddress()
			if err != nil {
				return nil, err
			}

			tmp.Address = factoid.ConvertFctAddressToUserStr(address)
			tmp.Name = string(we.GetName())
			bal, err := Wallet.FactoidBalance(tmp.Address)
			if err != nil {
				return nil, err
			}
			tmp.Balance = factoid.ConvertDecimalToFloat(uint64(bal))
			answer.FactoidAddresses = append(answer.FactoidAddresses, tmp)
		}
	}
	return answer, nil
}