Esempio n. 1
0
func SignAndBroadcast(chainID, nodeAddr, signAddr string, tx types.Tx, sign, broadcast, wait bool) (txResult *TxResult, err error) {
	var inputAddr []byte
	if sign {
		inputAddr, tx, err = signTx(signAddr, chainID, tx)
		if err != nil {
			return nil, err
		}
	}

	if broadcast {
		if wait {
			var ch chan Msg
			ch, err = subscribeAndWait(tx, chainID, nodeAddr, inputAddr)
			if err != nil {
				return nil, err
			} else {
				defer func() {
					if err != nil {
						// if broadcast threw an error, just return
						return
					}
					logger.Debugln("Waiting for tx to be committed ...")
					msg := <-ch
					if msg.Error != nil {
						logger.Infof("Encountered error waiting for event: %v\n", msg.Error)
						err = msg.Error
					} else {
						txResult.BlockHash = msg.BlockHash
						txResult.Return = msg.Value
						txResult.Exception = msg.Exception
					}
				}()
			}
		}
		var receipt *rtypes.Receipt
		receipt, err = Broadcast(tx, nodeAddr)
		if err != nil {
			return nil, err
		}
		txResult = &TxResult{
			Hash: receipt.TxHash,
		}
		if tx_, ok := tx.(*types.CallTx); ok {
			if len(tx_.Address) == 0 {
				txResult.Address = types.NewContractAddress(tx_.Input.Address, tx_.Input.Sequence)
			}
		}
	}
	return
}
Esempio n. 2
0
// Convenience function to return address of new contract
func NewContractAddress(caller []byte, nonce int) []byte {
	return types.NewContractAddress(caller, nonce)
}