コード例 #1
0
	def SettleTransaction(self,WavesAddress):


		#check transaction WavesAddress and WAVES : attchment BTCaddress and verify assetID 
		# trnc = ModuleHandler.wrapper("/transactions/address/" + WavesAddress + "/limit/1")
		pywaves.setNode(node = pywaves.getNode() , chain = self.CHAIN)
		print("getNode(): ",pywaves.getNode())
		trnc = pywaves.wrapper("/transactions/address/" + WavesAddress + "/limit/1" ,  host = self.TESTNET_NODE )
		print('trnc',trnc)
		BTCaddress = trnc[0][0]['attachment']
		if BTCaddress == '' :
			return {"result" : "there is not any transaction!" }

		
		assetDetail = ModuleHandler.wrapper("/assets/details/" + trnc[0][0]['assetId']  ,  host = self.TESTNET_NODE)
		print('assetDetail :  ',assetDetail)
		decimals = assetDetail['decimals']


		con = lite.connect('test.db')
		with con:
			cur = con.cursor()
			# cur.execute("SELECT MIN(cnt) FROM (SELECT COUNT(*) cnt FROM voting GROUP BY candidate) t;")
			# minimumMigdar = cur.fetchone()
			cur.execute("""SELECT * FROM addresses ORDER BY Inventory DESC """)						#TODO change query for get min count 
			con.commit()
			rows = cur.fetchall()
			cnt = 0
			remind = amount*(10**((-1)*decimals)) #### Decimals Decimals Decimals
			while remind > 0:
				serializedWallet = rows[cnt][1]				#ref to DB structure
				wallet = Wallet.deserialize(serializedWallet , network=BitcoinTestNet)
				inv = rows[cnt][3]

				if inv >= remind :
					
					blockcypher.simple_spend(from_privkey=ModuleHandler.encode( wallet.get_private_key_hex() ) , to_address = BTCaddress , to_satoshis = remind)
					cur.execute(""" UPDATE addresses SET Inventory = ? WHERE WavesAddress = ? """,  ( inv - remind , WavesAddress ))
				

				else :

					blockcypher.simple_spend(from_privkey=ModuleHandler.encode( wallet.get_private_key_hex() ) , to_address = BTCaddress , to_satoshis = inv)
					remind = remind - inv
					cur.execute(""" UPDATE addresses SET Inventory = ? WHERE WavesAddress = ? """,  ( 0 , WavesAddress ))
				con.commit()
			
				
		return res
コード例 #2
0
	def CreateWallet(self,WavesAddress):

		con = lite.connect('test.db')
		with con:
			cur = con.cursor()
			cur.execute("""SELECT id FROM addressid WHERE WavesAddress=:adr""",  {"adr": WavesAddress})
			row = cur.fetchone()
			if len(row)>0 :
				BTCWallet = get_wallet_addresses(wallet_name='Noay'+ str(row[0]), api_key=self.APIKEY, coin_symbol=self.coin_symbol)
				cur.execute("""SELECT * FROM addresses WHERE WavesAddress=:adr""",  {"adr": WavesAddress})
				row = cur.fetchone()
				wallet = Wallet.deserialize(row[1] ,  network=BitcoinTestNet)
				ModuleHandler.encode(wallet.get_public_key_hex())
				con.commit()
				return {  'addresses'  : BTCWallet['addresses'][0] 
				, 'public_key' : ModuleHandler.encode( wallet.get_public_key_hex() )  }
			con.commit()

		newWallet = Wallet.new_random_wallet(network=BitcoinTestNet)
		# private_key_hex = newWallet.get_private_key_hex()
		# public_key_hex = newWallet.get_public_key_hex()
		serializedWallet = newWallet.serialize()
		newAddress = newWallet.to_address()

		# print('newAddress : ',newAddress)

		con = lite.connect('test.db')
		with con:
			cur = con.cursor()
			cur.execute("CREATE TABLE IF NOT EXISTS addressid(id INTEGER PRIMARY KEY AUTOINCREMENT, WavesAddress TEXT  NOT NULL )")
			cur.execute("""INSERT INTO addressid (WavesAddress) VALUES(?)""",(WavesAddress,))
			cur.execute("""SELECT id FROM addressid WHERE WavesAddress=:adr""",  {"adr": WavesAddress})
			con.commit()
			row = cur.fetchone()

		# print("row[0] : ",row[0])
		#TODO handle {'error': 'Error: wallet exists'}

		BTCWallet = get_wallet_addresses(wallet_name='Noay'+ str(row[0]), api_key=self.APIKEY, coin_symbol=self.coin_symbol)
		try:
			s = BTCWallet['error']
			print('BTCWallet' , 0)
			BTCWallet = create_wallet_from_address(wallet_name= 'Noay'+ str(row[0]), address=newAddress, api_key=self.APIKEY , coin_symbol=self.coin_symbol)
		
		#TODO SAVE WAVESAddress , serializeWlt in DB
		
		except Exception as e:

			con = lite.connect('test.db')
			print('BTCWallet' , 1)
			with con:
				cur = con.cursor()
				cur.execute("CREATE TABLE IF NOT EXISTS addresses(WavesAddress TEXT , serializedWallet TEXT , BTCaddress TEXT , Inventory REAL)")
				cur.execute("""INSERT INTO addresses VALUES(?,?,?,0)""",(WavesAddress,serializedWallet,BTCWallet['addresses'][0]))
				con.commit()
				# con.close()
		# print('BTCWallet' , BTCWallet)



		return {  'addresses'  : BTCWallet['addresses'][0] 
				, 'public_key' : ModuleHandler.encode( newWallet.get_public_key_hex() )  } # public key btc