// if the nonce is given, the addr is not needed func checkCommon(addr, amtS, gasS, priceS string, seq uint64) (from common.Address, nonce uint64, amount, gas, price *big.Int, err error) { // resolve the big ints if amount, err = stringToBig(amtS); err != nil { err = fmt.Errorf("amt %s is bad hex: %v", amtS, err) return } if gas, err = stringToBig(gasS); err != nil { err = fmt.Errorf("gas %s is bad hex: %v", gasS, err) return } if price, err = stringToBig(priceS); err != nil { err = fmt.Errorf("price %s is bad hex: %v", priceS, err) return } // resolve the address if addr == "" { err = fmt.Errorf("--addr must be given") return } var addrBytes []byte addrBytes, err = hex.DecodeString(utils.StripHex(addr)) if err != nil { err = fmt.Errorf("addr is bad hex: %v", err) return } from = common.BytesToAddress(addrBytes) // resolve the nonce (or fetch it) if seq == 0 { if EthClient.Host == "" { // NOTE this error only applies to ethtx, not other possible consumers of ethtx/core err = fmt.Errorf("input must specify a nonce with the --nonce flag or use --node-addr (or ETHTX_NODE_ADDR) to fetch the nonce from a node") return } var r interface{} // fetch block num r, err = EthClient.RequestResponse("eth", "blockNumber") if err != nil { err = fmt.Errorf("Error fetching block number: %v", err) return } // NOTE: both block num and account nonces are hex. (why?!) blockNum := utils.HexToInt(r.(string)) r, err = EthClient.RequestResponse("eth", "getTransactionCount", addr, blockNum) if err != nil { err = fmt.Errorf("Error fetching account nonce: %v", err) return } nonce = uint64(utils.HexToInt(r.(string))) } else { nonce = seq } return }
// accepts hex or string encoded integers func stringToBig(s string) (*big.Int, error) { if strings.HasPrefix(s, "0x") { return hexToBig(utils.StripHex(s)) } d, err := strconv.ParseInt(s, 0, 64) if err != nil { return nil, err } return big.NewInt(d), nil }
func Create(fromAddr, amtS, gasS, priceS, data string, nonce uint64) (*Transaction, error) { from, nonce, amt, gas, price, err := checkCommon(fromAddr, amtS, gasS, priceS, nonce) if err != nil { return nil, err } dataBytes, err := hex.DecodeString(utils.StripHex(data)) if err != nil { return nil, fmt.Errorf("data is bad hex: %s", data) } return NewTransaction(nil, &from, nonce, amt, gas, price, dataBytes), nil }
func Call(fromAddr, toAddr, amtS, gasS, priceS, data string, nonce uint64) (*Transaction, error) { from, nonce, amt, gas, price, err := checkCommon(fromAddr, amtS, gasS, priceS, nonce) if err != nil { return nil, err } if toAddr == "" { return nil, fmt.Errorf("destination address must be given with --to flag") } toAddrBytes, err := hex.DecodeString(utils.StripHex(toAddr)) if err != nil { return nil, fmt.Errorf("toAddr is bad hex: %v", err) } to := common.BytesToAddress(toAddrBytes) dataBytes, err := hex.DecodeString(utils.StripHex(data)) if err != nil { return nil, fmt.Errorf("data is bad hex: %s", data) } return NewTransaction(&to, &from, nonce, amt, gas, price, dataBytes), nil }