def sign( tx: transactions.UnsignedEthTx, key: bytes) -> transactions.SignedEthTx: ''' Sign this transaction with a private key. A potentially already existing signature would be overridden. ''' return tx.sign(key)
def make_call_tx(contract: str, abi: List[Dict[str, Any]], method: str, args: List[Any], nonce: int, value: int = 0, gas: int = DEFAULT_GAS, gas_price: int = DEFAULT_GAS_PRICE) -> UnsignedEthTx: ''' Sends tokens to a recipient Args: contract (str): address of contract being called abi (dict): contract ABI method (str): the name of the method to call args (list): the arguments to the method call nonce (int): the account nonce for the txn value (int): ether in wei gas_price (int): the price of gas in wei or gwei Returns: (UnsignedEthTx): the unsigned tx object ''' logger.debug(f'making tx call {method} on {contract} ' f'with value {value} and {len(args)} args') gas_price = _adjust_gas_price(gas_price) chainId = config.get()['CHAIN_ID'] data = calldata.call(method, args, abi) txn = UnsignedEthTx(to=contract, value=value, gas=gas, gasPrice=gas_price, nonce=nonce, data=data, chainId=chainId) return txn
async def sign_and_broadcast(tx: UnsignedEthTx, ignore_result: bool = False) -> None: '''Sign an ethereum transaction and broadcast it to the network''' c = config.get() privkey = c['PRIVKEY'] address = c['ETH_ADDRESS'] unlock_code = c['GETH_UNLOCK'] if privkey is None and unlock_code is None: raise RuntimeError('Attempted to sign tx without access to key') if privkey is None: logger.debug('signing with ether node') await CONNECTION._RPC('personal_unlockAccount', [address, unlock_code]) tx_id = await CONNECTION.send_transaction(cast(str, address), tx) else: logger.debug('signing with local key') signed = tx.sign(cast(bytes, privkey)) serialized = signed.serialize_hex() tx_id = await CONNECTION.broadcast(serialized) logger.info(f'dispatched transaction {tx_id}') if not ignore_result: asyncio.ensure_future(_track_tx_result(tx_id))