def web3py_contract_deploy(self, w3, nonce, abi, bytecode, args): """ TODO 远程部署智能合约 :param w3: Web3 实例 :param abi: 智能合约编译的 abi :param bytecode: 智能合约编译bin code :return: tx_hash """ from web3.utils.transactions import ( fill_transaction_defaults, ) if not isinstance(w3, Web3): raise EtherError({"err": u"无效的Web3实例!", "code": 4000}) contract = w3.eth.contract(abi=abi, bytecode=bytecode) deploy_contract = contract.constructor(args) gas = deploy_contract.estimateGas() deploy_transaction = fill_transaction_defaults( w3, { "from": to_checksum_address(self.address), "to": "0x", "nonce": nonce, "gas": gas, "gasPrice": w3.eth.gasPrice, "chainId": int(w3.net.chainId), "data": deploy_contract.data_in_transaction }) signed_txn = w3.eth.account.signTransaction(deploy_transaction, self.private_key) tx_hash = w3.eth.sendRawTransaction(signed_txn.rawTransaction) return bytes_to_str(tx_hash)
def web3py_contract_transaction(self, w3, contract, to, value, nonce=None, chain_id=None, gas_price=None): """ TODO 发行的智能合约交易操作 调用之前enable该交易特色 web3.eth.enable_unaudited_features() :param w3: Web3 实例 ; eg: w3 = Web3(HTTPProvider('http://localhost:8545')) :param contract: 智能合约实例 ; eg: contract = web3.eth.contract(address=to_checksum_address(contract_address), abi=abi) :param to: 交易目的地址 :param value: 交易数目 :param chain_id: 私链对应的 id , ETH = 1 :param gas_price: GAS 的价格 :return: 返回交易哈希地址 """ if not isinstance(w3, Web3): raise EtherError({"err": u"无效的Web3实例!", "code": 4000}) if getattr(type(contract), '__name__') is not "Contract": raise EtherError({"err": u"无效的Contract实例!", "code": 4000}) # 验证地址 to_address = to try: to_address = to_checksum_address(to_address) except Exception: raise EtherError({"err": u"无效的转账地址!", "code": 4001}) try: _gasPrice = gas_price if gas_price else w3.eth.gasPrice _chainid = chain_id if chain_id else int(w3.net.chainId) _nonce = nonce if nonce else w3.eth.getTransactionCount( to_checksum_address(self.address)) tx_transfer = contract.functions.transfer(to_address, value) _gas = tx_transfer.estimateGas( {"from": to_checksum_address(self.address)}) contract_tx = tx_transfer.buildTransaction({ 'chainId': _chainid, 'gas': _gas, 'gasPrice': _gasPrice, 'nonce': _nonce }) signed_txn = w3.eth.account.signTransaction( contract_tx, self.private_key) tx_hash = w3.eth.sendRawTransaction(signed_txn.rawTransaction) return bytes_to_str(tx_hash) except Exception as e: if e.args and isinstance(e.args[0], dict): code = e.args[0].get("code", None) msg = e.args[0].get("message", None) if code == -32000 and isinstance( msg, str) and msg.startswith("known transaction:"): raise EtherError({"err": u"交易过于频繁, 请稍后!", "code": 4001}) raise EtherError({ "err": u"交易异常!", "exception": str(e), "code": 5000 })
def web3py_transaction(self, w3, to, value, nonce=None, chain_id=None, gas_price=None): """ TODO 私链交易转账操作 调用之前enable该交易特色 web3.eth.enable_unaudited_features() :param w3: Web3 实例 ; eg: w3 = Web3(HTTPProvider('http://localhost:8545')) :param to: 交易目的地址 :param value: 交易数目 :param chain_id: 私链对应的 id , ETH = 1 :param gas_price: GAS 的价格 :return: 返回交易哈希地址 """ if not isinstance(w3, Web3): raise EtherError({"err": u"无效的Web3实例!", "code": 4000}) # 验证地址 to_address = to try: to_address = to_checksum_address(to_address) except Exception: raise EtherError({"err": u"无效的转账地址!", "code": 4001}) try: _gasPrice = gas_price if gas_price else w3.eth.gasPrice _chainid = chain_id if chain_id else int(w3.net.chainId) _nonce = nonce if nonce else w3.eth.getTransactionCount( to_checksum_address(self.address)) _gas = w3.eth.estimateGas({ 'to': to_address, 'from': to_checksum_address(self.address), 'value': value }) # 交易参数 tx_params = { 'to': to_address, 'value': value, 'gas': _gas, 'gasPrice': _gasPrice, 'nonce': _nonce, 'chainId': _chainid } # 对交易进去签名 signed = w3.eth.account.signTransaction(tx_params, self.private_key) # When you run sendRawTransaction, you get back the hash of the transaction tx_hash = w3.eth.sendRawTransaction(signed.rawTransaction) # 本次交易的哈希地址 return bytes_to_str(tx_hash) except Exception as e: if e.args and isinstance(e.args[0], dict): code = e.args[0].get("code", None) msg = e.args[0].get("message", None) if code == -32000 and isinstance( msg, str) and msg.startswith("known transaction:"): raise EtherError({"err": u"交易过于频繁, 请稍后!", "code": 4001}) raise EtherError({ "err": u"交易异常!", "exception": str(e), "code": 5000 })
def public_key(self): if self._public_key and isinstance(self._public_key, (HDPublicKey, PublicKey)): return bytes_to_str(self._public_key.compressed_bytes) else: raise EtherError({"err": u"无效的公钥!", "code": 4000})