// Sends a transaction to the blockchain.
// If sending an ERC20 token, the argument token must be a valid 20-byte address
// If priceIn is 0, a suggested gas price will be sought from the blockchain.
// Use dryRun = true for testing (it will not send the transaction to the blockchain but provide a valid hash)
Sends a transaction to the blockchain.
If sending an ERC20 token, the argument token must be a valid 20-byte address
If priceIn is 0, a suggested gas price will be sought from the blockchain.
Use dryRun = true for testing (it will not send the transaction to the blockchain but provide a valid hash)
// Sends a transaction to the blockchain . // If sending an ERC20 token the argument token must be a valid 20 - byte address // If priceIn is 0 a suggested gas price will be sought from the blockchain . // Use dryRun = true for testing ( it will not send the transaction to the blockchain but provide a valid hash )
func (c *EthCli) SendTrx(fromAddress, toAddress, token, amount string, data []byte, key string, priceIn uint64, dryRun bool) (priceOut, gasLimit uint64, hash []byte, err error) {
	var nonce uint64
	var ok bool
	var to common.Address
	var fromKey *ecdsa.PrivateKey
	var amt *big.Int = new(big.Int)
	var raw, tmp, tmpAmt []byte
	if fromAddress[:2] != "0x" || len(fromAddress) != 42 {
		err = ErrBadFrom
	}
	if toAddress[:2] != "0x" || len(toAddress) != 42 {
		err = ErrBadTo
	}
	if token != "" && (token[:2] != "0x" || len(token) != 42) {
		err = ErrBadToken
	}
	if amount[:2] != "0x" {
		err = ErrWrongAmt
	}
	if len(key) != 64 {
		err = ErrBadKey
	}
	if fromKey, err = crypto.HexToECDSA(key); err != nil {
		return
	}
	amt, ok = amt.SetString(amount, 0)
	if !ok || len(amt.Bytes()) > 32 {
		return priceOut, gasLimit, hash, ErrWrongAmt
	}
	if nonce, err = c.GetTransactionCount(fromAddress, "latest"); err != nil {
		return
	}
	if token == "" {
		to = common.HexToAddress(toAddress)
	} else {
		if data != nil {
			return priceOut, gasLimit, hash, ErrSendTokenData
		}
		to = common.HexToAddress(token)
		var toAddr []byte
		toAddr, err = hex.DecodeString(toAddress[2:])
		tmpAmt = make([]byte, 32)
		copy(tmpAmt[32-len(amt.Bytes()):32], amt.Bytes()[:])
		amt.Sub(amt, amt) 
		tmp = make([]byte, 0, 4+32+32)
		tmp = append(tmp, 0xa9, 0x05, 0x9c, 0xbb)                                                 
		tmp = append(tmp, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00) 
		tmp = append(tmp, toAddr[:]...)                                                           
		tmp = append(tmp, tmpAmt[:]...)                                                           
		data = tmp
	}
	if priceIn == 0 {
		priceOut, err = c.GasPrice()
		if err != nil {
			return
		}
	} else {
		priceOut = priceIn
	}
	gasPrice := new(big.Int).SetUint64(priceOut)
	if token == "" {
		gasLimit, err = c.EstimateGas(toAddress, amount, "0x"+hex.EncodeToString(data))
	} else {
		gasLimit, err = c.EstimateGas(token, "0x00", "0x"+hex.EncodeToString(tmp))
	}
	if err != nil {
		return
	}
	if hash, raw, err = signTrx(nonce, to, amt, gasLimit, gasPrice, data, fromKey); err != nil {
		return
	}
	if !dryRun {
		_, err = c.sendRawTransaction(raw)
	}
	return
}
func ( c * EthCli ) SendTrx ( fromAddress , toAddress , token , amount string , data [ ] byte , key string , priceIn uint64 , dryRun bool ) ( priceOut , gasLimit uint64 , hash [ ] byte , err error ) { var nonce uint64 var ok bool var to common . Address var fromKey * ecdsa . PrivateKey var amt * big . Int = new ( big . Int ) var raw , tmp , tmpAmt [ ] byte if fromAddress [ : 2 ] != " " || len ( fromAddress ) != 42 { err = ErrBadFrom } if toAddress [ : 2 ] != " " || len ( toAddress ) != 42 { err = ErrBadTo } if token != " " && ( token [ : 2 ] != " " || len ( token ) != 42 ) { err = ErrBadToken } if amount [ : 2 ] != " " { err = ErrWrongAmt } if len ( key ) != 64 { err = ErrBadKey } if fromKey , err = crypto . HexToECDSA ( key ) ; err != nil { return } amt , ok = amt . SetString ( amount , 0 ) if ! ok || len ( amt . Bytes ( ) ) > 32 { return priceOut , gasLimit , hash , ErrWrongAmt } if nonce , err = c . GetTransactionCount ( fromAddress , " " ) ; err != nil { return } if token == " " { to = common . HexToAddress ( toAddress ) } else { if data != nil { return priceOut , gasLimit , hash , ErrSendTokenData } to = common . HexToAddress ( token ) var toAddr [ ] byte toAddr , err = hex . DecodeString ( toAddress [ 2 : ] ) tmpAmt = make ( [ ] byte , 32 ) copy ( tmpAmt [ 32 - len ( amt . Bytes ( ) ) : 32 ] , amt . Bytes ( ) [ : ] ) amt . Sub ( amt , amt ) tmp = make ( [ ] byte , 0 , 4 + 32 + 32 ) tmp = append ( tmp , 0xa9 , 0x05 , 0x9c , 0xbb ) tmp = append ( tmp , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 ) tmp = append ( tmp , toAddr [ : ] ... ) tmp = append ( tmp , tmpAmt [ : ] ... ) data = tmp } if priceIn == 0 { priceOut , err = c . GasPrice ( ) if err != nil { return } } else { priceOut = priceIn } gasPrice := new ( big . Int ) . SetUint64 ( priceOut ) if token == " " { gasLimit , err = c . EstimateGas ( toAddress , amount , " " + hex . EncodeToString ( data ) ) } else { gasLimit , err = c . EstimateGas ( token , " " , " " + hex . EncodeToString ( tmp ) ) } if err != nil { return } if hash , raw , err = signTrx ( nonce , to , amt , gasLimit , gasPrice , data , fromKey ) ; err != nil { return } if ! dryRun { _ , err = c . sendRawTransaction ( raw ) } return }
Sends a transaction to the blockchain.
Sends a transaction to the blockchain .
// check arguments // from // check amount and get amt for signTrx // get nonce // get to, data // make sure data comes empty! // amt = 0 as we do not send ether!! // build data for token transaction: methodId (4), to address (32), amount (32) // transfer = 0xa9059cbb // pad 12 zeroes // to // amount // get gasPrice // gasPrice and gasLimit // generate transaction, get hash and raw signed transaction // send transaction to blockchain