def construct_erc20_tx(self,addressFrom,addressTo,value,gasPrice=None): ''' 构造eth未签名交易 ''' contract_instance=self.get_contract_instance(CONTRACT_ADDRESS, CONTRACT_ABI) tx_dict = contract_instance.functions.transfer( checksum_encode(addressTo), int(value*(10**8)) ).buildTransaction({ 'from': addressFrom, 'nonce': self.web3.eth.getTransactionCount(addressFrom), }) tx = Transaction( nonce=tx_dict.get("nonce"), gasprice=tx_dict.get("gasPrice") if not gasPrice else gasPrice, startgas=tx_dict.get("gas"), to=tx_dict.get("to"), value=tx_dict.get("value"), data=binascii.unhexlify(tx_dict.get("data")[2:])) UnsignedTransaction = Transaction.exclude(['v', 'r', 's']) unsigned_tx = rlp.encode(tx, UnsignedTransaction) before_hash = utils.sha3(unsigned_tx) return binascii.hexlify(unsigned_tx).decode(),binascii.hexlify(before_hash).decode()
def invoke_contract(self, invoker, contract, method, args, gasLimit=800000): tx_dict = contract.functions[method](*args).buildTransaction({ "gas": gasLimit, 'gasPrice': self.web3.eth.gasPrice, 'nonce': self.web3.eth.getTransactionCount(checksum_encode(invoker)), }) tx = Transaction(nonce=tx_dict.get("nonce"), gasprice=tx_dict.get("gasPrice"), startgas=tx_dict.get("gas"), to=tx_dict.get("to"), value=tx_dict.get("value"), data=binascii.unhexlify(tx_dict.get("data")[2:])) UnsignedTransaction = Transaction.exclude(['v', 'r', 's']) unsigned_tx = rlp.encode(tx, UnsignedTransaction) return binascii.hexlify(unsigned_tx).decode()
def write_contract(self, invoker, contractAddress, method, args,gasPrice=None): ''' 调用合约里实现的方法 ''' invoker = checksum_encode(invoker) contractAddress = checksum_encode(contractAddress) contract_instance = self.get_contract_instance(contractAddress) if not contract_instance: return None tx_dict = contract_instance.functions[method](*args).buildTransaction({ 'gasPrice': self.web3.eth.gasPrice if not gasPrice else gasPrice, 'from':invoker, 'nonce': self.web3.eth.getTransactionCount(invoker), }) tx = Transaction( nonce=tx_dict.get("nonce"), gasprice=tx_dict.get("gasPrice"), startgas=tx_dict.get("gas"), to=tx_dict.get("to"), value=tx_dict.get("value"), data=binascii.unhexlify(tx_dict.get("data")[2:])) UnsignedTransaction = Transaction.exclude(['v', 'r', 's']) unsigned_tx = rlp.encode(tx, UnsignedTransaction) return binascii.hexlify(unsigned_tx).decode()
def get_tx_rawhash(tx, network_id=None): """Get a tx's rawhash. Copied from ethereum.transactions.Transaction.sign """ if network_id is None: rawhash = utils.sha3( rlp.encode(tx, Transaction.exclude(['v', 'r', 's']))) else: assert 1 <= network_id < 2**63 - 18 rlpdata = rlp.encode( rlp.infer_sedes(tx).serialize(tx)[:-3] + [network_id, b'', b'']) rawhash = utils.sha3(rlpdata) return rawhash
def construct_eth_tx(self, addressFrom, addressTo, value, gasPrice=None): ''' 构造eth未签名交易 ''' tx = Transaction( nonce=self.web3.eth.getTransactionCount(addressFrom), gasprice=self.web3.eth.gasPrice if not gasPrice else gasPrice, startgas=21000, to=addressTo, value=int(value), data=b'') UnsignedTransaction = Transaction.exclude(['v', 'r', 's']) unsigned_tx = rlp.encode(tx, UnsignedTransaction) before_hash = utils.sha3(unsigned_tx) return binascii.hexlify(unsigned_tx).decode()
def invoke_contract(self, invoker, contract, method, args,gasPrice=None): ''' 调用合约里实现的方法 ''' tx_dict = contract.functions[method](*args).buildTransaction({ 'gasPrice': self.web3.eth.gasPrice if not gasPrice else gasPrice, 'nonce': self.web3.eth.getTransactionCount(invoker), }) tx = Transaction( nonce=tx_dict.get("nonce"), gasprice=tx_dict.get("gasPrice"), startgas=tx_dict.get("gas"), to=tx_dict.get("to"), value=tx_dict.get("value"), data=binascii.unhexlify(tx_dict.get("data")[2:])) UnsignedTransaction = Transaction.exclude(['v', 'r', 's']) unsigned_tx = rlp.encode(tx, UnsignedTransaction) return binascii.hexlify(unsigned_tx).decode()