def lb_balance(address): w3 = getW3() address = w3.toChecksumAddress(to0x(address)) ac_balance = w3.eth.getBalance(address) if ac_balance is None: return 0 return ac_balance
def lb_tranfer(pvkey, toAddr, amount, gasLimit=None, gasPrice=None): acc = getAccount(pvkey) if acc: w3 = getW3() if gasLimit is None: gasLimit = 21000 if gasPrice is None: gasPrice = w3.eth.gasPrice _from = acc.address _from_balance = w3.eth.getBalance(_from) # print('from余额', _from_balance) param = { 'from': to0x(_from), 'nonce': w3.eth.getTransactionCount(_from), 'gasPrice': int(gasPrice), 'gas': int(gasLimit), 'to': w3.toChecksumAddress(to0x(toAddr)), 'value': amount, # 'value': w3.toWei(amount, 'ether'), # data:b'', } print('交易参数 ', param) signed_txn = w3.eth.account.signTransaction(param, pvkey) hash_tx = w3.eth.sendRawTransaction(signed_txn.rawTransaction).hex() print('交易哈希 ', hash_tx) return hash_tx return None
def newAccount(): w3 = getW3() acc = w3.eth.account.create() if acc is None: return None address = toLb(acc.address) return {'address': address, 'privateKey': acc.key.hex()}
def transferToken(): cttAddr = request.form.get('cttAddr') _privKey = request.form.get('private_key') _to = request.form.get('to_address') _amount = request.form.get('amount') _gasLimit = request.form.get('gas_limit') _gasPrice = request.form.get('gas_price') try: w3 = getW3() _amount = w3.toWei(_amount, 'ether') if _amount <= 0: raise Exception('转账金额需大于0') _txn = token_tranfer(cttAddr, _privKey, _to, _amount, _gasLimit, _gasPrice) # print(_txn) return jsonify({"data": _txn, "code": 200, "msg": "TOKEN交易成功"}) except Exception as e: param = { 'cttAddr': cttAddr, '_privKey': _privKey, '_to': _to, '_amount': _amount, '_gasLimit': _gasLimit, '_gasPrice': _gasPrice } logger.debug('transferToken 交易参数:%s' % param) logger.debug('transferToken 交易失败:%s' % (e)) return jsonify({"data": None, "code": 500, "msg": str(e)})
def transfer(): # _from = request.form.get('from_address') _privKey = request.form.get('private_key') _to = request.form.get('to_address') _amount = request.form.get('amount') _gasLimit = request.form.get('gas_limit') _gasPrice = request.form.get('gas_price') try: w3 = getW3() _amount = w3.toWei(_amount, 'ether') if _amount <= 0: raise Exception('转账金额需大于0') txn = lb_tranfer(_privKey, _to, _amount, _gasLimit, _gasPrice) return jsonify({"data": txn, "code": 200, "msg": "LB交易成功"}) except Exception as e: param = { '_privKey': _privKey, '_to': _to, '_amount': _amount, '_gasLimit': _gasLimit, '_gasPrice': _gasPrice } logger.debug('transfer 交易参数:%s' % param) logger.debug('transfer 交易失败:%s' % (e)) return jsonify({"data": None, "code": 500, "msg": str(e)})
def token_balance(cttAddr, address): address = to0x(address) cttAddr = to0x(cttAddr) contract = getContract(cttAddr) w3 = getW3() address = w3.toChecksumAddress(address) balance = contract.functions.balanceOf(address).call() if balance is None: return 0 return balance
def token_tranfer(cttAddr, pvkey, toAddr, tokenAmt, gasLimit=None, gasPrice=None, nonce=None): cttAddr = to0x(cttAddr) c = getContract(cttAddr) w3 = getW3() acc = getAccount(pvkey) if c is None: return None if nonce is None: nonce = w3.eth.getTransactionCount(acc.address), if gasLimit is None: gasLimit = 80000 if gasPrice is None: gasPrice = w3.eth.gasPrice if acc and tokenAmt > 0: fromAddr = w3.toChecksumAddress(to0x(acc.address)) toAddr = w3.toChecksumAddress(to0x(toAddr)) param = { 'from': fromAddr, 'value': 0, 'gas': int(gasLimit), 'gasPrice': int(gasPrice), 'nonce': int(nonce[0]), # 'chainId': 1, } # print('toAddr', toAddr) # print('tokenAmt', tokenAmt) # print('TOKEN 交易参数 ', param) unicorn_txn = c.functions.transfer(toAddr, tokenAmt).buildTransaction(param) signed_txn = w3.eth.account.signTransaction(unicorn_txn, private_key=acc.privateKey) hash_tx = w3.eth.sendRawTransaction(signed_txn.rawTransaction).hex() # print('TOKEN 交易哈希 ', hash_tx) return hash_tx return None
def getTransByHash(tx): w3 = getW3() tx = w3.eth.getTransactionReceipt(tx) return tx headers = { 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.111 Safari/537.36', 'Accept': 'application/json, text/plain, */*' } r = requests.get( 'http://47.52.110.153:8080/api/contract_orderList_info?&str={}'.format( tx), headers=headers) if r.status_code != 200: return None data = r.json() if data['code'] == '-1': return None data['data']['from'] = toLb(data['data']['from']) data['data']['to'] = toLb(data['data']['to']) return data
def getLbNet(): w3 = getW3() return w3.net
def getLbGas(): w3 = getW3() return w3.eth.gasPrice