def get_addresses(addresses): rows = session.query(EthereumAccount).count() if rows != len(addresses): addresses = {} account_list = session.query(EthereumAccount).all() for account in account_list: addresses[account.public_key] = account return addresses
def reset_data(): print('1GOT RESET REQUEST') session.query(Account).delete() print('2GOT RESET REQUEST') session.commit() print('3GOT RESET REQUEST') payment.create_user('hotwallet') print('4GOT RESET REQUEST') return ''
def withdraw_eth(username, amount, address): user_balance = table.get_item( Key={'UserIDandSymbol': '{username}.ETH'.format( username=username)})['Item'] account = session.query(Account).filter( Account.username == username).first() hotwallet = session.query(Account).filter( Account.username == 'hotwallet').first() gasprice = web3.toWei(10, 'Gwei') startgas = 21000 print('user balance', user_balance['Balance']) print('amount', amount, 'withdraw fee', web3.fromWei(gasprice * startgas, values.eth_base_unit)) if amount == 'all': amount = user_balance['Balance'] if amount <= 0: return { 'success': False, 'error': 'You can not withdraw 0 or a negative amount' } if amount > user_balance['Balance']: return { 'success': False, 'error': 'You can not withdraw more than your available balance' } if web3.toWei(amount, values.eth_base_unit) <= gasprice * startgas: return { 'success': False, 'error': 'You can not withdraw less than the withdrawal fee' } tx = Transaction( nonce=web3.eth.getTransactionCount(hotwallet.public_key), gasprice=gasprice, startgas=startgas, to=address, value=web3.toWei(amount, eth_base_unit) - gasprice * startgas, data=b'', ) tx.sign(bytes(private_key)) raw_tx = rlp.encode(tx) raw_tx_hex = web3.toHex(raw_tx) tx_id = web3.eth.sendRawTransaction(raw_tx_hex) table.update_item( Key={'UserIDandSymbol': '{username}.ETH'.format(username=username)}, UpdateExpression='SET Balance = Balance - :val1', ExpressionAttributeValues={':val1': amount}) return {'success': True, 'error': None, 'tx_id': tx_id} print('Withdrew {amount} from {user} to address {address}'.format( amount=amount, user=username, address=address))
def update_address(public_key, last_block): account = session.query(EthereumAccount).filter( EthereumAccount.public_key == public_key).one() account.balance = web3.fromWei(web3.eth.getBalance(account.public_key), values.eth_base_unit) account.last_block = last_block session.commit()
def send_to_hot_wallet(public_key): account = session.query(EthereumAccount).filter( EthereumAccount.public_key == public_key).one() hotwallet = session.query(EthereumAccount).filter( EthereumAccount.username == 'hotwallet').first() gasprice = web3.toWei(1, 'Gwei') startgas = 21000 tx = Transaction( nonce=web3.eth.getTransactionCount(account.public_key), gasprice=gasprice, startgas=startgas, to=hotwallet.public_key, value=web3.toWei(account.balance, values.eth_base_unit) - gasprice * startgas, data=b'', ) tx.sign(bytes(ord(x) for x in account.private_key)) raw_tx = rlp.encode(tx) raw_tx_hex = web3.toHex(raw_tx) web3.eth.sendRawTransaction(raw_tx_hex)
def get_accounts(wallet): #load accounts from SQL and add those to the Node wallet accounts = session.query(NanoAccount).all() loaded_accounts = rpc.account_list(wallet) #print('loaded accounts', loaded_accounts) balances = rpc.wallet_balances(wallet) for account in accounts: if account.account_id not in loaded_accounts: rpc.wallet_add(wallet, account.private_key) print('Added to wallet:', account.private_key) session.commit() return accounts
def send_to_hot_wallet(account, wallet): if account.username == 'hotwallet': return hotwallet_account = session.query(NanoAccount).filter(NanoAccount.username == 'hotwallet').one() balance = rpc.account_balance(account.account_id)['balance'] rpc.send( wallet = wallet, source = account.account_id, destination = hotwallet_account.account_id, amount = balance, )
def initialize_account(account, wallet): if account.username != 'hotwallet': hotwallet_account = session.query(NanoAccount).filter(NanoAccount.username == 'hotwallet').one() rpc.send( wallet = wallet, source = hotwallet_account.account_id, destination = account.account_id, amount = 0, ) pending = rpc.accounts_pending([account.account_id], count = 10, threshold=0) #print('Pending:', pending) for account_id, blocks in pending.items(): for block in blocks: rpc.receive(wallet, account_id, block) account.last_block = rpc.account_info(account.account_id)['frontier'] session.commit() return
def withdraw_nano(username, amount, address): amount = dc.Decimal(amount) if amount <= 0: return { 'success': False, 'error': 'You can not withdraw 0 or a negative amount' } hotwallet_account = session.query(NanoAccount).filter( NanoAccount.username == 'hotwallet').one() user_balance = table.get_item( Key={'UserIDandSymbol': '{username}.NANO'.format( username=username)})['Item'] print(rpc.account_balance(hotwallet_account.account_id)['balance']) if convert(user_balance['Balance'], from_unit=values.nano_base_unit, to_unit='XRB') >= amount: table.update_item(Key={ 'UserIDandSymbol': '{username}.NANO'.format(username=username) }, UpdateExpression='SET Balance = Balance - :val1', ExpressionAttributeValues={ ':val1': convert(amount, from_unit='XRB', to_unit=values.nano_base_unit) }) tx_id = rpc.send(wallet=values.nano_wallet, source=hotwallet_account.account_id, destination=address, amount='{0:f}'.format( convert(amount, from_unit='XRB', to_unit='raw'))) return {'success': True, 'error': None, 'tx_id': tx_id} else: return { 'success': False, 'error': 'You can not withdraw more than your available balance' }
def get_last_block(): last_block = session.query(func.max(EthereumAccount.last_block)).scalar() print(last_block)
def get_address(username): eth_addr = session.query(EthereumAccount).filter( EthereumAccount.username == username).one() nano_addr = session.query(NanoAccount).filter( NanoAccount.username == username).one() return {'ETH': eth_addr.public_key, 'NANO': nano_addr.account_id}